blob: 5b1c37e3913cd49950d3c0ce884a303526bb9f3b [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>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040045#include <asm/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)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200158 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400159};
160
161MODULE_DEVICE_TABLE(pci, megasas_pci_table);
162
163static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700164struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400165static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100166static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400167
Yang, Boc3518832009-10-06 14:18:02 -0600168static int megasas_poll_wait_aen;
169static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600170static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800171u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600172static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700173
Yang, Boc3518832009-10-06 14:18:02 -0600174/* define lock for aen poll */
175spinlock_t poll_aen_lock;
176
adam radford9c915a82010-12-21 13:34:31 -0800177void
bo yang7343eb62007-11-09 04:35:44 -0500178megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
179 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800180static u32
181megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
182static int
183megasas_adp_reset_gen2(struct megasas_instance *instance,
184 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800185static irqreturn_t megasas_isr(int irq, void *devp);
186static u32
187megasas_init_adapter_mfi(struct megasas_instance *instance);
188u32
189megasas_build_and_issue_cmd(struct megasas_instance *instance,
190 struct scsi_cmnd *scmd);
191static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800192int
adam radford229fe472014-03-10 02:51:56 -0700193wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
194 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800195void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700196static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
197 int initial);
adam radfordcd50ba82010-12-21 10:23:23 -0800198
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530199int
adam radfordcd50ba82010-12-21 10:23:23 -0800200megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
201{
202 instance->instancet->fire_cmd(instance,
203 cmd->frame_phys_addr, 0, instance->reg_set);
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530204 return 0;
adam radfordcd50ba82010-12-21 10:23:23 -0800205}
bo yang7343eb62007-11-09 04:35:44 -0500206
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400207/**
208 * megasas_get_cmd - Get a command from the free pool
209 * @instance: Adapter soft state
210 *
211 * Returns a free command from the pool
212 */
adam radford9c915a82010-12-21 13:34:31 -0800213struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400214 *instance)
215{
216 unsigned long flags;
217 struct megasas_cmd *cmd = NULL;
218
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530219 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400220
221 if (!list_empty(&instance->cmd_pool)) {
222 cmd = list_entry((&instance->cmd_pool)->next,
223 struct megasas_cmd, list);
224 list_del_init(&cmd->list);
225 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500226 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400227 }
228
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530229 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400230 return cmd;
231}
232
233/**
234 * megasas_return_cmd - Return a cmd to free command pool
235 * @instance: Adapter soft state
236 * @cmd: Command packet to be returned to free command pool
237 */
adam radford9c915a82010-12-21 13:34:31 -0800238inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400239megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
240{
241 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530242 u32 blk_tags;
243 struct megasas_cmd_fusion *cmd_fusion;
244 struct fusion_context *fusion = instance->ctrl_context;
245
246 /* This flag is used only for fusion adapter.
247 * Wait for Interrupt for Polled mode DCMD
248 */
249 if (cmd->flags & DRV_DCMD_POLLED_MODE)
250 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400251
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530252 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400253
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530254 if (fusion) {
255 blk_tags = instance->max_scsi_cmds + cmd->index;
256 cmd_fusion = fusion->cmd_list[blk_tags];
257 megasas_return_cmd_fusion(instance, cmd_fusion);
258 }
259 cmd->scmd = NULL;
260 cmd->frame_count = 0;
261 cmd->flags = 0;
262 if (!fusion && reset_devices)
263 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
264 list_add(&cmd->list, (&instance->cmd_pool)->next);
265
266 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
267
268}
Sumant Patro1341c932006-01-25 12:02:40 -0800269
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530270static const char *
271format_timestamp(uint32_t timestamp)
272{
273 static char buffer[32];
274
275 if ((timestamp & 0xff000000) == 0xff000000)
276 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
277 0x00ffffff);
278 else
279 snprintf(buffer, sizeof(buffer), "%us", timestamp);
280 return buffer;
281}
282
283static const char *
284format_class(int8_t class)
285{
286 static char buffer[6];
287
288 switch (class) {
289 case MFI_EVT_CLASS_DEBUG:
290 return "debug";
291 case MFI_EVT_CLASS_PROGRESS:
292 return "progress";
293 case MFI_EVT_CLASS_INFO:
294 return "info";
295 case MFI_EVT_CLASS_WARNING:
296 return "WARN";
297 case MFI_EVT_CLASS_CRITICAL:
298 return "CRIT";
299 case MFI_EVT_CLASS_FATAL:
300 return "FATAL";
301 case MFI_EVT_CLASS_DEAD:
302 return "DEAD";
303 default:
304 snprintf(buffer, sizeof(buffer), "%d", class);
305 return buffer;
306 }
307}
308
309/**
310 * megasas_decode_evt: Decode FW AEN event and print critical event
311 * for information.
312 * @instance: Adapter soft state
313 */
314static void
315megasas_decode_evt(struct megasas_instance *instance)
316{
317 struct megasas_evt_detail *evt_detail = instance->evt_detail;
318 union megasas_evt_class_locale class_locale;
319 class_locale.word = le32_to_cpu(evt_detail->cl.word);
320
321 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
322 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
323 le32_to_cpu(evt_detail->seq_num),
324 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
325 (class_locale.members.locale),
326 format_class(class_locale.members.class),
327 evt_detail->description);
328}
329
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400330/**
adam radford0d490162010-12-14 19:17:17 -0800331* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800332* (deviceid : 1064R, PERC5) controllers
333*/
334
335/**
336 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400337 * @regs: MFI register set
338 */
339static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530340megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400341{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530342 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500343
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530344 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400345 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400346
347 /* Dummy readl to force pci flush */
348 readl(&regs->outbound_intr_mask);
349}
350
351/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700352 * megasas_disable_intr_xscale -Disables interrupt
353 * @regs: MFI register set
354 */
355static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530356megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700357{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530358 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700359 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500360
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530361 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700362 writel(mask, &regs->outbound_intr_mask);
363 /* Dummy readl to force pci flush */
364 readl(&regs->outbound_intr_mask);
365}
366
367/**
Sumant Patro1341c932006-01-25 12:02:40 -0800368 * megasas_read_fw_status_reg_xscale - returns the current FW status value
369 * @regs: MFI register set
370 */
371static u32
372megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
373{
374 return readl(&(regs)->outbound_msg_0);
375}
376/**
377 * megasas_clear_interrupt_xscale - Check & clear interrupt
378 * @regs: MFI register set
379 */
adam radford0d490162010-12-14 19:17:17 -0800380static int
Sumant Patro1341c932006-01-25 12:02:40 -0800381megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
382{
383 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400384 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500385
Sumant Patro1341c932006-01-25 12:02:40 -0800386 /*
387 * Check if it is our interrupt
388 */
389 status = readl(&regs->outbound_intr_status);
390
bo yang39a98552010-09-22 22:36:29 -0400391 if (status & MFI_OB_INTR_STATUS_MASK)
392 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
393 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
394 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800395
396 /*
397 * Clear the interrupt by writing back the same value
398 */
bo yang39a98552010-09-22 22:36:29 -0400399 if (mfiStatus)
400 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800401
Yang, Bo06f579d2008-08-10 12:42:37 -0700402 /* Dummy readl to force pci flush */
403 readl(&regs->outbound_intr_status);
404
bo yang39a98552010-09-22 22:36:29 -0400405 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800406}
407
408/**
409 * megasas_fire_cmd_xscale - Sends command to the FW
410 * @frame_phys_addr : Physical address of cmd
411 * @frame_count : Number of frames for the command
412 * @regs : MFI register set
413 */
adam radford0d490162010-12-14 19:17:17 -0800414static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600415megasas_fire_cmd_xscale(struct megasas_instance *instance,
416 dma_addr_t frame_phys_addr,
417 u32 frame_count,
418 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800419{
bo yang39a98552010-09-22 22:36:29 -0400420 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500421
bo yang39a98552010-09-22 22:36:29 -0400422 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800423 writel((frame_phys_addr >> 3)|(frame_count),
424 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400425 spin_unlock_irqrestore(&instance->hba_lock, flags);
426}
427
428/**
429 * megasas_adp_reset_xscale - For controller reset
430 * @regs: MFI register set
431 */
432static int
433megasas_adp_reset_xscale(struct megasas_instance *instance,
434 struct megasas_register_set __iomem *regs)
435{
436 u32 i;
437 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500438
bo yang39a98552010-09-22 22:36:29 -0400439 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
440
441 for (i = 0; i < 3; i++)
442 msleep(1000); /* sleep for 3 secs */
443 pcidata = 0;
444 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500445 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400446 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500447 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400448 pcidata &= ~0x2;
449 pci_write_config_dword(instance->pdev,
450 MFI_1068_PCSR_OFFSET, pcidata);
451
452 for (i = 0; i < 2; i++)
453 msleep(1000); /* need to wait 2 secs again */
454
455 pcidata = 0;
456 pci_read_config_dword(instance->pdev,
457 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500458 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400459 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500460 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400461 pcidata = 0;
462 pci_write_config_dword(instance->pdev,
463 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
464 }
465 }
466 return 0;
467}
468
469/**
470 * megasas_check_reset_xscale - For controller reset check
471 * @regs: MFI register set
472 */
473static int
474megasas_check_reset_xscale(struct megasas_instance *instance,
475 struct megasas_register_set __iomem *regs)
476{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530477 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530478 (le32_to_cpu(*instance->consumer) ==
479 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400480 return 1;
bo yang39a98552010-09-22 22:36:29 -0400481 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800482}
483
484static struct megasas_instance_template megasas_instance_template_xscale = {
485
486 .fire_cmd = megasas_fire_cmd_xscale,
487 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700488 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800489 .clear_intr = megasas_clear_intr_xscale,
490 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400491 .adp_reset = megasas_adp_reset_xscale,
492 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800493 .service_isr = megasas_isr,
494 .tasklet = megasas_complete_cmd_dpc,
495 .init_adapter = megasas_init_adapter_mfi,
496 .build_and_issue_cmd = megasas_build_and_issue_cmd,
497 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800498};
499
500/**
adam radford0d490162010-12-14 19:17:17 -0800501* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800502* to xscale (deviceid : 1064R, PERC5) controllers
503*/
504
505/**
adam radford0d490162010-12-14 19:17:17 -0800506* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500507* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800508*/
509
510/**
511 * megasas_enable_intr_ppc - Enables interrupts
512 * @regs: MFI register set
513 */
514static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530515megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800516{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530517 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500518
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530519 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800520 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800521
bo yang39a98552010-09-22 22:36:29 -0400522 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800523
524 /* Dummy readl to force pci flush */
525 readl(&regs->outbound_intr_mask);
526}
527
528/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700529 * megasas_disable_intr_ppc - Disable interrupt
530 * @regs: MFI register set
531 */
532static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530533megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700534{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530535 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700536 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500537
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530538 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700539 writel(mask, &regs->outbound_intr_mask);
540 /* Dummy readl to force pci flush */
541 readl(&regs->outbound_intr_mask);
542}
543
544/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800545 * megasas_read_fw_status_reg_ppc - returns the current FW status value
546 * @regs: MFI register set
547 */
548static u32
549megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
550{
551 return readl(&(regs)->outbound_scratch_pad);
552}
553
554/**
555 * megasas_clear_interrupt_ppc - Check & clear interrupt
556 * @regs: MFI register set
557 */
adam radford0d490162010-12-14 19:17:17 -0800558static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800559megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
560{
adam radford3cc6851f92011-05-11 18:34:52 -0700561 u32 status, mfiStatus = 0;
562
Sumant Patrof9876f02006-02-03 15:34:35 -0800563 /*
564 * Check if it is our interrupt
565 */
566 status = readl(&regs->outbound_intr_status);
567
adam radford3cc6851f92011-05-11 18:34:52 -0700568 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
569 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
570
571 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
572 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800573
574 /*
575 * Clear the interrupt by writing back the same value
576 */
577 writel(status, &regs->outbound_doorbell_clear);
578
Yang, Bo06f579d2008-08-10 12:42:37 -0700579 /* Dummy readl to force pci flush */
580 readl(&regs->outbound_doorbell_clear);
581
adam radford3cc6851f92011-05-11 18:34:52 -0700582 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800583}
adam radford3cc6851f92011-05-11 18:34:52 -0700584
Sumant Patrof9876f02006-02-03 15:34:35 -0800585/**
586 * megasas_fire_cmd_ppc - Sends command to the FW
587 * @frame_phys_addr : Physical address of cmd
588 * @frame_count : Number of frames for the command
589 * @regs : MFI register set
590 */
adam radford0d490162010-12-14 19:17:17 -0800591static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600592megasas_fire_cmd_ppc(struct megasas_instance *instance,
593 dma_addr_t frame_phys_addr,
594 u32 frame_count,
595 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800596{
bo yang39a98552010-09-22 22:36:29 -0400597 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500598
bo yang39a98552010-09-22 22:36:29 -0400599 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800600 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800601 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400602 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800603}
604
bo yang39a98552010-09-22 22:36:29 -0400605/**
bo yang39a98552010-09-22 22:36:29 -0400606 * megasas_check_reset_ppc - For controller reset check
607 * @regs: MFI register set
608 */
609static int
610megasas_check_reset_ppc(struct megasas_instance *instance,
611 struct megasas_register_set __iomem *regs)
612{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530613 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700614 return 1;
615
bo yang39a98552010-09-22 22:36:29 -0400616 return 0;
617}
adam radford3cc6851f92011-05-11 18:34:52 -0700618
Sumant Patrof9876f02006-02-03 15:34:35 -0800619static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800620
Sumant Patrof9876f02006-02-03 15:34:35 -0800621 .fire_cmd = megasas_fire_cmd_ppc,
622 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700623 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800624 .clear_intr = megasas_clear_intr_ppc,
625 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700626 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400627 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800628 .service_isr = megasas_isr,
629 .tasklet = megasas_complete_cmd_dpc,
630 .init_adapter = megasas_init_adapter_mfi,
631 .build_and_issue_cmd = megasas_build_and_issue_cmd,
632 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800633};
634
635/**
Yang, Bo87911122009-10-06 14:31:54 -0600636 * megasas_enable_intr_skinny - Enables interrupts
637 * @regs: MFI register set
638 */
639static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530640megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600641{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530642 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500643
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530644 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600645 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
646
647 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
648
649 /* Dummy readl to force pci flush */
650 readl(&regs->outbound_intr_mask);
651}
652
653/**
654 * megasas_disable_intr_skinny - Disables interrupt
655 * @regs: MFI register set
656 */
657static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530658megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600659{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530660 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600661 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500662
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530663 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600664 writel(mask, &regs->outbound_intr_mask);
665 /* Dummy readl to force pci flush */
666 readl(&regs->outbound_intr_mask);
667}
668
669/**
670 * megasas_read_fw_status_reg_skinny - returns the current FW status value
671 * @regs: MFI register set
672 */
673static u32
674megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
675{
676 return readl(&(regs)->outbound_scratch_pad);
677}
678
679/**
680 * megasas_clear_interrupt_skinny - Check & clear interrupt
681 * @regs: MFI register set
682 */
683static int
684megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
685{
686 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800687 u32 mfiStatus = 0;
688
Yang, Bo87911122009-10-06 14:31:54 -0600689 /*
690 * Check if it is our interrupt
691 */
692 status = readl(&regs->outbound_intr_status);
693
694 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400695 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600696 }
697
698 /*
adam radfordebf054b2011-02-24 20:57:15 -0800699 * Check if it is our interrupt
700 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600701 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800702 MFI_STATE_FAULT) {
703 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
704 } else
705 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
706
707 /*
Yang, Bo87911122009-10-06 14:31:54 -0600708 * Clear the interrupt by writing back the same value
709 */
710 writel(status, &regs->outbound_intr_status);
711
712 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500713 * dummy read to flush PCI
714 */
Yang, Bo87911122009-10-06 14:31:54 -0600715 readl(&regs->outbound_intr_status);
716
adam radfordebf054b2011-02-24 20:57:15 -0800717 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600718}
719
720/**
721 * megasas_fire_cmd_skinny - Sends command to the FW
722 * @frame_phys_addr : Physical address of cmd
723 * @frame_count : Number of frames for the command
724 * @regs : MFI register set
725 */
726static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600727megasas_fire_cmd_skinny(struct megasas_instance *instance,
728 dma_addr_t frame_phys_addr,
729 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600730 struct megasas_register_set __iomem *regs)
731{
Yang, Bo0c79e682009-10-06 14:47:35 -0600732 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500733
bo yang39a98552010-09-22 22:36:29 -0400734 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530735 writel(upper_32_bits(frame_phys_addr),
736 &(regs)->inbound_high_queue_port);
737 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
738 &(regs)->inbound_low_queue_port);
Tomas Henzlb99dbe52016-02-01 15:12:04 +0100739 mmiowb();
bo yang39a98552010-09-22 22:36:29 -0400740 spin_unlock_irqrestore(&instance->hba_lock, flags);
741}
742
743/**
bo yang39a98552010-09-22 22:36:29 -0400744 * megasas_check_reset_skinny - For controller reset check
745 * @regs: MFI register set
746 */
747static int
748megasas_check_reset_skinny(struct megasas_instance *instance,
749 struct megasas_register_set __iomem *regs)
750{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530751 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700752 return 1;
753
bo yang39a98552010-09-22 22:36:29 -0400754 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600755}
756
757static struct megasas_instance_template megasas_instance_template_skinny = {
758
759 .fire_cmd = megasas_fire_cmd_skinny,
760 .enable_intr = megasas_enable_intr_skinny,
761 .disable_intr = megasas_disable_intr_skinny,
762 .clear_intr = megasas_clear_intr_skinny,
763 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800764 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400765 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800766 .service_isr = megasas_isr,
767 .tasklet = megasas_complete_cmd_dpc,
768 .init_adapter = megasas_init_adapter_mfi,
769 .build_and_issue_cmd = megasas_build_and_issue_cmd,
770 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600771};
772
773
774/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700775* The following functions are defined for gen2 (deviceid : 0x78 0x79)
776* controllers
777*/
778
779/**
780 * megasas_enable_intr_gen2 - Enables interrupts
781 * @regs: MFI register set
782 */
783static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530784megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700785{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530786 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500787
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530788 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700789 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
790
791 /* write ~0x00000005 (4 & 1) to the intr mask*/
792 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
793
794 /* Dummy readl to force pci flush */
795 readl(&regs->outbound_intr_mask);
796}
797
798/**
799 * megasas_disable_intr_gen2 - Disables interrupt
800 * @regs: MFI register set
801 */
802static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530803megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700804{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530805 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700806 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500807
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530808 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700809 writel(mask, &regs->outbound_intr_mask);
810 /* Dummy readl to force pci flush */
811 readl(&regs->outbound_intr_mask);
812}
813
814/**
815 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
816 * @regs: MFI register set
817 */
818static u32
819megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
820{
821 return readl(&(regs)->outbound_scratch_pad);
822}
823
824/**
825 * megasas_clear_interrupt_gen2 - Check & clear interrupt
826 * @regs: MFI register set
827 */
828static int
829megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
830{
831 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400832 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500833
Yang, Bo6610a6b2008-08-10 12:42:38 -0700834 /*
835 * Check if it is our interrupt
836 */
837 status = readl(&regs->outbound_intr_status);
838
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530839 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400840 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
841 }
842 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
843 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
844 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700845
846 /*
847 * Clear the interrupt by writing back the same value
848 */
bo yang39a98552010-09-22 22:36:29 -0400849 if (mfiStatus)
850 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700851
852 /* Dummy readl to force pci flush */
853 readl(&regs->outbound_intr_status);
854
bo yang39a98552010-09-22 22:36:29 -0400855 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700856}
857/**
858 * megasas_fire_cmd_gen2 - Sends command to the FW
859 * @frame_phys_addr : Physical address of cmd
860 * @frame_count : Number of frames for the command
861 * @regs : MFI register set
862 */
863static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600864megasas_fire_cmd_gen2(struct megasas_instance *instance,
865 dma_addr_t frame_phys_addr,
866 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700867 struct megasas_register_set __iomem *regs)
868{
bo yang39a98552010-09-22 22:36:29 -0400869 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500870
bo yang39a98552010-09-22 22:36:29 -0400871 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700872 writel((frame_phys_addr | (frame_count<<1))|1,
873 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400874 spin_unlock_irqrestore(&instance->hba_lock, flags);
875}
876
877/**
878 * megasas_adp_reset_gen2 - For controller reset
879 * @regs: MFI register set
880 */
881static int
882megasas_adp_reset_gen2(struct megasas_instance *instance,
883 struct megasas_register_set __iomem *reg_set)
884{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500885 u32 retry = 0 ;
886 u32 HostDiag;
887 u32 __iomem *seq_offset = &reg_set->seq_offset;
888 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400889
adam radfordebf054b2011-02-24 20:57:15 -0800890 if (instance->instancet == &megasas_instance_template_skinny) {
891 seq_offset = &reg_set->fusion_seq_offset;
892 hostdiag_offset = &reg_set->fusion_host_diag;
893 }
894
895 writel(0, seq_offset);
896 writel(4, seq_offset);
897 writel(0xb, seq_offset);
898 writel(2, seq_offset);
899 writel(7, seq_offset);
900 writel(0xd, seq_offset);
901
bo yang39a98552010-09-22 22:36:29 -0400902 msleep(1000);
903
adam radfordebf054b2011-02-24 20:57:15 -0800904 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400905
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500906 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400907 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800908 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500909 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400910 retry, HostDiag);
911
912 if (retry++ >= 100)
913 return 1;
914
915 }
916
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500917 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400918
adam radfordebf054b2011-02-24 20:57:15 -0800919 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400920
921 ssleep(10);
922
adam radfordebf054b2011-02-24 20:57:15 -0800923 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500924 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400925 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800926 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500927 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400928 retry, HostDiag);
929
930 if (retry++ >= 1000)
931 return 1;
932
933 }
934 return 0;
935}
936
937/**
938 * megasas_check_reset_gen2 - For controller reset check
939 * @regs: MFI register set
940 */
941static int
942megasas_check_reset_gen2(struct megasas_instance *instance,
943 struct megasas_register_set __iomem *regs)
944{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530945 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600946 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600947
bo yang39a98552010-09-22 22:36:29 -0400948 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700949}
950
951static struct megasas_instance_template megasas_instance_template_gen2 = {
952
953 .fire_cmd = megasas_fire_cmd_gen2,
954 .enable_intr = megasas_enable_intr_gen2,
955 .disable_intr = megasas_disable_intr_gen2,
956 .clear_intr = megasas_clear_intr_gen2,
957 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400958 .adp_reset = megasas_adp_reset_gen2,
959 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800960 .service_isr = megasas_isr,
961 .tasklet = megasas_complete_cmd_dpc,
962 .init_adapter = megasas_init_adapter_mfi,
963 .build_and_issue_cmd = megasas_build_and_issue_cmd,
964 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700965};
966
967/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800968* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400969* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800970*/
971
adam radford9c915a82010-12-21 13:34:31 -0800972/*
973 * Template added for TB (Fusion)
974 */
975extern struct megasas_instance_template megasas_instance_template_fusion;
976
Sumant Patrof9876f02006-02-03 15:34:35 -0800977/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400978 * megasas_issue_polled - Issues a polling command
979 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800980 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400981 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530982 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400983 */
adam radford9c915a82010-12-21 13:34:31 -0800984int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400985megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
986{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400987 struct megasas_header *frame_hdr = &cmd->frame->hdr;
988
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530989 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530990 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991
Sumit Saxena8a01a412016-01-28 21:04:32 +0530992 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530993 (instance->instancet->issue_dcmd(instance, cmd))) {
994 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
995 __func__, __LINE__);
996 return DCMD_NOT_FIRED;
997 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530999 return wait_and_poll(instance, cmd, instance->requestorId ?
1000 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001001}
1002
1003/**
1004 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1005 * @instance: Adapter soft state
1006 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301007 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001008 *
1009 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001010 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001011 * Used to issue ioctl commands.
1012 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301013int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001014megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301015 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001016{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301017 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301018 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001019
Sumit Saxena8a01a412016-01-28 21:04:32 +05301020 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301021 (instance->instancet->issue_dcmd(instance, cmd))) {
1022 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1023 __func__, __LINE__);
1024 return DCMD_NOT_FIRED;
1025 }
1026
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301027 if (timeout) {
1028 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301029 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301030 if (!ret) {
1031 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1032 __func__, __LINE__);
1033 return DCMD_TIMEOUT;
1034 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301035 } else
1036 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301037 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001038
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301039 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301040 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001041}
1042
1043/**
1044 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1045 * @instance: Adapter soft state
1046 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301047 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001048 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301049 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001050 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001051 * cmd and waits for return status.
1052 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001053 */
1054static int
1055megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301056 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001057{
1058 struct megasas_cmd *cmd;
1059 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301060 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001061
1062 cmd = megasas_get_cmd(instance);
1063
1064 if (!cmd)
1065 return -1;
1066
1067 abort_fr = &cmd->frame->abort;
1068
1069 /*
1070 * Prepare and issue the abort frame
1071 */
1072 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301073 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301074 abort_fr->flags = cpu_to_le16(0);
1075 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1076 abort_fr->abort_mfi_phys_addr_lo =
1077 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1078 abort_fr->abort_mfi_phys_addr_hi =
1079 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001080
1081 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301082 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001083
Sumit Saxena8a01a412016-01-28 21:04:32 +05301084 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301085 (instance->instancet->issue_dcmd(instance, cmd))) {
1086 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1087 __func__, __LINE__);
1088 return DCMD_NOT_FIRED;
1089 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001090
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301091 if (timeout) {
1092 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301093 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301094 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301095 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1096 __func__, __LINE__);
1097 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301098 }
1099 } else
1100 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301101 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301102
bo yang39a98552010-09-22 22:36:29 -04001103 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001104
1105 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301106 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1107 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001108}
1109
1110/**
1111 * megasas_make_sgl32 - Prepares 32-bit SGL
1112 * @instance: Adapter soft state
1113 * @scp: SCSI command from the mid-layer
1114 * @mfi_sgl: SGL to be filled in
1115 *
1116 * If successful, this function returns the number of SG elements. Otherwise,
1117 * it returnes -1.
1118 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001119static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001120megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1121 union megasas_sgl *mfi_sgl)
1122{
1123 int i;
1124 int sge_count;
1125 struct scatterlist *os_sgl;
1126
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001127 sge_count = scsi_dma_map(scp);
1128 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001129
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001130 if (sge_count) {
1131 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301132 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1133 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001134 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001136 return sge_count;
1137}
1138
1139/**
1140 * megasas_make_sgl64 - Prepares 64-bit SGL
1141 * @instance: Adapter soft state
1142 * @scp: SCSI command from the mid-layer
1143 * @mfi_sgl: SGL to be filled in
1144 *
1145 * If successful, this function returns the number of SG elements. Otherwise,
1146 * it returnes -1.
1147 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001148static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001149megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1150 union megasas_sgl *mfi_sgl)
1151{
1152 int i;
1153 int sge_count;
1154 struct scatterlist *os_sgl;
1155
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001156 sge_count = scsi_dma_map(scp);
1157 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001158
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001159 if (sge_count) {
1160 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301161 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1162 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001163 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001164 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165 return sge_count;
1166}
1167
Yang, Bof4c9a132009-10-06 14:43:28 -06001168/**
1169 * megasas_make_sgl_skinny - Prepares IEEE SGL
1170 * @instance: Adapter soft state
1171 * @scp: SCSI command from the mid-layer
1172 * @mfi_sgl: SGL to be filled in
1173 *
1174 * If successful, this function returns the number of SG elements. Otherwise,
1175 * it returnes -1.
1176 */
1177static int
1178megasas_make_sgl_skinny(struct megasas_instance *instance,
1179 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1180{
1181 int i;
1182 int sge_count;
1183 struct scatterlist *os_sgl;
1184
1185 sge_count = scsi_dma_map(scp);
1186
1187 if (sge_count) {
1188 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301189 mfi_sgl->sge_skinny[i].length =
1190 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001191 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301192 cpu_to_le64(sg_dma_address(os_sgl));
1193 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001194 }
1195 }
1196 return sge_count;
1197}
1198
Sumant Patrob1df99d2006-10-03 12:40:47 -07001199 /**
1200 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001201 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001202 * @sge_count : number of sg elements
1203 *
1204 * Returns the number of frames required for numnber of sge's (sge_count)
1205 */
1206
Yang, Bof4c9a132009-10-06 14:43:28 -06001207static u32 megasas_get_frame_count(struct megasas_instance *instance,
1208 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001209{
1210 int num_cnt;
1211 int sge_bytes;
1212 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001213 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001214
1215 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1216 sizeof(struct megasas_sge32);
1217
Yang, Bof4c9a132009-10-06 14:43:28 -06001218 if (instance->flag_ieee) {
1219 sge_sz = sizeof(struct megasas_sge_skinny);
1220 }
1221
Sumant Patrob1df99d2006-10-03 12:40:47 -07001222 /*
bo yangd532dbe2008-03-17 03:36:43 -04001223 * Main frame can contain 2 SGEs for 64-bit SGLs and
1224 * 3 SGEs for 32-bit SGLs for ldio &
1225 * 1 SGEs for 64-bit SGLs and
1226 * 2 SGEs for 32-bit SGLs for pthru frame
1227 */
1228 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001229 if (instance->flag_ieee == 1) {
1230 num_cnt = sge_count - 1;
1231 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001232 num_cnt = sge_count - 1;
1233 else
1234 num_cnt = sge_count - 2;
1235 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001236 if (instance->flag_ieee == 1) {
1237 num_cnt = sge_count - 1;
1238 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001239 num_cnt = sge_count - 2;
1240 else
1241 num_cnt = sge_count - 3;
1242 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001243
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001244 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001245 sge_bytes = sge_sz * num_cnt;
1246
1247 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1248 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1249 }
1250 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001251 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001252
1253 if (frame_count > 7)
1254 frame_count = 8;
1255 return frame_count;
1256}
1257
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001258/**
1259 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1260 * @instance: Adapter soft state
1261 * @scp: SCSI command
1262 * @cmd: Command to be prepared in
1263 *
1264 * This function prepares CDB commands. These are typcially pass-through
1265 * commands to the devices.
1266 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001267static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001268megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1269 struct megasas_cmd *cmd)
1270{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001271 u32 is_logical;
1272 u32 device_id;
1273 u16 flags = 0;
1274 struct megasas_pthru_frame *pthru;
1275
1276 is_logical = MEGASAS_IS_LOGICAL(scp);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301277 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001278 pthru = (struct megasas_pthru_frame *)cmd->frame;
1279
1280 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1281 flags = MFI_FRAME_DIR_WRITE;
1282 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1283 flags = MFI_FRAME_DIR_READ;
1284 else if (scp->sc_data_direction == PCI_DMA_NONE)
1285 flags = MFI_FRAME_DIR_NONE;
1286
Yang, Bof4c9a132009-10-06 14:43:28 -06001287 if (instance->flag_ieee == 1) {
1288 flags |= MFI_FRAME_IEEE;
1289 }
1290
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001291 /*
1292 * Prepare the DCDB frame
1293 */
1294 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1295 pthru->cmd_status = 0x0;
1296 pthru->scsi_status = 0x0;
1297 pthru->target_id = device_id;
1298 pthru->lun = scp->device->lun;
1299 pthru->cdb_len = scp->cmd_len;
1300 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001301 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301302 pthru->flags = cpu_to_le16(flags);
1303 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001304
1305 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1306
1307 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001308 * If the command is for the tape device, set the
1309 * pthru timeout to the os layer timeout value.
1310 */
Yang, Bo8d568252009-10-06 14:12:21 -06001311 if (scp->device->type == TYPE_TAPE) {
1312 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301313 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001314 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301315 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001316 }
1317
1318 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001319 * Construct SGL
1320 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001321 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301322 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001323 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1324 &pthru->sgl);
1325 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301326 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001327 pthru->sge_count = megasas_make_sgl64(instance, scp,
1328 &pthru->sgl);
1329 } else
1330 pthru->sge_count = megasas_make_sgl32(instance, scp,
1331 &pthru->sgl);
1332
Yang, Bobdc6fb82009-12-06 08:30:19 -07001333 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001334 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001335 pthru->sge_count);
1336 return 0;
1337 }
1338
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001339 /*
1340 * Sense info specific
1341 */
1342 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301343 pthru->sense_buf_phys_addr_hi =
1344 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1345 pthru->sense_buf_phys_addr_lo =
1346 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001347
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001348 /*
1349 * Compute the total number of frames this command consumes. FW uses
1350 * this number to pull sufficient number of frames from host memory.
1351 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001352 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001353 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001354
1355 return cmd->frame_count;
1356}
1357
1358/**
1359 * megasas_build_ldio - Prepares IOs to logical devices
1360 * @instance: Adapter soft state
1361 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001362 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001363 *
1364 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1365 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001366static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001367megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1368 struct megasas_cmd *cmd)
1369{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001370 u32 device_id;
1371 u8 sc = scp->cmnd[0];
1372 u16 flags = 0;
1373 struct megasas_io_frame *ldio;
1374
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301375 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001376 ldio = (struct megasas_io_frame *)cmd->frame;
1377
1378 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1379 flags = MFI_FRAME_DIR_WRITE;
1380 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1381 flags = MFI_FRAME_DIR_READ;
1382
Yang, Bof4c9a132009-10-06 14:43:28 -06001383 if (instance->flag_ieee == 1) {
1384 flags |= MFI_FRAME_IEEE;
1385 }
1386
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001387 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001388 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001389 */
1390 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1391 ldio->cmd_status = 0x0;
1392 ldio->scsi_status = 0x0;
1393 ldio->target_id = device_id;
1394 ldio->timeout = 0;
1395 ldio->reserved_0 = 0;
1396 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301397 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001398 ldio->start_lba_hi = 0;
1399 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1400
1401 /*
1402 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1403 */
1404 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301405 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1406 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1407 ((u32) scp->cmnd[2] << 8) |
1408 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001409
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301410 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001411 }
1412
1413 /*
1414 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1415 */
1416 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301417 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1418 ((u32) scp->cmnd[7] << 8));
1419 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1420 ((u32) scp->cmnd[3] << 16) |
1421 ((u32) scp->cmnd[4] << 8) |
1422 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423 }
1424
1425 /*
1426 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1427 */
1428 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301429 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1430 ((u32) scp->cmnd[7] << 16) |
1431 ((u32) scp->cmnd[8] << 8) |
1432 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001433
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301434 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1435 ((u32) scp->cmnd[3] << 16) |
1436 ((u32) scp->cmnd[4] << 8) |
1437 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001438 }
1439
1440 /*
1441 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1442 */
1443 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301444 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1445 ((u32) scp->cmnd[11] << 16) |
1446 ((u32) scp->cmnd[12] << 8) |
1447 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001448
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301449 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1450 ((u32) scp->cmnd[7] << 16) |
1451 ((u32) scp->cmnd[8] << 8) |
1452 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001453
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301454 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1455 ((u32) scp->cmnd[3] << 16) |
1456 ((u32) scp->cmnd[4] << 8) |
1457 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001458
1459 }
1460
1461 /*
1462 * Construct SGL
1463 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001464 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301465 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001466 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1467 &ldio->sgl);
1468 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301469 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001470 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1471 } else
1472 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1473
Yang, Bobdc6fb82009-12-06 08:30:19 -07001474 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001475 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001476 ldio->sge_count);
1477 return 0;
1478 }
1479
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001480 /*
1481 * Sense info specific
1482 */
1483 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1484 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301485 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001486
Sumant Patrob1df99d2006-10-03 12:40:47 -07001487 /*
1488 * Compute the total number of frames this command consumes. FW uses
1489 * this number to pull sufficient number of frames from host memory.
1490 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001491 cmd->frame_count = megasas_get_frame_count(instance,
1492 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001493
1494 return cmd->frame_count;
1495}
1496
1497/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301498 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1499 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001500 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001501 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001502 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301503inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001504{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301505 int ret;
1506
Sumant Patrocb59aa62006-01-25 11:53:25 -08001507 switch (cmd->cmnd[0]) {
1508 case READ_10:
1509 case WRITE_10:
1510 case READ_12:
1511 case WRITE_12:
1512 case READ_6:
1513 case WRITE_6:
1514 case READ_16:
1515 case WRITE_16:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301516 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1517 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1518 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001519 default:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301520 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1521 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001522 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301523 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001524}
1525
Sumant Patro658dced2006-10-03 13:09:14 -07001526 /**
1527 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001528 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001529 * @instance: Adapter soft state
1530 */
1531static inline void
1532megasas_dump_pending_frames(struct megasas_instance *instance)
1533{
1534 struct megasas_cmd *cmd;
1535 int i,n;
1536 union megasas_sgl *mfi_sgl;
1537 struct megasas_io_frame *ldio;
1538 struct megasas_pthru_frame *pthru;
1539 u32 sgcount;
1540 u32 max_cmd = instance->max_fw_cmds;
1541
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001542 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1543 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 -07001544 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001545 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 -07001546 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001547 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 -07001548
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001549 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001550 for (i = 0; i < max_cmd; i++) {
1551 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001552 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001553 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001554 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 +05301555 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001556 ldio = (struct megasas_io_frame *)cmd->frame;
1557 mfi_sgl = &ldio->sgl;
1558 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001559 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 +05301560 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1561 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1562 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1563 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001564 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001565 pthru = (struct megasas_pthru_frame *) cmd->frame;
1566 mfi_sgl = &pthru->sgl;
1567 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001568 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 +05301569 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1570 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1571 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1572 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001573 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001574 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1575 for (n = 0; n < sgcount; n++) {
1576 if (IS_DMA64)
1577 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1578 le32_to_cpu(mfi_sgl->sge64[n].length),
1579 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1580 else
1581 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1582 le32_to_cpu(mfi_sgl->sge32[n].length),
1583 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001584 }
1585 }
Sumant Patro658dced2006-10-03 13:09:14 -07001586 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001587 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001588 for (i = 0; i < max_cmd; i++) {
1589
1590 cmd = instance->cmd_list[i];
1591
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001592 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001593 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001594 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001595 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001596}
1597
adam radfordcd50ba82010-12-21 10:23:23 -08001598u32
1599megasas_build_and_issue_cmd(struct megasas_instance *instance,
1600 struct scsi_cmnd *scmd)
1601{
1602 struct megasas_cmd *cmd;
1603 u32 frame_count;
1604
1605 cmd = megasas_get_cmd(instance);
1606 if (!cmd)
1607 return SCSI_MLQUEUE_HOST_BUSY;
1608
1609 /*
1610 * Logical drive command
1611 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301612 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001613 frame_count = megasas_build_ldio(instance, scmd, cmd);
1614 else
1615 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1616
1617 if (!frame_count)
1618 goto out_return_cmd;
1619
1620 cmd->scmd = scmd;
1621 scmd->SCp.ptr = (char *)cmd;
1622
1623 /*
1624 * Issue the command to the FW
1625 */
1626 atomic_inc(&instance->fw_outstanding);
1627
1628 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1629 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001630
1631 return 0;
1632out_return_cmd:
1633 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301634 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001635}
1636
1637
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001638/**
1639 * megasas_queue_command - Queue entry point
1640 * @scmd: SCSI command to be queued
1641 * @done: Callback entry point
1642 */
1643static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301644megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001645{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001646 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301647 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001648
1649 instance = (struct megasas_instance *)
1650 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001651
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301652 if (instance->unload == 1) {
1653 scmd->result = DID_NO_CONNECT << 16;
1654 scmd->scsi_done(scmd);
1655 return 0;
1656 }
1657
bo yang39a98552010-09-22 22:36:29 -04001658 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001659 return SCSI_MLQUEUE_HOST_BUSY;
1660
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301661
adam radford229fe472014-03-10 02:51:56 -07001662 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301663 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001664 if (megasas_check_mpio_paths(instance, scmd) ==
1665 (DID_RESET << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001666 return SCSI_MLQUEUE_HOST_BUSY;
1667 } else {
adam radford229fe472014-03-10 02:51:56 -07001668 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301669 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001670 return 0;
1671 }
1672 }
1673
Sumit Saxena8a01a412016-01-28 21:04:32 +05301674 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001675 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301676 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301677 return 0;
1678 }
1679
Sumit Saxena18365b12016-01-28 21:04:25 +05301680 mr_device_priv_data = scmd->device->hostdata;
1681 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301682 scmd->result = DID_NO_CONNECT << 16;
1683 scmd->scsi_done(scmd);
1684 return 0;
1685 }
1686
Sumit Saxena8a01a412016-01-28 21:04:32 +05301687 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001688 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001689
Sumit Saxena8a01a412016-01-28 21:04:32 +05301690 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301691 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301692
bo yang39a98552010-09-22 22:36:29 -04001693
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001694 scmd->result = 0;
1695
Sumant Patrocb59aa62006-01-25 11:53:25 -08001696 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301697 (scmd->device->id >= instance->fw_supported_vd_count ||
1698 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001699 scmd->result = DID_BAD_TARGET << 16;
1700 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001701 }
1702
Kashyap Desai1e793f62016-10-21 06:33:32 -07001703 /*
1704 * FW takes care of flush cache on its own for Virtual Disk.
1705 * No need to send it down for VD. For JBOD send SYNCHRONIZE_CACHE to FW.
1706 */
1707 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) && MEGASAS_IS_LOGICAL(scmd)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001708 scmd->result = DID_OK << 16;
1709 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001710 }
1711
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301712 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001713
Sumant Patrocb59aa62006-01-25 11:53:25 -08001714 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301715 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001716 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001717}
1718
Yang, Bo044833b2009-10-06 14:33:06 -06001719static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1720{
1721 int i;
1722
1723 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1724
1725 if ((megasas_mgmt_info.instance[i]) &&
1726 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1727 return megasas_mgmt_info.instance[i];
1728 }
1729
1730 return NULL;
1731}
1732
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301733/*
Sumit Saxena18365b12016-01-28 21:04:25 +05301734* megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301735*
1736* @sdev: OS provided scsi device
1737*
1738* Returns void
1739*/
Sumit Saxena18365b12016-01-28 21:04:25 +05301740void megasas_update_sdev_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301741{
Sumit Saxena18365b12016-01-28 21:04:25 +05301742 u16 pd_index = 0;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301743 u32 device_id, ld;
1744 struct megasas_instance *instance;
1745 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301746 struct MR_PRIV_DEVICE *mr_device_priv_data;
1747 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301748 struct MR_LD_RAID *raid;
1749 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1750
1751 instance = megasas_lookup_instance(sdev->host->host_no);
1752 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301753 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301754
1755 if (!fusion)
1756 return;
1757
Sumit Saxena18365b12016-01-28 21:04:25 +05301758 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1759 instance->use_seqnum_jbod_fp) {
1760 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1761 sdev->id;
1762 pd_sync = (void *)fusion->pd_seq_sync
1763 [(instance->pd_seq_map_id - 1) & 1];
1764 mr_device_priv_data->is_tm_capable =
1765 pd_sync->seq[pd_index].capability.tmCapable;
1766 } else {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301767 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1768 + sdev->id;
1769 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1770 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1771 raid = MR_LdRaidGet(ld, local_map_ptr);
1772
1773 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Sumit Saxena18365b12016-01-28 21:04:25 +05301774 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1775 mr_device_priv_data->is_tm_capable =
1776 raid->capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301777 }
1778}
1779
Sumit Saxena2216c302016-01-28 21:04:26 +05301780static void megasas_set_device_queue_depth(struct scsi_device *sdev)
1781{
1782 u16 pd_index = 0;
1783 int ret = DCMD_FAILED;
1784 struct megasas_instance *instance;
1785
1786 instance = megasas_lookup_instance(sdev->host->host_no);
1787
1788 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1789 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
1790
1791 if (instance->pd_info) {
1792 mutex_lock(&instance->hba_mutex);
1793 ret = megasas_get_pd_info(instance, pd_index);
1794 mutex_unlock(&instance->hba_mutex);
1795 }
1796
1797 if (ret != DCMD_SUCCESS)
1798 return;
1799
1800 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1801
1802 switch (instance->pd_list[pd_index].interface) {
1803 case SAS_PD:
1804 scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
1805 break;
1806
1807 case SATA_PD:
1808 scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
1809 break;
1810
1811 default:
1812 scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
1813 }
1814 }
1815 }
1816}
1817
Sumit Saxena18365b12016-01-28 21:04:25 +05301818
Christoph Hellwig147aab62006-02-17 12:13:48 +01001819static int megasas_slave_configure(struct scsi_device *sdev)
1820{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301821 u16 pd_index = 0;
1822 struct megasas_instance *instance;
1823
1824 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001825 if (instance->pd_list_not_supported) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301826 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1827 sdev->type == TYPE_DISK) {
1828 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1829 sdev->id;
1830 if (instance->pd_list[pd_index].driveState !=
1831 MR_PD_STATE_SYSTEM)
1832 return -ENXIO;
1833 }
1834 }
Sumit Saxena2216c302016-01-28 21:04:26 +05301835 megasas_set_device_queue_depth(sdev);
Sumit Saxena18365b12016-01-28 21:04:25 +05301836 megasas_update_sdev_properties(sdev);
1837
Yang, Bo044833b2009-10-06 14:33:06 -06001838 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001839 * The RAID firmware may require extended timeouts.
1840 */
Yang, Bo044833b2009-10-06 14:33:06 -06001841 blk_queue_rq_timeout(sdev->request_queue,
Sumit Saxenae3d178c2016-01-28 21:04:34 +05301842 scmd_timeout * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301843
Yang, Bo044833b2009-10-06 14:33:06 -06001844 return 0;
1845}
1846
1847static int megasas_slave_alloc(struct scsi_device *sdev)
1848{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001849 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001850 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301851 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001852
Yang, Bo044833b2009-10-06 14:33:06 -06001853 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301854 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001855 /*
1856 * Open the OS scan to the SYSTEM PD
1857 */
1858 pd_index =
1859 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1860 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08001861 if ((instance->pd_list_not_supported ||
1862 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301863 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301864 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001865 }
1866 return -ENXIO;
1867 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301868
1869scan_target:
1870 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1871 GFP_KERNEL);
1872 if (!mr_device_priv_data)
1873 return -ENOMEM;
1874 sdev->hostdata = mr_device_priv_data;
Christoph Hellwig147aab62006-02-17 12:13:48 +01001875 return 0;
1876}
1877
Sumit Saxena18365b12016-01-28 21:04:25 +05301878static void megasas_slave_destroy(struct scsi_device *sdev)
1879{
1880 kfree(sdev->hostdata);
1881 sdev->hostdata = NULL;
1882}
1883
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301884/*
1885* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1886* kill adapter
1887* @instance: Adapter soft state
1888*
1889*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301890static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301891{
1892 int i;
1893 struct megasas_cmd *cmd_mfi;
1894 struct megasas_cmd_fusion *cmd_fusion;
1895 struct fusion_context *fusion = instance->ctrl_context;
1896
1897 /* Find all outstanding ioctls */
1898 if (fusion) {
1899 for (i = 0; i < instance->max_fw_cmds; i++) {
1900 cmd_fusion = fusion->cmd_list[i];
1901 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1902 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1903 if (cmd_mfi->sync_cmd &&
Shivasharan Sd8817f52017-08-23 04:47:04 -07001904 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
1905 cmd_mfi->frame->hdr.cmd_status =
1906 MFI_STAT_WRONG_STATE;
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301907 megasas_complete_cmd(instance,
1908 cmd_mfi, DID_OK);
Shivasharan Sd8817f52017-08-23 04:47:04 -07001909 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301910 }
1911 }
1912 } else {
1913 for (i = 0; i < instance->max_fw_cmds; i++) {
1914 cmd_mfi = instance->cmd_list[i];
1915 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1916 MFI_CMD_ABORT)
1917 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1918 }
1919 }
1920}
1921
1922
adam radford9c915a82010-12-21 13:34:31 -08001923void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001924{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301925 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301926 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301927 /* Wait 1 second to ensure IO or ioctls in build have posted */
1928 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001929 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301930 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301931 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001932 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001933 /* Flush */
1934 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05301935 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07001936 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001937 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301938 writel(MFI_STOP_ADP,
1939 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001940 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301941 /* Complete outstanding ioctls when adapter is killed */
1942 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001943}
1944
1945 /**
1946 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1947 * restored to max value
1948 * @instance: Adapter soft state
1949 *
1950 */
1951void
1952megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1953{
1954 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301955
adam radford9c915a82010-12-21 13:34:31 -08001956 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001957 && time_after(jiffies, instance->last_time + 5 * HZ)
1958 && atomic_read(&instance->fw_outstanding) <
1959 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001960
1961 spin_lock_irqsave(instance->host->host_lock, flags);
1962 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001963
Sumit Saxena308ec452016-01-28 21:04:30 +05301964 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08001965 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001966 }
1967}
1968
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001969/**
bo yang7343eb62007-11-09 04:35:44 -05001970 * megasas_complete_cmd_dpc - Returns FW's controller structure
1971 * @instance_addr: Address of adapter soft state
1972 *
1973 * Tasklet to complete cmds
1974 */
1975static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1976{
1977 u32 producer;
1978 u32 consumer;
1979 u32 context;
1980 struct megasas_cmd *cmd;
1981 struct megasas_instance *instance =
1982 (struct megasas_instance *)instance_addr;
1983 unsigned long flags;
1984
1985 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301986 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05001987 return;
1988
1989 spin_lock_irqsave(&instance->completion_lock, flags);
1990
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301991 producer = le32_to_cpu(*instance->producer);
1992 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001993
1994 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301995 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001996 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001997 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04001998 context);
1999 BUG();
2000 }
bo yang7343eb62007-11-09 04:35:44 -05002001
2002 cmd = instance->cmd_list[context];
2003
2004 megasas_complete_cmd(instance, cmd, DID_OK);
2005
2006 consumer++;
2007 if (consumer == (instance->max_fw_cmds + 1)) {
2008 consumer = 0;
2009 }
2010 }
2011
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302012 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002013
2014 spin_unlock_irqrestore(&instance->completion_lock, flags);
2015
2016 /*
2017 * Check if we can restore can_queue
2018 */
adam radford9c915a82010-12-21 13:34:31 -08002019 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002020}
2021
adam radford229fe472014-03-10 02:51:56 -07002022/**
2023 * megasas_start_timer - Initializes a timer object
2024 * @instance: Adapter soft state
2025 * @timer: timer object to be initialized
2026 * @fn: timer function
2027 * @interval: time interval between timer function call
2028 *
2029 */
2030void megasas_start_timer(struct megasas_instance *instance,
2031 struct timer_list *timer,
2032 void *fn, unsigned long interval)
2033{
2034 init_timer(timer);
2035 timer->expires = jiffies + interval;
2036 timer->data = (unsigned long)instance;
2037 timer->function = fn;
2038 add_timer(timer);
2039}
2040
Yang, Bo707e09b2010-10-12 07:20:27 -06002041static void
2042megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2043
2044static void
2045process_fw_state_change_wq(struct work_struct *work);
2046
2047void megasas_do_ocr(struct megasas_instance *instance)
2048{
2049 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2050 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2051 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302052 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002053 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302054 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302055 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002056 instance->issuepend_done = 0;
2057
2058 atomic_set(&instance->fw_outstanding, 0);
2059 megasas_internal_reset_defer_cmds(instance);
2060 process_fw_state_change_wq(&instance->work_init);
2061}
2062
Adam Radford4cbfea82014-07-09 15:17:56 -07002063static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2064 int initial)
adam radford229fe472014-03-10 02:51:56 -07002065{
2066 struct megasas_cmd *cmd;
2067 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002068 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002069 dma_addr_t new_affiliation_111_h;
2070 int ld, retval = 0;
2071 u8 thisVf;
2072
2073 cmd = megasas_get_cmd(instance);
2074
2075 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002076 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2077 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002078 instance->host->host_no);
2079 return -ENOMEM;
2080 }
2081
2082 dcmd = &cmd->frame->dcmd;
2083
Adam Radford4cbfea82014-07-09 15:17:56 -07002084 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002085 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2086 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002087 megasas_return_cmd(instance, cmd);
2088 return -ENOMEM;
2089 }
2090
2091 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002092 memset(instance->vf_affiliation_111, 0,
2093 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002094 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002095 new_affiliation_111 =
2096 pci_alloc_consistent(instance->pdev,
2097 sizeof(struct MR_LD_VF_AFFILIATION_111),
2098 &new_affiliation_111_h);
2099 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002100 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2101 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002102 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002103 megasas_return_cmd(instance, cmd);
2104 return -ENOMEM;
2105 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002106 memset(new_affiliation_111, 0,
2107 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002108 }
2109
2110 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2111
2112 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302113 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002114 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302115 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002116 dcmd->timeout = 0;
2117 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302118 dcmd->data_xfer_len =
2119 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2120 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002121
Adam Radford4cbfea82014-07-09 15:17:56 -07002122 if (initial)
2123 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302124 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002125 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302126 dcmd->sgl.sge32[0].phys_addr =
2127 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002128
Christoph Hellwig2213a462015-04-23 16:33:54 +05302129 dcmd->sgl.sge32[0].length = cpu_to_le32(
2130 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002131
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002132 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002133 "scsi%d\n", instance->host->host_no);
2134
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302135 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002136 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2137 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002138 dcmd->cmd_status, instance->host->host_no);
2139 retval = 1; /* Do a scan if we couldn't get affiliation */
2140 goto out;
2141 }
2142
2143 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002144 thisVf = new_affiliation_111->thisVf;
2145 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2146 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2147 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002148 dev_warn(&instance->pdev->dev, "SR-IOV: "
2149 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002150 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002151 memcpy(instance->vf_affiliation_111,
2152 new_affiliation_111,
2153 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002154 retval = 1;
2155 goto out;
2156 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002157 }
2158out:
2159 if (new_affiliation_111) {
2160 pci_free_consistent(instance->pdev,
2161 sizeof(struct MR_LD_VF_AFFILIATION_111),
2162 new_affiliation_111,
2163 new_affiliation_111_h);
2164 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302165
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302166 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002167
2168 return retval;
2169}
2170
2171static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2172 int initial)
2173{
2174 struct megasas_cmd *cmd;
2175 struct megasas_dcmd_frame *dcmd;
2176 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2177 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2178 dma_addr_t new_affiliation_h;
2179 int i, j, retval = 0, found = 0, doscan = 0;
2180 u8 thisVf;
2181
2182 cmd = megasas_get_cmd(instance);
2183
2184 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002185 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2186 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002187 instance->host->host_no);
2188 return -ENOMEM;
2189 }
2190
2191 dcmd = &cmd->frame->dcmd;
2192
2193 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002194 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2195 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002196 megasas_return_cmd(instance, cmd);
2197 return -ENOMEM;
2198 }
2199
2200 if (initial)
2201 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2202 sizeof(struct MR_LD_VF_AFFILIATION));
2203 else {
2204 new_affiliation =
2205 pci_alloc_consistent(instance->pdev,
2206 (MAX_LOGICAL_DRIVES + 1) *
2207 sizeof(struct MR_LD_VF_AFFILIATION),
2208 &new_affiliation_h);
2209 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002210 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2211 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002212 instance->host->host_no);
2213 megasas_return_cmd(instance, cmd);
2214 return -ENOMEM;
2215 }
2216 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2217 sizeof(struct MR_LD_VF_AFFILIATION));
2218 }
2219
2220 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2221
2222 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302223 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002224 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302225 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002226 dcmd->timeout = 0;
2227 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302228 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2229 sizeof(struct MR_LD_VF_AFFILIATION));
2230 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002231
2232 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302233 dcmd->sgl.sge32[0].phys_addr =
2234 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002235 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302236 dcmd->sgl.sge32[0].phys_addr =
2237 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002238
Christoph Hellwig2213a462015-04-23 16:33:54 +05302239 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2240 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002241
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002242 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002243 "scsi%d\n", instance->host->host_no);
2244
Adam Radford4cbfea82014-07-09 15:17:56 -07002245
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302246 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002247 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2248 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002249 dcmd->cmd_status, instance->host->host_no);
2250 retval = 1; /* Do a scan if we couldn't get affiliation */
2251 goto out;
2252 }
2253
2254 if (!initial) {
2255 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002256 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2257 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002258 instance->host->host_no);
2259 retval = 1;
2260 goto out;
2261 }
2262 newmap = new_affiliation->map;
2263 savedmap = instance->vf_affiliation->map;
2264 thisVf = new_affiliation->thisVf;
2265 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2266 found = 0;
2267 for (j = 0; j < instance->vf_affiliation->ldCount;
2268 j++) {
2269 if (newmap->ref.targetId ==
2270 savedmap->ref.targetId) {
2271 found = 1;
2272 if (newmap->policy[thisVf] !=
2273 savedmap->policy[thisVf]) {
2274 doscan = 1;
2275 goto out;
2276 }
adam radford229fe472014-03-10 02:51:56 -07002277 }
2278 savedmap = (struct MR_LD_VF_MAP *)
2279 ((unsigned char *)savedmap +
2280 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002281 }
2282 if (!found && newmap->policy[thisVf] !=
2283 MR_LD_ACCESS_HIDDEN) {
2284 doscan = 1;
2285 goto out;
2286 }
2287 newmap = (struct MR_LD_VF_MAP *)
2288 ((unsigned char *)newmap + newmap->size);
2289 }
2290
2291 newmap = new_affiliation->map;
2292 savedmap = instance->vf_affiliation->map;
2293
2294 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2295 found = 0;
2296 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2297 if (savedmap->ref.targetId ==
2298 newmap->ref.targetId) {
2299 found = 1;
2300 if (savedmap->policy[thisVf] !=
2301 newmap->policy[thisVf]) {
2302 doscan = 1;
2303 goto out;
2304 }
2305 }
adam radford229fe472014-03-10 02:51:56 -07002306 newmap = (struct MR_LD_VF_MAP *)
2307 ((unsigned char *)newmap +
2308 newmap->size);
2309 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002310 if (!found && savedmap->policy[thisVf] !=
2311 MR_LD_ACCESS_HIDDEN) {
2312 doscan = 1;
2313 goto out;
2314 }
2315 savedmap = (struct MR_LD_VF_MAP *)
2316 ((unsigned char *)savedmap +
2317 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002318 }
2319 }
2320out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002321 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002322 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2323 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002324 memcpy(instance->vf_affiliation, new_affiliation,
2325 new_affiliation->size);
2326 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002327 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002328
2329 if (new_affiliation)
2330 pci_free_consistent(instance->pdev,
2331 (MAX_LOGICAL_DRIVES + 1) *
2332 sizeof(struct MR_LD_VF_AFFILIATION),
2333 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302334 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002335
2336 return retval;
2337}
2338
Adam Radford4cbfea82014-07-09 15:17:56 -07002339/* This function will get the current SR-IOV LD/VF affiliation */
2340static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2341 int initial)
2342{
2343 int retval;
2344
2345 if (instance->PlasmaFW111)
2346 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2347 else
2348 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2349 return retval;
2350}
2351
adam radford229fe472014-03-10 02:51:56 -07002352/* This function will tell FW to start the SR-IOV heartbeat */
2353int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2354 int initial)
2355{
2356 struct megasas_cmd *cmd;
2357 struct megasas_dcmd_frame *dcmd;
2358 int retval = 0;
2359
2360 cmd = megasas_get_cmd(instance);
2361
2362 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002363 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2364 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002365 instance->host->host_no);
2366 return -ENOMEM;
2367 }
2368
2369 dcmd = &cmd->frame->dcmd;
2370
2371 if (initial) {
2372 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002373 pci_zalloc_consistent(instance->pdev,
2374 sizeof(struct MR_CTRL_HB_HOST_MEM),
2375 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002376 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002377 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2378 " memory for heartbeat host memory for scsi%d\n",
2379 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002380 retval = -ENOMEM;
2381 goto out;
2382 }
adam radford229fe472014-03-10 02:51:56 -07002383 }
2384
2385 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2386
Christoph Hellwig2213a462015-04-23 16:33:54 +05302387 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002388 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302389 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002390 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302391 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002392 dcmd->timeout = 0;
2393 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302394 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2395 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2396 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2397 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002398
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002399 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002400 instance->host->host_no);
2401
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302402 if (instance->ctrl_context && !instance->mask_interrupts)
2403 retval = megasas_issue_blocked_cmd(instance, cmd,
2404 MEGASAS_ROUTINE_WAIT_TIME_VF);
2405 else
2406 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002407
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302408 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302409 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2410 "_MEM_ALLOC DCMD %s for scsi%d\n",
2411 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2412 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002413 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002414 }
2415
2416out:
2417 megasas_return_cmd(instance, cmd);
2418
2419 return retval;
2420}
2421
2422/* Handler for SR-IOV heartbeat */
2423void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2424{
2425 struct megasas_instance *instance =
2426 (struct megasas_instance *)instance_addr;
2427
2428 if (instance->hb_host_mem->HB.fwCounter !=
2429 instance->hb_host_mem->HB.driverCounter) {
2430 instance->hb_host_mem->HB.driverCounter =
2431 instance->hb_host_mem->HB.fwCounter;
2432 mod_timer(&instance->sriov_heartbeat_timer,
2433 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2434 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002435 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002436 "completed for scsi%d\n", instance->host->host_no);
2437 schedule_work(&instance->work_init);
2438 }
2439}
2440
bo yang7343eb62007-11-09 04:35:44 -05002441/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002442 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2443 * @instance: Adapter soft state
2444 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002445 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002446 * complete all its outstanding commands. Returns error if one or more IOs
2447 * are pending after this time period. It also marks the controller dead.
2448 */
2449static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2450{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302451 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002452 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002453 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002454 unsigned long flags;
2455 struct list_head clist_local;
2456 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002457 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002458
Sumit Saxenaccc75072016-01-28 21:04:33 +05302459 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2460 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2461 __func__, __LINE__);
2462 return FAILED;
2463 }
bo yang39a98552010-09-22 22:36:29 -04002464
Sumit Saxena8a01a412016-01-28 21:04:32 +05302465 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002466
2467 INIT_LIST_HEAD(&clist_local);
2468 spin_lock_irqsave(&instance->hba_lock, flags);
2469 list_splice_init(&instance->internal_reset_pending_q,
2470 &clist_local);
2471 spin_unlock_irqrestore(&instance->hba_lock, flags);
2472
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002473 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002474 for (i = 0; i < wait_time; i++) {
2475 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302476 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002477 break;
2478 }
2479
Sumit Saxena8a01a412016-01-28 21:04:32 +05302480 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002481 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302482 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002483 return FAILED;
2484 }
2485
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002486 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002487 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002488 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002489 struct megasas_cmd, list);
2490 list_del_init(&reset_cmd->list);
2491 if (reset_cmd->scmd) {
2492 reset_cmd->scmd->result = DID_RESET << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002493 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002494 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002495 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002496
2497 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2498 megasas_return_cmd(instance, reset_cmd);
2499 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002500 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002501 "reset queue\n",
2502 reset_cmd);
2503
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302504 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002505 instance->instancet->fire_cmd(instance,
2506 reset_cmd->frame_phys_addr,
2507 0, instance->reg_set);
2508 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002509 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002510 "cmds lst\n",
2511 reset_cmd);
2512 }
2513 reset_index++;
2514 }
2515
2516 return SUCCESS;
2517 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002518
adam radfordc007b8b2012-07-17 18:20:24 -07002519 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302520 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002521
2522 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002523 break;
2524
2525 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002526 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002527 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002528 /*
2529 * Call cmd completion routine. Cmd to be
2530 * be completed directly without depending on isr.
2531 */
2532 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002533 }
2534
2535 msleep(1000);
2536 }
2537
Yang, Bo707e09b2010-10-12 07:20:27 -06002538 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302539 outstanding = atomic_read(&instance->fw_outstanding);
2540 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002541
Sumit Saxenaccc75072016-01-28 21:04:33 +05302542 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2543 goto no_outstanding;
2544
2545 if (instance->disableOnlineCtrlReset)
2546 goto kill_hba_and_failed;
2547 do {
2548 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2549 dev_info(&instance->pdev->dev,
2550 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2551 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2552 if (i == 3)
2553 goto kill_hba_and_failed;
2554 megasas_do_ocr(instance);
2555
2556 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2557 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2558 __func__, __LINE__);
2559 return FAILED;
2560 }
2561 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2562 __func__, __LINE__);
2563
2564 for (sl = 0; sl < 10; sl++)
2565 msleep(500);
2566
2567 outstanding = atomic_read(&instance->fw_outstanding);
2568
2569 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2570 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2571 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002572 }
2573 i++;
2574 } while (i <= 3);
2575
Sumit Saxenaccc75072016-01-28 21:04:33 +05302576no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002577
Sumit Saxenaccc75072016-01-28 21:04:33 +05302578 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2579 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002580 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302581
2582kill_hba_and_failed:
2583
2584 /* Reset not supported, kill adapter */
2585 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2586 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2587 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2588 atomic_read(&instance->fw_outstanding));
2589 megasas_dump_pending_frames(instance);
2590 megaraid_sas_kill_hba(instance);
2591
2592 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002593}
2594
2595/**
2596 * megasas_generic_reset - Generic reset routine
2597 * @scmd: Mid-layer SCSI command
2598 *
2599 * This routine implements a generic reset handler for device, bus and host
2600 * reset requests. Device, bus and host specific reset handlers can use this
2601 * function after they do their specific tasks.
2602 */
2603static int megasas_generic_reset(struct scsi_cmnd *scmd)
2604{
2605 int ret_val;
2606 struct megasas_instance *instance;
2607
2608 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2609
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002610 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2611 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002612
Sumit Saxena8a01a412016-01-28 21:04:32 +05302613 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002614 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002615 return FAILED;
2616 }
2617
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002618 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002619 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002620 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002621 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002622 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002623
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002624 return ret_val;
2625}
2626
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002627/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002628 * megasas_reset_timer - quiesce the adapter if required
2629 * @scmd: scsi cmnd
2630 *
2631 * Sets the FW busy flag and reduces the host->can_queue if the
2632 * cmd has not been completed within the timeout period.
2633 */
2634static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002635blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002636{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002637 struct megasas_instance *instance;
2638 unsigned long flags;
2639
2640 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302641 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002642 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002643 }
2644
adam radfordf575c5d2011-10-13 16:01:12 -07002645 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002646 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2647 /* FW is busy, throttle IO */
2648 spin_lock_irqsave(instance->host->host_lock, flags);
2649
adam radfordc5daa6a2012-07-17 18:20:03 -07002650 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002651 instance->last_time = jiffies;
2652 instance->flag |= MEGASAS_FW_BUSY;
2653
2654 spin_unlock_irqrestore(instance->host->host_lock, flags);
2655 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002656 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002657}
2658
2659/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002660 * megasas_reset_bus_host - Bus & host reset handler entry point
2661 */
2662static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2663{
2664 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002665 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002666
adam radford9c915a82010-12-21 13:34:31 -08002667 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002668
2669 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002670 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002671 */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302672 if (instance->ctrl_context)
adam radford229fe472014-03-10 02:51:56 -07002673 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002674 else
2675 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002676
2677 return ret;
2678}
2679
2680/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002681 * megasas_task_abort - Issues task abort request to firmware
2682 * (supported only for fusion adapters)
2683 * @scmd: SCSI command pointer
2684 */
2685static int megasas_task_abort(struct scsi_cmnd *scmd)
2686{
2687 int ret;
2688 struct megasas_instance *instance;
2689
2690 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2691
2692 if (instance->ctrl_context)
2693 ret = megasas_task_abort_fusion(scmd);
2694 else {
2695 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2696 ret = FAILED;
2697 }
2698
2699 return ret;
2700}
2701
2702/**
2703 * megasas_reset_target: Issues target reset request to firmware
2704 * (supported only for fusion adapters)
2705 * @scmd: SCSI command pointer
2706 */
2707static int megasas_reset_target(struct scsi_cmnd *scmd)
2708{
2709 int ret;
2710 struct megasas_instance *instance;
2711
2712 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2713
2714 if (instance->ctrl_context)
2715 ret = megasas_reset_target_fusion(scmd);
2716 else {
2717 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2718 ret = FAILED;
2719 }
2720
2721 return ret;
2722}
2723
2724/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002725 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002726 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002727 * @bdev: block device
2728 * @capacity: drive capacity
2729 * @geom: geometry parameters
2730 */
2731static int
2732megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2733 sector_t capacity, int geom[])
2734{
2735 int heads;
2736 int sectors;
2737 sector_t cylinders;
2738 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002739
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002740 /* Default heads (64) & sectors (32) */
2741 heads = 64;
2742 sectors = 32;
2743
2744 tmp = heads * sectors;
2745 cylinders = capacity;
2746
2747 sector_div(cylinders, tmp);
2748
2749 /*
2750 * Handle extended translation size for logical drives > 1Gb
2751 */
2752
2753 if (capacity >= 0x200000) {
2754 heads = 255;
2755 sectors = 63;
2756 tmp = heads*sectors;
2757 cylinders = capacity;
2758 sector_div(cylinders, tmp);
2759 }
2760
2761 geom[0] = heads;
2762 geom[1] = sectors;
2763 geom[2] = cylinders;
2764
2765 return 0;
2766}
2767
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002768static void megasas_aen_polling(struct work_struct *work);
2769
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002770/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002771 * megasas_service_aen - Processes an event notification
2772 * @instance: Adapter soft state
2773 * @cmd: AEN command completed by the ISR
2774 *
2775 * For AEN, driver sends a command down to FW that is held by the FW till an
2776 * event occurs. When an event of interest occurs, FW completes the command
2777 * that it was previously holding.
2778 *
2779 * This routines sends SIGIO signal to processes that have registered with the
2780 * driver for AEN.
2781 */
2782static void
2783megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2784{
Yang, Boc3518832009-10-06 14:18:02 -06002785 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002786
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002787 /*
2788 * Don't signal app if it is just an aborted previously registered aen
2789 */
Yang, Boc3518832009-10-06 14:18:02 -06002790 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2791 spin_lock_irqsave(&poll_aen_lock, flags);
2792 megasas_poll_wait_aen = 1;
2793 spin_unlock_irqrestore(&poll_aen_lock, flags);
2794 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002795 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002796 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002797 else
2798 cmd->abort_aen = 0;
2799
2800 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302801
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302802 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002803
bo yang39a98552010-09-22 22:36:29 -04002804 if ((instance->unload == 0) &&
2805 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002806 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002807
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002808 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2809 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002810 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002811 } else {
2812 ev->instance = instance;
2813 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002814 INIT_DELAYED_WORK(&ev->hotplug_work,
2815 megasas_aen_polling);
2816 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002817 }
2818 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002819}
2820
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302821static ssize_t
2822megasas_fw_crash_buffer_store(struct device *cdev,
2823 struct device_attribute *attr, const char *buf, size_t count)
2824{
2825 struct Scsi_Host *shost = class_to_shost(cdev);
2826 struct megasas_instance *instance =
2827 (struct megasas_instance *) shost->hostdata;
2828 int val = 0;
2829 unsigned long flags;
2830
2831 if (kstrtoint(buf, 0, &val) != 0)
2832 return -EINVAL;
2833
2834 spin_lock_irqsave(&instance->crashdump_lock, flags);
2835 instance->fw_crash_buffer_offset = val;
2836 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2837 return strlen(buf);
2838}
2839
2840static ssize_t
2841megasas_fw_crash_buffer_show(struct device *cdev,
2842 struct device_attribute *attr, char *buf)
2843{
2844 struct Scsi_Host *shost = class_to_shost(cdev);
2845 struct megasas_instance *instance =
2846 (struct megasas_instance *) shost->hostdata;
2847 u32 size;
2848 unsigned long buff_addr;
2849 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2850 unsigned long src_addr;
2851 unsigned long flags;
2852 u32 buff_offset;
2853
2854 spin_lock_irqsave(&instance->crashdump_lock, flags);
2855 buff_offset = instance->fw_crash_buffer_offset;
2856 if (!instance->crash_dump_buf &&
2857 !((instance->fw_crash_state == AVAILABLE) ||
2858 (instance->fw_crash_state == COPYING))) {
2859 dev_err(&instance->pdev->dev,
2860 "Firmware crash dump is not available\n");
2861 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2862 return -EINVAL;
2863 }
2864
2865 buff_addr = (unsigned long) buf;
2866
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002867 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302868 dev_err(&instance->pdev->dev,
2869 "Firmware crash dump offset is out of range\n");
2870 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2871 return 0;
2872 }
2873
2874 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2875 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2876
2877 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2878 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002879 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302880 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2881
2882 return size;
2883}
2884
2885static ssize_t
2886megasas_fw_crash_buffer_size_show(struct device *cdev,
2887 struct device_attribute *attr, char *buf)
2888{
2889 struct Scsi_Host *shost = class_to_shost(cdev);
2890 struct megasas_instance *instance =
2891 (struct megasas_instance *) shost->hostdata;
2892
2893 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2894 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2895}
2896
2897static ssize_t
2898megasas_fw_crash_state_store(struct device *cdev,
2899 struct device_attribute *attr, const char *buf, size_t count)
2900{
2901 struct Scsi_Host *shost = class_to_shost(cdev);
2902 struct megasas_instance *instance =
2903 (struct megasas_instance *) shost->hostdata;
2904 int val = 0;
2905 unsigned long flags;
2906
2907 if (kstrtoint(buf, 0, &val) != 0)
2908 return -EINVAL;
2909
2910 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2911 dev_err(&instance->pdev->dev, "application updates invalid "
2912 "firmware crash state\n");
2913 return -EINVAL;
2914 }
2915
2916 instance->fw_crash_state = val;
2917
2918 if ((val == COPIED) || (val == COPY_ERROR)) {
2919 spin_lock_irqsave(&instance->crashdump_lock, flags);
2920 megasas_free_host_crash_buffer(instance);
2921 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2922 if (val == COPY_ERROR)
2923 dev_info(&instance->pdev->dev, "application failed to "
2924 "copy Firmware crash dump\n");
2925 else
2926 dev_info(&instance->pdev->dev, "Firmware crash dump "
2927 "copied successfully\n");
2928 }
2929 return strlen(buf);
2930}
2931
2932static ssize_t
2933megasas_fw_crash_state_show(struct device *cdev,
2934 struct device_attribute *attr, char *buf)
2935{
2936 struct Scsi_Host *shost = class_to_shost(cdev);
2937 struct megasas_instance *instance =
2938 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002939
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302940 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2941}
2942
2943static ssize_t
2944megasas_page_size_show(struct device *cdev,
2945 struct device_attribute *attr, char *buf)
2946{
2947 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2948}
2949
Sumit Saxena308ec452016-01-28 21:04:30 +05302950static ssize_t
2951megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
2952 char *buf)
2953{
2954 struct Scsi_Host *shost = class_to_shost(cdev);
2955 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
2956
2957 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
2958}
2959
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302960static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2961 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2962static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2963 megasas_fw_crash_buffer_size_show, NULL);
2964static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2965 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2966static DEVICE_ATTR(page_size, S_IRUGO,
2967 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05302968static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
2969 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302970
2971struct device_attribute *megaraid_host_attrs[] = {
2972 &dev_attr_fw_crash_buffer_size,
2973 &dev_attr_fw_crash_buffer,
2974 &dev_attr_fw_crash_state,
2975 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05302976 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302977 NULL,
2978};
2979
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002980/*
2981 * Scsi host template for megaraid_sas driver
2982 */
2983static struct scsi_host_template megasas_template = {
2984
2985 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05302986 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002987 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002988 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002989 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05302990 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002991 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002992 .eh_target_reset_handler = megasas_reset_target,
2993 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002994 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002995 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302996 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002997 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002998 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002999 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003000 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003001};
3002
3003/**
3004 * megasas_complete_int_cmd - Completes an internal command
3005 * @instance: Adapter soft state
3006 * @cmd: Command to be completed
3007 *
3008 * The megasas_issue_blocked_cmd() function waits for a command to complete
3009 * after it issues a command. This function wakes up that waiting routine by
3010 * calling wake_up() on the wait queue.
3011 */
3012static void
3013megasas_complete_int_cmd(struct megasas_instance *instance,
3014 struct megasas_cmd *cmd)
3015{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303016 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003017 wake_up(&instance->int_cmd_wait_q);
3018}
3019
3020/**
3021 * megasas_complete_abort - Completes aborting a command
3022 * @instance: Adapter soft state
3023 * @cmd: Cmd that was issued to abort another cmd
3024 *
adam radford0d490162010-12-14 19:17:17 -08003025 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3026 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003027 * wakes up all functions waiting on the same wait queue.
3028 */
3029static void
3030megasas_complete_abort(struct megasas_instance *instance,
3031 struct megasas_cmd *cmd)
3032{
3033 if (cmd->sync_cmd) {
3034 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303035 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003036 wake_up(&instance->abort_cmd_wait_q);
3037 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003038}
3039
3040/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003041 * megasas_complete_cmd - Completes a command
3042 * @instance: Adapter soft state
3043 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003044 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003045 * SCSI mid-layer instead of the value returned
3046 * by the FW. This should be used if caller wants
3047 * an alternate status (as in the case of aborted
3048 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003049 */
adam radford9c915a82010-12-21 13:34:31 -08003050void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003051megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3052 u8 alt_status)
3053{
3054 int exception = 0;
3055 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003056 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003057 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303058 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003059
bo yang39a98552010-09-22 22:36:29 -04003060 /* flag for the retry reset */
3061 cmd->retry_for_fw_reset = 0;
3062
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003063 if (cmd->scmd)
3064 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003065
3066 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003067 case MFI_CMD_INVALID:
3068 /* Some older 1068 controller FW may keep a pended
3069 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3070 when booting the kdump kernel. Ignore this command to
3071 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003072 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3073 "completed\n");
3074 dev_warn(&instance->pdev->dev, "If you have a controller "
3075 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003076 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003077 case MFI_CMD_PD_SCSI_IO:
3078 case MFI_CMD_LD_SCSI_IO:
3079
3080 /*
3081 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3082 * issued either through an IO path or an IOCTL path. If it
3083 * was via IOCTL, we will send it to internal completion.
3084 */
3085 if (cmd->sync_cmd) {
3086 cmd->sync_cmd = 0;
3087 megasas_complete_int_cmd(instance, cmd);
3088 break;
3089 }
3090
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003091 case MFI_CMD_LD_READ:
3092 case MFI_CMD_LD_WRITE:
3093
3094 if (alt_status) {
3095 cmd->scmd->result = alt_status << 16;
3096 exception = 1;
3097 }
3098
3099 if (exception) {
3100
Sumant Patroe4a082c2006-05-30 12:03:37 -07003101 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003102
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003103 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003104 cmd->scmd->scsi_done(cmd->scmd);
3105 megasas_return_cmd(instance, cmd);
3106
3107 break;
3108 }
3109
3110 switch (hdr->cmd_status) {
3111
3112 case MFI_STAT_OK:
3113 cmd->scmd->result = DID_OK << 16;
3114 break;
3115
3116 case MFI_STAT_SCSI_IO_FAILED:
3117 case MFI_STAT_LD_INIT_IN_PROGRESS:
3118 cmd->scmd->result =
3119 (DID_ERROR << 16) | hdr->scsi_status;
3120 break;
3121
3122 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3123
3124 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3125
3126 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3127 memset(cmd->scmd->sense_buffer, 0,
3128 SCSI_SENSE_BUFFERSIZE);
3129 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3130 hdr->sense_len);
3131
3132 cmd->scmd->result |= DRIVER_SENSE << 24;
3133 }
3134
3135 break;
3136
3137 case MFI_STAT_LD_OFFLINE:
3138 case MFI_STAT_DEVICE_NOT_FOUND:
3139 cmd->scmd->result = DID_BAD_TARGET << 16;
3140 break;
3141
3142 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003143 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003144 hdr->cmd_status);
3145 cmd->scmd->result = DID_ERROR << 16;
3146 break;
3147 }
3148
Sumant Patroe4a082c2006-05-30 12:03:37 -07003149 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003150
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003151 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003152 cmd->scmd->scsi_done(cmd->scmd);
3153 megasas_return_cmd(instance, cmd);
3154
3155 break;
3156
3157 case MFI_CMD_SMP:
3158 case MFI_CMD_STP:
3159 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303160 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003161 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303162 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3163 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303164 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003165 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303166 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003167 if (cmd->frame->hdr.cmd_status != 0) {
3168 if (cmd->frame->hdr.cmd_status !=
3169 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003170 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003171 cmd->frame->hdr.cmd_status);
3172 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303173 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003174 spin_unlock_irqrestore(
3175 instance->host->host_lock,
3176 flags);
3177 break;
3178 }
3179 } else
3180 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303181 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303182
3183 /*
3184 * Set fast path IO to ZERO.
3185 * Validate Map will set proper value.
3186 * Meanwhile all IOs will go as LD IO.
3187 */
3188 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003189 fusion->fast_path_io = 1;
3190 else
3191 fusion->fast_path_io = 0;
3192 megasas_sync_map_info(instance);
3193 spin_unlock_irqrestore(instance->host->host_lock,
3194 flags);
3195 break;
3196 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303197 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3198 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003199 spin_lock_irqsave(&poll_aen_lock, flags);
3200 megasas_poll_wait_aen = 0;
3201 spin_unlock_irqrestore(&poll_aen_lock, flags);
3202 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003203
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303204 /* FW has an updated PD sequence */
3205 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3206 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3207
3208 spin_lock_irqsave(instance->host->host_lock, flags);
3209 status = cmd->frame->hdr.cmd_status;
3210 instance->jbod_seq_cmd = NULL;
3211 megasas_return_cmd(instance, cmd);
3212
3213 if (status == MFI_STAT_OK) {
3214 instance->pd_seq_map_id++;
3215 /* Re-register a pd sync seq num cmd */
3216 if (megasas_sync_pd_seq_num(instance, true))
3217 instance->use_seqnum_jbod_fp = false;
3218 } else
3219 instance->use_seqnum_jbod_fp = false;
3220
3221 spin_unlock_irqrestore(instance->host->host_lock, flags);
3222 break;
3223 }
3224
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003225 /*
3226 * See if got an event notification
3227 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303228 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003229 megasas_service_aen(instance, cmd);
3230 else
3231 megasas_complete_int_cmd(instance, cmd);
3232
3233 break;
3234
3235 case MFI_CMD_ABORT:
3236 /*
3237 * Cmd issued to abort another cmd returned
3238 */
3239 megasas_complete_abort(instance, cmd);
3240 break;
3241
3242 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003243 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003244 hdr->cmd);
3245 break;
3246 }
3247}
3248
3249/**
bo yang39a98552010-09-22 22:36:29 -04003250 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003251 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003252 * @instance: Adapter soft state
3253 */
3254static inline void
3255megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3256{
3257 struct megasas_cmd *cmd;
3258 struct list_head clist_local;
3259 union megasas_evt_class_locale class_locale;
3260 unsigned long flags;
3261 u32 seq_num;
3262
3263 INIT_LIST_HEAD(&clist_local);
3264 spin_lock_irqsave(&instance->hba_lock, flags);
3265 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3266 spin_unlock_irqrestore(&instance->hba_lock, flags);
3267
3268 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003269 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003270 struct megasas_cmd, list);
3271 list_del_init(&cmd->list);
3272
3273 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003274 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3275 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003276 cmd, cmd->scmd, cmd->sync_cmd);
3277
3278 cmd->retry_for_fw_reset++;
3279
3280 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003281 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003282 "was tried multiple times during reset."
3283 "Shutting down the HBA\n",
3284 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303285 instance->instancet->disable_intr(instance);
3286 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003287 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003288 return;
3289 }
3290 }
3291
3292 if (cmd->sync_cmd == 1) {
3293 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003294 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003295 "cmd attached to internal command!\n");
3296 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003297 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003298 "on the internal reset queue,"
3299 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303300 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003301 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003302 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003303 0, instance->reg_set);
3304 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003305 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003306 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003307 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003308
3309 atomic_inc(&instance->fw_outstanding);
3310 instance->instancet->fire_cmd(instance,
3311 cmd->frame_phys_addr,
3312 cmd->frame_count-1, instance->reg_set);
3313 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003314 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003315 "internal reset defer list while re-issue!!\n",
3316 cmd);
3317 }
3318 }
3319
3320 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003321 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003322 megasas_return_cmd(instance, instance->aen_cmd);
3323
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003324 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003325 }
3326
3327 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003328 * Initiate AEN (Asynchronous Event Notification)
3329 */
bo yang39a98552010-09-22 22:36:29 -04003330 seq_num = instance->last_seq_num;
3331 class_locale.members.reserved = 0;
3332 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3333 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3334
3335 megasas_register_aen(instance, seq_num, class_locale.word);
3336}
3337
3338/**
3339 * Move the internal reset pending commands to a deferred queue.
3340 *
3341 * We move the commands pending at internal reset time to a
3342 * pending queue. This queue would be flushed after successful
3343 * completion of the internal reset sequence. if the internal reset
3344 * did not complete in time, the kernel reset handler would flush
3345 * these commands.
3346 **/
3347static void
3348megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3349{
3350 struct megasas_cmd *cmd;
3351 int i;
3352 u32 max_cmd = instance->max_fw_cmds;
3353 u32 defer_index;
3354 unsigned long flags;
3355
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003356 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303357 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003358 for (i = 0; i < max_cmd; i++) {
3359 cmd = instance->cmd_list[i];
3360 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003361 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003362 "on the defer queue as internal\n",
3363 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3364
3365 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003366 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003367 " moving this cmd:%p, %d %p, it was"
3368 "discovered on some list?\n",
3369 cmd, cmd->sync_cmd, cmd->scmd);
3370
3371 list_del_init(&cmd->list);
3372 }
3373 defer_index++;
3374 list_add_tail(&cmd->list,
3375 &instance->internal_reset_pending_q);
3376 }
3377 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303378 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003379}
3380
3381
3382static void
3383process_fw_state_change_wq(struct work_struct *work)
3384{
3385 struct megasas_instance *instance =
3386 container_of(work, struct megasas_instance, work_init);
3387 u32 wait;
3388 unsigned long flags;
3389
Sumit Saxena8a01a412016-01-28 21:04:32 +05303390 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003391 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303392 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003393 return ;
3394 }
3395
Sumit Saxena8a01a412016-01-28 21:04:32 +05303396 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003397 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003398 "state, restarting it...\n");
3399
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303400 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003401 atomic_set(&instance->fw_outstanding, 0);
3402
3403 atomic_set(&instance->fw_reset_no_pci_access, 1);
3404 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003405 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003406
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003407 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003408 "initiating next stage...\n");
3409
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003410 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003411 "state 2 starting...\n");
3412
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003413 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003414 for (wait = 0; wait < 30; wait++) {
3415 msleep(1000);
3416 }
3417
adam radford058a8fa2011-10-08 18:14:27 -07003418 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003419 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003420
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303421 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003422 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003423 return ;
3424 }
3425
3426 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3427 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3428 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3429 ) {
3430 *instance->consumer = *instance->producer;
3431 } else {
3432 *instance->consumer = 0;
3433 *instance->producer = 0;
3434 }
3435
3436 megasas_issue_init_mfi(instance);
3437
3438 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303439 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003440 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303441 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003442
3443 megasas_issue_pending_cmds_again(instance);
3444 instance->issuepend_done = 1;
3445 }
bo yang39a98552010-09-22 22:36:29 -04003446}
3447
3448/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003449 * megasas_deplete_reply_queue - Processes all completed commands
3450 * @instance: Adapter soft state
3451 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003452 * SCSI mid-layer instead of the status
3453 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003454 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003455 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003456static int
bo yang39a98552010-09-22 22:36:29 -04003457megasas_deplete_reply_queue(struct megasas_instance *instance,
3458 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003459{
bo yang39a98552010-09-22 22:36:29 -04003460 u32 mfiStatus;
3461 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003462
bo yang39a98552010-09-22 22:36:29 -04003463 if ((mfiStatus = instance->instancet->check_reset(instance,
3464 instance->reg_set)) == 1) {
3465 return IRQ_HANDLED;
3466 }
3467
3468 if ((mfiStatus = instance->instancet->clear_intr(
3469 instance->reg_set)
3470 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003471 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003472 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003473 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003474 }
3475
3476 instance->mfiStatus = mfiStatus;
3477
3478 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3479 fw_state = instance->instancet->read_fw_status_reg(
3480 instance->reg_set) & MFI_STATE_MASK;
3481
3482 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003483 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003484 fw_state);
3485 }
3486
3487 if ((fw_state == MFI_STATE_FAULT) &&
3488 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003489 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003490
3491 if ((instance->pdev->device ==
3492 PCI_DEVICE_ID_LSI_SAS1064R) ||
3493 (instance->pdev->device ==
3494 PCI_DEVICE_ID_DELL_PERC5) ||
3495 (instance->pdev->device ==
3496 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3497
3498 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303499 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003500 }
3501
3502
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303503 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303504 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003505 instance->issuepend_done = 0;
3506
3507 atomic_set(&instance->fw_outstanding, 0);
3508 megasas_internal_reset_defer_cmds(instance);
3509
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003510 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303511 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003512
3513 schedule_work(&instance->work_init);
3514 return IRQ_HANDLED;
3515
3516 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003517 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003518 fw_state, instance->disableOnlineCtrlReset);
3519 }
3520 }
3521
Sumant Patro5d018ad2006-10-03 13:13:18 -07003522 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003523 return IRQ_HANDLED;
3524}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003525/**
3526 * megasas_isr - isr entry point
3527 */
David Howells7d12e782006-10-05 14:55:46 +01003528static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003529{
adam radfordc8e858f2011-10-08 18:15:13 -07003530 struct megasas_irq_context *irq_context = devp;
3531 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003532 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003533 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003534
adam radfordc8e858f2011-10-08 18:15:13 -07003535 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003536 return IRQ_HANDLED;
3537
bo yang39a98552010-09-22 22:36:29 -04003538 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003539 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003540 spin_unlock_irqrestore(&instance->hba_lock, flags);
3541
3542 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003543}
3544
3545/**
3546 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003547 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003548 *
3549 * During the initialization, FW passes can potentially be in any one of
3550 * several possible states. If the FW in operational, waiting-for-handshake
3551 * states, driver must take steps to bring it to ready state. Otherwise, it
3552 * has to wait for the ready state.
3553 */
adam radford9c915a82010-12-21 13:34:31 -08003554int
adam radford058a8fa2011-10-08 18:14:27 -07003555megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003556{
3557 int i;
3558 u8 max_wait;
3559 u32 fw_state;
3560 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003561 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003562
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003563 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3564 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565
Sumant Patroe3bbff92006-10-03 12:28:49 -07003566 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003567 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003568 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003569
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003570 while (fw_state != MFI_STATE_READY) {
3571
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003572 switch (fw_state) {
3573
3574 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003575 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003576 if (ocr) {
3577 max_wait = MEGASAS_RESET_WAIT_TIME;
3578 cur_state = MFI_STATE_FAULT;
3579 break;
3580 } else
3581 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003582
3583 case MFI_STATE_WAIT_HANDSHAKE:
3584 /*
3585 * Set the CLR bit in inbound doorbell
3586 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003587 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003588 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3589 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003590 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303591 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003592 writel(
3593 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003594 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303595 else
Yang, Bo87911122009-10-06 14:31:54 -06003596 writel(
3597 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3598 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003599
Yang, Bo7218df62009-10-06 14:52:20 -06003600 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003601 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3602 break;
3603
Sumant Patroe3bbff92006-10-03 12:28:49 -07003604 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003605 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003606 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3607 (instance->pdev->device ==
3608 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303609 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003610 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003611 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303612 else
Yang, Bo87911122009-10-06 14:31:54 -06003613 writel(MFI_INIT_HOTPLUG,
3614 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003615
Yang, Bo7218df62009-10-06 14:52:20 -06003616 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003617 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3618 break;
3619
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003620 case MFI_STATE_OPERATIONAL:
3621 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003622 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003623 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303624 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003625 if ((instance->pdev->device ==
3626 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3627 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003628 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303629 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003630 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003631 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303632
3633 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003634 for (i = 0; i < (10 * 1000); i += 20) {
3635 if (readl(
3636 &instance->
3637 reg_set->
3638 doorbell) & 1)
3639 msleep(20);
3640 else
3641 break;
3642 }
3643 }
Yang, Bo87911122009-10-06 14:31:54 -06003644 } else
3645 writel(MFI_RESET_FLAGS,
3646 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003647
Yang, Bo7218df62009-10-06 14:52:20 -06003648 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003649 cur_state = MFI_STATE_OPERATIONAL;
3650 break;
3651
3652 case MFI_STATE_UNDEFINED:
3653 /*
3654 * This state should not last for more than 2 seconds
3655 */
Yang, Bo7218df62009-10-06 14:52:20 -06003656 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003657 cur_state = MFI_STATE_UNDEFINED;
3658 break;
3659
3660 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003661 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003662 cur_state = MFI_STATE_BB_INIT;
3663 break;
3664
3665 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003666 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003667 cur_state = MFI_STATE_FW_INIT;
3668 break;
3669
3670 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003671 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003672 cur_state = MFI_STATE_FW_INIT_2;
3673 break;
3674
3675 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003676 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003677 cur_state = MFI_STATE_DEVICE_SCAN;
3678 break;
3679
3680 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003681 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003682 cur_state = MFI_STATE_FLUSH_CACHE;
3683 break;
3684
3685 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003686 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003687 fw_state);
3688 return -ENODEV;
3689 }
3690
3691 /*
3692 * The cur_state should not last for more than max_wait secs
3693 */
3694 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003695 curr_abs_state = instance->instancet->
3696 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003697
Yang, Bo7218df62009-10-06 14:52:20 -06003698 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003699 msleep(1);
3700 } else
3701 break;
3702 }
3703
3704 /*
3705 * Return error if fw_state hasn't changed after max_wait
3706 */
Yang, Bo7218df62009-10-06 14:52:20 -06003707 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003708 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003709 "in %d secs\n", fw_state, max_wait);
3710 return -ENODEV;
3711 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003712
3713 abs_state = curr_abs_state;
3714 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003715 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003716 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003717
3718 return 0;
3719}
3720
3721/**
3722 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3723 * @instance: Adapter soft state
3724 */
3725static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3726{
3727 int i;
adam radford9c915a82010-12-21 13:34:31 -08003728 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003729 struct megasas_cmd *cmd;
3730
3731 if (!instance->frame_dma_pool)
3732 return;
3733
3734 /*
3735 * Return all frames to pool
3736 */
3737 for (i = 0; i < max_cmd; i++) {
3738
3739 cmd = instance->cmd_list[i];
3740
3741 if (cmd->frame)
3742 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3743 cmd->frame_phys_addr);
3744
3745 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003746 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003747 cmd->sense_phys_addr);
3748 }
3749
3750 /*
3751 * Now destroy the pool itself
3752 */
3753 pci_pool_destroy(instance->frame_dma_pool);
3754 pci_pool_destroy(instance->sense_dma_pool);
3755
3756 instance->frame_dma_pool = NULL;
3757 instance->sense_dma_pool = NULL;
3758}
3759
3760/**
3761 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3762 * @instance: Adapter soft state
3763 *
3764 * Each command packet has an embedded DMA memory buffer that is used for
3765 * filling MFI frame and the SG list that immediately follows the frame. This
3766 * function creates those DMA memory buffers for each command packet by using
3767 * PCI pool facility.
3768 */
3769static int megasas_create_frame_pool(struct megasas_instance *instance)
3770{
3771 int i;
3772 u32 max_cmd;
3773 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003774 u32 total_sz;
3775 u32 frame_count;
3776 struct megasas_cmd *cmd;
3777
adam radford9c915a82010-12-21 13:34:31 -08003778 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003779
3780 /*
3781 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3782 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3783 */
3784 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3785 sizeof(struct megasas_sge32);
3786
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003787 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003788 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003789
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003790 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303791 * For MFI controllers.
3792 * max_num_sge = 60
3793 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3794 * Total 960 byte (15 MFI frame of 64 byte)
3795 *
3796 * Fusion adapter require only 3 extra frame.
3797 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3798 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3799 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003800 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303801 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003802 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3803 /*
3804 * Use DMA pool facility provided by PCI layer
3805 */
3806 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303807 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003808
3809 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003810 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003811 return -ENOMEM;
3812 }
3813
3814 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3815 instance->pdev, 128, 4, 0);
3816
3817 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003818 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003819
3820 pci_pool_destroy(instance->frame_dma_pool);
3821 instance->frame_dma_pool = NULL;
3822
3823 return -ENOMEM;
3824 }
3825
3826 /*
3827 * Allocate and attach a frame to each of the commands in cmd_list.
3828 * By making cmd->index as the context instead of the &cmd, we can
3829 * always use 32bit context regardless of the architecture
3830 */
3831 for (i = 0; i < max_cmd; i++) {
3832
3833 cmd = instance->cmd_list[i];
3834
3835 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3836 GFP_KERNEL, &cmd->frame_phys_addr);
3837
3838 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3839 GFP_KERNEL, &cmd->sense_phys_addr);
3840
3841 /*
3842 * megasas_teardown_frame_pool() takes care of freeing
3843 * whatever has been allocated
3844 */
3845 if (!cmd->frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003846 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003847 megasas_teardown_frame_pool(instance);
3848 return -ENOMEM;
3849 }
3850
Yang, Bo707e09b2010-10-12 07:20:27 -06003851 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303852 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003853 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303854 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003855 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003856 }
3857
3858 return 0;
3859}
3860
3861/**
3862 * megasas_free_cmds - Free all the cmds in the free cmd pool
3863 * @instance: Adapter soft state
3864 */
adam radford9c915a82010-12-21 13:34:31 -08003865void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003866{
3867 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003868
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003869 /* First free the MFI frame pool */
3870 megasas_teardown_frame_pool(instance);
3871
3872 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003873 for (i = 0; i < instance->max_mfi_cmds; i++)
3874
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003875 kfree(instance->cmd_list[i]);
3876
3877 /* Free the cmd_list buffer itself */
3878 kfree(instance->cmd_list);
3879 instance->cmd_list = NULL;
3880
3881 INIT_LIST_HEAD(&instance->cmd_pool);
3882}
3883
3884/**
3885 * megasas_alloc_cmds - Allocates the command packets
3886 * @instance: Adapter soft state
3887 *
3888 * Each command that is issued to the FW, whether IO commands from the OS or
3889 * internal commands like IOCTLs, are wrapped in local data structure called
3890 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3891 * the FW.
3892 *
3893 * Each frame has a 32-bit field called context (tag). This context is used
3894 * to get back the megasas_cmd from the frame when a frame gets completed in
3895 * the ISR. Typically the address of the megasas_cmd itself would be used as
3896 * the context. But we wanted to keep the differences between 32 and 64 bit
3897 * systems to the mininum. We always use 32 bit integers for the context. In
3898 * this driver, the 32 bit values are the indices into an array cmd_list.
3899 * This array is used only to look up the megasas_cmd given the context. The
3900 * free commands themselves are maintained in a linked list called cmd_pool.
3901 */
adam radford9c915a82010-12-21 13:34:31 -08003902int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003903{
3904 int i;
3905 int j;
3906 u32 max_cmd;
3907 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303908 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003909
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303910 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003911 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003912
3913 /*
3914 * instance->cmd_list is an array of struct megasas_cmd pointers.
3915 * Allocate the dynamic array first and then allocate individual
3916 * commands.
3917 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003918 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003919
3920 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003921 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003922 return -ENOMEM;
3923 }
3924
adam radford9c915a82010-12-21 13:34:31 -08003925 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003926
3927 for (i = 0; i < max_cmd; i++) {
3928 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3929 GFP_KERNEL);
3930
3931 if (!instance->cmd_list[i]) {
3932
3933 for (j = 0; j < i; j++)
3934 kfree(instance->cmd_list[j]);
3935
3936 kfree(instance->cmd_list);
3937 instance->cmd_list = NULL;
3938
3939 return -ENOMEM;
3940 }
3941 }
3942
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003943 for (i = 0; i < max_cmd; i++) {
3944 cmd = instance->cmd_list[i];
3945 memset(cmd, 0, sizeof(struct megasas_cmd));
3946 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003947 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003948 cmd->instance = instance;
3949
3950 list_add_tail(&cmd->list, &instance->cmd_pool);
3951 }
3952
3953 /*
3954 * Create a frame pool and assign one frame to each cmd
3955 */
3956 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003957 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003958 megasas_free_cmds(instance);
Jason Yan650c8ec2019-02-15 19:50:27 +08003959 return -ENOMEM;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003960 }
3961
3962 return 0;
3963}
3964
Yang, Bo81e403c2009-10-06 14:27:54 -06003965/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303966 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
3967 * @instance: Adapter soft state
3968 *
3969 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
3970 * or FW is not under OCR.
3971 */
3972inline int
3973dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
3974
3975 if (!instance->ctrl_context)
3976 return KILL_ADAPTER;
3977 else if (instance->unload ||
3978 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
3979 return IGNORE_TIMEOUT;
3980 else
3981 return INITIATE_OCR;
3982}
3983
Sumit Saxena2216c302016-01-28 21:04:26 +05303984static int
3985megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
3986{
3987 int ret;
3988 struct megasas_cmd *cmd;
3989 struct megasas_dcmd_frame *dcmd;
3990
3991 cmd = megasas_get_cmd(instance);
3992
3993 if (!cmd) {
3994 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
3995 return -ENOMEM;
3996 }
3997
3998 dcmd = &cmd->frame->dcmd;
3999
4000 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4001 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4002
4003 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4004 dcmd->cmd = MFI_CMD_DCMD;
4005 dcmd->cmd_status = 0xFF;
4006 dcmd->sge_count = 1;
4007 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4008 dcmd->timeout = 0;
4009 dcmd->pad_0 = 0;
4010 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4011 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4012 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4013 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4014
4015 if (instance->ctrl_context && !instance->mask_interrupts)
4016 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4017 else
4018 ret = megasas_issue_polled(instance, cmd);
4019
4020 switch (ret) {
4021 case DCMD_SUCCESS:
4022 instance->pd_list[device_id].interface =
4023 instance->pd_info->state.ddf.pdType.intf;
4024 break;
4025
4026 case DCMD_TIMEOUT:
4027
4028 switch (dcmd_timeout_ocr_possible(instance)) {
4029 case INITIATE_OCR:
4030 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4031 megasas_reset_fusion(instance->host,
4032 MFI_IO_TIMEOUT_OCR);
4033 break;
4034 case KILL_ADAPTER:
4035 megaraid_sas_kill_hba(instance);
4036 break;
4037 case IGNORE_TIMEOUT:
4038 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4039 __func__, __LINE__);
4040 break;
4041 }
4042
4043 break;
4044 }
4045
4046 if (ret != DCMD_TIMEOUT)
4047 megasas_return_cmd(instance, cmd);
4048
4049 return ret;
4050}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304051/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004052 * megasas_get_pd_list_info - Returns FW's pd_list structure
4053 * @instance: Adapter soft state
4054 * @pd_list: pd_list structure
4055 *
4056 * Issues an internal command (DCMD) to get the FW's controller PD
4057 * list structure. This information is mainly used to find out SYSTEM
4058 * supported by the FW.
4059 */
4060static int
4061megasas_get_pd_list(struct megasas_instance *instance)
4062{
4063 int ret = 0, pd_index = 0;
4064 struct megasas_cmd *cmd;
4065 struct megasas_dcmd_frame *dcmd;
4066 struct MR_PD_LIST *ci;
4067 struct MR_PD_ADDRESS *pd_addr;
4068 dma_addr_t ci_h = 0;
4069
Sumit Saxenad9083162016-07-08 03:30:16 -07004070 if (instance->pd_list_not_supported) {
4071 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4072 "not supported by firmware\n");
4073 return ret;
4074 }
4075
Yang, Bo81e403c2009-10-06 14:27:54 -06004076 cmd = megasas_get_cmd(instance);
4077
4078 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004079 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004080 return -ENOMEM;
4081 }
4082
4083 dcmd = &cmd->frame->dcmd;
4084
4085 ci = pci_alloc_consistent(instance->pdev,
4086 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4087
4088 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004089 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004090 megasas_return_cmd(instance, cmd);
4091 return -ENOMEM;
4092 }
4093
4094 memset(ci, 0, sizeof(*ci));
4095 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4096
4097 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4098 dcmd->mbox.b[1] = 0;
4099 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304100 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004101 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304102 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004103 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004104 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304105 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4106 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4107 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4108 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004109
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304110 if (instance->ctrl_context && !instance->mask_interrupts)
4111 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304112 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304113 else
4114 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004115
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304116 switch (ret) {
4117 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004118 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4119 "failed/not supported by firmware\n");
4120
4121 if (instance->ctrl_context)
4122 megaraid_sas_kill_hba(instance);
4123 else
4124 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304125 break;
4126 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004127
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304128 switch (dcmd_timeout_ocr_possible(instance)) {
4129 case INITIATE_OCR:
4130 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4131 /*
4132 * DCMD failed from AEN path.
4133 * AEN path already hold reset_mutex to avoid PCI access
4134 * while OCR is in progress.
4135 */
4136 mutex_unlock(&instance->reset_mutex);
4137 megasas_reset_fusion(instance->host,
4138 MFI_IO_TIMEOUT_OCR);
4139 mutex_lock(&instance->reset_mutex);
4140 break;
4141 case KILL_ADAPTER:
4142 megaraid_sas_kill_hba(instance);
4143 break;
4144 case IGNORE_TIMEOUT:
4145 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4146 __func__, __LINE__);
4147 break;
4148 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004149
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304150 break;
4151
4152 case DCMD_SUCCESS:
4153 pd_addr = ci->addr;
4154
4155 if ((le32_to_cpu(ci->count) >
4156 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4157 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004158
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304159 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304160 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004161
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304162 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304163 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304164 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304165 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304166 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304167 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304168 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004169 pd_addr++;
4170 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304171
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304172 memcpy(instance->pd_list, instance->local_pd_list,
4173 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304174 break;
4175
Yang, Bo81e403c2009-10-06 14:27:54 -06004176 }
4177
4178 pci_free_consistent(instance->pdev,
4179 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4180 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304181
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304182 if (ret != DCMD_TIMEOUT)
4183 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004184
4185 return ret;
4186}
4187
Yang, Bobdc6fb82009-12-06 08:30:19 -07004188/*
4189 * megasas_get_ld_list_info - Returns FW's ld_list structure
4190 * @instance: Adapter soft state
4191 * @ld_list: ld_list structure
4192 *
4193 * Issues an internal command (DCMD) to get the FW's controller PD
4194 * list structure. This information is mainly used to find out SYSTEM
4195 * supported by the FW.
4196 */
4197static int
4198megasas_get_ld_list(struct megasas_instance *instance)
4199{
4200 int ret = 0, ld_index = 0, ids = 0;
4201 struct megasas_cmd *cmd;
4202 struct megasas_dcmd_frame *dcmd;
4203 struct MR_LD_LIST *ci;
4204 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304205 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004206
4207 cmd = megasas_get_cmd(instance);
4208
4209 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004210 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004211 return -ENOMEM;
4212 }
4213
4214 dcmd = &cmd->frame->dcmd;
4215
4216 ci = pci_alloc_consistent(instance->pdev,
4217 sizeof(struct MR_LD_LIST),
4218 &ci_h);
4219
4220 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004221 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004222 megasas_return_cmd(instance, cmd);
4223 return -ENOMEM;
4224 }
4225
4226 memset(ci, 0, sizeof(*ci));
4227 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4228
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304229 if (instance->supportmax256vd)
4230 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004231 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304232 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004233 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304234 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004235 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304236 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4237 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4238 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4239 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004240 dcmd->pad_0 = 0;
4241
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304242 if (instance->ctrl_context && !instance->mask_interrupts)
4243 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304244 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304245 else
4246 ret = megasas_issue_polled(instance, cmd);
4247
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304248 ld_count = le32_to_cpu(ci->ldCount);
4249
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304250 switch (ret) {
4251 case DCMD_FAILED:
4252 megaraid_sas_kill_hba(instance);
4253 break;
4254 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004255
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304256 switch (dcmd_timeout_ocr_possible(instance)) {
4257 case INITIATE_OCR:
4258 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4259 /*
4260 * DCMD failed from AEN path.
4261 * AEN path already hold reset_mutex to avoid PCI access
4262 * while OCR is in progress.
4263 */
4264 mutex_unlock(&instance->reset_mutex);
4265 megasas_reset_fusion(instance->host,
4266 MFI_IO_TIMEOUT_OCR);
4267 mutex_lock(&instance->reset_mutex);
4268 break;
4269 case KILL_ADAPTER:
4270 megaraid_sas_kill_hba(instance);
4271 break;
4272 case IGNORE_TIMEOUT:
4273 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4274 __func__, __LINE__);
4275 break;
4276 }
4277
4278 break;
4279
4280 case DCMD_SUCCESS:
4281 if (ld_count > instance->fw_supported_vd_count)
4282 break;
4283
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304284 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004285
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304286 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004287 if (ci->ldList[ld_index].state != 0) {
4288 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304289 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004290 }
4291 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304292
4293 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004294 }
4295
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304296 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004297
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304298 if (ret != DCMD_TIMEOUT)
4299 megasas_return_cmd(instance, cmd);
4300
Yang, Bobdc6fb82009-12-06 08:30:19 -07004301 return ret;
4302}
4303
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004304/**
adam radford21c9e162013-09-06 15:27:14 -07004305 * megasas_ld_list_query - Returns FW's ld_list structure
4306 * @instance: Adapter soft state
4307 * @ld_list: ld_list structure
4308 *
4309 * Issues an internal command (DCMD) to get the FW's controller PD
4310 * list structure. This information is mainly used to find out SYSTEM
4311 * supported by the FW.
4312 */
4313static int
4314megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4315{
4316 int ret = 0, ld_index = 0, ids = 0;
4317 struct megasas_cmd *cmd;
4318 struct megasas_dcmd_frame *dcmd;
4319 struct MR_LD_TARGETID_LIST *ci;
4320 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304321 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004322
4323 cmd = megasas_get_cmd(instance);
4324
4325 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004326 dev_warn(&instance->pdev->dev,
4327 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004328 return -ENOMEM;
4329 }
4330
4331 dcmd = &cmd->frame->dcmd;
4332
4333 ci = pci_alloc_consistent(instance->pdev,
4334 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4335
4336 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004337 dev_warn(&instance->pdev->dev,
4338 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004339 megasas_return_cmd(instance, cmd);
4340 return -ENOMEM;
4341 }
4342
4343 memset(ci, 0, sizeof(*ci));
4344 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4345
4346 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304347 if (instance->supportmax256vd)
4348 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004349
4350 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304351 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004352 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304353 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004354 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304355 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4356 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4357 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4358 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004359 dcmd->pad_0 = 0;
4360
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304361 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304362 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304363 else
4364 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004365
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304366 switch (ret) {
4367 case DCMD_FAILED:
4368 dev_info(&instance->pdev->dev,
4369 "DCMD not supported by firmware - %s %d\n",
4370 __func__, __LINE__);
4371 ret = megasas_get_ld_list(instance);
4372 break;
4373 case DCMD_TIMEOUT:
4374 switch (dcmd_timeout_ocr_possible(instance)) {
4375 case INITIATE_OCR:
4376 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4377 /*
4378 * DCMD failed from AEN path.
4379 * AEN path already hold reset_mutex to avoid PCI access
4380 * while OCR is in progress.
4381 */
4382 mutex_unlock(&instance->reset_mutex);
4383 megasas_reset_fusion(instance->host,
4384 MFI_IO_TIMEOUT_OCR);
4385 mutex_lock(&instance->reset_mutex);
4386 break;
4387 case KILL_ADAPTER:
4388 megaraid_sas_kill_hba(instance);
4389 break;
4390 case IGNORE_TIMEOUT:
4391 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4392 __func__, __LINE__);
4393 break;
4394 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304395
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304396 break;
4397 case DCMD_SUCCESS:
4398 tgtid_count = le32_to_cpu(ci->count);
4399
4400 if ((tgtid_count > (instance->fw_supported_vd_count)))
4401 break;
4402
adam radford21c9e162013-09-06 15:27:14 -07004403 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304404 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004405 ids = ci->targetId[ld_index];
4406 instance->ld_ids[ids] = ci->targetId[ld_index];
4407 }
4408
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304409 break;
adam radford21c9e162013-09-06 15:27:14 -07004410 }
4411
4412 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304413 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004414
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304415 if (ret != DCMD_TIMEOUT)
4416 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004417
4418 return ret;
4419}
4420
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304421/*
4422 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4423 * instance : Controller's instance
4424*/
4425static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4426{
4427 struct fusion_context *fusion;
4428 u32 old_map_sz;
4429 u32 new_map_sz;
4430
4431 fusion = instance->ctrl_context;
4432 /* For MFI based controllers return dummy success */
4433 if (!fusion)
4434 return;
4435
4436 instance->supportmax256vd =
4437 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4438 /* Below is additional check to address future FW enhancement */
4439 if (instance->ctrl_info->max_lds > 64)
4440 instance->supportmax256vd = 1;
4441
4442 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4443 * MEGASAS_MAX_DEV_PER_CHANNEL;
4444 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4445 * MEGASAS_MAX_DEV_PER_CHANNEL;
4446 if (instance->supportmax256vd) {
4447 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4448 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4449 } else {
4450 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4451 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4452 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304453
4454 dev_info(&instance->pdev->dev,
4455 "firmware type\t: %s\n",
4456 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4457 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304458
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004459 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304460 (sizeof(struct MR_LD_SPAN_MAP) *
4461 (instance->fw_supported_vd_count - 1));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004462 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4463 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304464 (sizeof(struct MR_LD_SPAN_MAP) *
4465 (instance->drv_supported_vd_count - 1));
4466
4467 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4468
4469
4470 if (instance->supportmax256vd)
4471 fusion->current_map_sz = new_map_sz;
4472 else
4473 fusion->current_map_sz = old_map_sz;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304474}
4475
adam radford21c9e162013-09-06 15:27:14 -07004476/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004477 * megasas_get_controller_info - Returns FW's controller structure
4478 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004479 *
4480 * Issues an internal command (DCMD) to get the FW's controller structure.
4481 * This information is mainly used to find out the maximum IO transfer per
4482 * command supported by the FW.
4483 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304484int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304485megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004486{
4487 int ret = 0;
4488 struct megasas_cmd *cmd;
4489 struct megasas_dcmd_frame *dcmd;
4490 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304491 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004492 dma_addr_t ci_h = 0;
4493
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304494 ctrl_info = instance->ctrl_info;
4495
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004496 cmd = megasas_get_cmd(instance);
4497
4498 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004499 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004500 return -ENOMEM;
4501 }
4502
4503 dcmd = &cmd->frame->dcmd;
4504
4505 ci = pci_alloc_consistent(instance->pdev,
4506 sizeof(struct megasas_ctrl_info), &ci_h);
4507
4508 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004509 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004510 megasas_return_cmd(instance, cmd);
4511 return -ENOMEM;
4512 }
4513
4514 memset(ci, 0, sizeof(*ci));
4515 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4516
4517 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304518 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004519 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304520 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004521 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004522 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304523 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4524 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4525 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4526 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304527 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004528
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304529 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304530 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304531 else
4532 ret = megasas_issue_polled(instance, cmd);
4533
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304534 switch (ret) {
4535 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004536 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304537 /* Save required controller information in
4538 * CPU endianness format.
4539 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304540 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4541 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4542 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304543
4544 /* Update the latest Ext VD info.
4545 * From Init path, store current firmware details.
4546 * From OCR path, detect any firmware properties changes.
4547 * in case of Firmware upgrade without system reboot.
4548 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304549 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304550 instance->use_seqnum_jbod_fp =
4551 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304552
4553 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304554 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4555 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304556 "controller type\t: %s(%dMB)\n",
4557 instance->is_imr ? "iMR" : "MR",
4558 le16_to_cpu(ctrl_info->memory_size));
4559
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304560 instance->disableOnlineCtrlReset =
4561 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304562 instance->secure_jbod_support =
4563 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304564 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4565 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304566 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4567 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304568 break;
4569
4570 case DCMD_TIMEOUT:
4571 switch (dcmd_timeout_ocr_possible(instance)) {
4572 case INITIATE_OCR:
4573 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4574 megasas_reset_fusion(instance->host,
4575 MFI_IO_TIMEOUT_OCR);
4576 break;
4577 case KILL_ADAPTER:
4578 megaraid_sas_kill_hba(instance);
4579 break;
4580 case IGNORE_TIMEOUT:
4581 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4582 __func__, __LINE__);
4583 break;
4584 }
4585 case DCMD_FAILED:
4586 megaraid_sas_kill_hba(instance);
4587 break;
4588
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304589 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004590
4591 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4592 ci, ci_h);
4593
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304594 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304595
4596
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004597 return ret;
4598}
4599
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304600/*
4601 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4602 * to firmware
4603 *
4604 * @instance: Adapter soft state
4605 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4606 MR_CRASH_BUF_TURN_OFF = 0
4607 MR_CRASH_BUF_TURN_ON = 1
4608 * @return 0 on success non-zero on failure.
4609 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4610 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4611 * that driver supports crash dump feature. This DCMD will be sent only if
4612 * crash dump feature is supported by the FW.
4613 *
4614 */
4615int megasas_set_crash_dump_params(struct megasas_instance *instance,
4616 u8 crash_buf_state)
4617{
4618 int ret = 0;
4619 struct megasas_cmd *cmd;
4620 struct megasas_dcmd_frame *dcmd;
4621
4622 cmd = megasas_get_cmd(instance);
4623
4624 if (!cmd) {
4625 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4626 return -ENOMEM;
4627 }
4628
4629
4630 dcmd = &cmd->frame->dcmd;
4631
4632 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4633 dcmd->mbox.b[0] = crash_buf_state;
4634 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304635 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304636 dcmd->sge_count = 1;
4637 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4638 dcmd->timeout = 0;
4639 dcmd->pad_0 = 0;
4640 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4641 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4642 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4643 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4644
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304645 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304646 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304647 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304648 ret = megasas_issue_polled(instance, cmd);
4649
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304650 if (ret == DCMD_TIMEOUT) {
4651 switch (dcmd_timeout_ocr_possible(instance)) {
4652 case INITIATE_OCR:
4653 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4654 megasas_reset_fusion(instance->host,
4655 MFI_IO_TIMEOUT_OCR);
4656 break;
4657 case KILL_ADAPTER:
4658 megaraid_sas_kill_hba(instance);
4659 break;
4660 case IGNORE_TIMEOUT:
4661 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4662 __func__, __LINE__);
4663 break;
4664 }
4665 } else
4666 megasas_return_cmd(instance, cmd);
4667
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304668 return ret;
4669}
4670
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004671/**
bo yang31ea7082007-11-07 12:09:50 -05004672 * megasas_issue_init_mfi - Initializes the FW
4673 * @instance: Adapter soft state
4674 *
4675 * Issues the INIT MFI cmd
4676 */
4677static int
4678megasas_issue_init_mfi(struct megasas_instance *instance)
4679{
Christoph Hellwig9ab9ed32015-04-23 16:32:54 +05304680 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004681 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004682 struct megasas_init_frame *init_frame;
4683 struct megasas_init_queue_info *initq_info;
4684 dma_addr_t init_frame_h;
4685 dma_addr_t initq_info_h;
4686
4687 /*
4688 * Prepare a init frame. Note the init frame points to queue info
4689 * structure. Each frame has SGL allocated after first 64 bytes. For
4690 * this frame - since we don't need any SGL - we use SGL's space as
4691 * queue info structure
4692 *
4693 * We will not get a NULL command below. We just created the pool.
4694 */
4695 cmd = megasas_get_cmd(instance);
4696
4697 init_frame = (struct megasas_init_frame *)cmd->frame;
4698 initq_info = (struct megasas_init_queue_info *)
4699 ((unsigned long)init_frame + 64);
4700
4701 init_frame_h = cmd->frame_phys_addr;
4702 initq_info_h = init_frame_h + 64;
4703
4704 context = init_frame->context;
4705 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4706 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4707 init_frame->context = context;
4708
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304709 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4710 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004711
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304712 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4713 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004714
4715 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304716 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304717 init_frame->queue_info_new_phys_addr_lo =
4718 cpu_to_le32(lower_32_bits(initq_info_h));
4719 init_frame->queue_info_new_phys_addr_hi =
4720 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004721
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304722 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004723
4724 /*
4725 * disable the intr before firing the init frame to FW
4726 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304727 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004728
4729 /*
4730 * Issue the init frame in polled mode
4731 */
4732
4733 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004734 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004735 megasas_return_cmd(instance, cmd);
4736 goto fail_fw_init;
4737 }
4738
4739 megasas_return_cmd(instance, cmd);
4740
4741 return 0;
4742
4743fail_fw_init:
4744 return -EINVAL;
4745}
4746
adam radfordcd50ba82010-12-21 10:23:23 -08004747static u32
4748megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004749{
adam radfordcd50ba82010-12-21 10:23:23 -08004750 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004751 u32 context_sz;
4752 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004753
4754 reg_set = instance->reg_set;
4755
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004756 /*
4757 * Get various operational parameters from status register
4758 */
Sumant Patro1341c932006-01-25 12:02:40 -08004759 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004760 /*
4761 * Reduce the max supported cmds by 1. This is to ensure that the
4762 * reply_q_sz (1 more than the max cmd that driver may send)
4763 * does not exceed max cmds that the FW can support
4764 */
4765 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004766 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004767 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004768 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004769 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304770 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4771 * are reserved for IOCTL + driver's internal DCMDs.
4772 */
4773 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4774 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4775 instance->max_scsi_cmds = (instance->max_fw_cmds -
4776 MEGASAS_SKINNY_INT_CMDS);
4777 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4778 } else {
4779 instance->max_scsi_cmds = (instance->max_fw_cmds -
4780 MEGASAS_INT_CMDS);
4781 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4782 }
4783
Sumit Saxena308ec452016-01-28 21:04:30 +05304784 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304785 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004786 * Create a pool of commands
4787 */
4788 if (megasas_alloc_cmds(instance))
4789 goto fail_alloc_cmds;
4790
4791 /*
4792 * Allocate memory for reply queue. Length of reply queue should
4793 * be _one_ more than the maximum commands handled by the firmware.
4794 *
4795 * Note: When FW completes commands, it places corresponding contex
4796 * values in this circular reply queue. This circular queue is a fairly
4797 * typical producer-consumer queue. FW is the producer (of completed
4798 * commands) and the driver is the consumer.
4799 */
4800 context_sz = sizeof(u32);
4801 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4802
4803 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4804 reply_q_sz,
4805 &instance->reply_queue_h);
4806
4807 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004808 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004809 goto fail_reply_queue;
4810 }
4811
bo yang31ea7082007-11-07 12:09:50 -05004812 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004813 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004814
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304815 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304816 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4817 "Fail from %s %d\n", instance->unique_id,
4818 __func__, __LINE__);
4819 goto fail_fw_init;
4820 }
4821
bo yang39a98552010-09-22 22:36:29 -04004822 instance->fw_support_ieee = 0;
4823 instance->fw_support_ieee =
4824 (instance->instancet->read_fw_status_reg(reg_set) &
4825 0x04000000);
4826
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004827 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004828 instance->fw_support_ieee);
4829
4830 if (instance->fw_support_ieee)
4831 instance->flag_ieee = 1;
4832
adam radfordcd50ba82010-12-21 10:23:23 -08004833 return 0;
4834
4835fail_fw_init:
4836
4837 pci_free_consistent(instance->pdev, reply_q_sz,
4838 instance->reply_queue, instance->reply_queue_h);
4839fail_reply_queue:
4840 megasas_free_cmds(instance);
4841
4842fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004843 return 1;
4844}
4845
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304846/*
4847 * megasas_setup_irqs_msix - register legacy interrupts.
4848 * @instance: Adapter soft state
4849 *
4850 * Do not enable interrupt, only setup ISRs.
4851 *
4852 * Return 0 on success.
4853 */
4854static int
4855megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4856{
4857 struct pci_dev *pdev;
4858
4859 pdev = instance->pdev;
4860 instance->irq_context[0].instance = instance;
4861 instance->irq_context[0].MSIxIndex = 0;
4862 if (request_irq(pdev->irq, instance->instancet->service_isr,
4863 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4864 dev_err(&instance->pdev->dev,
4865 "Failed to register IRQ from %s %d\n",
4866 __func__, __LINE__);
4867 return -1;
4868 }
4869 return 0;
4870}
4871
4872/**
4873 * megasas_setup_irqs_msix - register MSI-x interrupts.
4874 * @instance: Adapter soft state
4875 * @is_probe: Driver probe check
4876 *
4877 * Do not enable interrupt, only setup ISRs.
4878 *
4879 * Return 0 on success.
4880 */
4881static int
4882megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4883{
4884 int i, j, cpu;
4885 struct pci_dev *pdev;
4886
4887 pdev = instance->pdev;
4888
4889 /* Try MSI-x */
4890 cpu = cpumask_first(cpu_online_mask);
4891 for (i = 0; i < instance->msix_vectors; i++) {
4892 instance->irq_context[i].instance = instance;
4893 instance->irq_context[i].MSIxIndex = i;
4894 if (request_irq(instance->msixentry[i].vector,
4895 instance->instancet->service_isr, 0, "megasas",
4896 &instance->irq_context[i])) {
4897 dev_err(&instance->pdev->dev,
4898 "Failed to register IRQ for vector %d.\n", i);
4899 for (j = 0; j < i; j++) {
4900 if (smp_affinity_enable)
4901 irq_set_affinity_hint(
4902 instance->msixentry[j].vector, NULL);
4903 free_irq(instance->msixentry[j].vector,
4904 &instance->irq_context[j]);
4905 }
4906 /* Retry irq register for IO_APIC*/
4907 instance->msix_vectors = 0;
4908 if (is_probe)
4909 return megasas_setup_irqs_ioapic(instance);
4910 else
4911 return -1;
4912 }
4913 if (smp_affinity_enable) {
4914 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4915 get_cpu_mask(cpu)))
4916 dev_err(&instance->pdev->dev,
4917 "Failed to set affinity hint"
4918 " for cpu %d\n", cpu);
4919 cpu = cpumask_next(cpu, cpu_online_mask);
4920 }
4921 }
4922 return 0;
4923}
4924
4925/*
4926 * megasas_destroy_irqs- unregister interrupts.
4927 * @instance: Adapter soft state
4928 * return: void
4929 */
4930static void
4931megasas_destroy_irqs(struct megasas_instance *instance) {
4932
4933 int i;
4934
4935 if (instance->msix_vectors)
4936 for (i = 0; i < instance->msix_vectors; i++) {
4937 if (smp_affinity_enable)
4938 irq_set_affinity_hint(
4939 instance->msixentry[i].vector, NULL);
4940 free_irq(instance->msixentry[i].vector,
4941 &instance->irq_context[i]);
4942 }
4943 else
4944 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4945}
4946
adam radfordcd50ba82010-12-21 10:23:23 -08004947/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304948 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4949 * @instance: Adapter soft state
4950 * @is_probe: Driver probe check
4951 *
4952 * Return 0 on success.
4953 */
4954void
4955megasas_setup_jbod_map(struct megasas_instance *instance)
4956{
4957 int i;
4958 struct fusion_context *fusion = instance->ctrl_context;
4959 u32 pd_seq_map_sz;
4960
4961 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4962 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4963
4964 if (reset_devices || !fusion ||
4965 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4966 dev_info(&instance->pdev->dev,
4967 "Jbod map is not supported %s %d\n",
4968 __func__, __LINE__);
4969 instance->use_seqnum_jbod_fp = false;
4970 return;
4971 }
4972
4973 if (fusion->pd_seq_sync[0])
4974 goto skip_alloc;
4975
4976 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4977 fusion->pd_seq_sync[i] = dma_alloc_coherent
4978 (&instance->pdev->dev, pd_seq_map_sz,
4979 &fusion->pd_seq_phys[i], GFP_KERNEL);
4980 if (!fusion->pd_seq_sync[i]) {
4981 dev_err(&instance->pdev->dev,
4982 "Failed to allocate memory from %s %d\n",
4983 __func__, __LINE__);
4984 if (i == 1) {
4985 dma_free_coherent(&instance->pdev->dev,
4986 pd_seq_map_sz, fusion->pd_seq_sync[0],
4987 fusion->pd_seq_phys[0]);
4988 fusion->pd_seq_sync[0] = NULL;
4989 }
4990 instance->use_seqnum_jbod_fp = false;
4991 return;
4992 }
4993 }
4994
4995skip_alloc:
4996 if (!megasas_sync_pd_seq_num(instance, false) &&
4997 !megasas_sync_pd_seq_num(instance, true))
4998 instance->use_seqnum_jbod_fp = true;
4999 else
5000 instance->use_seqnum_jbod_fp = false;
5001}
5002
5003/**
adam radfordcd50ba82010-12-21 10:23:23 -08005004 * megasas_init_fw - Initializes the FW
5005 * @instance: Adapter soft state
5006 *
5007 * This is the main function for initializing firmware
5008 */
5009
5010static int megasas_init_fw(struct megasas_instance *instance)
5011{
5012 u32 max_sectors_1;
5013 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305014 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005015 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005016 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305017 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005018 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305019 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005020 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305021 struct fusion_context *fusion;
5022
5023 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005024
5025 /* Find first memory bar */
5026 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005027 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005028 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005029 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005030 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005031 return -EBUSY;
5032 }
5033
Ben Collins11f8a7b2013-09-13 12:46:44 -04005034 base_addr = pci_resource_start(instance->pdev, instance->bar);
5035 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005036
5037 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005038 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005039 goto fail_ioremap;
5040 }
5041
5042 reg_set = instance->reg_set;
5043
5044 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08005045 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005046 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005047 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305048 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305049 case PCI_DEVICE_ID_LSI_INTRUDER:
5050 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305051 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5052 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005053 instance->instancet = &megasas_instance_template_fusion;
5054 break;
adam radfordcd50ba82010-12-21 10:23:23 -08005055 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 break;
5072 }
5073
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305074 if (megasas_transition_to_ready(instance, 0)) {
5075 atomic_set(&instance->fw_reset_no_pci_access, 1);
5076 instance->instancet->adp_reset
5077 (instance, instance->reg_set);
5078 atomic_set(&instance->fw_reset_no_pci_access, 0);
5079 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005080 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305081 __func__);
5082
5083 /*waitting for about 30 second before retry*/
5084 ssleep(30);
5085
5086 if (megasas_transition_to_ready(instance, 0))
5087 goto fail_ready_state;
5088 }
adam radfordcd50ba82010-12-21 10:23:23 -08005089
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305090 /*
5091 * MSI-X host index 0 is common for all adapter.
5092 * It is used for all MPT based Adapters.
5093 */
5094 instance->reply_post_host_index_addr[0] =
Christoph Hellwig8a232bb2015-04-23 16:32:39 +05305095 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305096 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5097
adam radford3f1abce2011-05-11 18:33:47 -07005098 /* Check if MSI-X is supported while in ready state */
5099 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5100 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005101 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305102 scratch_pad_2 = readl
5103 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005104 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305105 if (fusion) {
5106 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5107 instance->msix_vectors = (scratch_pad_2
5108 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5109 fw_msix_count = instance->msix_vectors;
5110 } else { /* Invader series supports more than 8 MSI-x vectors*/
5111 instance->msix_vectors = ((scratch_pad_2
5112 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5113 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sumit Saxena179ac142016-01-28 21:04:28 +05305114 if (rdpq_enable)
5115 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5116 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305117 fw_msix_count = instance->msix_vectors;
5118 /* Save 1-15 reply post index address to local memory
5119 * Index 0 is already saved from reg offset
5120 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5121 */
5122 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5123 instance->reply_post_host_index_addr[loop] =
5124 (u32 __iomem *)
5125 ((u8 __iomem *)instance->reg_set +
5126 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5127 + (loop * 0x10));
5128 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305129 }
5130 if (msix_vectors)
5131 instance->msix_vectors = min(msix_vectors,
5132 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305133 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005134 instance->msix_vectors = 1;
5135 /* Don't bother allocating more MSI-X vectors than cpus */
5136 instance->msix_vectors = min(instance->msix_vectors,
5137 (unsigned int)num_online_cpus());
5138 for (i = 0; i < instance->msix_vectors; i++)
5139 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005140 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
5141 1, instance->msix_vectors);
Jiang Liuc12de882014-11-03 20:44:20 +08005142 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005143 instance->msix_vectors = i;
5144 else
adam radfordc8e858f2011-10-08 18:15:13 -07005145 instance->msix_vectors = 0;
5146 }
adam radford3f1abce2011-05-11 18:33:47 -07005147
Tomas Henzl258c3af2015-06-02 16:09:46 +05305148 dev_info(&instance->pdev->dev,
5149 "firmware supports msix\t: (%d)", fw_msix_count);
5150 dev_info(&instance->pdev->dev,
5151 "current msix/online cpus\t: (%d/%d)\n",
5152 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305153 dev_info(&instance->pdev->dev,
5154 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305155
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305156 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5157 (unsigned long)instance);
5158
Tomas Henzl258c3af2015-06-02 16:09:46 +05305159 if (instance->msix_vectors ?
5160 megasas_setup_irqs_msix(instance, 1) :
5161 megasas_setup_irqs_ioapic(instance))
5162 goto fail_setup_irqs;
5163
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305164 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5165 GFP_KERNEL);
5166 if (instance->ctrl_info == NULL)
5167 goto fail_init_adapter;
5168
5169 /*
5170 * Below are default value for legacy Firmware.
5171 * non-fusion based controllers
5172 */
5173 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5174 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005175 /* Get operational params, sge flags, send init cmd to controller */
5176 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005177 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005178
Tomas Henzl258c3af2015-06-02 16:09:46 +05305179
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305180 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005181
Andy Lutomirski13f30772016-05-03 10:24:31 -07005182 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005183
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305184 megasas_setup_jbod_map(instance);
5185
bo yang39a98552010-09-22 22:36:29 -04005186 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005187 * the following function will get the PD LIST.
5188 */
5189 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005190 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005191 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005192 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305193 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005194 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005195
Yang, Bobdc6fb82009-12-06 08:30:19 -07005196 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07005197 if (megasas_ld_list_query(instance,
5198 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5199 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07005200
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005201 /*
5202 * Compute the max allowed sectors per IO: The controller info has two
5203 * limits on max sectors. Driver should use the minimum of these two.
5204 *
5205 * 1 << stripe_sz_ops.min = max sectors per strip
5206 *
5207 * Note that older firmwares ( < FW ver 30) didn't report information
5208 * to calculate max_sectors_1. So the number ended up as zero always.
5209 */
bo yang14faea92007-11-09 04:14:00 -05005210 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305211 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005212
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305213 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5214 le16_to_cpu(ctrl_info->max_strips_per_io);
5215 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005216
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005217 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305218
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305219 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5220 instance->passive = ctrl_info->cluster.passive;
5221 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305222 instance->UnevenSpanSupport =
5223 ctrl_info->adapterOperations2.supportUnevenSpans;
5224 if (instance->UnevenSpanSupport) {
5225 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305226 if (MR_ValidateMapInfo(instance))
5227 fusion->fast_path_io = 1;
5228 else
5229 fusion->fast_path_io = 0;
5230
5231 }
5232 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305233 instance->requestorId = ctrl_info->iov.requestorId;
5234 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5235 if (!ctrl_info->adapterOperations2.activePassive)
5236 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305237
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305238 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5239 instance->PlasmaFW111 ? "1.11" : "new");
5240
5241 if (instance->PlasmaFW111) {
5242 iovPtr = (struct IOV_111 *)
5243 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5244 instance->requestorId = iovPtr->requestorId;
5245 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305246 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305247 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5248 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305249 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305250
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305251 instance->crash_dump_fw_support =
5252 ctrl_info->adapterOperations3.supportCrashDump;
5253 instance->crash_dump_drv_support =
5254 (instance->crash_dump_fw_support &&
5255 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305256 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305257 megasas_set_crash_dump_params(instance,
5258 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005259
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305260 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305261 if (instance->crash_dump_buf)
5262 pci_free_consistent(instance->pdev,
5263 CRASH_DMA_BUF_SIZE,
5264 instance->crash_dump_buf,
5265 instance->crash_dump_h);
5266 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005267 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305268
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305269
5270 dev_info(&instance->pdev->dev,
5271 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5272 le16_to_cpu(ctrl_info->pci.vendor_id),
5273 le16_to_cpu(ctrl_info->pci.device_id),
5274 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5275 le16_to_cpu(ctrl_info->pci.sub_device_id));
5276 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5277 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305278 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5279 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305280 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5281 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305282
5283
bo yang14faea92007-11-09 04:14:00 -05005284 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305285 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005286 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5287 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005288
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305289 /* Check for valid throttlequeuedepth module parameter */
5290 if (throttlequeuedepth &&
5291 throttlequeuedepth <= instance->max_scsi_cmds)
5292 instance->throttlequeuedepth = throttlequeuedepth;
5293 else
5294 instance->throttlequeuedepth =
5295 MEGASAS_THROTTLE_QUEUE_DEPTH;
5296
Shivasharan S7efc4152017-08-23 04:46:56 -07005297 if ((resetwaittime < 1) ||
5298 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305299 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5300
5301 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5302 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005303
adam radford229fe472014-03-10 02:51:56 -07005304 /* Launch SR-IOV heartbeat timer */
5305 if (instance->requestorId) {
5306 if (!megasas_sriov_start_heartbeat(instance, 1))
5307 megasas_start_timer(instance,
5308 &instance->sriov_heartbeat_timer,
5309 megasas_sriov_heartbeat_handler,
5310 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5311 else
5312 instance->skip_heartbeat_timer_del = 1;
5313 }
5314
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005315 return 0;
5316
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305317fail_get_pd_list:
5318 instance->instancet->disable_intr(instance);
adam radfordeb1b1232011-02-24 20:55:56 -08005319fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305320 megasas_destroy_irqs(instance);
5321fail_setup_irqs:
5322 if (instance->msix_vectors)
5323 pci_disable_msix(instance->pdev);
5324 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005325fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305326 kfree(instance->ctrl_info);
5327 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005328 iounmap(instance->reg_set);
5329
5330 fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005331 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005332
5333 return -EINVAL;
5334}
5335
5336/**
5337 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005338 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005339 */
5340static void megasas_release_mfi(struct megasas_instance *instance)
5341{
adam radford9c915a82010-12-21 13:34:31 -08005342 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005343
adam radford9c915a82010-12-21 13:34:31 -08005344 if (instance->reply_queue)
5345 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005346 instance->reply_queue, instance->reply_queue_h);
5347
5348 megasas_free_cmds(instance);
5349
5350 iounmap(instance->reg_set);
5351
Yinghai Lue7f85162016-08-05 23:37:34 -07005352 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005353}
5354
5355/**
5356 * megasas_get_seq_num - Gets latest event sequence numbers
5357 * @instance: Adapter soft state
5358 * @eli: FW event log sequence numbers information
5359 *
5360 * FW maintains a log of all events in a non-volatile area. Upper layers would
5361 * usually find out the latest sequence number of the events, the seq number at
5362 * the boot etc. They would "read" all the events below the latest seq number
5363 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5364 * number), they would subsribe to AEN (asynchronous event notification) and
5365 * wait for the events to happen.
5366 */
5367static int
5368megasas_get_seq_num(struct megasas_instance *instance,
5369 struct megasas_evt_log_info *eli)
5370{
5371 struct megasas_cmd *cmd;
5372 struct megasas_dcmd_frame *dcmd;
5373 struct megasas_evt_log_info *el_info;
5374 dma_addr_t el_info_h = 0;
5375
5376 cmd = megasas_get_cmd(instance);
5377
5378 if (!cmd) {
5379 return -ENOMEM;
5380 }
5381
5382 dcmd = &cmd->frame->dcmd;
5383 el_info = pci_alloc_consistent(instance->pdev,
5384 sizeof(struct megasas_evt_log_info),
5385 &el_info_h);
5386
5387 if (!el_info) {
5388 megasas_return_cmd(instance, cmd);
5389 return -ENOMEM;
5390 }
5391
5392 memset(el_info, 0, sizeof(*el_info));
5393 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5394
5395 dcmd->cmd = MFI_CMD_DCMD;
5396 dcmd->cmd_status = 0x0;
5397 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305398 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005399 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005400 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305401 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5402 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5403 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5404 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005405
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305406 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5407 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305408 /*
5409 * Copy the data back into callers buffer
5410 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305411 eli->newest_seq_num = el_info->newest_seq_num;
5412 eli->oldest_seq_num = el_info->oldest_seq_num;
5413 eli->clear_seq_num = el_info->clear_seq_num;
5414 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5415 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305416 } else
5417 dev_err(&instance->pdev->dev, "DCMD failed "
5418 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005419
5420 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5421 el_info, el_info_h);
5422
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305423 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005424
5425 return 0;
5426}
5427
5428/**
5429 * megasas_register_aen - Registers for asynchronous event notification
5430 * @instance: Adapter soft state
5431 * @seq_num: The starting sequence number
5432 * @class_locale: Class of the event
5433 *
5434 * This function subscribes for AEN for events beyond the @seq_num. It requests
5435 * to be notified if and only if the event is of type @class_locale
5436 */
5437static int
5438megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5439 u32 class_locale_word)
5440{
5441 int ret_val;
5442 struct megasas_cmd *cmd;
5443 struct megasas_dcmd_frame *dcmd;
5444 union megasas_evt_class_locale curr_aen;
5445 union megasas_evt_class_locale prev_aen;
5446
5447 /*
5448 * If there an AEN pending already (aen_cmd), check if the
5449 * class_locale of that pending AEN is inclusive of the new
5450 * AEN request we currently have. If it is, then we don't have
5451 * to do anything. In other words, whichever events the current
5452 * AEN request is subscribing to, have already been subscribed
5453 * to.
5454 *
5455 * If the old_cmd is _not_ inclusive, then we have to abort
5456 * that command, form a class_locale that is superset of both
5457 * old and current and re-issue to the FW
5458 */
5459
5460 curr_aen.word = class_locale_word;
5461
5462 if (instance->aen_cmd) {
5463
Christoph Hellwiga9555532015-04-23 16:34:24 +05305464 prev_aen.word =
5465 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005466
Shivasharan Sc62da792017-08-23 04:47:01 -07005467 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5468 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5469 dev_info(&instance->pdev->dev,
5470 "%s %d out of range class %d send by application\n",
5471 __func__, __LINE__, curr_aen.members.class);
5472 return 0;
5473 }
5474
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005475 /*
5476 * A class whose enum value is smaller is inclusive of all
5477 * higher values. If a PROGRESS (= -1) was previously
5478 * registered, then a new registration requests for higher
5479 * classes need not be sent to FW. They are automatically
5480 * included.
5481 *
5482 * Locale numbers don't have such hierarchy. They are bitmap
5483 * values
5484 */
5485 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305486 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005487 curr_aen.members.locale)) {
5488 /*
5489 * Previously issued event registration includes
5490 * current request. Nothing to do.
5491 */
5492 return 0;
5493 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305494 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005495
5496 if (prev_aen.members.class < curr_aen.members.class)
5497 curr_aen.members.class = prev_aen.members.class;
5498
5499 instance->aen_cmd->abort_aen = 1;
5500 ret_val = megasas_issue_blocked_abort_cmd(instance,
5501 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305502 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005503
5504 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005505 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005506 "previous AEN command\n");
5507 return ret_val;
5508 }
5509 }
5510 }
5511
5512 cmd = megasas_get_cmd(instance);
5513
5514 if (!cmd)
5515 return -ENOMEM;
5516
5517 dcmd = &cmd->frame->dcmd;
5518
5519 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5520
5521 /*
5522 * Prepare DCMD for aen registration
5523 */
5524 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5525
5526 dcmd->cmd = MFI_CMD_DCMD;
5527 dcmd->cmd_status = 0x0;
5528 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305529 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005530 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005531 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305532 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5533 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5534 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005535 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305536 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5537 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5538 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005539
Yang, Bof4c9a132009-10-06 14:43:28 -06005540 if (instance->aen_cmd != NULL) {
5541 megasas_return_cmd(instance, cmd);
5542 return 0;
5543 }
5544
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005545 /*
5546 * Store reference to the cmd used to register for AEN. When an
5547 * application wants us to register for AEN, we have to abort this
5548 * cmd and re-register with a new EVENT LOCALE supplied by that app
5549 */
5550 instance->aen_cmd = cmd;
5551
5552 /*
5553 * Issue the aen registration frame
5554 */
adam radford9c915a82010-12-21 13:34:31 -08005555 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005556
5557 return 0;
5558}
5559
5560/**
5561 * megasas_start_aen - Subscribes to AEN during driver load time
5562 * @instance: Adapter soft state
5563 */
5564static int megasas_start_aen(struct megasas_instance *instance)
5565{
5566 struct megasas_evt_log_info eli;
5567 union megasas_evt_class_locale class_locale;
5568
5569 /*
5570 * Get the latest sequence number from FW
5571 */
5572 memset(&eli, 0, sizeof(eli));
5573
5574 if (megasas_get_seq_num(instance, &eli))
5575 return -1;
5576
5577 /*
5578 * Register AEN with FW for latest sequence number plus 1
5579 */
5580 class_locale.members.reserved = 0;
5581 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5582 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5583
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305584 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305585 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305586 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005587}
5588
5589/**
5590 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5591 * @instance: Adapter soft state
5592 */
5593static int megasas_io_attach(struct megasas_instance *instance)
5594{
5595 struct Scsi_Host *host = instance->host;
5596
5597 /*
5598 * Export parameters required by SCSI mid-layer
5599 */
5600 host->irq = instance->pdev->irq;
5601 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305602 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005603 host->this_id = instance->init_id;
5604 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005605
5606 if (instance->fw_support_ieee)
5607 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5608
Yang, Bo1fd10682010-10-12 07:18:50 -06005609 /*
5610 * Check if the module parameter value for max_sectors can be used
5611 */
5612 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5613 instance->max_sectors_per_req = max_sectors;
5614 else {
5615 if (max_sectors) {
5616 if (((instance->pdev->device ==
5617 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5618 (instance->pdev->device ==
5619 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5620 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5621 instance->max_sectors_per_req = max_sectors;
5622 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005623 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005624 "and <= %d (or < 1MB for GEN2 controller)\n",
5625 instance->max_sectors_per_req);
5626 }
5627 }
5628 }
5629
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005630 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005631 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005632 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5633 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5634 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005635 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005636
5637 /*
5638 * Notify the mid-layer about the new controller
5639 */
5640 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305641 dev_err(&instance->pdev->dev,
5642 "Failed to add host from %s %d\n",
5643 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005644 return -ENODEV;
5645 }
5646
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005647 return 0;
5648}
5649
bo yang31ea7082007-11-07 12:09:50 -05005650static int
5651megasas_set_dma_mask(struct pci_dev *pdev)
5652{
5653 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005654 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005655 */
5656 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005657 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005658
Yang Hongyang284901a2009-04-06 19:01:15 -07005659 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005660 goto fail_set_dma_mask;
5661 }
5662 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005663 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005664 goto fail_set_dma_mask;
5665 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305666 /*
5667 * Ensure that all data structures are allocated in 32-bit
5668 * memory.
5669 */
5670 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5671 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5672 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5673 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5674 dev_info(&pdev->dev, "set 32bit DMA mask"
5675 "and 32 bit consistent mask\n");
5676 else
5677 goto fail_set_dma_mask;
5678 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305679
bo yang31ea7082007-11-07 12:09:50 -05005680 return 0;
5681
5682fail_set_dma_mask:
5683 return 1;
5684}
5685
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005686/**
5687 * megasas_probe_one - PCI hotplug entry point
5688 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005689 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005690 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005691static int megasas_probe_one(struct pci_dev *pdev,
5692 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005693{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305694 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005695 struct Scsi_Host *host;
5696 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005697 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305698 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005699
5700 /* Reset MSI-X in the kdump kernel */
5701 if (reset_devices) {
5702 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5703 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005704 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005705 &control);
5706 if (control & PCI_MSIX_FLAGS_ENABLE) {
5707 dev_info(&pdev->dev, "resetting MSI-X\n");
5708 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005709 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005710 control &
5711 ~PCI_MSIX_FLAGS_ENABLE);
5712 }
5713 }
5714 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005715
5716 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005717 * PCI prepping: enable device set bus mastering and dma mask
5718 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005719 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005720
5721 if (rval) {
5722 return rval;
5723 }
5724
5725 pci_set_master(pdev);
5726
bo yang31ea7082007-11-07 12:09:50 -05005727 if (megasas_set_dma_mask(pdev))
5728 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005729
5730 host = scsi_host_alloc(&megasas_template,
5731 sizeof(struct megasas_instance));
5732
5733 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005734 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005735 goto fail_alloc_instance;
5736 }
5737
5738 instance = (struct megasas_instance *)host->hostdata;
5739 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005740 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08005741 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005742
adam radford9c915a82010-12-21 13:34:31 -08005743 switch (instance->pdev->device) {
5744 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005745 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005746 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305747 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305748 case PCI_DEVICE_ID_LSI_INTRUDER:
5749 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305750 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5751 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005752 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305753 instance->ctrl_context_pages =
5754 get_order(sizeof(struct fusion_context));
5755 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5756 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005757 if (!instance->ctrl_context) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005758 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005759 "memory for Fusion context info\n");
5760 goto fail_alloc_dma_buf;
5761 }
5762 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305763 memset(fusion, 0,
5764 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305765 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5766 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5767 fusion->adapter_type = THUNDERBOLT_SERIES;
5768 else
5769 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08005770 }
5771 break;
5772 default: /* For all other supported controllers */
5773
5774 instance->producer =
5775 pci_alloc_consistent(pdev, sizeof(u32),
5776 &instance->producer_h);
5777 instance->consumer =
5778 pci_alloc_consistent(pdev, sizeof(u32),
5779 &instance->consumer_h);
5780
5781 if (!instance->producer || !instance->consumer) {
Colin Ian King4086eae52016-09-12 13:42:12 +01005782 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005783 "memory for producer, consumer\n");
5784 goto fail_alloc_dma_buf;
5785 }
5786
5787 *instance->producer = 0;
5788 *instance->consumer = 0;
5789 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005790 }
5791
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305792 /* Crash dump feature related initialisation*/
5793 instance->drv_buf_index = 0;
5794 instance->drv_buf_alloc = 0;
5795 instance->crash_dump_fw_support = 0;
5796 instance->crash_dump_app_support = 0;
5797 instance->fw_crash_state = UNAVAILABLE;
5798 spin_lock_init(&instance->crashdump_lock);
5799 instance->crash_dump_buf = NULL;
5800
Yang, Boc3518832009-10-06 14:18:02 -06005801 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005802 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005803 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005804 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05305805 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305806 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005807
5808 instance->evt_detail = pci_alloc_consistent(pdev,
5809 sizeof(struct
5810 megasas_evt_detail),
5811 &instance->evt_detail_h);
5812
5813 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005814 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005815 "event detail structure\n");
5816 goto fail_alloc_dma_buf;
5817 }
5818
Sumit Saxenac3e385a2016-04-15 00:23:30 -07005819 if (!reset_devices) {
5820 instance->system_info_buf = pci_zalloc_consistent(pdev,
5821 sizeof(struct MR_DRV_SYSTEM_INFO),
5822 &instance->system_info_h);
5823 if (!instance->system_info_buf)
5824 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
Sumit Saxena2216c302016-01-28 21:04:26 +05305825
Sumit Saxenac3e385a2016-04-15 00:23:30 -07005826 instance->pd_info = pci_alloc_consistent(pdev,
5827 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
5828
5829 if (!instance->pd_info)
5830 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
5831
5832 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5833 CRASH_DMA_BUF_SIZE,
5834 &instance->crash_dump_h);
5835 if (!instance->crash_dump_buf)
5836 dev_err(&pdev->dev, "Can't allocate Firmware "
5837 "crash dump DMA buffer\n");
5838 }
Sumit Saxena2216c302016-01-28 21:04:26 +05305839
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005840 /*
5841 * Initialize locks and queues
5842 */
5843 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005844 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005845
Sumant Patroe4a082c2006-05-30 12:03:37 -07005846 atomic_set(&instance->fw_outstanding,0);
5847
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005848 init_waitqueue_head(&instance->int_cmd_wait_q);
5849 init_waitqueue_head(&instance->abort_cmd_wait_q);
5850
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305851 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005852 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005853 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005854
adam radford9c915a82010-12-21 13:34:31 -08005855 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05305856 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005857
5858 /*
5859 * Initialize PCI related and misc parameters
5860 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005861 instance->host = host;
5862 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5863 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305864 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005865
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305866
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005867 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305868 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005869 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005870
Sumant Patro658dced2006-10-03 13:09:14 -07005871 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005872 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005873 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005874 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005875 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305876 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005877
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305878 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005879 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305880 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5881 } else
adam radford9c915a82010-12-21 13:34:31 -08005882 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005884 /*
adam radford0a770662011-02-24 20:56:12 -08005885 * Initialize MFI Firmware
5886 */
5887 if (megasas_init_fw(instance))
5888 goto fail_init_mfi;
5889
adam radford229fe472014-03-10 02:51:56 -07005890 if (instance->requestorId) {
5891 if (instance->PlasmaFW111) {
5892 instance->vf_affiliation_111 =
5893 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5894 &instance->vf_affiliation_111_h);
5895 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005896 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005897 "memory for VF affiliation buffer\n");
5898 } else {
5899 instance->vf_affiliation =
5900 pci_alloc_consistent(pdev,
5901 (MAX_LOGICAL_DRIVES + 1) *
5902 sizeof(struct MR_LD_VF_AFFILIATION),
5903 &instance->vf_affiliation_h);
5904 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005905 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005906 "memory for VF affiliation buffer\n");
5907 }
5908 }
5909
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005910 /*
5911 * Store instance in PCI softstate
5912 */
5913 pci_set_drvdata(pdev, instance);
5914
5915 /*
5916 * Add this controller to megasas_mgmt_info structure so that it
5917 * can be exported to management applications
5918 */
5919 megasas_mgmt_info.count++;
5920 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5921 megasas_mgmt_info.max_index++;
5922
5923 /*
adam radford541f90b2011-05-11 18:34:29 -07005924 * Register with SCSI mid-layer
5925 */
5926 if (megasas_io_attach(instance))
5927 goto fail_io_attach;
5928
5929 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305930 /*
5931 * Trigger SCSI to scan our drives
5932 */
5933 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005934
5935 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005936 * Initiate AEN (Asynchronous Event Notification)
5937 */
5938 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005939 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005940 goto fail_start_aen;
5941 }
5942
Adam Radford9ea81f82014-07-09 15:17:57 -07005943 /* Get current SR-IOV LD/VF affiliation */
5944 if (instance->requestorId)
5945 megasas_get_ld_vf_affiliation(instance, 1);
5946
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005947 return 0;
5948
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005949fail_start_aen:
5950fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005951 megasas_mgmt_info.count--;
5952 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5953 megasas_mgmt_info.max_index--;
5954
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305955 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305956 megasas_destroy_irqs(instance);
5957
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305958 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08005959 megasas_release_fusion(instance);
5960 else
5961 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005962 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005963 pci_disable_msix(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305964fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005965fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005966 if (instance->evt_detail)
5967 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5968 instance->evt_detail,
5969 instance->evt_detail_h);
5970
Sumit Saxena2216c302016-01-28 21:04:26 +05305971 if (instance->pd_info)
5972 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
5973 instance->pd_info,
5974 instance->pd_info_h);
adam radfordeb1b1232011-02-24 20:55:56 -08005975 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005976 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5977 instance->producer_h);
5978 if (instance->consumer)
5979 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5980 instance->consumer_h);
5981 scsi_host_put(host);
5982
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005983fail_alloc_instance:
5984fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005985 pci_disable_device(pdev);
5986
5987 return -ENODEV;
5988}
5989
5990/**
5991 * megasas_flush_cache - Requests FW to flush all its caches
5992 * @instance: Adapter soft state
5993 */
5994static void megasas_flush_cache(struct megasas_instance *instance)
5995{
5996 struct megasas_cmd *cmd;
5997 struct megasas_dcmd_frame *dcmd;
5998
Sumit Saxena8a01a412016-01-28 21:04:32 +05305999 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006000 return;
6001
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006002 cmd = megasas_get_cmd(instance);
6003
6004 if (!cmd)
6005 return;
6006
6007 dcmd = &cmd->frame->dcmd;
6008
6009 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6010
6011 dcmd->cmd = MFI_CMD_DCMD;
6012 dcmd->cmd_status = 0x0;
6013 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306014 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006015 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006016 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006017 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306018 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006019 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6020
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306021 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6022 != DCMD_SUCCESS) {
6023 dev_err(&instance->pdev->dev,
6024 "return from %s %d\n", __func__, __LINE__);
6025 return;
6026 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006027
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306028 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006029}
6030
6031/**
6032 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6033 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006034 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006035 */
bo yang31ea7082007-11-07 12:09:50 -05006036static void megasas_shutdown_controller(struct megasas_instance *instance,
6037 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006038{
6039 struct megasas_cmd *cmd;
6040 struct megasas_dcmd_frame *dcmd;
6041
Sumit Saxena8a01a412016-01-28 21:04:32 +05306042 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006043 return;
6044
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006045 cmd = megasas_get_cmd(instance);
6046
6047 if (!cmd)
6048 return;
6049
6050 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306051 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306052 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006053 if (instance->map_update_cmd)
6054 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306055 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306056 if (instance->jbod_seq_cmd)
6057 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306058 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306059
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006060 dcmd = &cmd->frame->dcmd;
6061
6062 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6063
6064 dcmd->cmd = MFI_CMD_DCMD;
6065 dcmd->cmd_status = 0x0;
6066 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306067 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006068 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006069 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006070 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306071 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006072
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306073 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6074 != DCMD_SUCCESS) {
6075 dev_err(&instance->pdev->dev,
6076 "return from %s %d\n", __func__, __LINE__);
6077 return;
6078 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006079
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306080 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006081}
6082
Jiri Slaby33139b22008-05-01 17:56:02 +02006083#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006084/**
bo yangad84db22007-11-09 04:40:16 -05006085 * megasas_suspend - driver suspend entry point
6086 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006087 * @state: PCI power state to suspend routine
6088 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006089static int
bo yang31ea7082007-11-07 12:09:50 -05006090megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6091{
6092 struct Scsi_Host *host;
6093 struct megasas_instance *instance;
6094
6095 instance = pci_get_drvdata(pdev);
6096 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006097 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006098
adam radford229fe472014-03-10 02:51:56 -07006099 /* Shutdown SR-IOV heartbeat timer */
6100 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6101 del_timer_sync(&instance->sriov_heartbeat_timer);
6102
bo yang31ea7082007-11-07 12:09:50 -05006103 megasas_flush_cache(instance);
6104 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006105
6106 /* cancel the delayed work if this work still in queue */
6107 if (instance->ev != NULL) {
6108 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006109 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006110 instance->ev = NULL;
6111 }
6112
bo yang31ea7082007-11-07 12:09:50 -05006113 tasklet_kill(&instance->isr_tasklet);
6114
6115 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306116 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006117
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306118 megasas_destroy_irqs(instance);
6119
adam radfordc8e858f2011-10-08 18:15:13 -07006120 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08006121 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006122
6123 pci_save_state(pdev);
6124 pci_disable_device(pdev);
6125
6126 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6127
6128 return 0;
6129}
6130
6131/**
6132 * megasas_resume- driver resume entry point
6133 * @pdev: PCI device structure
6134 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006135static int
bo yang31ea7082007-11-07 12:09:50 -05006136megasas_resume(struct pci_dev *pdev)
6137{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306138 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006139 struct Scsi_Host *host;
6140 struct megasas_instance *instance;
6141
6142 instance = pci_get_drvdata(pdev);
6143 host = instance->host;
6144 pci_set_power_state(pdev, PCI_D0);
6145 pci_enable_wake(pdev, PCI_D0, 0);
6146 pci_restore_state(pdev);
6147
6148 /*
6149 * PCI prepping: enable device set bus mastering and dma mask
6150 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006151 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006152
6153 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006154 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006155 return rval;
6156 }
6157
6158 pci_set_master(pdev);
6159
6160 if (megasas_set_dma_mask(pdev))
6161 goto fail_set_dma_mask;
6162
6163 /*
6164 * Initialize MFI Firmware
6165 */
6166
bo yang31ea7082007-11-07 12:09:50 -05006167 atomic_set(&instance->fw_outstanding, 0);
6168
6169 /*
6170 * We expect the FW state to be READY
6171 */
adam radford058a8fa2011-10-08 18:14:27 -07006172 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006173 goto fail_ready_state;
6174
adam radford3f1abce2011-05-11 18:33:47 -07006175 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02006176 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02006177 pci_enable_msix_exact(instance->pdev, instance->msixentry,
6178 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02006179 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006180
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306181 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006182 megasas_reset_reply_desc(instance);
6183 if (megasas_ioc_init_fusion(instance)) {
6184 megasas_free_cmds(instance);
6185 megasas_free_cmds_fusion(instance);
6186 goto fail_init_mfi;
6187 }
6188 if (!megasas_get_map_info(instance))
6189 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306190 } else {
adam radford9c915a82010-12-21 13:34:31 -08006191 *instance->producer = 0;
6192 *instance->consumer = 0;
6193 if (megasas_issue_init_mfi(instance))
6194 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006195 }
bo yang31ea7082007-11-07 12:09:50 -05006196
Shivasharan S4fa0de82018-06-04 03:45:10 -07006197 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
6198 goto fail_init_mfi;
6199
adam radford9c915a82010-12-21 13:34:31 -08006200 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6201 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006202
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306203 if (instance->msix_vectors ?
6204 megasas_setup_irqs_msix(instance, 0) :
6205 megasas_setup_irqs_ioapic(instance))
6206 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006207
adam radford229fe472014-03-10 02:51:56 -07006208 /* Re-launch SR-IOV heartbeat timer */
6209 if (instance->requestorId) {
6210 if (!megasas_sriov_start_heartbeat(instance, 0))
6211 megasas_start_timer(instance,
6212 &instance->sriov_heartbeat_timer,
6213 megasas_sriov_heartbeat_handler,
6214 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306215 else {
adam radford229fe472014-03-10 02:51:56 -07006216 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306217 goto fail_init_mfi;
6218 }
adam radford229fe472014-03-10 02:51:56 -07006219 }
6220
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306221 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306222 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006223 instance->unload = 0;
6224
adam radford541f90b2011-05-11 18:34:29 -07006225 /*
6226 * Initiate AEN (Asynchronous Event Notification)
6227 */
6228 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006229 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006230
bo yang31ea7082007-11-07 12:09:50 -05006231 return 0;
6232
bo yang31ea7082007-11-07 12:09:50 -05006233fail_init_mfi:
6234 if (instance->evt_detail)
6235 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6236 instance->evt_detail,
6237 instance->evt_detail_h);
6238
Sumit Saxena2216c302016-01-28 21:04:26 +05306239 if (instance->pd_info)
6240 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6241 instance->pd_info,
6242 instance->pd_info_h);
bo yang31ea7082007-11-07 12:09:50 -05006243 if (instance->producer)
6244 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6245 instance->producer_h);
6246 if (instance->consumer)
6247 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6248 instance->consumer_h);
6249 scsi_host_put(host);
6250
6251fail_set_dma_mask:
6252fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006253fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006254
6255 pci_disable_device(pdev);
6256
6257 return -ENODEV;
6258}
Jiri Slaby33139b22008-05-01 17:56:02 +02006259#else
6260#define megasas_suspend NULL
6261#define megasas_resume NULL
6262#endif
bo yang31ea7082007-11-07 12:09:50 -05006263
6264/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006265 * megasas_detach_one - PCI hot"un"plug entry point
6266 * @pdev: PCI device structure
6267 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006268static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006269{
6270 int i;
6271 struct Scsi_Host *host;
6272 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006273 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306274 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006275
6276 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006277 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006278 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006279 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006280
adam radford229fe472014-03-10 02:51:56 -07006281 /* Shutdown SR-IOV heartbeat timer */
6282 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6283 del_timer_sync(&instance->sriov_heartbeat_timer);
6284
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306285 if (instance->fw_crash_state != UNAVAILABLE)
6286 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006287 scsi_remove_host(instance->host);
6288 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006289 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006290
6291 /* cancel the delayed work if this work still in queue*/
6292 if (instance->ev != NULL) {
6293 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006294 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006295 instance->ev = NULL;
6296 }
6297
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306298 /* cancel all wait events */
6299 wake_up_all(&instance->int_cmd_wait_q);
6300
Sumant Patro5d018ad2006-10-03 13:13:18 -07006301 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006302
6303 /*
6304 * Take the instance off the instance array. Note that we will not
6305 * decrement the max_index. We let this array be sparse array
6306 */
6307 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6308 if (megasas_mgmt_info.instance[i] == instance) {
6309 megasas_mgmt_info.count--;
6310 megasas_mgmt_info.instance[i] = NULL;
6311
6312 break;
6313 }
6314 }
6315
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306316 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006317
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306318 megasas_destroy_irqs(instance);
6319
adam radfordc8e858f2011-10-08 18:15:13 -07006320 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08006321 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006322
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306323 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006324 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306325 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6326 (sizeof(struct MR_PD_CFG_SEQ) *
6327 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306328 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006329 if (fusion->ld_map[i])
6330 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306331 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006332 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306333 fusion->ld_map_phys[i]);
6334 if (fusion->ld_drv_map[i])
6335 free_pages((ulong)fusion->ld_drv_map[i],
6336 fusion->drv_map_pages);
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006337 if (fusion->pd_seq_sync[i])
6338 dma_free_coherent(&instance->pdev->dev,
6339 pd_seq_map_sz,
6340 fusion->pd_seq_sync[i],
6341 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306342 }
6343 free_pages((ulong)instance->ctrl_context,
6344 instance->ctrl_context_pages);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306345 } else {
adam radford9c915a82010-12-21 13:34:31 -08006346 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006347 pci_free_consistent(pdev, sizeof(u32),
6348 instance->producer,
6349 instance->producer_h);
6350 pci_free_consistent(pdev, sizeof(u32),
6351 instance->consumer,
6352 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08006353 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006354
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306355 kfree(instance->ctrl_info);
6356
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05306357 if (instance->evt_detail)
6358 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6359 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07006360
Sumit Saxena2216c302016-01-28 21:04:26 +05306361 if (instance->pd_info)
6362 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6363 instance->pd_info,
6364 instance->pd_info_h);
adam radford229fe472014-03-10 02:51:56 -07006365 if (instance->vf_affiliation)
6366 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6367 sizeof(struct MR_LD_VF_AFFILIATION),
6368 instance->vf_affiliation,
6369 instance->vf_affiliation_h);
6370
6371 if (instance->vf_affiliation_111)
6372 pci_free_consistent(pdev,
6373 sizeof(struct MR_LD_VF_AFFILIATION_111),
6374 instance->vf_affiliation_111,
6375 instance->vf_affiliation_111_h);
6376
6377 if (instance->hb_host_mem)
6378 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6379 instance->hb_host_mem,
6380 instance->hb_host_mem_h);
6381
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306382 if (instance->crash_dump_buf)
6383 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6384 instance->crash_dump_buf, instance->crash_dump_h);
6385
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306386 if (instance->system_info_buf)
6387 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6388 instance->system_info_buf, instance->system_info_h);
6389
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006390 scsi_host_put(host);
6391
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006392 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006393}
6394
6395/**
6396 * megasas_shutdown - Shutdown entry point
6397 * @device: Generic device structure
6398 */
6399static void megasas_shutdown(struct pci_dev *pdev)
6400{
6401 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006402
Yang, Bo0c79e682009-10-06 14:47:35 -06006403 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006404 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006405 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306406 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306407 megasas_destroy_irqs(instance);
6408
adam radfordc8e858f2011-10-08 18:15:13 -07006409 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07006410 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006411}
6412
6413/**
6414 * megasas_mgmt_open - char node "open" entry point
6415 */
6416static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6417{
6418 /*
6419 * Allow only those users with admin rights
6420 */
6421 if (!capable(CAP_SYS_ADMIN))
6422 return -EACCES;
6423
6424 return 0;
6425}
6426
6427/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006428 * megasas_mgmt_fasync - Async notifier registration from applications
6429 *
6430 * This function adds the calling process to a driver global queue. When an
6431 * event occurs, SIGIO will be sent to all processes in this queue.
6432 */
6433static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6434{
6435 int rc;
6436
Arjan van de Ven0b950672006-01-11 13:16:10 +01006437 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006438
6439 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6440
Arjan van de Ven0b950672006-01-11 13:16:10 +01006441 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006442
6443 if (rc >= 0) {
6444 /* For sanity check when we get ioctl */
6445 filep->private_data = filep;
6446 return 0;
6447 }
6448
6449 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6450
6451 return rc;
6452}
6453
6454/**
Yang, Boc3518832009-10-06 14:18:02 -06006455 * megasas_mgmt_poll - char node "poll" entry point
6456 * */
6457static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6458{
6459 unsigned int mask;
6460 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006461
Yang, Boc3518832009-10-06 14:18:02 -06006462 poll_wait(file, &megasas_poll_wait, wait);
6463 spin_lock_irqsave(&poll_aen_lock, flags);
6464 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006465 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006466 else
6467 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306468 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006469 spin_unlock_irqrestore(&poll_aen_lock, flags);
6470 return mask;
6471}
6472
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306473/*
6474 * megasas_set_crash_dump_params_ioctl:
6475 * Send CRASH_DUMP_MODE DCMD to all controllers
6476 * @cmd: MFI command frame
6477 */
6478
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006479static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306480{
6481 struct megasas_instance *local_instance;
6482 int i, error = 0;
6483 int crash_support;
6484
6485 crash_support = cmd->frame->dcmd.mbox.w[0];
6486
6487 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6488 local_instance = megasas_mgmt_info.instance[i];
6489 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05306490 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306491 MEGASAS_HBA_OPERATIONAL) &&
6492 !megasas_set_crash_dump_params(local_instance,
6493 crash_support)) {
6494 local_instance->crash_dump_app_support =
6495 crash_support;
6496 dev_info(&local_instance->pdev->dev,
6497 "Application firmware crash "
6498 "dump mode set success\n");
6499 error = 0;
6500 } else {
6501 dev_info(&local_instance->pdev->dev,
6502 "Application firmware crash "
6503 "dump mode set failed\n");
6504 error = -1;
6505 }
6506 }
6507 }
6508 return error;
6509}
6510
Yang, Boc3518832009-10-06 14:18:02 -06006511/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006512 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6513 * @instance: Adapter soft state
6514 * @argp: User's ioctl packet
6515 */
6516static int
6517megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6518 struct megasas_iocpacket __user * user_ioc,
6519 struct megasas_iocpacket *ioc)
6520{
6521 struct megasas_sge32 *kern_sge32;
6522 struct megasas_cmd *cmd;
6523 void *kbuff_arr[MAX_IOCTL_SGE];
6524 dma_addr_t buf_handle = 0;
6525 int error = 0, i;
6526 void *sense = NULL;
6527 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006528 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006529
6530 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6531
6532 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006533 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006534 ioc->sge_count, MAX_IOCTL_SGE);
6535 return -EINVAL;
6536 }
6537
6538 cmd = megasas_get_cmd(instance);
6539 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006540 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006541 return -ENOMEM;
6542 }
6543
6544 /*
6545 * User's IOCTL packet has 2 frames (maximum). Copy those two
6546 * frames into our cmd's frames. cmd->frame's context will get
6547 * overwritten when we copy from user's frames. So set that value
6548 * alone separately
6549 */
6550 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306551 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006552 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306553 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6554 MFI_FRAME_SGL64 |
6555 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006556
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306557 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6558 error = megasas_set_crash_dump_params_ioctl(cmd);
6559 megasas_return_cmd(instance, cmd);
6560 return error;
6561 }
6562
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006563 /*
6564 * The management interface between applications and the fw uses
6565 * MFI frames. E.g, RAID configuration changes, LD property changes
6566 * etc are accomplishes through different kinds of MFI frames. The
6567 * driver needs to care only about substituting user buffers with
6568 * kernel buffers in SGLs. The location of SGL is embedded in the
6569 * struct iocpacket itself.
6570 */
6571 kern_sge32 = (struct megasas_sge32 *)
6572 ((unsigned long)cmd->frame + ioc->sgl_off);
6573
6574 /*
6575 * For each user buffer, create a mirror buffer and copy in
6576 */
6577 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006578 if (!ioc->sgl[i].iov_len)
6579 continue;
6580
Sumant Patro9f35fa82007-02-14 12:55:45 -08006581 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006582 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006583 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006584 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006585 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6586 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006587 error = -ENOMEM;
6588 goto out;
6589 }
6590
6591 /*
6592 * We don't change the dma_coherent_mask, so
6593 * pci_alloc_consistent only returns 32bit addresses
6594 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306595 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6596 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006597
6598 /*
6599 * We created a kernel buffer corresponding to the
6600 * user buffer. Now copy in from the user buffer
6601 */
6602 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6603 (u32) (ioc->sgl[i].iov_len))) {
6604 error = -EFAULT;
6605 goto out;
6606 }
6607 }
6608
6609 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006610 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6611 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006612 if (!sense) {
6613 error = -ENOMEM;
6614 goto out;
6615 }
6616
6617 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006618 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306619 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006620 }
6621
6622 /*
6623 * Set the sync_cmd flag so that the ISR knows not to complete this
6624 * cmd to the SCSI mid-layer
6625 */
6626 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306627 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6628 cmd->sync_cmd = 0;
6629 dev_err(&instance->pdev->dev,
6630 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6631 __func__, __LINE__, cmd->frame->dcmd.opcode,
6632 cmd->cmd_status_drv);
6633 return -EBUSY;
6634 }
6635
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006636 cmd->sync_cmd = 0;
6637
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306638 if (instance->unload == 1) {
6639 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6640 "don't submit data to application\n");
6641 goto out;
6642 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006643 /*
6644 * copy out the kernel buffers to user buffers
6645 */
6646 for (i = 0; i < ioc->sge_count; i++) {
6647 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6648 ioc->sgl[i].iov_len)) {
6649 error = -EFAULT;
6650 goto out;
6651 }
6652 }
6653
6654 /*
6655 * copy out the sense
6656 */
6657 if (ioc->sense_len) {
6658 /*
bo yangb70a41e2008-03-18 03:13:06 -04006659 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006660 * sense buffer address
6661 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006662 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6663 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006664
bo yangb70a41e2008-03-18 03:13:06 -04006665 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6666 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006667 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05006668 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006669 error = -EFAULT;
6670 goto out;
6671 }
6672 }
6673
6674 /*
6675 * copy the status codes returned by the fw
6676 */
6677 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6678 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006679 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006680 error = -EFAULT;
6681 }
6682
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006683out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006684 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006685 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006686 sense, sense_handle);
6687 }
6688
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006689 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01006690 if (kbuff_arr[i]) {
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006691 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306692 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006693 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306694 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306695 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01006696 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006697 }
6698
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306699 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006700 return error;
6701}
6702
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006703static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6704{
6705 struct megasas_iocpacket __user *user_ioc =
6706 (struct megasas_iocpacket __user *)arg;
6707 struct megasas_iocpacket *ioc;
6708 struct megasas_instance *instance;
6709 int error;
bo yang39a98552010-09-22 22:36:29 -04006710 int i;
6711 unsigned long flags;
6712 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006713
Markus Elfring709ab232016-08-21 10:39:04 +02006714 ioc = memdup_user(user_ioc, sizeof(*ioc));
6715 if (IS_ERR(ioc))
6716 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006717
6718 instance = megasas_lookup_instance(ioc->host_no);
6719 if (!instance) {
6720 error = -ENODEV;
6721 goto out_kfree_ioc;
6722 }
6723
adam radford229fe472014-03-10 02:51:56 -07006724 /* Adjust ioctl wait time for VF mode */
6725 if (instance->requestorId)
6726 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6727
6728 /* Block ioctls in VF mode */
6729 if (instance->requestorId && !allow_vf_ioctls) {
6730 error = -ENODEV;
6731 goto out_kfree_ioc;
6732 }
6733
Sumit Saxena8a01a412016-01-28 21:04:32 +05306734 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006735 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006736 error = -ENODEV;
6737 goto out_kfree_ioc;
6738 }
6739
6740 if (instance->unload == 1) {
6741 error = -ENODEV;
6742 goto out_kfree_ioc;
6743 }
6744
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006745 if (down_interruptible(&instance->ioctl_sem)) {
6746 error = -ERESTARTSYS;
6747 goto out_kfree_ioc;
6748 }
bo yang39a98552010-09-22 22:36:29 -04006749
6750 for (i = 0; i < wait_time; i++) {
6751
6752 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306753 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006754 spin_unlock_irqrestore(&instance->hba_lock, flags);
6755 break;
6756 }
6757 spin_unlock_irqrestore(&instance->hba_lock, flags);
6758
6759 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006760 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04006761 "for controller reset to finish\n");
6762 }
6763
6764 msleep(1000);
6765 }
6766
6767 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306768 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006769 spin_unlock_irqrestore(&instance->hba_lock, flags);
6770
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006771 dev_err(&instance->pdev->dev, "timed out while"
bo yang39a98552010-09-22 22:36:29 -04006772 "waiting for HBA to recover\n");
6773 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006774 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006775 }
6776 spin_unlock_irqrestore(&instance->hba_lock, flags);
6777
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006778 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006779out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006780 up(&instance->ioctl_sem);
6781
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006782out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006783 kfree(ioc);
6784 return error;
6785}
6786
6787static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6788{
6789 struct megasas_instance *instance;
6790 struct megasas_aen aen;
6791 int error;
bo yang39a98552010-09-22 22:36:29 -04006792 int i;
6793 unsigned long flags;
6794 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006795
6796 if (file->private_data != file) {
6797 printk(KERN_DEBUG "megasas: fasync_helper was not "
6798 "called first\n");
6799 return -EINVAL;
6800 }
6801
6802 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6803 return -EFAULT;
6804
6805 instance = megasas_lookup_instance(aen.host_no);
6806
6807 if (!instance)
6808 return -ENODEV;
6809
Sumit Saxena8a01a412016-01-28 21:04:32 +05306810 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04006811 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006812 }
6813
6814 if (instance->unload == 1) {
6815 return -ENODEV;
6816 }
6817
bo yang39a98552010-09-22 22:36:29 -04006818 for (i = 0; i < wait_time; i++) {
6819
6820 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306821 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006822 spin_unlock_irqrestore(&instance->hba_lock,
6823 flags);
6824 break;
6825 }
6826
6827 spin_unlock_irqrestore(&instance->hba_lock, flags);
6828
6829 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006830 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04006831 "controller reset to finish\n");
6832 }
6833
6834 msleep(1000);
6835 }
6836
6837 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306838 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006839 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006840 dev_err(&instance->pdev->dev, "timed out while waiting"
6841 "for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04006842 return -ENODEV;
6843 }
6844 spin_unlock_irqrestore(&instance->hba_lock, flags);
6845
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306846 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006847 error = megasas_register_aen(instance, aen.seq_num,
6848 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306849 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006850 return error;
6851}
6852
6853/**
6854 * megasas_mgmt_ioctl - char node ioctl entry point
6855 */
6856static long
6857megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6858{
6859 switch (cmd) {
6860 case MEGASAS_IOC_FIRMWARE:
6861 return megasas_mgmt_ioctl_fw(file, arg);
6862
6863 case MEGASAS_IOC_GET_AEN:
6864 return megasas_mgmt_ioctl_aen(file, arg);
6865 }
6866
6867 return -ENOTTY;
6868}
6869
6870#ifdef CONFIG_COMPAT
6871static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6872{
6873 struct compat_megasas_iocpacket __user *cioc =
6874 (struct compat_megasas_iocpacket __user *)arg;
6875 struct megasas_iocpacket __user *ioc =
6876 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6877 int i;
6878 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006879 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306880 u32 local_sense_off;
6881 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306882 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006883
Jeff Garzik83aabc12006-10-04 06:34:03 -04006884 if (clear_user(ioc, sizeof(*ioc)))
6885 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006886
6887 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6888 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6889 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6890 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6891 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6892 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6893 return -EFAULT;
6894
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006895 /*
6896 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6897 * sense_len is not null, so prepare the 64bit value under
6898 * the same condition.
6899 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306900 if (get_user(local_sense_off, &ioc->sense_off) ||
6901 get_user(local_sense_len, &ioc->sense_len) ||
6902 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306903 return -EFAULT;
6904
Wenwen Wangb18de4e2018-10-06 13:34:21 -05006905 if (local_sense_off != user_sense_off)
6906 return -EINVAL;
6907
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306908 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006909 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306910 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006911 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306912 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006913 if (get_user(ptr, sense_cioc_ptr) ||
6914 put_user(compat_ptr(ptr), sense_ioc_ptr))
6915 return -EFAULT;
6916 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006917
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006918 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006919 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6920 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6921 copy_in_user(&ioc->sgl[i].iov_len,
6922 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6923 return -EFAULT;
6924 }
6925
6926 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6927
6928 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6929 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6930 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6931 return -EFAULT;
6932 }
6933 return error;
6934}
6935
6936static long
6937megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6938 unsigned long arg)
6939{
6940 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006941 case MEGASAS_IOC_FIRMWARE32:
6942 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006943 case MEGASAS_IOC_GET_AEN:
6944 return megasas_mgmt_ioctl_aen(file, arg);
6945 }
6946
6947 return -ENOTTY;
6948}
6949#endif
6950
6951/*
6952 * File operations structure for management interface
6953 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006954static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006955 .owner = THIS_MODULE,
6956 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006957 .fasync = megasas_mgmt_fasync,
6958 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006959 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006960#ifdef CONFIG_COMPAT
6961 .compat_ioctl = megasas_mgmt_compat_ioctl,
6962#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006963 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006964};
6965
6966/*
6967 * PCI hotplug support registration structure
6968 */
6969static struct pci_driver megasas_pci_driver = {
6970
6971 .name = "megaraid_sas",
6972 .id_table = megasas_pci_table,
6973 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006974 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006975 .suspend = megasas_suspend,
6976 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006977 .shutdown = megasas_shutdown,
6978};
6979
6980/*
6981 * Sysfs driver attributes
6982 */
6983static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6984{
6985 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6986 MEGASAS_VERSION);
6987}
6988
6989static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6990
6991static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306992megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6993{
6994 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6995 MEGASAS_RELDATE);
6996}
6997
6998static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6999
7000static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06007001megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
7002{
7003 return sprintf(buf, "%u\n", support_poll_for_event);
7004}
7005
7006static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
7007 megasas_sysfs_show_support_poll_for_event, NULL);
7008
Yang, Bo837f5fe2010-10-11 06:59:20 -06007009 static ssize_t
7010megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
7011{
7012 return sprintf(buf, "%u\n", support_device_change);
7013}
7014
7015static DRIVER_ATTR(support_device_change, S_IRUGO,
7016 megasas_sysfs_show_support_device_change, NULL);
7017
Yang, Bo72c4fd32009-10-06 14:20:59 -06007018static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07007019megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
7020{
bo yangad84db22007-11-09 04:40:16 -05007021 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007022}
7023
7024static ssize_t
7025megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
7026{
7027 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007028
7029 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007030 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7031 retval = -EINVAL;
7032 }
7033 return retval;
7034}
7035
Joe Malicki66dca9b2008-08-14 17:14:48 -04007036static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05007037 megasas_sysfs_set_dbg_lvl);
7038
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007039static void
7040megasas_aen_polling(struct work_struct *work)
7041{
7042 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007043 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007044 struct megasas_instance *instance = ev->instance;
7045 union megasas_evt_class_locale class_locale;
7046 struct Scsi_Host *host;
7047 struct scsi_device *sdev1;
7048 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007049 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007050 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007051 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007052 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307053 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007054
7055 if (!instance) {
7056 printk(KERN_ERR "invalid instance!\n");
7057 kfree(ev);
7058 return;
7059 }
adam radford229fe472014-03-10 02:51:56 -07007060
7061 /* Adjust event workqueue thread wait time for VF mode */
7062 if (instance->requestorId)
7063 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7064
7065 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307066 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007067
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007068 instance->ev = NULL;
7069 host = instance->host;
7070 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307071 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007072
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307073 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307074
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007075 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307076 case MR_EVT_PD_REMOVED:
7077 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307078 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307079 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007080 break;
7081
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307082 case MR_EVT_LD_OFFLINE:
7083 case MR_EVT_CFG_CLEARED:
7084 case MR_EVT_LD_DELETED:
7085 case MR_EVT_LD_CREATED:
7086 if (!instance->requestorId ||
7087 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7088 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007089
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307090 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307091 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007092
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307093 break;
7094
7095 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7096 case MR_EVT_FOREIGN_CFG_IMPORTED:
7097 case MR_EVT_LD_STATE_CHANGE:
7098 dcmd_ret = megasas_get_pd_list(instance);
7099
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307100 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307101 break;
7102
7103 if (!instance->requestorId ||
7104 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7105 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7106
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307107 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307108 break;
7109
7110 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7111 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7112 instance->host->host_no);
7113 break;
7114
7115 case MR_EVT_CTRL_PROP_CHANGED:
7116 dcmd_ret = megasas_get_ctrl_info(instance);
7117 break;
7118 default:
7119 doscan = 0;
7120 break;
7121 }
7122 } else {
7123 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7124 mutex_unlock(&instance->reset_mutex);
7125 kfree(ev);
7126 return;
7127 }
7128
7129 mutex_unlock(&instance->reset_mutex);
7130
7131 if (doscan & SCAN_PD_CHANNEL) {
7132 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7133 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7134 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007135 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307136 if (instance->pd_list[pd_index].driveState ==
7137 MR_PD_STATE_SYSTEM) {
7138 if (!sdev1)
7139 scsi_add_device(host, i, j, 0);
7140 else
Yang, Boc9786842009-12-06 08:39:25 -07007141 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007142 } else {
7143 if (sdev1) {
7144 scsi_remove_device(sdev1);
7145 scsi_device_put(sdev1);
7146 }
7147 }
Yang, Boc9786842009-12-06 08:39:25 -07007148 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007149 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007150 }
7151
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307152 if (doscan & SCAN_VD_CHANNEL) {
7153 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7154 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7155 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7156 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7157 if (instance->ld_ids[ld_index] != 0xff) {
7158 if (!sdev1)
7159 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7160 else
7161 scsi_device_put(sdev1);
7162 } else {
7163 if (sdev1) {
7164 scsi_remove_device(sdev1);
7165 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007166 }
7167 }
7168 }
7169 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007170 }
7171
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307172 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307173 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7174 else
7175 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007176
7177 /* Register AEN with FW for latest sequence number plus 1 */
7178 class_locale.members.reserved = 0;
7179 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7180 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307181
7182 if (instance->aen_cmd != NULL) {
7183 kfree(ev);
7184 return;
7185 }
7186
7187 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007188 error = megasas_register_aen(instance, seq_num,
7189 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007190 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307191 dev_err(&instance->pdev->dev,
7192 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007193
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307194 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007195 kfree(ev);
7196}
7197
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007198/**
7199 * megasas_init - Driver load entry point
7200 */
7201static int __init megasas_init(void)
7202{
7203 int rval;
7204
7205 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007206 * Booted in kdump kernel, minimize memory footprints by
7207 * disabling few features
7208 */
7209 if (reset_devices) {
7210 msix_vectors = 1;
7211 rdpq_enable = 0;
7212 dual_qdepth_disable = 1;
7213 }
7214
7215 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007216 * Announce driver version and other information
7217 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307218 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007219
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007220 spin_lock_init(&poll_aen_lock);
7221
Yang, Bo72c4fd32009-10-06 14:20:59 -06007222 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007223 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007224
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007225 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7226
7227 /*
7228 * Register character device node
7229 */
7230 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7231
7232 if (rval < 0) {
7233 printk(KERN_DEBUG "megasas: failed to open device node\n");
7234 return rval;
7235 }
7236
7237 megasas_mgmt_majorno = rval;
7238
7239 /*
7240 * Register ourselves as PCI hotplug module
7241 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007242 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007243
7244 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007245 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007246 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007247 }
7248
Jeff Garzik83aabc12006-10-04 06:34:03 -04007249 rval = driver_create_file(&megasas_pci_driver.driver,
7250 &driver_attr_version);
7251 if (rval)
7252 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007253
7254 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307255 &driver_attr_release_date);
7256 if (rval)
7257 goto err_dcf_rel_date;
7258
7259 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007260 &driver_attr_support_poll_for_event);
7261 if (rval)
7262 goto err_dcf_support_poll_for_event;
7263
Jeff Garzik83aabc12006-10-04 06:34:03 -04007264 rval = driver_create_file(&megasas_pci_driver.driver,
7265 &driver_attr_dbg_lvl);
7266 if (rval)
7267 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007268 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007269 &driver_attr_support_device_change);
7270 if (rval)
7271 goto err_dcf_support_device_change;
7272
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007273 return rval;
bo yangad84db22007-11-09 04:40:16 -05007274
Yang, Bo837f5fe2010-10-11 06:59:20 -06007275err_dcf_support_device_change:
7276 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007277 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007278err_dcf_dbg_lvl:
7279 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007280 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007281err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307282 driver_remove_file(&megasas_pci_driver.driver,
7283 &driver_attr_release_date);
7284err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007285 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7286err_dcf_attr_ver:
7287 pci_unregister_driver(&megasas_pci_driver);
7288err_pcidrv:
7289 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007290 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007291}
7292
7293/**
7294 * megasas_exit - Driver unload entry point
7295 */
7296static void __exit megasas_exit(void)
7297{
Sumant Patro658dced2006-10-03 13:09:14 -07007298 driver_remove_file(&megasas_pci_driver.driver,
7299 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007300 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007301 &driver_attr_support_poll_for_event);
7302 driver_remove_file(&megasas_pci_driver.driver,
7303 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307304 driver_remove_file(&megasas_pci_driver.driver,
7305 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007306 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007307
7308 pci_unregister_driver(&megasas_pci_driver);
7309 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7310}
7311
7312module_init(megasas_init);
7313module_exit(megasas_exit);