blob: c1ed25adb17ec1f11b039854ffa62e590032f9d6 [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 -0800192void
193megasas_release_fusion(struct megasas_instance *instance);
194int
195megasas_ioc_init_fusion(struct megasas_instance *instance);
196void
197megasas_free_cmds_fusion(struct megasas_instance *instance);
198u8
199megasas_get_map_info(struct megasas_instance *instance);
200int
201megasas_sync_map_info(struct megasas_instance *instance);
202int
adam radford229fe472014-03-10 02:51:56 -0700203wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
204 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800205void megasas_reset_reply_desc(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -0800206void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700207static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
208 int initial);
209int megasas_check_mpio_paths(struct megasas_instance *instance,
210 struct scsi_cmnd *scmd);
adam radfordcd50ba82010-12-21 10:23:23 -0800211
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530212int
adam radfordcd50ba82010-12-21 10:23:23 -0800213megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
214{
215 instance->instancet->fire_cmd(instance,
216 cmd->frame_phys_addr, 0, instance->reg_set);
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530217 return 0;
adam radfordcd50ba82010-12-21 10:23:23 -0800218}
bo yang7343eb62007-11-09 04:35:44 -0500219
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400220/**
221 * megasas_get_cmd - Get a command from the free pool
222 * @instance: Adapter soft state
223 *
224 * Returns a free command from the pool
225 */
adam radford9c915a82010-12-21 13:34:31 -0800226struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400227 *instance)
228{
229 unsigned long flags;
230 struct megasas_cmd *cmd = NULL;
231
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530232 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400233
234 if (!list_empty(&instance->cmd_pool)) {
235 cmd = list_entry((&instance->cmd_pool)->next,
236 struct megasas_cmd, list);
237 list_del_init(&cmd->list);
238 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500239 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400240 }
241
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530242 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400243 return cmd;
244}
245
246/**
247 * megasas_return_cmd - Return a cmd to free command pool
248 * @instance: Adapter soft state
249 * @cmd: Command packet to be returned to free command pool
250 */
adam radford9c915a82010-12-21 13:34:31 -0800251inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400252megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
253{
254 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530255 u32 blk_tags;
256 struct megasas_cmd_fusion *cmd_fusion;
257 struct fusion_context *fusion = instance->ctrl_context;
258
259 /* This flag is used only for fusion adapter.
260 * Wait for Interrupt for Polled mode DCMD
261 */
262 if (cmd->flags & DRV_DCMD_POLLED_MODE)
263 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400264
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530265 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400266
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530267 if (fusion) {
268 blk_tags = instance->max_scsi_cmds + cmd->index;
269 cmd_fusion = fusion->cmd_list[blk_tags];
270 megasas_return_cmd_fusion(instance, cmd_fusion);
271 }
272 cmd->scmd = NULL;
273 cmd->frame_count = 0;
274 cmd->flags = 0;
275 if (!fusion && reset_devices)
276 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
277 list_add(&cmd->list, (&instance->cmd_pool)->next);
278
279 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
280
281}
Sumant Patro1341c932006-01-25 12:02:40 -0800282
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530283static const char *
284format_timestamp(uint32_t timestamp)
285{
286 static char buffer[32];
287
288 if ((timestamp & 0xff000000) == 0xff000000)
289 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
290 0x00ffffff);
291 else
292 snprintf(buffer, sizeof(buffer), "%us", timestamp);
293 return buffer;
294}
295
296static const char *
297format_class(int8_t class)
298{
299 static char buffer[6];
300
301 switch (class) {
302 case MFI_EVT_CLASS_DEBUG:
303 return "debug";
304 case MFI_EVT_CLASS_PROGRESS:
305 return "progress";
306 case MFI_EVT_CLASS_INFO:
307 return "info";
308 case MFI_EVT_CLASS_WARNING:
309 return "WARN";
310 case MFI_EVT_CLASS_CRITICAL:
311 return "CRIT";
312 case MFI_EVT_CLASS_FATAL:
313 return "FATAL";
314 case MFI_EVT_CLASS_DEAD:
315 return "DEAD";
316 default:
317 snprintf(buffer, sizeof(buffer), "%d", class);
318 return buffer;
319 }
320}
321
322/**
323 * megasas_decode_evt: Decode FW AEN event and print critical event
324 * for information.
325 * @instance: Adapter soft state
326 */
327static void
328megasas_decode_evt(struct megasas_instance *instance)
329{
330 struct megasas_evt_detail *evt_detail = instance->evt_detail;
331 union megasas_evt_class_locale class_locale;
332 class_locale.word = le32_to_cpu(evt_detail->cl.word);
333
334 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
335 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
336 le32_to_cpu(evt_detail->seq_num),
337 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
338 (class_locale.members.locale),
339 format_class(class_locale.members.class),
340 evt_detail->description);
341}
342
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400343/**
adam radford0d490162010-12-14 19:17:17 -0800344* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800345* (deviceid : 1064R, PERC5) controllers
346*/
347
348/**
349 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400350 * @regs: MFI register set
351 */
352static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530353megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400354{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530355 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500356
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530357 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400358 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400359
360 /* Dummy readl to force pci flush */
361 readl(&regs->outbound_intr_mask);
362}
363
364/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700365 * megasas_disable_intr_xscale -Disables interrupt
366 * @regs: MFI register set
367 */
368static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530369megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700370{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530371 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700372 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500373
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530374 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700375 writel(mask, &regs->outbound_intr_mask);
376 /* Dummy readl to force pci flush */
377 readl(&regs->outbound_intr_mask);
378}
379
380/**
Sumant Patro1341c932006-01-25 12:02:40 -0800381 * megasas_read_fw_status_reg_xscale - returns the current FW status value
382 * @regs: MFI register set
383 */
384static u32
385megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
386{
387 return readl(&(regs)->outbound_msg_0);
388}
389/**
390 * megasas_clear_interrupt_xscale - Check & clear interrupt
391 * @regs: MFI register set
392 */
adam radford0d490162010-12-14 19:17:17 -0800393static int
Sumant Patro1341c932006-01-25 12:02:40 -0800394megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
395{
396 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400397 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500398
Sumant Patro1341c932006-01-25 12:02:40 -0800399 /*
400 * Check if it is our interrupt
401 */
402 status = readl(&regs->outbound_intr_status);
403
bo yang39a98552010-09-22 22:36:29 -0400404 if (status & MFI_OB_INTR_STATUS_MASK)
405 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
406 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
407 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800408
409 /*
410 * Clear the interrupt by writing back the same value
411 */
bo yang39a98552010-09-22 22:36:29 -0400412 if (mfiStatus)
413 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800414
Yang, Bo06f579d2008-08-10 12:42:37 -0700415 /* Dummy readl to force pci flush */
416 readl(&regs->outbound_intr_status);
417
bo yang39a98552010-09-22 22:36:29 -0400418 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800419}
420
421/**
422 * megasas_fire_cmd_xscale - Sends command to the FW
423 * @frame_phys_addr : Physical address of cmd
424 * @frame_count : Number of frames for the command
425 * @regs : MFI register set
426 */
adam radford0d490162010-12-14 19:17:17 -0800427static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600428megasas_fire_cmd_xscale(struct megasas_instance *instance,
429 dma_addr_t frame_phys_addr,
430 u32 frame_count,
431 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800432{
bo yang39a98552010-09-22 22:36:29 -0400433 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500434
bo yang39a98552010-09-22 22:36:29 -0400435 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800436 writel((frame_phys_addr >> 3)|(frame_count),
437 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400438 spin_unlock_irqrestore(&instance->hba_lock, flags);
439}
440
441/**
442 * megasas_adp_reset_xscale - For controller reset
443 * @regs: MFI register set
444 */
445static int
446megasas_adp_reset_xscale(struct megasas_instance *instance,
447 struct megasas_register_set __iomem *regs)
448{
449 u32 i;
450 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500451
bo yang39a98552010-09-22 22:36:29 -0400452 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
453
454 for (i = 0; i < 3; i++)
455 msleep(1000); /* sleep for 3 secs */
456 pcidata = 0;
457 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500458 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400459 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500460 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400461 pcidata &= ~0x2;
462 pci_write_config_dword(instance->pdev,
463 MFI_1068_PCSR_OFFSET, pcidata);
464
465 for (i = 0; i < 2; i++)
466 msleep(1000); /* need to wait 2 secs again */
467
468 pcidata = 0;
469 pci_read_config_dword(instance->pdev,
470 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500471 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400472 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500473 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400474 pcidata = 0;
475 pci_write_config_dword(instance->pdev,
476 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
477 }
478 }
479 return 0;
480}
481
482/**
483 * megasas_check_reset_xscale - For controller reset check
484 * @regs: MFI register set
485 */
486static int
487megasas_check_reset_xscale(struct megasas_instance *instance,
488 struct megasas_register_set __iomem *regs)
489{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530490 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530491 (le32_to_cpu(*instance->consumer) ==
492 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400493 return 1;
bo yang39a98552010-09-22 22:36:29 -0400494 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800495}
496
497static struct megasas_instance_template megasas_instance_template_xscale = {
498
499 .fire_cmd = megasas_fire_cmd_xscale,
500 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700501 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800502 .clear_intr = megasas_clear_intr_xscale,
503 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400504 .adp_reset = megasas_adp_reset_xscale,
505 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800506 .service_isr = megasas_isr,
507 .tasklet = megasas_complete_cmd_dpc,
508 .init_adapter = megasas_init_adapter_mfi,
509 .build_and_issue_cmd = megasas_build_and_issue_cmd,
510 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800511};
512
513/**
adam radford0d490162010-12-14 19:17:17 -0800514* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800515* to xscale (deviceid : 1064R, PERC5) controllers
516*/
517
518/**
adam radford0d490162010-12-14 19:17:17 -0800519* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500520* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800521*/
522
523/**
524 * megasas_enable_intr_ppc - Enables interrupts
525 * @regs: MFI register set
526 */
527static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530528megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800529{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530530 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500531
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530532 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800533 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800534
bo yang39a98552010-09-22 22:36:29 -0400535 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800536
537 /* Dummy readl to force pci flush */
538 readl(&regs->outbound_intr_mask);
539}
540
541/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700542 * megasas_disable_intr_ppc - Disable interrupt
543 * @regs: MFI register set
544 */
545static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530546megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700547{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530548 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700549 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500550
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530551 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700552 writel(mask, &regs->outbound_intr_mask);
553 /* Dummy readl to force pci flush */
554 readl(&regs->outbound_intr_mask);
555}
556
557/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800558 * megasas_read_fw_status_reg_ppc - returns the current FW status value
559 * @regs: MFI register set
560 */
561static u32
562megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
563{
564 return readl(&(regs)->outbound_scratch_pad);
565}
566
567/**
568 * megasas_clear_interrupt_ppc - Check & clear interrupt
569 * @regs: MFI register set
570 */
adam radford0d490162010-12-14 19:17:17 -0800571static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800572megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
573{
adam radford3cc6851f92011-05-11 18:34:52 -0700574 u32 status, mfiStatus = 0;
575
Sumant Patrof9876f02006-02-03 15:34:35 -0800576 /*
577 * Check if it is our interrupt
578 */
579 status = readl(&regs->outbound_intr_status);
580
adam radford3cc6851f92011-05-11 18:34:52 -0700581 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
582 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
583
584 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
585 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800586
587 /*
588 * Clear the interrupt by writing back the same value
589 */
590 writel(status, &regs->outbound_doorbell_clear);
591
Yang, Bo06f579d2008-08-10 12:42:37 -0700592 /* Dummy readl to force pci flush */
593 readl(&regs->outbound_doorbell_clear);
594
adam radford3cc6851f92011-05-11 18:34:52 -0700595 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800596}
adam radford3cc6851f92011-05-11 18:34:52 -0700597
Sumant Patrof9876f02006-02-03 15:34:35 -0800598/**
599 * megasas_fire_cmd_ppc - Sends command to the FW
600 * @frame_phys_addr : Physical address of cmd
601 * @frame_count : Number of frames for the command
602 * @regs : MFI register set
603 */
adam radford0d490162010-12-14 19:17:17 -0800604static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600605megasas_fire_cmd_ppc(struct megasas_instance *instance,
606 dma_addr_t frame_phys_addr,
607 u32 frame_count,
608 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800609{
bo yang39a98552010-09-22 22:36:29 -0400610 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500611
bo yang39a98552010-09-22 22:36:29 -0400612 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800613 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800614 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400615 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800616}
617
bo yang39a98552010-09-22 22:36:29 -0400618/**
bo yang39a98552010-09-22 22:36:29 -0400619 * megasas_check_reset_ppc - For controller reset check
620 * @regs: MFI register set
621 */
622static int
623megasas_check_reset_ppc(struct megasas_instance *instance,
624 struct megasas_register_set __iomem *regs)
625{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530626 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700627 return 1;
628
bo yang39a98552010-09-22 22:36:29 -0400629 return 0;
630}
adam radford3cc6851f92011-05-11 18:34:52 -0700631
Sumant Patrof9876f02006-02-03 15:34:35 -0800632static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800633
Sumant Patrof9876f02006-02-03 15:34:35 -0800634 .fire_cmd = megasas_fire_cmd_ppc,
635 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700636 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800637 .clear_intr = megasas_clear_intr_ppc,
638 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700639 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400640 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800641 .service_isr = megasas_isr,
642 .tasklet = megasas_complete_cmd_dpc,
643 .init_adapter = megasas_init_adapter_mfi,
644 .build_and_issue_cmd = megasas_build_and_issue_cmd,
645 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800646};
647
648/**
Yang, Bo87911122009-10-06 14:31:54 -0600649 * megasas_enable_intr_skinny - Enables interrupts
650 * @regs: MFI register set
651 */
652static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530653megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600654{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530655 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500656
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530657 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600658 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
659
660 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
661
662 /* Dummy readl to force pci flush */
663 readl(&regs->outbound_intr_mask);
664}
665
666/**
667 * megasas_disable_intr_skinny - Disables interrupt
668 * @regs: MFI register set
669 */
670static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530671megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600672{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530673 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600674 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500675
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530676 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600677 writel(mask, &regs->outbound_intr_mask);
678 /* Dummy readl to force pci flush */
679 readl(&regs->outbound_intr_mask);
680}
681
682/**
683 * megasas_read_fw_status_reg_skinny - returns the current FW status value
684 * @regs: MFI register set
685 */
686static u32
687megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
688{
689 return readl(&(regs)->outbound_scratch_pad);
690}
691
692/**
693 * megasas_clear_interrupt_skinny - Check & clear interrupt
694 * @regs: MFI register set
695 */
696static int
697megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
698{
699 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800700 u32 mfiStatus = 0;
701
Yang, Bo87911122009-10-06 14:31:54 -0600702 /*
703 * Check if it is our interrupt
704 */
705 status = readl(&regs->outbound_intr_status);
706
707 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400708 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600709 }
710
711 /*
adam radfordebf054b2011-02-24 20:57:15 -0800712 * Check if it is our interrupt
713 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600714 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800715 MFI_STATE_FAULT) {
716 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
717 } else
718 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
719
720 /*
Yang, Bo87911122009-10-06 14:31:54 -0600721 * Clear the interrupt by writing back the same value
722 */
723 writel(status, &regs->outbound_intr_status);
724
725 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500726 * dummy read to flush PCI
727 */
Yang, Bo87911122009-10-06 14:31:54 -0600728 readl(&regs->outbound_intr_status);
729
adam radfordebf054b2011-02-24 20:57:15 -0800730 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600731}
732
733/**
734 * megasas_fire_cmd_skinny - Sends command to the FW
735 * @frame_phys_addr : Physical address of cmd
736 * @frame_count : Number of frames for the command
737 * @regs : MFI register set
738 */
739static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600740megasas_fire_cmd_skinny(struct megasas_instance *instance,
741 dma_addr_t frame_phys_addr,
742 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600743 struct megasas_register_set __iomem *regs)
744{
Yang, Bo0c79e682009-10-06 14:47:35 -0600745 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500746
bo yang39a98552010-09-22 22:36:29 -0400747 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530748 writel(upper_32_bits(frame_phys_addr),
749 &(regs)->inbound_high_queue_port);
750 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
751 &(regs)->inbound_low_queue_port);
Tomas Henzlb99dbe52016-02-01 15:12:04 +0100752 mmiowb();
bo yang39a98552010-09-22 22:36:29 -0400753 spin_unlock_irqrestore(&instance->hba_lock, flags);
754}
755
756/**
bo yang39a98552010-09-22 22:36:29 -0400757 * megasas_check_reset_skinny - For controller reset check
758 * @regs: MFI register set
759 */
760static int
761megasas_check_reset_skinny(struct megasas_instance *instance,
762 struct megasas_register_set __iomem *regs)
763{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530764 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700765 return 1;
766
bo yang39a98552010-09-22 22:36:29 -0400767 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600768}
769
770static struct megasas_instance_template megasas_instance_template_skinny = {
771
772 .fire_cmd = megasas_fire_cmd_skinny,
773 .enable_intr = megasas_enable_intr_skinny,
774 .disable_intr = megasas_disable_intr_skinny,
775 .clear_intr = megasas_clear_intr_skinny,
776 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800777 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400778 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800779 .service_isr = megasas_isr,
780 .tasklet = megasas_complete_cmd_dpc,
781 .init_adapter = megasas_init_adapter_mfi,
782 .build_and_issue_cmd = megasas_build_and_issue_cmd,
783 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600784};
785
786
787/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700788* The following functions are defined for gen2 (deviceid : 0x78 0x79)
789* controllers
790*/
791
792/**
793 * megasas_enable_intr_gen2 - Enables interrupts
794 * @regs: MFI register set
795 */
796static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530797megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700798{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530799 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500800
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530801 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700802 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
803
804 /* write ~0x00000005 (4 & 1) to the intr mask*/
805 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
806
807 /* Dummy readl to force pci flush */
808 readl(&regs->outbound_intr_mask);
809}
810
811/**
812 * megasas_disable_intr_gen2 - Disables interrupt
813 * @regs: MFI register set
814 */
815static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530816megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700817{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530818 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700819 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500820
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530821 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700822 writel(mask, &regs->outbound_intr_mask);
823 /* Dummy readl to force pci flush */
824 readl(&regs->outbound_intr_mask);
825}
826
827/**
828 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
829 * @regs: MFI register set
830 */
831static u32
832megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
833{
834 return readl(&(regs)->outbound_scratch_pad);
835}
836
837/**
838 * megasas_clear_interrupt_gen2 - Check & clear interrupt
839 * @regs: MFI register set
840 */
841static int
842megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
843{
844 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400845 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500846
Yang, Bo6610a6b2008-08-10 12:42:38 -0700847 /*
848 * Check if it is our interrupt
849 */
850 status = readl(&regs->outbound_intr_status);
851
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530852 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400853 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
854 }
855 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
856 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
857 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700858
859 /*
860 * Clear the interrupt by writing back the same value
861 */
bo yang39a98552010-09-22 22:36:29 -0400862 if (mfiStatus)
863 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700864
865 /* Dummy readl to force pci flush */
866 readl(&regs->outbound_intr_status);
867
bo yang39a98552010-09-22 22:36:29 -0400868 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700869}
870/**
871 * megasas_fire_cmd_gen2 - Sends command to the FW
872 * @frame_phys_addr : Physical address of cmd
873 * @frame_count : Number of frames for the command
874 * @regs : MFI register set
875 */
876static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600877megasas_fire_cmd_gen2(struct megasas_instance *instance,
878 dma_addr_t frame_phys_addr,
879 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700880 struct megasas_register_set __iomem *regs)
881{
bo yang39a98552010-09-22 22:36:29 -0400882 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500883
bo yang39a98552010-09-22 22:36:29 -0400884 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700885 writel((frame_phys_addr | (frame_count<<1))|1,
886 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400887 spin_unlock_irqrestore(&instance->hba_lock, flags);
888}
889
890/**
891 * megasas_adp_reset_gen2 - For controller reset
892 * @regs: MFI register set
893 */
894static int
895megasas_adp_reset_gen2(struct megasas_instance *instance,
896 struct megasas_register_set __iomem *reg_set)
897{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500898 u32 retry = 0 ;
899 u32 HostDiag;
900 u32 __iomem *seq_offset = &reg_set->seq_offset;
901 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400902
adam radfordebf054b2011-02-24 20:57:15 -0800903 if (instance->instancet == &megasas_instance_template_skinny) {
904 seq_offset = &reg_set->fusion_seq_offset;
905 hostdiag_offset = &reg_set->fusion_host_diag;
906 }
907
908 writel(0, seq_offset);
909 writel(4, seq_offset);
910 writel(0xb, seq_offset);
911 writel(2, seq_offset);
912 writel(7, seq_offset);
913 writel(0xd, seq_offset);
914
bo yang39a98552010-09-22 22:36:29 -0400915 msleep(1000);
916
adam radfordebf054b2011-02-24 20:57:15 -0800917 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400918
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500919 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400920 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800921 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500922 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400923 retry, HostDiag);
924
925 if (retry++ >= 100)
926 return 1;
927
928 }
929
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500930 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400931
adam radfordebf054b2011-02-24 20:57:15 -0800932 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400933
934 ssleep(10);
935
adam radfordebf054b2011-02-24 20:57:15 -0800936 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500937 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400938 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800939 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500940 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400941 retry, HostDiag);
942
943 if (retry++ >= 1000)
944 return 1;
945
946 }
947 return 0;
948}
949
950/**
951 * megasas_check_reset_gen2 - For controller reset check
952 * @regs: MFI register set
953 */
954static int
955megasas_check_reset_gen2(struct megasas_instance *instance,
956 struct megasas_register_set __iomem *regs)
957{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530958 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600959 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600960
bo yang39a98552010-09-22 22:36:29 -0400961 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700962}
963
964static struct megasas_instance_template megasas_instance_template_gen2 = {
965
966 .fire_cmd = megasas_fire_cmd_gen2,
967 .enable_intr = megasas_enable_intr_gen2,
968 .disable_intr = megasas_disable_intr_gen2,
969 .clear_intr = megasas_clear_intr_gen2,
970 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400971 .adp_reset = megasas_adp_reset_gen2,
972 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800973 .service_isr = megasas_isr,
974 .tasklet = megasas_complete_cmd_dpc,
975 .init_adapter = megasas_init_adapter_mfi,
976 .build_and_issue_cmd = megasas_build_and_issue_cmd,
977 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700978};
979
980/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800981* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400982* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800983*/
984
adam radford9c915a82010-12-21 13:34:31 -0800985/*
986 * Template added for TB (Fusion)
987 */
988extern struct megasas_instance_template megasas_instance_template_fusion;
989
Sumant Patrof9876f02006-02-03 15:34:35 -0800990/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991 * megasas_issue_polled - Issues a polling command
992 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800993 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400994 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530995 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996 */
adam radford9c915a82010-12-21 13:34:31 -0800997int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
999{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001000 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1001
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301002 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301003 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001004
Sumit Saxena8a01a412016-01-28 21:04:32 +05301005 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301006 (instance->instancet->issue_dcmd(instance, cmd))) {
1007 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1008 __func__, __LINE__);
1009 return DCMD_NOT_FIRED;
1010 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001011
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301012 return wait_and_poll(instance, cmd, instance->requestorId ?
1013 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001014}
1015
1016/**
1017 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1018 * @instance: Adapter soft state
1019 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301020 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001021 *
1022 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001023 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001024 * Used to issue ioctl commands.
1025 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301026int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001027megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301028 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001029{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301030 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301031 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001032
Sumit Saxena8a01a412016-01-28 21:04:32 +05301033 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301034 (instance->instancet->issue_dcmd(instance, cmd))) {
1035 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1036 __func__, __LINE__);
1037 return DCMD_NOT_FIRED;
1038 }
1039
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301040 if (timeout) {
1041 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301042 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301043 if (!ret) {
1044 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1045 __func__, __LINE__);
1046 return DCMD_TIMEOUT;
1047 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301048 } else
1049 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301050 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001051
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301052 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301053 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001054}
1055
1056/**
1057 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1058 * @instance: Adapter soft state
1059 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301060 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001061 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301062 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001063 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001064 * cmd and waits for return status.
1065 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001066 */
1067static int
1068megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301069 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001070{
1071 struct megasas_cmd *cmd;
1072 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301073 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001074
1075 cmd = megasas_get_cmd(instance);
1076
1077 if (!cmd)
1078 return -1;
1079
1080 abort_fr = &cmd->frame->abort;
1081
1082 /*
1083 * Prepare and issue the abort frame
1084 */
1085 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301086 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301087 abort_fr->flags = cpu_to_le16(0);
1088 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1089 abort_fr->abort_mfi_phys_addr_lo =
1090 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1091 abort_fr->abort_mfi_phys_addr_hi =
1092 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001093
1094 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301095 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001096
Sumit Saxena8a01a412016-01-28 21:04:32 +05301097 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301098 (instance->instancet->issue_dcmd(instance, cmd))) {
1099 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1100 __func__, __LINE__);
1101 return DCMD_NOT_FIRED;
1102 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001103
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301104 if (timeout) {
1105 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301106 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301107 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301108 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1109 __func__, __LINE__);
1110 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301111 }
1112 } else
1113 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301114 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301115
bo yang39a98552010-09-22 22:36:29 -04001116 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001117
1118 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301119 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1120 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001121}
1122
1123/**
1124 * megasas_make_sgl32 - Prepares 32-bit SGL
1125 * @instance: Adapter soft state
1126 * @scp: SCSI command from the mid-layer
1127 * @mfi_sgl: SGL to be filled in
1128 *
1129 * If successful, this function returns the number of SG elements. Otherwise,
1130 * it returnes -1.
1131 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001132static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001133megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1134 union megasas_sgl *mfi_sgl)
1135{
1136 int i;
1137 int sge_count;
1138 struct scatterlist *os_sgl;
1139
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001140 sge_count = scsi_dma_map(scp);
1141 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001142
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001143 if (sge_count) {
1144 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301145 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1146 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001147 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001148 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001149 return sge_count;
1150}
1151
1152/**
1153 * megasas_make_sgl64 - Prepares 64-bit SGL
1154 * @instance: Adapter soft state
1155 * @scp: SCSI command from the mid-layer
1156 * @mfi_sgl: SGL to be filled in
1157 *
1158 * If successful, this function returns the number of SG elements. Otherwise,
1159 * it returnes -1.
1160 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001161static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001162megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1163 union megasas_sgl *mfi_sgl)
1164{
1165 int i;
1166 int sge_count;
1167 struct scatterlist *os_sgl;
1168
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001169 sge_count = scsi_dma_map(scp);
1170 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001171
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001172 if (sge_count) {
1173 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301174 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1175 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001176 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001177 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001178 return sge_count;
1179}
1180
Yang, Bof4c9a132009-10-06 14:43:28 -06001181/**
1182 * megasas_make_sgl_skinny - Prepares IEEE SGL
1183 * @instance: Adapter soft state
1184 * @scp: SCSI command from the mid-layer
1185 * @mfi_sgl: SGL to be filled in
1186 *
1187 * If successful, this function returns the number of SG elements. Otherwise,
1188 * it returnes -1.
1189 */
1190static int
1191megasas_make_sgl_skinny(struct megasas_instance *instance,
1192 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1193{
1194 int i;
1195 int sge_count;
1196 struct scatterlist *os_sgl;
1197
1198 sge_count = scsi_dma_map(scp);
1199
1200 if (sge_count) {
1201 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301202 mfi_sgl->sge_skinny[i].length =
1203 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001204 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301205 cpu_to_le64(sg_dma_address(os_sgl));
1206 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001207 }
1208 }
1209 return sge_count;
1210}
1211
Sumant Patrob1df99d2006-10-03 12:40:47 -07001212 /**
1213 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001214 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001215 * @sge_count : number of sg elements
1216 *
1217 * Returns the number of frames required for numnber of sge's (sge_count)
1218 */
1219
Yang, Bof4c9a132009-10-06 14:43:28 -06001220static u32 megasas_get_frame_count(struct megasas_instance *instance,
1221 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001222{
1223 int num_cnt;
1224 int sge_bytes;
1225 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001226 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001227
1228 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1229 sizeof(struct megasas_sge32);
1230
Yang, Bof4c9a132009-10-06 14:43:28 -06001231 if (instance->flag_ieee) {
1232 sge_sz = sizeof(struct megasas_sge_skinny);
1233 }
1234
Sumant Patrob1df99d2006-10-03 12:40:47 -07001235 /*
bo yangd532dbe2008-03-17 03:36:43 -04001236 * Main frame can contain 2 SGEs for 64-bit SGLs and
1237 * 3 SGEs for 32-bit SGLs for ldio &
1238 * 1 SGEs for 64-bit SGLs and
1239 * 2 SGEs for 32-bit SGLs for pthru frame
1240 */
1241 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001242 if (instance->flag_ieee == 1) {
1243 num_cnt = sge_count - 1;
1244 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001245 num_cnt = sge_count - 1;
1246 else
1247 num_cnt = sge_count - 2;
1248 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001249 if (instance->flag_ieee == 1) {
1250 num_cnt = sge_count - 1;
1251 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001252 num_cnt = sge_count - 2;
1253 else
1254 num_cnt = sge_count - 3;
1255 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001256
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001257 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001258 sge_bytes = sge_sz * num_cnt;
1259
1260 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1261 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1262 }
1263 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001264 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001265
1266 if (frame_count > 7)
1267 frame_count = 8;
1268 return frame_count;
1269}
1270
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001271/**
1272 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1273 * @instance: Adapter soft state
1274 * @scp: SCSI command
1275 * @cmd: Command to be prepared in
1276 *
1277 * This function prepares CDB commands. These are typcially pass-through
1278 * commands to the devices.
1279 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001280static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001281megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1282 struct megasas_cmd *cmd)
1283{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001284 u32 is_logical;
1285 u32 device_id;
1286 u16 flags = 0;
1287 struct megasas_pthru_frame *pthru;
1288
1289 is_logical = MEGASAS_IS_LOGICAL(scp);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301290 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001291 pthru = (struct megasas_pthru_frame *)cmd->frame;
1292
1293 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1294 flags = MFI_FRAME_DIR_WRITE;
1295 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1296 flags = MFI_FRAME_DIR_READ;
1297 else if (scp->sc_data_direction == PCI_DMA_NONE)
1298 flags = MFI_FRAME_DIR_NONE;
1299
Yang, Bof4c9a132009-10-06 14:43:28 -06001300 if (instance->flag_ieee == 1) {
1301 flags |= MFI_FRAME_IEEE;
1302 }
1303
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001304 /*
1305 * Prepare the DCDB frame
1306 */
1307 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1308 pthru->cmd_status = 0x0;
1309 pthru->scsi_status = 0x0;
1310 pthru->target_id = device_id;
1311 pthru->lun = scp->device->lun;
1312 pthru->cdb_len = scp->cmd_len;
1313 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001314 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301315 pthru->flags = cpu_to_le16(flags);
1316 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001317
1318 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1319
1320 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001321 * If the command is for the tape device, set the
1322 * pthru timeout to the os layer timeout value.
1323 */
Yang, Bo8d568252009-10-06 14:12:21 -06001324 if (scp->device->type == TYPE_TAPE) {
1325 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301326 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001327 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301328 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001329 }
1330
1331 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001332 * Construct SGL
1333 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001334 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301335 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001336 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1337 &pthru->sgl);
1338 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301339 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001340 pthru->sge_count = megasas_make_sgl64(instance, scp,
1341 &pthru->sgl);
1342 } else
1343 pthru->sge_count = megasas_make_sgl32(instance, scp,
1344 &pthru->sgl);
1345
Yang, Bobdc6fb82009-12-06 08:30:19 -07001346 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001347 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001348 pthru->sge_count);
1349 return 0;
1350 }
1351
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001352 /*
1353 * Sense info specific
1354 */
1355 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301356 pthru->sense_buf_phys_addr_hi =
1357 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1358 pthru->sense_buf_phys_addr_lo =
1359 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001360
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001361 /*
1362 * Compute the total number of frames this command consumes. FW uses
1363 * this number to pull sufficient number of frames from host memory.
1364 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001365 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001366 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001367
1368 return cmd->frame_count;
1369}
1370
1371/**
1372 * megasas_build_ldio - Prepares IOs to logical devices
1373 * @instance: Adapter soft state
1374 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001375 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001376 *
1377 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1378 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001379static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001380megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1381 struct megasas_cmd *cmd)
1382{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001383 u32 device_id;
1384 u8 sc = scp->cmnd[0];
1385 u16 flags = 0;
1386 struct megasas_io_frame *ldio;
1387
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301388 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001389 ldio = (struct megasas_io_frame *)cmd->frame;
1390
1391 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1392 flags = MFI_FRAME_DIR_WRITE;
1393 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1394 flags = MFI_FRAME_DIR_READ;
1395
Yang, Bof4c9a132009-10-06 14:43:28 -06001396 if (instance->flag_ieee == 1) {
1397 flags |= MFI_FRAME_IEEE;
1398 }
1399
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001400 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001401 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001402 */
1403 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1404 ldio->cmd_status = 0x0;
1405 ldio->scsi_status = 0x0;
1406 ldio->target_id = device_id;
1407 ldio->timeout = 0;
1408 ldio->reserved_0 = 0;
1409 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301410 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001411 ldio->start_lba_hi = 0;
1412 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1413
1414 /*
1415 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1416 */
1417 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301418 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1419 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1420 ((u32) scp->cmnd[2] << 8) |
1421 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001422
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301423 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001424 }
1425
1426 /*
1427 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1428 */
1429 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301430 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1431 ((u32) scp->cmnd[7] << 8));
1432 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1433 ((u32) scp->cmnd[3] << 16) |
1434 ((u32) scp->cmnd[4] << 8) |
1435 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001436 }
1437
1438 /*
1439 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1440 */
1441 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301442 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1443 ((u32) scp->cmnd[7] << 16) |
1444 ((u32) scp->cmnd[8] << 8) |
1445 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001446
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301447 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1448 ((u32) scp->cmnd[3] << 16) |
1449 ((u32) scp->cmnd[4] << 8) |
1450 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001451 }
1452
1453 /*
1454 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1455 */
1456 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301457 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1458 ((u32) scp->cmnd[11] << 16) |
1459 ((u32) scp->cmnd[12] << 8) |
1460 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001461
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301462 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1463 ((u32) scp->cmnd[7] << 16) |
1464 ((u32) scp->cmnd[8] << 8) |
1465 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001466
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301467 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1468 ((u32) scp->cmnd[3] << 16) |
1469 ((u32) scp->cmnd[4] << 8) |
1470 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001471
1472 }
1473
1474 /*
1475 * Construct SGL
1476 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001477 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301478 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001479 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1480 &ldio->sgl);
1481 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301482 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001483 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1484 } else
1485 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1486
Yang, Bobdc6fb82009-12-06 08:30:19 -07001487 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001488 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001489 ldio->sge_count);
1490 return 0;
1491 }
1492
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001493 /*
1494 * Sense info specific
1495 */
1496 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1497 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301498 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001499
Sumant Patrob1df99d2006-10-03 12:40:47 -07001500 /*
1501 * Compute the total number of frames this command consumes. FW uses
1502 * this number to pull sufficient number of frames from host memory.
1503 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001504 cmd->frame_count = megasas_get_frame_count(instance,
1505 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001506
1507 return cmd->frame_count;
1508}
1509
1510/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301511 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1512 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001513 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001514 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001515 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301516inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001517{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301518 int ret;
1519
Sumant Patrocb59aa62006-01-25 11:53:25 -08001520 switch (cmd->cmnd[0]) {
1521 case READ_10:
1522 case WRITE_10:
1523 case READ_12:
1524 case WRITE_12:
1525 case READ_6:
1526 case WRITE_6:
1527 case READ_16:
1528 case WRITE_16:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301529 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1530 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1531 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001532 default:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301533 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1534 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001535 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301536 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001537}
1538
Sumant Patro658dced2006-10-03 13:09:14 -07001539 /**
1540 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001541 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001542 * @instance: Adapter soft state
1543 */
1544static inline void
1545megasas_dump_pending_frames(struct megasas_instance *instance)
1546{
1547 struct megasas_cmd *cmd;
1548 int i,n;
1549 union megasas_sgl *mfi_sgl;
1550 struct megasas_io_frame *ldio;
1551 struct megasas_pthru_frame *pthru;
1552 u32 sgcount;
1553 u32 max_cmd = instance->max_fw_cmds;
1554
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001555 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1556 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 -07001557 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001558 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 -07001559 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001560 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 -07001561
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001562 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001563 for (i = 0; i < max_cmd; i++) {
1564 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001565 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001566 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001567 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 +05301568 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001569 ldio = (struct megasas_io_frame *)cmd->frame;
1570 mfi_sgl = &ldio->sgl;
1571 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001572 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 +05301573 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1574 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1575 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1576 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001577 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001578 pthru = (struct megasas_pthru_frame *) cmd->frame;
1579 mfi_sgl = &pthru->sgl;
1580 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001581 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 +05301582 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1583 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1584 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1585 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001586 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001587 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1588 for (n = 0; n < sgcount; n++) {
1589 if (IS_DMA64)
1590 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1591 le32_to_cpu(mfi_sgl->sge64[n].length),
1592 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1593 else
1594 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1595 le32_to_cpu(mfi_sgl->sge32[n].length),
1596 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001597 }
1598 }
Sumant Patro658dced2006-10-03 13:09:14 -07001599 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001600 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001601 for (i = 0; i < max_cmd; i++) {
1602
1603 cmd = instance->cmd_list[i];
1604
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001605 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001606 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001607 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001608 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001609}
1610
adam radfordcd50ba82010-12-21 10:23:23 -08001611u32
1612megasas_build_and_issue_cmd(struct megasas_instance *instance,
1613 struct scsi_cmnd *scmd)
1614{
1615 struct megasas_cmd *cmd;
1616 u32 frame_count;
1617
1618 cmd = megasas_get_cmd(instance);
1619 if (!cmd)
1620 return SCSI_MLQUEUE_HOST_BUSY;
1621
1622 /*
1623 * Logical drive command
1624 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301625 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001626 frame_count = megasas_build_ldio(instance, scmd, cmd);
1627 else
1628 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1629
1630 if (!frame_count)
1631 goto out_return_cmd;
1632
1633 cmd->scmd = scmd;
1634 scmd->SCp.ptr = (char *)cmd;
1635
1636 /*
1637 * Issue the command to the FW
1638 */
1639 atomic_inc(&instance->fw_outstanding);
1640
1641 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1642 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001643
1644 return 0;
1645out_return_cmd:
1646 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301647 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001648}
1649
1650
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001651/**
1652 * megasas_queue_command - Queue entry point
1653 * @scmd: SCSI command to be queued
1654 * @done: Callback entry point
1655 */
1656static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301657megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001658{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001659 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301660 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001661
1662 instance = (struct megasas_instance *)
1663 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001664
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301665 if (instance->unload == 1) {
1666 scmd->result = DID_NO_CONNECT << 16;
1667 scmd->scsi_done(scmd);
1668 return 0;
1669 }
1670
bo yang39a98552010-09-22 22:36:29 -04001671 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001672 return SCSI_MLQUEUE_HOST_BUSY;
1673
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301674
adam radford229fe472014-03-10 02:51:56 -07001675 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301676 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001677 if (megasas_check_mpio_paths(instance, scmd) ==
1678 (DID_RESET << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001679 return SCSI_MLQUEUE_HOST_BUSY;
1680 } else {
adam radford229fe472014-03-10 02:51:56 -07001681 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301682 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001683 return 0;
1684 }
1685 }
1686
Sumit Saxena8a01a412016-01-28 21:04:32 +05301687 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001688 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301689 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301690 return 0;
1691 }
1692
Sumit Saxena18365b12016-01-28 21:04:25 +05301693 mr_device_priv_data = scmd->device->hostdata;
1694 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301695 scmd->result = DID_NO_CONNECT << 16;
1696 scmd->scsi_done(scmd);
1697 return 0;
1698 }
1699
Sumit Saxena8a01a412016-01-28 21:04:32 +05301700 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001701 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001702
Sumit Saxena8a01a412016-01-28 21:04:32 +05301703 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301704 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301705
bo yang39a98552010-09-22 22:36:29 -04001706
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001707 scmd->result = 0;
1708
Sumant Patrocb59aa62006-01-25 11:53:25 -08001709 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301710 (scmd->device->id >= instance->fw_supported_vd_count ||
1711 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001712 scmd->result = DID_BAD_TARGET << 16;
1713 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001714 }
1715
Sumant Patro02b01e02007-02-14 13:00:55 -08001716 switch (scmd->cmnd[0]) {
1717 case SYNCHRONIZE_CACHE:
1718 /*
1719 * FW takes care of flush cache on its own
1720 * No need to send it down
1721 */
1722 scmd->result = DID_OK << 16;
1723 goto out_done;
1724 default:
1725 break;
1726 }
1727
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301728 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001729
Sumant Patrocb59aa62006-01-25 11:53:25 -08001730 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301731 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001732 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001733}
1734
Yang, Bo044833b2009-10-06 14:33:06 -06001735static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1736{
1737 int i;
1738
1739 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1740
1741 if ((megasas_mgmt_info.instance[i]) &&
1742 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1743 return megasas_mgmt_info.instance[i];
1744 }
1745
1746 return NULL;
1747}
1748
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301749/*
Sumit Saxena18365b12016-01-28 21:04:25 +05301750* megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301751*
1752* @sdev: OS provided scsi device
1753*
1754* Returns void
1755*/
Sumit Saxena18365b12016-01-28 21:04:25 +05301756void megasas_update_sdev_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301757{
Sumit Saxena18365b12016-01-28 21:04:25 +05301758 u16 pd_index = 0;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301759 u32 device_id, ld;
1760 struct megasas_instance *instance;
1761 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301762 struct MR_PRIV_DEVICE *mr_device_priv_data;
1763 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301764 struct MR_LD_RAID *raid;
1765 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1766
1767 instance = megasas_lookup_instance(sdev->host->host_no);
1768 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301769 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301770
1771 if (!fusion)
1772 return;
1773
Sumit Saxena18365b12016-01-28 21:04:25 +05301774 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1775 instance->use_seqnum_jbod_fp) {
1776 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1777 sdev->id;
1778 pd_sync = (void *)fusion->pd_seq_sync
1779 [(instance->pd_seq_map_id - 1) & 1];
1780 mr_device_priv_data->is_tm_capable =
1781 pd_sync->seq[pd_index].capability.tmCapable;
1782 } else {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301783 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1784 + sdev->id;
1785 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1786 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1787 raid = MR_LdRaidGet(ld, local_map_ptr);
1788
1789 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Sumit Saxena18365b12016-01-28 21:04:25 +05301790 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1791 mr_device_priv_data->is_tm_capable =
1792 raid->capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301793 }
1794}
1795
Sumit Saxena2216c302016-01-28 21:04:26 +05301796static void megasas_set_device_queue_depth(struct scsi_device *sdev)
1797{
1798 u16 pd_index = 0;
1799 int ret = DCMD_FAILED;
1800 struct megasas_instance *instance;
1801
1802 instance = megasas_lookup_instance(sdev->host->host_no);
1803
1804 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1805 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
1806
1807 if (instance->pd_info) {
1808 mutex_lock(&instance->hba_mutex);
1809 ret = megasas_get_pd_info(instance, pd_index);
1810 mutex_unlock(&instance->hba_mutex);
1811 }
1812
1813 if (ret != DCMD_SUCCESS)
1814 return;
1815
1816 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1817
1818 switch (instance->pd_list[pd_index].interface) {
1819 case SAS_PD:
1820 scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
1821 break;
1822
1823 case SATA_PD:
1824 scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
1825 break;
1826
1827 default:
1828 scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
1829 }
1830 }
1831 }
1832}
1833
Sumit Saxena18365b12016-01-28 21:04:25 +05301834
Christoph Hellwig147aab62006-02-17 12:13:48 +01001835static int megasas_slave_configure(struct scsi_device *sdev)
1836{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301837 u16 pd_index = 0;
1838 struct megasas_instance *instance;
1839
1840 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001841 if (instance->pd_list_not_supported) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301842 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1843 sdev->type == TYPE_DISK) {
1844 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1845 sdev->id;
1846 if (instance->pd_list[pd_index].driveState !=
1847 MR_PD_STATE_SYSTEM)
1848 return -ENXIO;
1849 }
1850 }
Sumit Saxena2216c302016-01-28 21:04:26 +05301851 megasas_set_device_queue_depth(sdev);
Sumit Saxena18365b12016-01-28 21:04:25 +05301852 megasas_update_sdev_properties(sdev);
1853
Yang, Bo044833b2009-10-06 14:33:06 -06001854 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001855 * The RAID firmware may require extended timeouts.
1856 */
Yang, Bo044833b2009-10-06 14:33:06 -06001857 blk_queue_rq_timeout(sdev->request_queue,
Sumit Saxenae3d178c2016-01-28 21:04:34 +05301858 scmd_timeout * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301859
Yang, Bo044833b2009-10-06 14:33:06 -06001860 return 0;
1861}
1862
1863static int megasas_slave_alloc(struct scsi_device *sdev)
1864{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001865 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001866 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301867 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001868
Yang, Bo044833b2009-10-06 14:33:06 -06001869 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301870 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001871 /*
1872 * Open the OS scan to the SYSTEM PD
1873 */
1874 pd_index =
1875 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1876 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08001877 if ((instance->pd_list_not_supported ||
1878 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301879 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301880 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001881 }
1882 return -ENXIO;
1883 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301884
1885scan_target:
1886 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1887 GFP_KERNEL);
1888 if (!mr_device_priv_data)
1889 return -ENOMEM;
1890 sdev->hostdata = mr_device_priv_data;
Christoph Hellwig147aab62006-02-17 12:13:48 +01001891 return 0;
1892}
1893
Sumit Saxena18365b12016-01-28 21:04:25 +05301894static void megasas_slave_destroy(struct scsi_device *sdev)
1895{
1896 kfree(sdev->hostdata);
1897 sdev->hostdata = NULL;
1898}
1899
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301900/*
1901* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1902* kill adapter
1903* @instance: Adapter soft state
1904*
1905*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301906static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301907{
1908 int i;
1909 struct megasas_cmd *cmd_mfi;
1910 struct megasas_cmd_fusion *cmd_fusion;
1911 struct fusion_context *fusion = instance->ctrl_context;
1912
1913 /* Find all outstanding ioctls */
1914 if (fusion) {
1915 for (i = 0; i < instance->max_fw_cmds; i++) {
1916 cmd_fusion = fusion->cmd_list[i];
1917 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1918 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1919 if (cmd_mfi->sync_cmd &&
1920 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1921 megasas_complete_cmd(instance,
1922 cmd_mfi, DID_OK);
1923 }
1924 }
1925 } else {
1926 for (i = 0; i < instance->max_fw_cmds; i++) {
1927 cmd_mfi = instance->cmd_list[i];
1928 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1929 MFI_CMD_ABORT)
1930 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1931 }
1932 }
1933}
1934
1935
adam radford9c915a82010-12-21 13:34:31 -08001936void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001937{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301938 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301939 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301940 /* Wait 1 second to ensure IO or ioctls in build have posted */
1941 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001942 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301943 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301944 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001945 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001946 /* Flush */
1947 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05301948 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07001949 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001950 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301951 writel(MFI_STOP_ADP,
1952 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001953 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301954 /* Complete outstanding ioctls when adapter is killed */
1955 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001956}
1957
1958 /**
1959 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1960 * restored to max value
1961 * @instance: Adapter soft state
1962 *
1963 */
1964void
1965megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1966{
1967 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301968
adam radford9c915a82010-12-21 13:34:31 -08001969 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001970 && time_after(jiffies, instance->last_time + 5 * HZ)
1971 && atomic_read(&instance->fw_outstanding) <
1972 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001973
1974 spin_lock_irqsave(instance->host->host_lock, flags);
1975 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001976
Sumit Saxena308ec452016-01-28 21:04:30 +05301977 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08001978 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001979 }
1980}
1981
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001982/**
bo yang7343eb62007-11-09 04:35:44 -05001983 * megasas_complete_cmd_dpc - Returns FW's controller structure
1984 * @instance_addr: Address of adapter soft state
1985 *
1986 * Tasklet to complete cmds
1987 */
1988static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1989{
1990 u32 producer;
1991 u32 consumer;
1992 u32 context;
1993 struct megasas_cmd *cmd;
1994 struct megasas_instance *instance =
1995 (struct megasas_instance *)instance_addr;
1996 unsigned long flags;
1997
1998 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301999 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002000 return;
2001
2002 spin_lock_irqsave(&instance->completion_lock, flags);
2003
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302004 producer = le32_to_cpu(*instance->producer);
2005 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002006
2007 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302008 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002009 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002010 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002011 context);
2012 BUG();
2013 }
bo yang7343eb62007-11-09 04:35:44 -05002014
2015 cmd = instance->cmd_list[context];
2016
2017 megasas_complete_cmd(instance, cmd, DID_OK);
2018
2019 consumer++;
2020 if (consumer == (instance->max_fw_cmds + 1)) {
2021 consumer = 0;
2022 }
2023 }
2024
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302025 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002026
2027 spin_unlock_irqrestore(&instance->completion_lock, flags);
2028
2029 /*
2030 * Check if we can restore can_queue
2031 */
adam radford9c915a82010-12-21 13:34:31 -08002032 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002033}
2034
adam radford229fe472014-03-10 02:51:56 -07002035/**
2036 * megasas_start_timer - Initializes a timer object
2037 * @instance: Adapter soft state
2038 * @timer: timer object to be initialized
2039 * @fn: timer function
2040 * @interval: time interval between timer function call
2041 *
2042 */
2043void megasas_start_timer(struct megasas_instance *instance,
2044 struct timer_list *timer,
2045 void *fn, unsigned long interval)
2046{
2047 init_timer(timer);
2048 timer->expires = jiffies + interval;
2049 timer->data = (unsigned long)instance;
2050 timer->function = fn;
2051 add_timer(timer);
2052}
2053
Yang, Bo707e09b2010-10-12 07:20:27 -06002054static void
2055megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2056
2057static void
2058process_fw_state_change_wq(struct work_struct *work);
2059
2060void megasas_do_ocr(struct megasas_instance *instance)
2061{
2062 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2063 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2064 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302065 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002066 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302067 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302068 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002069 instance->issuepend_done = 0;
2070
2071 atomic_set(&instance->fw_outstanding, 0);
2072 megasas_internal_reset_defer_cmds(instance);
2073 process_fw_state_change_wq(&instance->work_init);
2074}
2075
Adam Radford4cbfea82014-07-09 15:17:56 -07002076static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2077 int initial)
adam radford229fe472014-03-10 02:51:56 -07002078{
2079 struct megasas_cmd *cmd;
2080 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002081 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002082 dma_addr_t new_affiliation_111_h;
2083 int ld, retval = 0;
2084 u8 thisVf;
2085
2086 cmd = megasas_get_cmd(instance);
2087
2088 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002089 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2090 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002091 instance->host->host_no);
2092 return -ENOMEM;
2093 }
2094
2095 dcmd = &cmd->frame->dcmd;
2096
Adam Radford4cbfea82014-07-09 15:17:56 -07002097 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002098 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2099 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002100 megasas_return_cmd(instance, cmd);
2101 return -ENOMEM;
2102 }
2103
2104 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002105 memset(instance->vf_affiliation_111, 0,
2106 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002107 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002108 new_affiliation_111 =
2109 pci_alloc_consistent(instance->pdev,
2110 sizeof(struct MR_LD_VF_AFFILIATION_111),
2111 &new_affiliation_111_h);
2112 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002113 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2114 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002115 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002116 megasas_return_cmd(instance, cmd);
2117 return -ENOMEM;
2118 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002119 memset(new_affiliation_111, 0,
2120 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002121 }
2122
2123 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2124
2125 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302126 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002127 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302128 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002129 dcmd->timeout = 0;
2130 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302131 dcmd->data_xfer_len =
2132 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2133 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002134
Adam Radford4cbfea82014-07-09 15:17:56 -07002135 if (initial)
2136 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302137 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002138 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302139 dcmd->sgl.sge32[0].phys_addr =
2140 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002141
Christoph Hellwig2213a462015-04-23 16:33:54 +05302142 dcmd->sgl.sge32[0].length = cpu_to_le32(
2143 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002144
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002145 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002146 "scsi%d\n", instance->host->host_no);
2147
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302148 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002149 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2150 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002151 dcmd->cmd_status, instance->host->host_no);
2152 retval = 1; /* Do a scan if we couldn't get affiliation */
2153 goto out;
2154 }
2155
2156 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002157 thisVf = new_affiliation_111->thisVf;
2158 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2159 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2160 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002161 dev_warn(&instance->pdev->dev, "SR-IOV: "
2162 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002163 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002164 memcpy(instance->vf_affiliation_111,
2165 new_affiliation_111,
2166 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002167 retval = 1;
2168 goto out;
2169 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002170 }
2171out:
2172 if (new_affiliation_111) {
2173 pci_free_consistent(instance->pdev,
2174 sizeof(struct MR_LD_VF_AFFILIATION_111),
2175 new_affiliation_111,
2176 new_affiliation_111_h);
2177 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302178
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302179 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002180
2181 return retval;
2182}
2183
2184static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2185 int initial)
2186{
2187 struct megasas_cmd *cmd;
2188 struct megasas_dcmd_frame *dcmd;
2189 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2190 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2191 dma_addr_t new_affiliation_h;
2192 int i, j, retval = 0, found = 0, doscan = 0;
2193 u8 thisVf;
2194
2195 cmd = megasas_get_cmd(instance);
2196
2197 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002198 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2199 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002200 instance->host->host_no);
2201 return -ENOMEM;
2202 }
2203
2204 dcmd = &cmd->frame->dcmd;
2205
2206 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002207 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2208 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002209 megasas_return_cmd(instance, cmd);
2210 return -ENOMEM;
2211 }
2212
2213 if (initial)
2214 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2215 sizeof(struct MR_LD_VF_AFFILIATION));
2216 else {
2217 new_affiliation =
2218 pci_alloc_consistent(instance->pdev,
2219 (MAX_LOGICAL_DRIVES + 1) *
2220 sizeof(struct MR_LD_VF_AFFILIATION),
2221 &new_affiliation_h);
2222 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002223 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2224 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002225 instance->host->host_no);
2226 megasas_return_cmd(instance, cmd);
2227 return -ENOMEM;
2228 }
2229 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2230 sizeof(struct MR_LD_VF_AFFILIATION));
2231 }
2232
2233 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2234
2235 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302236 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002237 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302238 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002239 dcmd->timeout = 0;
2240 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302241 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2242 sizeof(struct MR_LD_VF_AFFILIATION));
2243 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002244
2245 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302246 dcmd->sgl.sge32[0].phys_addr =
2247 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002248 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302249 dcmd->sgl.sge32[0].phys_addr =
2250 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002251
Christoph Hellwig2213a462015-04-23 16:33:54 +05302252 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2253 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002254
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002255 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002256 "scsi%d\n", instance->host->host_no);
2257
Adam Radford4cbfea82014-07-09 15:17:56 -07002258
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302259 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002260 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2261 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002262 dcmd->cmd_status, instance->host->host_no);
2263 retval = 1; /* Do a scan if we couldn't get affiliation */
2264 goto out;
2265 }
2266
2267 if (!initial) {
2268 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002269 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2270 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002271 instance->host->host_no);
2272 retval = 1;
2273 goto out;
2274 }
2275 newmap = new_affiliation->map;
2276 savedmap = instance->vf_affiliation->map;
2277 thisVf = new_affiliation->thisVf;
2278 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2279 found = 0;
2280 for (j = 0; j < instance->vf_affiliation->ldCount;
2281 j++) {
2282 if (newmap->ref.targetId ==
2283 savedmap->ref.targetId) {
2284 found = 1;
2285 if (newmap->policy[thisVf] !=
2286 savedmap->policy[thisVf]) {
2287 doscan = 1;
2288 goto out;
2289 }
adam radford229fe472014-03-10 02:51:56 -07002290 }
2291 savedmap = (struct MR_LD_VF_MAP *)
2292 ((unsigned char *)savedmap +
2293 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002294 }
2295 if (!found && newmap->policy[thisVf] !=
2296 MR_LD_ACCESS_HIDDEN) {
2297 doscan = 1;
2298 goto out;
2299 }
2300 newmap = (struct MR_LD_VF_MAP *)
2301 ((unsigned char *)newmap + newmap->size);
2302 }
2303
2304 newmap = new_affiliation->map;
2305 savedmap = instance->vf_affiliation->map;
2306
2307 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2308 found = 0;
2309 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2310 if (savedmap->ref.targetId ==
2311 newmap->ref.targetId) {
2312 found = 1;
2313 if (savedmap->policy[thisVf] !=
2314 newmap->policy[thisVf]) {
2315 doscan = 1;
2316 goto out;
2317 }
2318 }
adam radford229fe472014-03-10 02:51:56 -07002319 newmap = (struct MR_LD_VF_MAP *)
2320 ((unsigned char *)newmap +
2321 newmap->size);
2322 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002323 if (!found && savedmap->policy[thisVf] !=
2324 MR_LD_ACCESS_HIDDEN) {
2325 doscan = 1;
2326 goto out;
2327 }
2328 savedmap = (struct MR_LD_VF_MAP *)
2329 ((unsigned char *)savedmap +
2330 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002331 }
2332 }
2333out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002334 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002335 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2336 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002337 memcpy(instance->vf_affiliation, new_affiliation,
2338 new_affiliation->size);
2339 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002340 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002341
2342 if (new_affiliation)
2343 pci_free_consistent(instance->pdev,
2344 (MAX_LOGICAL_DRIVES + 1) *
2345 sizeof(struct MR_LD_VF_AFFILIATION),
2346 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302347 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002348
2349 return retval;
2350}
2351
Adam Radford4cbfea82014-07-09 15:17:56 -07002352/* This function will get the current SR-IOV LD/VF affiliation */
2353static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2354 int initial)
2355{
2356 int retval;
2357
2358 if (instance->PlasmaFW111)
2359 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2360 else
2361 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2362 return retval;
2363}
2364
adam radford229fe472014-03-10 02:51:56 -07002365/* This function will tell FW to start the SR-IOV heartbeat */
2366int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2367 int initial)
2368{
2369 struct megasas_cmd *cmd;
2370 struct megasas_dcmd_frame *dcmd;
2371 int retval = 0;
2372
2373 cmd = megasas_get_cmd(instance);
2374
2375 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002376 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2377 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002378 instance->host->host_no);
2379 return -ENOMEM;
2380 }
2381
2382 dcmd = &cmd->frame->dcmd;
2383
2384 if (initial) {
2385 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002386 pci_zalloc_consistent(instance->pdev,
2387 sizeof(struct MR_CTRL_HB_HOST_MEM),
2388 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002389 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002390 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2391 " memory for heartbeat host memory for scsi%d\n",
2392 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002393 retval = -ENOMEM;
2394 goto out;
2395 }
adam radford229fe472014-03-10 02:51:56 -07002396 }
2397
2398 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2399
Christoph Hellwig2213a462015-04-23 16:33:54 +05302400 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002401 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302402 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002403 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302404 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002405 dcmd->timeout = 0;
2406 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302407 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2408 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2409 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2410 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002411
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002412 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002413 instance->host->host_no);
2414
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302415 if (instance->ctrl_context && !instance->mask_interrupts)
2416 retval = megasas_issue_blocked_cmd(instance, cmd,
2417 MEGASAS_ROUTINE_WAIT_TIME_VF);
2418 else
2419 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002420
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302421 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302422 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2423 "_MEM_ALLOC DCMD %s for scsi%d\n",
2424 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2425 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002426 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002427 }
2428
2429out:
2430 megasas_return_cmd(instance, cmd);
2431
2432 return retval;
2433}
2434
2435/* Handler for SR-IOV heartbeat */
2436void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2437{
2438 struct megasas_instance *instance =
2439 (struct megasas_instance *)instance_addr;
2440
2441 if (instance->hb_host_mem->HB.fwCounter !=
2442 instance->hb_host_mem->HB.driverCounter) {
2443 instance->hb_host_mem->HB.driverCounter =
2444 instance->hb_host_mem->HB.fwCounter;
2445 mod_timer(&instance->sriov_heartbeat_timer,
2446 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2447 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002448 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002449 "completed for scsi%d\n", instance->host->host_no);
2450 schedule_work(&instance->work_init);
2451 }
2452}
2453
bo yang7343eb62007-11-09 04:35:44 -05002454/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002455 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2456 * @instance: Adapter soft state
2457 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002458 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002459 * complete all its outstanding commands. Returns error if one or more IOs
2460 * are pending after this time period. It also marks the controller dead.
2461 */
2462static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2463{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302464 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002465 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002466 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002467 unsigned long flags;
2468 struct list_head clist_local;
2469 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002470 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002471
Sumit Saxenaccc75072016-01-28 21:04:33 +05302472 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2473 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2474 __func__, __LINE__);
2475 return FAILED;
2476 }
bo yang39a98552010-09-22 22:36:29 -04002477
Sumit Saxena8a01a412016-01-28 21:04:32 +05302478 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002479
2480 INIT_LIST_HEAD(&clist_local);
2481 spin_lock_irqsave(&instance->hba_lock, flags);
2482 list_splice_init(&instance->internal_reset_pending_q,
2483 &clist_local);
2484 spin_unlock_irqrestore(&instance->hba_lock, flags);
2485
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002486 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002487 for (i = 0; i < wait_time; i++) {
2488 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302489 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002490 break;
2491 }
2492
Sumit Saxena8a01a412016-01-28 21:04:32 +05302493 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002494 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302495 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002496 return FAILED;
2497 }
2498
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002499 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002500 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002501 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002502 struct megasas_cmd, list);
2503 list_del_init(&reset_cmd->list);
2504 if (reset_cmd->scmd) {
2505 reset_cmd->scmd->result = DID_RESET << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002506 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002507 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002508 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002509
2510 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2511 megasas_return_cmd(instance, reset_cmd);
2512 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002513 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002514 "reset queue\n",
2515 reset_cmd);
2516
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302517 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002518 instance->instancet->fire_cmd(instance,
2519 reset_cmd->frame_phys_addr,
2520 0, instance->reg_set);
2521 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002522 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002523 "cmds lst\n",
2524 reset_cmd);
2525 }
2526 reset_index++;
2527 }
2528
2529 return SUCCESS;
2530 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002531
adam radfordc007b8b2012-07-17 18:20:24 -07002532 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302533 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002534
2535 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002536 break;
2537
2538 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002539 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002540 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002541 /*
2542 * Call cmd completion routine. Cmd to be
2543 * be completed directly without depending on isr.
2544 */
2545 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002546 }
2547
2548 msleep(1000);
2549 }
2550
Yang, Bo707e09b2010-10-12 07:20:27 -06002551 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302552 outstanding = atomic_read(&instance->fw_outstanding);
2553 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002554
Sumit Saxenaccc75072016-01-28 21:04:33 +05302555 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2556 goto no_outstanding;
2557
2558 if (instance->disableOnlineCtrlReset)
2559 goto kill_hba_and_failed;
2560 do {
2561 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2562 dev_info(&instance->pdev->dev,
2563 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2564 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2565 if (i == 3)
2566 goto kill_hba_and_failed;
2567 megasas_do_ocr(instance);
2568
2569 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2570 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2571 __func__, __LINE__);
2572 return FAILED;
2573 }
2574 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2575 __func__, __LINE__);
2576
2577 for (sl = 0; sl < 10; sl++)
2578 msleep(500);
2579
2580 outstanding = atomic_read(&instance->fw_outstanding);
2581
2582 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2583 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2584 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002585 }
2586 i++;
2587 } while (i <= 3);
2588
Sumit Saxenaccc75072016-01-28 21:04:33 +05302589no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002590
Sumit Saxenaccc75072016-01-28 21:04:33 +05302591 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2592 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002593 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302594
2595kill_hba_and_failed:
2596
2597 /* Reset not supported, kill adapter */
2598 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2599 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2600 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2601 atomic_read(&instance->fw_outstanding));
2602 megasas_dump_pending_frames(instance);
2603 megaraid_sas_kill_hba(instance);
2604
2605 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002606}
2607
2608/**
2609 * megasas_generic_reset - Generic reset routine
2610 * @scmd: Mid-layer SCSI command
2611 *
2612 * This routine implements a generic reset handler for device, bus and host
2613 * reset requests. Device, bus and host specific reset handlers can use this
2614 * function after they do their specific tasks.
2615 */
2616static int megasas_generic_reset(struct scsi_cmnd *scmd)
2617{
2618 int ret_val;
2619 struct megasas_instance *instance;
2620
2621 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2622
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002623 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2624 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002625
Sumit Saxena8a01a412016-01-28 21:04:32 +05302626 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002627 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002628 return FAILED;
2629 }
2630
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002631 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002632 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002633 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002634 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002635 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002636
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002637 return ret_val;
2638}
2639
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002640/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002641 * megasas_reset_timer - quiesce the adapter if required
2642 * @scmd: scsi cmnd
2643 *
2644 * Sets the FW busy flag and reduces the host->can_queue if the
2645 * cmd has not been completed within the timeout period.
2646 */
2647static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002648blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002649{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002650 struct megasas_instance *instance;
2651 unsigned long flags;
2652
2653 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302654 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002655 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002656 }
2657
adam radfordf575c5d2011-10-13 16:01:12 -07002658 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002659 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2660 /* FW is busy, throttle IO */
2661 spin_lock_irqsave(instance->host->host_lock, flags);
2662
adam radfordc5daa6a2012-07-17 18:20:03 -07002663 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002664 instance->last_time = jiffies;
2665 instance->flag |= MEGASAS_FW_BUSY;
2666
2667 spin_unlock_irqrestore(instance->host->host_lock, flags);
2668 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002669 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002670}
2671
2672/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002673 * megasas_reset_bus_host - Bus & host reset handler entry point
2674 */
2675static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2676{
2677 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002678 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002679
adam radford9c915a82010-12-21 13:34:31 -08002680 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002681
2682 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002683 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002684 */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302685 if (instance->ctrl_context)
adam radford229fe472014-03-10 02:51:56 -07002686 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002687 else
2688 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002689
2690 return ret;
2691}
2692
2693/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002694 * megasas_task_abort - Issues task abort request to firmware
2695 * (supported only for fusion adapters)
2696 * @scmd: SCSI command pointer
2697 */
2698static int megasas_task_abort(struct scsi_cmnd *scmd)
2699{
2700 int ret;
2701 struct megasas_instance *instance;
2702
2703 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2704
2705 if (instance->ctrl_context)
2706 ret = megasas_task_abort_fusion(scmd);
2707 else {
2708 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2709 ret = FAILED;
2710 }
2711
2712 return ret;
2713}
2714
2715/**
2716 * megasas_reset_target: Issues target reset request to firmware
2717 * (supported only for fusion adapters)
2718 * @scmd: SCSI command pointer
2719 */
2720static int megasas_reset_target(struct scsi_cmnd *scmd)
2721{
2722 int ret;
2723 struct megasas_instance *instance;
2724
2725 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2726
2727 if (instance->ctrl_context)
2728 ret = megasas_reset_target_fusion(scmd);
2729 else {
2730 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2731 ret = FAILED;
2732 }
2733
2734 return ret;
2735}
2736
2737/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002738 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002739 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002740 * @bdev: block device
2741 * @capacity: drive capacity
2742 * @geom: geometry parameters
2743 */
2744static int
2745megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2746 sector_t capacity, int geom[])
2747{
2748 int heads;
2749 int sectors;
2750 sector_t cylinders;
2751 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002752
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002753 /* Default heads (64) & sectors (32) */
2754 heads = 64;
2755 sectors = 32;
2756
2757 tmp = heads * sectors;
2758 cylinders = capacity;
2759
2760 sector_div(cylinders, tmp);
2761
2762 /*
2763 * Handle extended translation size for logical drives > 1Gb
2764 */
2765
2766 if (capacity >= 0x200000) {
2767 heads = 255;
2768 sectors = 63;
2769 tmp = heads*sectors;
2770 cylinders = capacity;
2771 sector_div(cylinders, tmp);
2772 }
2773
2774 geom[0] = heads;
2775 geom[1] = sectors;
2776 geom[2] = cylinders;
2777
2778 return 0;
2779}
2780
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002781static void megasas_aen_polling(struct work_struct *work);
2782
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002783/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002784 * megasas_service_aen - Processes an event notification
2785 * @instance: Adapter soft state
2786 * @cmd: AEN command completed by the ISR
2787 *
2788 * For AEN, driver sends a command down to FW that is held by the FW till an
2789 * event occurs. When an event of interest occurs, FW completes the command
2790 * that it was previously holding.
2791 *
2792 * This routines sends SIGIO signal to processes that have registered with the
2793 * driver for AEN.
2794 */
2795static void
2796megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2797{
Yang, Boc3518832009-10-06 14:18:02 -06002798 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002799
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002800 /*
2801 * Don't signal app if it is just an aborted previously registered aen
2802 */
Yang, Boc3518832009-10-06 14:18:02 -06002803 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2804 spin_lock_irqsave(&poll_aen_lock, flags);
2805 megasas_poll_wait_aen = 1;
2806 spin_unlock_irqrestore(&poll_aen_lock, flags);
2807 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002808 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002809 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002810 else
2811 cmd->abort_aen = 0;
2812
2813 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302814
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302815 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002816
bo yang39a98552010-09-22 22:36:29 -04002817 if ((instance->unload == 0) &&
2818 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002819 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002820
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002821 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2822 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002823 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002824 } else {
2825 ev->instance = instance;
2826 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002827 INIT_DELAYED_WORK(&ev->hotplug_work,
2828 megasas_aen_polling);
2829 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002830 }
2831 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002832}
2833
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302834static ssize_t
2835megasas_fw_crash_buffer_store(struct device *cdev,
2836 struct device_attribute *attr, const char *buf, size_t count)
2837{
2838 struct Scsi_Host *shost = class_to_shost(cdev);
2839 struct megasas_instance *instance =
2840 (struct megasas_instance *) shost->hostdata;
2841 int val = 0;
2842 unsigned long flags;
2843
2844 if (kstrtoint(buf, 0, &val) != 0)
2845 return -EINVAL;
2846
2847 spin_lock_irqsave(&instance->crashdump_lock, flags);
2848 instance->fw_crash_buffer_offset = val;
2849 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2850 return strlen(buf);
2851}
2852
2853static ssize_t
2854megasas_fw_crash_buffer_show(struct device *cdev,
2855 struct device_attribute *attr, char *buf)
2856{
2857 struct Scsi_Host *shost = class_to_shost(cdev);
2858 struct megasas_instance *instance =
2859 (struct megasas_instance *) shost->hostdata;
2860 u32 size;
2861 unsigned long buff_addr;
2862 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2863 unsigned long src_addr;
2864 unsigned long flags;
2865 u32 buff_offset;
2866
2867 spin_lock_irqsave(&instance->crashdump_lock, flags);
2868 buff_offset = instance->fw_crash_buffer_offset;
2869 if (!instance->crash_dump_buf &&
2870 !((instance->fw_crash_state == AVAILABLE) ||
2871 (instance->fw_crash_state == COPYING))) {
2872 dev_err(&instance->pdev->dev,
2873 "Firmware crash dump is not available\n");
2874 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2875 return -EINVAL;
2876 }
2877
2878 buff_addr = (unsigned long) buf;
2879
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002880 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302881 dev_err(&instance->pdev->dev,
2882 "Firmware crash dump offset is out of range\n");
2883 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2884 return 0;
2885 }
2886
2887 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2888 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2889
2890 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2891 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002892 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302893 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2894
2895 return size;
2896}
2897
2898static ssize_t
2899megasas_fw_crash_buffer_size_show(struct device *cdev,
2900 struct device_attribute *attr, char *buf)
2901{
2902 struct Scsi_Host *shost = class_to_shost(cdev);
2903 struct megasas_instance *instance =
2904 (struct megasas_instance *) shost->hostdata;
2905
2906 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2907 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2908}
2909
2910static ssize_t
2911megasas_fw_crash_state_store(struct device *cdev,
2912 struct device_attribute *attr, const char *buf, size_t count)
2913{
2914 struct Scsi_Host *shost = class_to_shost(cdev);
2915 struct megasas_instance *instance =
2916 (struct megasas_instance *) shost->hostdata;
2917 int val = 0;
2918 unsigned long flags;
2919
2920 if (kstrtoint(buf, 0, &val) != 0)
2921 return -EINVAL;
2922
2923 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2924 dev_err(&instance->pdev->dev, "application updates invalid "
2925 "firmware crash state\n");
2926 return -EINVAL;
2927 }
2928
2929 instance->fw_crash_state = val;
2930
2931 if ((val == COPIED) || (val == COPY_ERROR)) {
2932 spin_lock_irqsave(&instance->crashdump_lock, flags);
2933 megasas_free_host_crash_buffer(instance);
2934 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2935 if (val == COPY_ERROR)
2936 dev_info(&instance->pdev->dev, "application failed to "
2937 "copy Firmware crash dump\n");
2938 else
2939 dev_info(&instance->pdev->dev, "Firmware crash dump "
2940 "copied successfully\n");
2941 }
2942 return strlen(buf);
2943}
2944
2945static ssize_t
2946megasas_fw_crash_state_show(struct device *cdev,
2947 struct device_attribute *attr, char *buf)
2948{
2949 struct Scsi_Host *shost = class_to_shost(cdev);
2950 struct megasas_instance *instance =
2951 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002952
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302953 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2954}
2955
2956static ssize_t
2957megasas_page_size_show(struct device *cdev,
2958 struct device_attribute *attr, char *buf)
2959{
2960 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2961}
2962
Sumit Saxena308ec452016-01-28 21:04:30 +05302963static ssize_t
2964megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
2965 char *buf)
2966{
2967 struct Scsi_Host *shost = class_to_shost(cdev);
2968 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
2969
2970 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
2971}
2972
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302973static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2974 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2975static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2976 megasas_fw_crash_buffer_size_show, NULL);
2977static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2978 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2979static DEVICE_ATTR(page_size, S_IRUGO,
2980 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05302981static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
2982 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302983
2984struct device_attribute *megaraid_host_attrs[] = {
2985 &dev_attr_fw_crash_buffer_size,
2986 &dev_attr_fw_crash_buffer,
2987 &dev_attr_fw_crash_state,
2988 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05302989 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302990 NULL,
2991};
2992
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002993/*
2994 * Scsi host template for megaraid_sas driver
2995 */
2996static struct scsi_host_template megasas_template = {
2997
2998 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05302999 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003000 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003001 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003002 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303003 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003004 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003005 .eh_target_reset_handler = megasas_reset_target,
3006 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003007 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003008 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303009 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003010 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003011 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003012 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003013 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003014};
3015
3016/**
3017 * megasas_complete_int_cmd - Completes an internal command
3018 * @instance: Adapter soft state
3019 * @cmd: Command to be completed
3020 *
3021 * The megasas_issue_blocked_cmd() function waits for a command to complete
3022 * after it issues a command. This function wakes up that waiting routine by
3023 * calling wake_up() on the wait queue.
3024 */
3025static void
3026megasas_complete_int_cmd(struct megasas_instance *instance,
3027 struct megasas_cmd *cmd)
3028{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303029 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003030 wake_up(&instance->int_cmd_wait_q);
3031}
3032
3033/**
3034 * megasas_complete_abort - Completes aborting a command
3035 * @instance: Adapter soft state
3036 * @cmd: Cmd that was issued to abort another cmd
3037 *
adam radford0d490162010-12-14 19:17:17 -08003038 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3039 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003040 * wakes up all functions waiting on the same wait queue.
3041 */
3042static void
3043megasas_complete_abort(struct megasas_instance *instance,
3044 struct megasas_cmd *cmd)
3045{
3046 if (cmd->sync_cmd) {
3047 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303048 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003049 wake_up(&instance->abort_cmd_wait_q);
3050 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003051}
3052
3053/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003054 * megasas_complete_cmd - Completes a command
3055 * @instance: Adapter soft state
3056 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003057 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003058 * SCSI mid-layer instead of the value returned
3059 * by the FW. This should be used if caller wants
3060 * an alternate status (as in the case of aborted
3061 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003062 */
adam radford9c915a82010-12-21 13:34:31 -08003063void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003064megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3065 u8 alt_status)
3066{
3067 int exception = 0;
3068 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003069 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003070 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303071 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003072
bo yang39a98552010-09-22 22:36:29 -04003073 /* flag for the retry reset */
3074 cmd->retry_for_fw_reset = 0;
3075
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003076 if (cmd->scmd)
3077 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003078
3079 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003080 case MFI_CMD_INVALID:
3081 /* Some older 1068 controller FW may keep a pended
3082 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3083 when booting the kdump kernel. Ignore this command to
3084 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003085 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3086 "completed\n");
3087 dev_warn(&instance->pdev->dev, "If you have a controller "
3088 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003089 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003090 case MFI_CMD_PD_SCSI_IO:
3091 case MFI_CMD_LD_SCSI_IO:
3092
3093 /*
3094 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3095 * issued either through an IO path or an IOCTL path. If it
3096 * was via IOCTL, we will send it to internal completion.
3097 */
3098 if (cmd->sync_cmd) {
3099 cmd->sync_cmd = 0;
3100 megasas_complete_int_cmd(instance, cmd);
3101 break;
3102 }
3103
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003104 case MFI_CMD_LD_READ:
3105 case MFI_CMD_LD_WRITE:
3106
3107 if (alt_status) {
3108 cmd->scmd->result = alt_status << 16;
3109 exception = 1;
3110 }
3111
3112 if (exception) {
3113
Sumant Patroe4a082c2006-05-30 12:03:37 -07003114 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003115
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003116 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003117 cmd->scmd->scsi_done(cmd->scmd);
3118 megasas_return_cmd(instance, cmd);
3119
3120 break;
3121 }
3122
3123 switch (hdr->cmd_status) {
3124
3125 case MFI_STAT_OK:
3126 cmd->scmd->result = DID_OK << 16;
3127 break;
3128
3129 case MFI_STAT_SCSI_IO_FAILED:
3130 case MFI_STAT_LD_INIT_IN_PROGRESS:
3131 cmd->scmd->result =
3132 (DID_ERROR << 16) | hdr->scsi_status;
3133 break;
3134
3135 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3136
3137 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3138
3139 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3140 memset(cmd->scmd->sense_buffer, 0,
3141 SCSI_SENSE_BUFFERSIZE);
3142 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3143 hdr->sense_len);
3144
3145 cmd->scmd->result |= DRIVER_SENSE << 24;
3146 }
3147
3148 break;
3149
3150 case MFI_STAT_LD_OFFLINE:
3151 case MFI_STAT_DEVICE_NOT_FOUND:
3152 cmd->scmd->result = DID_BAD_TARGET << 16;
3153 break;
3154
3155 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003156 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003157 hdr->cmd_status);
3158 cmd->scmd->result = DID_ERROR << 16;
3159 break;
3160 }
3161
Sumant Patroe4a082c2006-05-30 12:03:37 -07003162 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003163
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003164 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003165 cmd->scmd->scsi_done(cmd->scmd);
3166 megasas_return_cmd(instance, cmd);
3167
3168 break;
3169
3170 case MFI_CMD_SMP:
3171 case MFI_CMD_STP:
3172 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303173 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003174 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303175 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3176 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303177 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003178 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303179 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003180 if (cmd->frame->hdr.cmd_status != 0) {
3181 if (cmd->frame->hdr.cmd_status !=
3182 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003183 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003184 cmd->frame->hdr.cmd_status);
3185 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303186 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003187 spin_unlock_irqrestore(
3188 instance->host->host_lock,
3189 flags);
3190 break;
3191 }
3192 } else
3193 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303194 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303195
3196 /*
3197 * Set fast path IO to ZERO.
3198 * Validate Map will set proper value.
3199 * Meanwhile all IOs will go as LD IO.
3200 */
3201 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003202 fusion->fast_path_io = 1;
3203 else
3204 fusion->fast_path_io = 0;
3205 megasas_sync_map_info(instance);
3206 spin_unlock_irqrestore(instance->host->host_lock,
3207 flags);
3208 break;
3209 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303210 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3211 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003212 spin_lock_irqsave(&poll_aen_lock, flags);
3213 megasas_poll_wait_aen = 0;
3214 spin_unlock_irqrestore(&poll_aen_lock, flags);
3215 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003216
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303217 /* FW has an updated PD sequence */
3218 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3219 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3220
3221 spin_lock_irqsave(instance->host->host_lock, flags);
3222 status = cmd->frame->hdr.cmd_status;
3223 instance->jbod_seq_cmd = NULL;
3224 megasas_return_cmd(instance, cmd);
3225
3226 if (status == MFI_STAT_OK) {
3227 instance->pd_seq_map_id++;
3228 /* Re-register a pd sync seq num cmd */
3229 if (megasas_sync_pd_seq_num(instance, true))
3230 instance->use_seqnum_jbod_fp = false;
3231 } else
3232 instance->use_seqnum_jbod_fp = false;
3233
3234 spin_unlock_irqrestore(instance->host->host_lock, flags);
3235 break;
3236 }
3237
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003238 /*
3239 * See if got an event notification
3240 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303241 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003242 megasas_service_aen(instance, cmd);
3243 else
3244 megasas_complete_int_cmd(instance, cmd);
3245
3246 break;
3247
3248 case MFI_CMD_ABORT:
3249 /*
3250 * Cmd issued to abort another cmd returned
3251 */
3252 megasas_complete_abort(instance, cmd);
3253 break;
3254
3255 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003256 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003257 hdr->cmd);
3258 break;
3259 }
3260}
3261
3262/**
bo yang39a98552010-09-22 22:36:29 -04003263 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003264 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003265 * @instance: Adapter soft state
3266 */
3267static inline void
3268megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3269{
3270 struct megasas_cmd *cmd;
3271 struct list_head clist_local;
3272 union megasas_evt_class_locale class_locale;
3273 unsigned long flags;
3274 u32 seq_num;
3275
3276 INIT_LIST_HEAD(&clist_local);
3277 spin_lock_irqsave(&instance->hba_lock, flags);
3278 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3279 spin_unlock_irqrestore(&instance->hba_lock, flags);
3280
3281 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003282 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003283 struct megasas_cmd, list);
3284 list_del_init(&cmd->list);
3285
3286 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003287 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3288 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003289 cmd, cmd->scmd, cmd->sync_cmd);
3290
3291 cmd->retry_for_fw_reset++;
3292
3293 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003294 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003295 "was tried multiple times during reset."
3296 "Shutting down the HBA\n",
3297 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303298 instance->instancet->disable_intr(instance);
3299 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003300 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003301 return;
3302 }
3303 }
3304
3305 if (cmd->sync_cmd == 1) {
3306 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003307 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003308 "cmd attached to internal command!\n");
3309 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003310 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003311 "on the internal reset queue,"
3312 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303313 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003314 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003315 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003316 0, instance->reg_set);
3317 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003318 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003319 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003320 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003321
3322 atomic_inc(&instance->fw_outstanding);
3323 instance->instancet->fire_cmd(instance,
3324 cmd->frame_phys_addr,
3325 cmd->frame_count-1, instance->reg_set);
3326 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003327 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003328 "internal reset defer list while re-issue!!\n",
3329 cmd);
3330 }
3331 }
3332
3333 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003334 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003335 megasas_return_cmd(instance, instance->aen_cmd);
3336
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003337 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003338 }
3339
3340 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003341 * Initiate AEN (Asynchronous Event Notification)
3342 */
bo yang39a98552010-09-22 22:36:29 -04003343 seq_num = instance->last_seq_num;
3344 class_locale.members.reserved = 0;
3345 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3346 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3347
3348 megasas_register_aen(instance, seq_num, class_locale.word);
3349}
3350
3351/**
3352 * Move the internal reset pending commands to a deferred queue.
3353 *
3354 * We move the commands pending at internal reset time to a
3355 * pending queue. This queue would be flushed after successful
3356 * completion of the internal reset sequence. if the internal reset
3357 * did not complete in time, the kernel reset handler would flush
3358 * these commands.
3359 **/
3360static void
3361megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3362{
3363 struct megasas_cmd *cmd;
3364 int i;
3365 u32 max_cmd = instance->max_fw_cmds;
3366 u32 defer_index;
3367 unsigned long flags;
3368
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003369 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303370 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003371 for (i = 0; i < max_cmd; i++) {
3372 cmd = instance->cmd_list[i];
3373 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003374 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003375 "on the defer queue as internal\n",
3376 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3377
3378 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003379 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003380 " moving this cmd:%p, %d %p, it was"
3381 "discovered on some list?\n",
3382 cmd, cmd->sync_cmd, cmd->scmd);
3383
3384 list_del_init(&cmd->list);
3385 }
3386 defer_index++;
3387 list_add_tail(&cmd->list,
3388 &instance->internal_reset_pending_q);
3389 }
3390 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303391 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003392}
3393
3394
3395static void
3396process_fw_state_change_wq(struct work_struct *work)
3397{
3398 struct megasas_instance *instance =
3399 container_of(work, struct megasas_instance, work_init);
3400 u32 wait;
3401 unsigned long flags;
3402
Sumit Saxena8a01a412016-01-28 21:04:32 +05303403 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003404 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303405 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003406 return ;
3407 }
3408
Sumit Saxena8a01a412016-01-28 21:04:32 +05303409 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003410 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003411 "state, restarting it...\n");
3412
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303413 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003414 atomic_set(&instance->fw_outstanding, 0);
3415
3416 atomic_set(&instance->fw_reset_no_pci_access, 1);
3417 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003418 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003419
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003420 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003421 "initiating next stage...\n");
3422
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003423 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003424 "state 2 starting...\n");
3425
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003426 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003427 for (wait = 0; wait < 30; wait++) {
3428 msleep(1000);
3429 }
3430
adam radford058a8fa2011-10-08 18:14:27 -07003431 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003432 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003433
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303434 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003435 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003436 return ;
3437 }
3438
3439 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3440 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3441 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3442 ) {
3443 *instance->consumer = *instance->producer;
3444 } else {
3445 *instance->consumer = 0;
3446 *instance->producer = 0;
3447 }
3448
3449 megasas_issue_init_mfi(instance);
3450
3451 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303452 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003453 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303454 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003455
3456 megasas_issue_pending_cmds_again(instance);
3457 instance->issuepend_done = 1;
3458 }
bo yang39a98552010-09-22 22:36:29 -04003459}
3460
3461/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003462 * megasas_deplete_reply_queue - Processes all completed commands
3463 * @instance: Adapter soft state
3464 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003465 * SCSI mid-layer instead of the status
3466 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003467 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003468 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003469static int
bo yang39a98552010-09-22 22:36:29 -04003470megasas_deplete_reply_queue(struct megasas_instance *instance,
3471 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003472{
bo yang39a98552010-09-22 22:36:29 -04003473 u32 mfiStatus;
3474 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003475
bo yang39a98552010-09-22 22:36:29 -04003476 if ((mfiStatus = instance->instancet->check_reset(instance,
3477 instance->reg_set)) == 1) {
3478 return IRQ_HANDLED;
3479 }
3480
3481 if ((mfiStatus = instance->instancet->clear_intr(
3482 instance->reg_set)
3483 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003484 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003485 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003486 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003487 }
3488
3489 instance->mfiStatus = mfiStatus;
3490
3491 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3492 fw_state = instance->instancet->read_fw_status_reg(
3493 instance->reg_set) & MFI_STATE_MASK;
3494
3495 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003496 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003497 fw_state);
3498 }
3499
3500 if ((fw_state == MFI_STATE_FAULT) &&
3501 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003502 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003503
3504 if ((instance->pdev->device ==
3505 PCI_DEVICE_ID_LSI_SAS1064R) ||
3506 (instance->pdev->device ==
3507 PCI_DEVICE_ID_DELL_PERC5) ||
3508 (instance->pdev->device ==
3509 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3510
3511 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303512 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003513 }
3514
3515
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303516 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303517 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003518 instance->issuepend_done = 0;
3519
3520 atomic_set(&instance->fw_outstanding, 0);
3521 megasas_internal_reset_defer_cmds(instance);
3522
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003523 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303524 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003525
3526 schedule_work(&instance->work_init);
3527 return IRQ_HANDLED;
3528
3529 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003530 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003531 fw_state, instance->disableOnlineCtrlReset);
3532 }
3533 }
3534
Sumant Patro5d018ad2006-10-03 13:13:18 -07003535 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003536 return IRQ_HANDLED;
3537}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003538/**
3539 * megasas_isr - isr entry point
3540 */
David Howells7d12e782006-10-05 14:55:46 +01003541static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003542{
adam radfordc8e858f2011-10-08 18:15:13 -07003543 struct megasas_irq_context *irq_context = devp;
3544 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003545 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003546 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003547
adam radfordc8e858f2011-10-08 18:15:13 -07003548 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003549 return IRQ_HANDLED;
3550
bo yang39a98552010-09-22 22:36:29 -04003551 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003552 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003553 spin_unlock_irqrestore(&instance->hba_lock, flags);
3554
3555 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003556}
3557
3558/**
3559 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003560 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003561 *
3562 * During the initialization, FW passes can potentially be in any one of
3563 * several possible states. If the FW in operational, waiting-for-handshake
3564 * states, driver must take steps to bring it to ready state. Otherwise, it
3565 * has to wait for the ready state.
3566 */
adam radford9c915a82010-12-21 13:34:31 -08003567int
adam radford058a8fa2011-10-08 18:14:27 -07003568megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003569{
3570 int i;
3571 u8 max_wait;
3572 u32 fw_state;
3573 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003574 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003575
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003576 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3577 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003578
Sumant Patroe3bbff92006-10-03 12:28:49 -07003579 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003580 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003581 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003582
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003583 while (fw_state != MFI_STATE_READY) {
3584
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003585 switch (fw_state) {
3586
3587 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003588 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003589 if (ocr) {
3590 max_wait = MEGASAS_RESET_WAIT_TIME;
3591 cur_state = MFI_STATE_FAULT;
3592 break;
3593 } else
3594 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003595
3596 case MFI_STATE_WAIT_HANDSHAKE:
3597 /*
3598 * Set the CLR bit in inbound doorbell
3599 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003600 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003601 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3602 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003603 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303604 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003605 writel(
3606 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003607 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303608 else
Yang, Bo87911122009-10-06 14:31:54 -06003609 writel(
3610 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3611 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003612
Yang, Bo7218df62009-10-06 14:52:20 -06003613 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003614 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3615 break;
3616
Sumant Patroe3bbff92006-10-03 12:28:49 -07003617 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003618 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003619 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3620 (instance->pdev->device ==
3621 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303622 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003623 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003624 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303625 else
Yang, Bo87911122009-10-06 14:31:54 -06003626 writel(MFI_INIT_HOTPLUG,
3627 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003628
Yang, Bo7218df62009-10-06 14:52:20 -06003629 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003630 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3631 break;
3632
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003633 case MFI_STATE_OPERATIONAL:
3634 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003635 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003636 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303637 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003638 if ((instance->pdev->device ==
3639 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3640 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003641 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303642 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003643 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003644 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303645
3646 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003647 for (i = 0; i < (10 * 1000); i += 20) {
3648 if (readl(
3649 &instance->
3650 reg_set->
3651 doorbell) & 1)
3652 msleep(20);
3653 else
3654 break;
3655 }
3656 }
Yang, Bo87911122009-10-06 14:31:54 -06003657 } else
3658 writel(MFI_RESET_FLAGS,
3659 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003660
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_OPERATIONAL;
3663 break;
3664
3665 case MFI_STATE_UNDEFINED:
3666 /*
3667 * This state should not last for more than 2 seconds
3668 */
Yang, Bo7218df62009-10-06 14:52:20 -06003669 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003670 cur_state = MFI_STATE_UNDEFINED;
3671 break;
3672
3673 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003674 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003675 cur_state = MFI_STATE_BB_INIT;
3676 break;
3677
3678 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003679 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003680 cur_state = MFI_STATE_FW_INIT;
3681 break;
3682
3683 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003684 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003685 cur_state = MFI_STATE_FW_INIT_2;
3686 break;
3687
3688 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003689 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003690 cur_state = MFI_STATE_DEVICE_SCAN;
3691 break;
3692
3693 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003694 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003695 cur_state = MFI_STATE_FLUSH_CACHE;
3696 break;
3697
3698 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003699 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003700 fw_state);
3701 return -ENODEV;
3702 }
3703
3704 /*
3705 * The cur_state should not last for more than max_wait secs
3706 */
3707 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003708 curr_abs_state = instance->instancet->
3709 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003710
Yang, Bo7218df62009-10-06 14:52:20 -06003711 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003712 msleep(1);
3713 } else
3714 break;
3715 }
3716
3717 /*
3718 * Return error if fw_state hasn't changed after max_wait
3719 */
Yang, Bo7218df62009-10-06 14:52:20 -06003720 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003721 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003722 "in %d secs\n", fw_state, max_wait);
3723 return -ENODEV;
3724 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003725
3726 abs_state = curr_abs_state;
3727 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003728 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003729 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003730
3731 return 0;
3732}
3733
3734/**
3735 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3736 * @instance: Adapter soft state
3737 */
3738static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3739{
3740 int i;
adam radford9c915a82010-12-21 13:34:31 -08003741 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003742 struct megasas_cmd *cmd;
3743
3744 if (!instance->frame_dma_pool)
3745 return;
3746
3747 /*
3748 * Return all frames to pool
3749 */
3750 for (i = 0; i < max_cmd; i++) {
3751
3752 cmd = instance->cmd_list[i];
3753
3754 if (cmd->frame)
3755 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3756 cmd->frame_phys_addr);
3757
3758 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003759 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003760 cmd->sense_phys_addr);
3761 }
3762
3763 /*
3764 * Now destroy the pool itself
3765 */
3766 pci_pool_destroy(instance->frame_dma_pool);
3767 pci_pool_destroy(instance->sense_dma_pool);
3768
3769 instance->frame_dma_pool = NULL;
3770 instance->sense_dma_pool = NULL;
3771}
3772
3773/**
3774 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3775 * @instance: Adapter soft state
3776 *
3777 * Each command packet has an embedded DMA memory buffer that is used for
3778 * filling MFI frame and the SG list that immediately follows the frame. This
3779 * function creates those DMA memory buffers for each command packet by using
3780 * PCI pool facility.
3781 */
3782static int megasas_create_frame_pool(struct megasas_instance *instance)
3783{
3784 int i;
3785 u32 max_cmd;
3786 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003787 u32 total_sz;
3788 u32 frame_count;
3789 struct megasas_cmd *cmd;
3790
adam radford9c915a82010-12-21 13:34:31 -08003791 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003792
3793 /*
3794 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3795 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3796 */
3797 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3798 sizeof(struct megasas_sge32);
3799
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003800 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003801 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003802
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003803 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303804 * For MFI controllers.
3805 * max_num_sge = 60
3806 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3807 * Total 960 byte (15 MFI frame of 64 byte)
3808 *
3809 * Fusion adapter require only 3 extra frame.
3810 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3811 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3812 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003813 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303814 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003815 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3816 /*
3817 * Use DMA pool facility provided by PCI layer
3818 */
3819 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303820 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003821
3822 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003823 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003824 return -ENOMEM;
3825 }
3826
3827 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3828 instance->pdev, 128, 4, 0);
3829
3830 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003831 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003832
3833 pci_pool_destroy(instance->frame_dma_pool);
3834 instance->frame_dma_pool = NULL;
3835
3836 return -ENOMEM;
3837 }
3838
3839 /*
3840 * Allocate and attach a frame to each of the commands in cmd_list.
3841 * By making cmd->index as the context instead of the &cmd, we can
3842 * always use 32bit context regardless of the architecture
3843 */
3844 for (i = 0; i < max_cmd; i++) {
3845
3846 cmd = instance->cmd_list[i];
3847
3848 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3849 GFP_KERNEL, &cmd->frame_phys_addr);
3850
3851 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3852 GFP_KERNEL, &cmd->sense_phys_addr);
3853
3854 /*
3855 * megasas_teardown_frame_pool() takes care of freeing
3856 * whatever has been allocated
3857 */
3858 if (!cmd->frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003859 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003860 megasas_teardown_frame_pool(instance);
3861 return -ENOMEM;
3862 }
3863
Yang, Bo707e09b2010-10-12 07:20:27 -06003864 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303865 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003866 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303867 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003868 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003869 }
3870
3871 return 0;
3872}
3873
3874/**
3875 * megasas_free_cmds - Free all the cmds in the free cmd pool
3876 * @instance: Adapter soft state
3877 */
adam radford9c915a82010-12-21 13:34:31 -08003878void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003879{
3880 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003881
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003882 /* First free the MFI frame pool */
3883 megasas_teardown_frame_pool(instance);
3884
3885 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003886 for (i = 0; i < instance->max_mfi_cmds; i++)
3887
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003888 kfree(instance->cmd_list[i]);
3889
3890 /* Free the cmd_list buffer itself */
3891 kfree(instance->cmd_list);
3892 instance->cmd_list = NULL;
3893
3894 INIT_LIST_HEAD(&instance->cmd_pool);
3895}
3896
3897/**
3898 * megasas_alloc_cmds - Allocates the command packets
3899 * @instance: Adapter soft state
3900 *
3901 * Each command that is issued to the FW, whether IO commands from the OS or
3902 * internal commands like IOCTLs, are wrapped in local data structure called
3903 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3904 * the FW.
3905 *
3906 * Each frame has a 32-bit field called context (tag). This context is used
3907 * to get back the megasas_cmd from the frame when a frame gets completed in
3908 * the ISR. Typically the address of the megasas_cmd itself would be used as
3909 * the context. But we wanted to keep the differences between 32 and 64 bit
3910 * systems to the mininum. We always use 32 bit integers for the context. In
3911 * this driver, the 32 bit values are the indices into an array cmd_list.
3912 * This array is used only to look up the megasas_cmd given the context. The
3913 * free commands themselves are maintained in a linked list called cmd_pool.
3914 */
adam radford9c915a82010-12-21 13:34:31 -08003915int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003916{
3917 int i;
3918 int j;
3919 u32 max_cmd;
3920 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303921 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003922
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303923 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003924 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003925
3926 /*
3927 * instance->cmd_list is an array of struct megasas_cmd pointers.
3928 * Allocate the dynamic array first and then allocate individual
3929 * commands.
3930 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003931 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003932
3933 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003934 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003935 return -ENOMEM;
3936 }
3937
adam radford9c915a82010-12-21 13:34:31 -08003938 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003939
3940 for (i = 0; i < max_cmd; i++) {
3941 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3942 GFP_KERNEL);
3943
3944 if (!instance->cmd_list[i]) {
3945
3946 for (j = 0; j < i; j++)
3947 kfree(instance->cmd_list[j]);
3948
3949 kfree(instance->cmd_list);
3950 instance->cmd_list = NULL;
3951
3952 return -ENOMEM;
3953 }
3954 }
3955
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003956 for (i = 0; i < max_cmd; i++) {
3957 cmd = instance->cmd_list[i];
3958 memset(cmd, 0, sizeof(struct megasas_cmd));
3959 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003960 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003961 cmd->instance = instance;
3962
3963 list_add_tail(&cmd->list, &instance->cmd_pool);
3964 }
3965
3966 /*
3967 * Create a frame pool and assign one frame to each cmd
3968 */
3969 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003970 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003971 megasas_free_cmds(instance);
3972 }
3973
3974 return 0;
3975}
3976
Yang, Bo81e403c2009-10-06 14:27:54 -06003977/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303978 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
3979 * @instance: Adapter soft state
3980 *
3981 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
3982 * or FW is not under OCR.
3983 */
3984inline int
3985dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
3986
3987 if (!instance->ctrl_context)
3988 return KILL_ADAPTER;
3989 else if (instance->unload ||
3990 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
3991 return IGNORE_TIMEOUT;
3992 else
3993 return INITIATE_OCR;
3994}
3995
Sumit Saxena2216c302016-01-28 21:04:26 +05303996static int
3997megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
3998{
3999 int ret;
4000 struct megasas_cmd *cmd;
4001 struct megasas_dcmd_frame *dcmd;
4002
4003 cmd = megasas_get_cmd(instance);
4004
4005 if (!cmd) {
4006 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4007 return -ENOMEM;
4008 }
4009
4010 dcmd = &cmd->frame->dcmd;
4011
4012 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4013 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4014
4015 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4016 dcmd->cmd = MFI_CMD_DCMD;
4017 dcmd->cmd_status = 0xFF;
4018 dcmd->sge_count = 1;
4019 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4020 dcmd->timeout = 0;
4021 dcmd->pad_0 = 0;
4022 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4023 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4024 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4025 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4026
4027 if (instance->ctrl_context && !instance->mask_interrupts)
4028 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4029 else
4030 ret = megasas_issue_polled(instance, cmd);
4031
4032 switch (ret) {
4033 case DCMD_SUCCESS:
4034 instance->pd_list[device_id].interface =
4035 instance->pd_info->state.ddf.pdType.intf;
4036 break;
4037
4038 case DCMD_TIMEOUT:
4039
4040 switch (dcmd_timeout_ocr_possible(instance)) {
4041 case INITIATE_OCR:
4042 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4043 megasas_reset_fusion(instance->host,
4044 MFI_IO_TIMEOUT_OCR);
4045 break;
4046 case KILL_ADAPTER:
4047 megaraid_sas_kill_hba(instance);
4048 break;
4049 case IGNORE_TIMEOUT:
4050 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4051 __func__, __LINE__);
4052 break;
4053 }
4054
4055 break;
4056 }
4057
4058 if (ret != DCMD_TIMEOUT)
4059 megasas_return_cmd(instance, cmd);
4060
4061 return ret;
4062}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304063/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004064 * megasas_get_pd_list_info - Returns FW's pd_list structure
4065 * @instance: Adapter soft state
4066 * @pd_list: pd_list structure
4067 *
4068 * Issues an internal command (DCMD) to get the FW's controller PD
4069 * list structure. This information is mainly used to find out SYSTEM
4070 * supported by the FW.
4071 */
4072static int
4073megasas_get_pd_list(struct megasas_instance *instance)
4074{
4075 int ret = 0, pd_index = 0;
4076 struct megasas_cmd *cmd;
4077 struct megasas_dcmd_frame *dcmd;
4078 struct MR_PD_LIST *ci;
4079 struct MR_PD_ADDRESS *pd_addr;
4080 dma_addr_t ci_h = 0;
4081
Sumit Saxenad9083162016-07-08 03:30:16 -07004082 if (instance->pd_list_not_supported) {
4083 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4084 "not supported by firmware\n");
4085 return ret;
4086 }
4087
Yang, Bo81e403c2009-10-06 14:27:54 -06004088 cmd = megasas_get_cmd(instance);
4089
4090 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004091 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004092 return -ENOMEM;
4093 }
4094
4095 dcmd = &cmd->frame->dcmd;
4096
4097 ci = pci_alloc_consistent(instance->pdev,
4098 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4099
4100 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004101 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004102 megasas_return_cmd(instance, cmd);
4103 return -ENOMEM;
4104 }
4105
4106 memset(ci, 0, sizeof(*ci));
4107 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4108
4109 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4110 dcmd->mbox.b[1] = 0;
4111 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304112 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004113 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304114 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004115 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004116 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304117 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4118 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4119 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4120 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004121
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304122 if (instance->ctrl_context && !instance->mask_interrupts)
4123 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304124 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304125 else
4126 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004127
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304128 switch (ret) {
4129 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004130 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4131 "failed/not supported by firmware\n");
4132
4133 if (instance->ctrl_context)
4134 megaraid_sas_kill_hba(instance);
4135 else
4136 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304137 break;
4138 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004139
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304140 switch (dcmd_timeout_ocr_possible(instance)) {
4141 case INITIATE_OCR:
4142 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4143 /*
4144 * DCMD failed from AEN path.
4145 * AEN path already hold reset_mutex to avoid PCI access
4146 * while OCR is in progress.
4147 */
4148 mutex_unlock(&instance->reset_mutex);
4149 megasas_reset_fusion(instance->host,
4150 MFI_IO_TIMEOUT_OCR);
4151 mutex_lock(&instance->reset_mutex);
4152 break;
4153 case KILL_ADAPTER:
4154 megaraid_sas_kill_hba(instance);
4155 break;
4156 case IGNORE_TIMEOUT:
4157 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4158 __func__, __LINE__);
4159 break;
4160 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004161
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304162 break;
4163
4164 case DCMD_SUCCESS:
4165 pd_addr = ci->addr;
4166
4167 if ((le32_to_cpu(ci->count) >
4168 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4169 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004170
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304171 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304172 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004173
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304174 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304175 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304176 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304177 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304178 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304179 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304180 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004181 pd_addr++;
4182 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304183
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304184 memcpy(instance->pd_list, instance->local_pd_list,
4185 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304186 break;
4187
Yang, Bo81e403c2009-10-06 14:27:54 -06004188 }
4189
4190 pci_free_consistent(instance->pdev,
4191 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4192 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304193
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304194 if (ret != DCMD_TIMEOUT)
4195 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004196
4197 return ret;
4198}
4199
Yang, Bobdc6fb82009-12-06 08:30:19 -07004200/*
4201 * megasas_get_ld_list_info - Returns FW's ld_list structure
4202 * @instance: Adapter soft state
4203 * @ld_list: ld_list structure
4204 *
4205 * Issues an internal command (DCMD) to get the FW's controller PD
4206 * list structure. This information is mainly used to find out SYSTEM
4207 * supported by the FW.
4208 */
4209static int
4210megasas_get_ld_list(struct megasas_instance *instance)
4211{
4212 int ret = 0, ld_index = 0, ids = 0;
4213 struct megasas_cmd *cmd;
4214 struct megasas_dcmd_frame *dcmd;
4215 struct MR_LD_LIST *ci;
4216 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304217 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004218
4219 cmd = megasas_get_cmd(instance);
4220
4221 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004222 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004223 return -ENOMEM;
4224 }
4225
4226 dcmd = &cmd->frame->dcmd;
4227
4228 ci = pci_alloc_consistent(instance->pdev,
4229 sizeof(struct MR_LD_LIST),
4230 &ci_h);
4231
4232 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004233 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004234 megasas_return_cmd(instance, cmd);
4235 return -ENOMEM;
4236 }
4237
4238 memset(ci, 0, sizeof(*ci));
4239 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4240
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304241 if (instance->supportmax256vd)
4242 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004243 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304244 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004245 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304246 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004247 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304248 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4249 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4250 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4251 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004252 dcmd->pad_0 = 0;
4253
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304254 if (instance->ctrl_context && !instance->mask_interrupts)
4255 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304256 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304257 else
4258 ret = megasas_issue_polled(instance, cmd);
4259
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304260 ld_count = le32_to_cpu(ci->ldCount);
4261
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304262 switch (ret) {
4263 case DCMD_FAILED:
4264 megaraid_sas_kill_hba(instance);
4265 break;
4266 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004267
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304268 switch (dcmd_timeout_ocr_possible(instance)) {
4269 case INITIATE_OCR:
4270 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4271 /*
4272 * DCMD failed from AEN path.
4273 * AEN path already hold reset_mutex to avoid PCI access
4274 * while OCR is in progress.
4275 */
4276 mutex_unlock(&instance->reset_mutex);
4277 megasas_reset_fusion(instance->host,
4278 MFI_IO_TIMEOUT_OCR);
4279 mutex_lock(&instance->reset_mutex);
4280 break;
4281 case KILL_ADAPTER:
4282 megaraid_sas_kill_hba(instance);
4283 break;
4284 case IGNORE_TIMEOUT:
4285 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4286 __func__, __LINE__);
4287 break;
4288 }
4289
4290 break;
4291
4292 case DCMD_SUCCESS:
4293 if (ld_count > instance->fw_supported_vd_count)
4294 break;
4295
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304296 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004297
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304298 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004299 if (ci->ldList[ld_index].state != 0) {
4300 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304301 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004302 }
4303 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304304
4305 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004306 }
4307
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304308 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004309
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304310 if (ret != DCMD_TIMEOUT)
4311 megasas_return_cmd(instance, cmd);
4312
Yang, Bobdc6fb82009-12-06 08:30:19 -07004313 return ret;
4314}
4315
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004316/**
adam radford21c9e162013-09-06 15:27:14 -07004317 * megasas_ld_list_query - Returns FW's ld_list structure
4318 * @instance: Adapter soft state
4319 * @ld_list: ld_list structure
4320 *
4321 * Issues an internal command (DCMD) to get the FW's controller PD
4322 * list structure. This information is mainly used to find out SYSTEM
4323 * supported by the FW.
4324 */
4325static int
4326megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4327{
4328 int ret = 0, ld_index = 0, ids = 0;
4329 struct megasas_cmd *cmd;
4330 struct megasas_dcmd_frame *dcmd;
4331 struct MR_LD_TARGETID_LIST *ci;
4332 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304333 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004334
4335 cmd = megasas_get_cmd(instance);
4336
4337 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004338 dev_warn(&instance->pdev->dev,
4339 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004340 return -ENOMEM;
4341 }
4342
4343 dcmd = &cmd->frame->dcmd;
4344
4345 ci = pci_alloc_consistent(instance->pdev,
4346 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4347
4348 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004349 dev_warn(&instance->pdev->dev,
4350 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004351 megasas_return_cmd(instance, cmd);
4352 return -ENOMEM;
4353 }
4354
4355 memset(ci, 0, sizeof(*ci));
4356 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4357
4358 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304359 if (instance->supportmax256vd)
4360 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004361
4362 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304363 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004364 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304365 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004366 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304367 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4368 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4369 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4370 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004371 dcmd->pad_0 = 0;
4372
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304373 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304374 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304375 else
4376 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004377
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304378 switch (ret) {
4379 case DCMD_FAILED:
4380 dev_info(&instance->pdev->dev,
4381 "DCMD not supported by firmware - %s %d\n",
4382 __func__, __LINE__);
4383 ret = megasas_get_ld_list(instance);
4384 break;
4385 case DCMD_TIMEOUT:
4386 switch (dcmd_timeout_ocr_possible(instance)) {
4387 case INITIATE_OCR:
4388 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4389 /*
4390 * DCMD failed from AEN path.
4391 * AEN path already hold reset_mutex to avoid PCI access
4392 * while OCR is in progress.
4393 */
4394 mutex_unlock(&instance->reset_mutex);
4395 megasas_reset_fusion(instance->host,
4396 MFI_IO_TIMEOUT_OCR);
4397 mutex_lock(&instance->reset_mutex);
4398 break;
4399 case KILL_ADAPTER:
4400 megaraid_sas_kill_hba(instance);
4401 break;
4402 case IGNORE_TIMEOUT:
4403 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4404 __func__, __LINE__);
4405 break;
4406 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304407
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304408 break;
4409 case DCMD_SUCCESS:
4410 tgtid_count = le32_to_cpu(ci->count);
4411
4412 if ((tgtid_count > (instance->fw_supported_vd_count)))
4413 break;
4414
adam radford21c9e162013-09-06 15:27:14 -07004415 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304416 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004417 ids = ci->targetId[ld_index];
4418 instance->ld_ids[ids] = ci->targetId[ld_index];
4419 }
4420
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304421 break;
adam radford21c9e162013-09-06 15:27:14 -07004422 }
4423
4424 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304425 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004426
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304427 if (ret != DCMD_TIMEOUT)
4428 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004429
4430 return ret;
4431}
4432
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304433/*
4434 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4435 * instance : Controller's instance
4436*/
4437static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4438{
4439 struct fusion_context *fusion;
4440 u32 old_map_sz;
4441 u32 new_map_sz;
4442
4443 fusion = instance->ctrl_context;
4444 /* For MFI based controllers return dummy success */
4445 if (!fusion)
4446 return;
4447
4448 instance->supportmax256vd =
4449 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4450 /* Below is additional check to address future FW enhancement */
4451 if (instance->ctrl_info->max_lds > 64)
4452 instance->supportmax256vd = 1;
4453
4454 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4455 * MEGASAS_MAX_DEV_PER_CHANNEL;
4456 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4457 * MEGASAS_MAX_DEV_PER_CHANNEL;
4458 if (instance->supportmax256vd) {
4459 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4460 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4461 } else {
4462 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4463 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4464 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304465
4466 dev_info(&instance->pdev->dev,
4467 "firmware type\t: %s\n",
4468 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4469 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304470
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004471 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304472 (sizeof(struct MR_LD_SPAN_MAP) *
4473 (instance->fw_supported_vd_count - 1));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004474 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4475 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304476 (sizeof(struct MR_LD_SPAN_MAP) *
4477 (instance->drv_supported_vd_count - 1));
4478
4479 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4480
4481
4482 if (instance->supportmax256vd)
4483 fusion->current_map_sz = new_map_sz;
4484 else
4485 fusion->current_map_sz = old_map_sz;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304486}
4487
adam radford21c9e162013-09-06 15:27:14 -07004488/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004489 * megasas_get_controller_info - Returns FW's controller structure
4490 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004491 *
4492 * Issues an internal command (DCMD) to get the FW's controller structure.
4493 * This information is mainly used to find out the maximum IO transfer per
4494 * command supported by the FW.
4495 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304496int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304497megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004498{
4499 int ret = 0;
4500 struct megasas_cmd *cmd;
4501 struct megasas_dcmd_frame *dcmd;
4502 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304503 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004504 dma_addr_t ci_h = 0;
4505
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304506 ctrl_info = instance->ctrl_info;
4507
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004508 cmd = megasas_get_cmd(instance);
4509
4510 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004511 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004512 return -ENOMEM;
4513 }
4514
4515 dcmd = &cmd->frame->dcmd;
4516
4517 ci = pci_alloc_consistent(instance->pdev,
4518 sizeof(struct megasas_ctrl_info), &ci_h);
4519
4520 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004521 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004522 megasas_return_cmd(instance, cmd);
4523 return -ENOMEM;
4524 }
4525
4526 memset(ci, 0, sizeof(*ci));
4527 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4528
4529 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304530 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004531 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304532 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004533 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004534 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304535 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4536 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4537 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4538 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304539 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004540
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304541 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304542 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304543 else
4544 ret = megasas_issue_polled(instance, cmd);
4545
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304546 switch (ret) {
4547 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004548 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304549 /* Save required controller information in
4550 * CPU endianness format.
4551 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304552 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4553 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4554 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304555
4556 /* Update the latest Ext VD info.
4557 * From Init path, store current firmware details.
4558 * From OCR path, detect any firmware properties changes.
4559 * in case of Firmware upgrade without system reboot.
4560 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304561 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304562 instance->use_seqnum_jbod_fp =
4563 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304564
4565 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304566 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4567 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304568 "controller type\t: %s(%dMB)\n",
4569 instance->is_imr ? "iMR" : "MR",
4570 le16_to_cpu(ctrl_info->memory_size));
4571
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304572 instance->disableOnlineCtrlReset =
4573 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304574 instance->secure_jbod_support =
4575 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304576 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4577 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304578 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4579 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304580 break;
4581
4582 case DCMD_TIMEOUT:
4583 switch (dcmd_timeout_ocr_possible(instance)) {
4584 case INITIATE_OCR:
4585 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4586 megasas_reset_fusion(instance->host,
4587 MFI_IO_TIMEOUT_OCR);
4588 break;
4589 case KILL_ADAPTER:
4590 megaraid_sas_kill_hba(instance);
4591 break;
4592 case IGNORE_TIMEOUT:
4593 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4594 __func__, __LINE__);
4595 break;
4596 }
4597 case DCMD_FAILED:
4598 megaraid_sas_kill_hba(instance);
4599 break;
4600
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304601 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004602
4603 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4604 ci, ci_h);
4605
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304606 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304607
4608
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004609 return ret;
4610}
4611
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304612/*
4613 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4614 * to firmware
4615 *
4616 * @instance: Adapter soft state
4617 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4618 MR_CRASH_BUF_TURN_OFF = 0
4619 MR_CRASH_BUF_TURN_ON = 1
4620 * @return 0 on success non-zero on failure.
4621 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4622 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4623 * that driver supports crash dump feature. This DCMD will be sent only if
4624 * crash dump feature is supported by the FW.
4625 *
4626 */
4627int megasas_set_crash_dump_params(struct megasas_instance *instance,
4628 u8 crash_buf_state)
4629{
4630 int ret = 0;
4631 struct megasas_cmd *cmd;
4632 struct megasas_dcmd_frame *dcmd;
4633
4634 cmd = megasas_get_cmd(instance);
4635
4636 if (!cmd) {
4637 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4638 return -ENOMEM;
4639 }
4640
4641
4642 dcmd = &cmd->frame->dcmd;
4643
4644 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4645 dcmd->mbox.b[0] = crash_buf_state;
4646 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304647 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304648 dcmd->sge_count = 1;
4649 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4650 dcmd->timeout = 0;
4651 dcmd->pad_0 = 0;
4652 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4653 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4654 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4655 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4656
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304657 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304658 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304659 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304660 ret = megasas_issue_polled(instance, cmd);
4661
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304662 if (ret == DCMD_TIMEOUT) {
4663 switch (dcmd_timeout_ocr_possible(instance)) {
4664 case INITIATE_OCR:
4665 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4666 megasas_reset_fusion(instance->host,
4667 MFI_IO_TIMEOUT_OCR);
4668 break;
4669 case KILL_ADAPTER:
4670 megaraid_sas_kill_hba(instance);
4671 break;
4672 case IGNORE_TIMEOUT:
4673 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4674 __func__, __LINE__);
4675 break;
4676 }
4677 } else
4678 megasas_return_cmd(instance, cmd);
4679
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304680 return ret;
4681}
4682
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004683/**
bo yang31ea7082007-11-07 12:09:50 -05004684 * megasas_issue_init_mfi - Initializes the FW
4685 * @instance: Adapter soft state
4686 *
4687 * Issues the INIT MFI cmd
4688 */
4689static int
4690megasas_issue_init_mfi(struct megasas_instance *instance)
4691{
Christoph Hellwig9ab9ed32015-04-23 16:32:54 +05304692 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004693 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004694 struct megasas_init_frame *init_frame;
4695 struct megasas_init_queue_info *initq_info;
4696 dma_addr_t init_frame_h;
4697 dma_addr_t initq_info_h;
4698
4699 /*
4700 * Prepare a init frame. Note the init frame points to queue info
4701 * structure. Each frame has SGL allocated after first 64 bytes. For
4702 * this frame - since we don't need any SGL - we use SGL's space as
4703 * queue info structure
4704 *
4705 * We will not get a NULL command below. We just created the pool.
4706 */
4707 cmd = megasas_get_cmd(instance);
4708
4709 init_frame = (struct megasas_init_frame *)cmd->frame;
4710 initq_info = (struct megasas_init_queue_info *)
4711 ((unsigned long)init_frame + 64);
4712
4713 init_frame_h = cmd->frame_phys_addr;
4714 initq_info_h = init_frame_h + 64;
4715
4716 context = init_frame->context;
4717 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4718 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4719 init_frame->context = context;
4720
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304721 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4722 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004723
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304724 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4725 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004726
4727 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304728 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304729 init_frame->queue_info_new_phys_addr_lo =
4730 cpu_to_le32(lower_32_bits(initq_info_h));
4731 init_frame->queue_info_new_phys_addr_hi =
4732 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004733
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304734 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004735
4736 /*
4737 * disable the intr before firing the init frame to FW
4738 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304739 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004740
4741 /*
4742 * Issue the init frame in polled mode
4743 */
4744
4745 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004746 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004747 megasas_return_cmd(instance, cmd);
4748 goto fail_fw_init;
4749 }
4750
4751 megasas_return_cmd(instance, cmd);
4752
4753 return 0;
4754
4755fail_fw_init:
4756 return -EINVAL;
4757}
4758
adam radfordcd50ba82010-12-21 10:23:23 -08004759static u32
4760megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004761{
adam radfordcd50ba82010-12-21 10:23:23 -08004762 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004763 u32 context_sz;
4764 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004765
4766 reg_set = instance->reg_set;
4767
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004768 /*
4769 * Get various operational parameters from status register
4770 */
Sumant Patro1341c932006-01-25 12:02:40 -08004771 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004772 /*
4773 * Reduce the max supported cmds by 1. This is to ensure that the
4774 * reply_q_sz (1 more than the max cmd that driver may send)
4775 * does not exceed max cmds that the FW can support
4776 */
4777 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004778 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004779 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004780 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004781 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304782 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4783 * are reserved for IOCTL + driver's internal DCMDs.
4784 */
4785 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4786 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4787 instance->max_scsi_cmds = (instance->max_fw_cmds -
4788 MEGASAS_SKINNY_INT_CMDS);
4789 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4790 } else {
4791 instance->max_scsi_cmds = (instance->max_fw_cmds -
4792 MEGASAS_INT_CMDS);
4793 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4794 }
4795
Sumit Saxena308ec452016-01-28 21:04:30 +05304796 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304797 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004798 * Create a pool of commands
4799 */
4800 if (megasas_alloc_cmds(instance))
4801 goto fail_alloc_cmds;
4802
4803 /*
4804 * Allocate memory for reply queue. Length of reply queue should
4805 * be _one_ more than the maximum commands handled by the firmware.
4806 *
4807 * Note: When FW completes commands, it places corresponding contex
4808 * values in this circular reply queue. This circular queue is a fairly
4809 * typical producer-consumer queue. FW is the producer (of completed
4810 * commands) and the driver is the consumer.
4811 */
4812 context_sz = sizeof(u32);
4813 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4814
4815 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4816 reply_q_sz,
4817 &instance->reply_queue_h);
4818
4819 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004820 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004821 goto fail_reply_queue;
4822 }
4823
bo yang31ea7082007-11-07 12:09:50 -05004824 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004825 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004826
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304827 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304828 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4829 "Fail from %s %d\n", instance->unique_id,
4830 __func__, __LINE__);
4831 goto fail_fw_init;
4832 }
4833
bo yang39a98552010-09-22 22:36:29 -04004834 instance->fw_support_ieee = 0;
4835 instance->fw_support_ieee =
4836 (instance->instancet->read_fw_status_reg(reg_set) &
4837 0x04000000);
4838
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004839 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004840 instance->fw_support_ieee);
4841
4842 if (instance->fw_support_ieee)
4843 instance->flag_ieee = 1;
4844
adam radfordcd50ba82010-12-21 10:23:23 -08004845 return 0;
4846
4847fail_fw_init:
4848
4849 pci_free_consistent(instance->pdev, reply_q_sz,
4850 instance->reply_queue, instance->reply_queue_h);
4851fail_reply_queue:
4852 megasas_free_cmds(instance);
4853
4854fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004855 return 1;
4856}
4857
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304858/*
4859 * megasas_setup_irqs_msix - register legacy interrupts.
4860 * @instance: Adapter soft state
4861 *
4862 * Do not enable interrupt, only setup ISRs.
4863 *
4864 * Return 0 on success.
4865 */
4866static int
4867megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4868{
4869 struct pci_dev *pdev;
4870
4871 pdev = instance->pdev;
4872 instance->irq_context[0].instance = instance;
4873 instance->irq_context[0].MSIxIndex = 0;
4874 if (request_irq(pdev->irq, instance->instancet->service_isr,
4875 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4876 dev_err(&instance->pdev->dev,
4877 "Failed to register IRQ from %s %d\n",
4878 __func__, __LINE__);
4879 return -1;
4880 }
4881 return 0;
4882}
4883
4884/**
4885 * megasas_setup_irqs_msix - register MSI-x interrupts.
4886 * @instance: Adapter soft state
4887 * @is_probe: Driver probe check
4888 *
4889 * Do not enable interrupt, only setup ISRs.
4890 *
4891 * Return 0 on success.
4892 */
4893static int
4894megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4895{
4896 int i, j, cpu;
4897 struct pci_dev *pdev;
4898
4899 pdev = instance->pdev;
4900
4901 /* Try MSI-x */
4902 cpu = cpumask_first(cpu_online_mask);
4903 for (i = 0; i < instance->msix_vectors; i++) {
4904 instance->irq_context[i].instance = instance;
4905 instance->irq_context[i].MSIxIndex = i;
4906 if (request_irq(instance->msixentry[i].vector,
4907 instance->instancet->service_isr, 0, "megasas",
4908 &instance->irq_context[i])) {
4909 dev_err(&instance->pdev->dev,
4910 "Failed to register IRQ for vector %d.\n", i);
4911 for (j = 0; j < i; j++) {
4912 if (smp_affinity_enable)
4913 irq_set_affinity_hint(
4914 instance->msixentry[j].vector, NULL);
4915 free_irq(instance->msixentry[j].vector,
4916 &instance->irq_context[j]);
4917 }
4918 /* Retry irq register for IO_APIC*/
4919 instance->msix_vectors = 0;
4920 if (is_probe)
4921 return megasas_setup_irqs_ioapic(instance);
4922 else
4923 return -1;
4924 }
4925 if (smp_affinity_enable) {
4926 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4927 get_cpu_mask(cpu)))
4928 dev_err(&instance->pdev->dev,
4929 "Failed to set affinity hint"
4930 " for cpu %d\n", cpu);
4931 cpu = cpumask_next(cpu, cpu_online_mask);
4932 }
4933 }
4934 return 0;
4935}
4936
4937/*
4938 * megasas_destroy_irqs- unregister interrupts.
4939 * @instance: Adapter soft state
4940 * return: void
4941 */
4942static void
4943megasas_destroy_irqs(struct megasas_instance *instance) {
4944
4945 int i;
4946
4947 if (instance->msix_vectors)
4948 for (i = 0; i < instance->msix_vectors; i++) {
4949 if (smp_affinity_enable)
4950 irq_set_affinity_hint(
4951 instance->msixentry[i].vector, NULL);
4952 free_irq(instance->msixentry[i].vector,
4953 &instance->irq_context[i]);
4954 }
4955 else
4956 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4957}
4958
adam radfordcd50ba82010-12-21 10:23:23 -08004959/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304960 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4961 * @instance: Adapter soft state
4962 * @is_probe: Driver probe check
4963 *
4964 * Return 0 on success.
4965 */
4966void
4967megasas_setup_jbod_map(struct megasas_instance *instance)
4968{
4969 int i;
4970 struct fusion_context *fusion = instance->ctrl_context;
4971 u32 pd_seq_map_sz;
4972
4973 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4974 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4975
4976 if (reset_devices || !fusion ||
4977 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4978 dev_info(&instance->pdev->dev,
4979 "Jbod map is not supported %s %d\n",
4980 __func__, __LINE__);
4981 instance->use_seqnum_jbod_fp = false;
4982 return;
4983 }
4984
4985 if (fusion->pd_seq_sync[0])
4986 goto skip_alloc;
4987
4988 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4989 fusion->pd_seq_sync[i] = dma_alloc_coherent
4990 (&instance->pdev->dev, pd_seq_map_sz,
4991 &fusion->pd_seq_phys[i], GFP_KERNEL);
4992 if (!fusion->pd_seq_sync[i]) {
4993 dev_err(&instance->pdev->dev,
4994 "Failed to allocate memory from %s %d\n",
4995 __func__, __LINE__);
4996 if (i == 1) {
4997 dma_free_coherent(&instance->pdev->dev,
4998 pd_seq_map_sz, fusion->pd_seq_sync[0],
4999 fusion->pd_seq_phys[0]);
5000 fusion->pd_seq_sync[0] = NULL;
5001 }
5002 instance->use_seqnum_jbod_fp = false;
5003 return;
5004 }
5005 }
5006
5007skip_alloc:
5008 if (!megasas_sync_pd_seq_num(instance, false) &&
5009 !megasas_sync_pd_seq_num(instance, true))
5010 instance->use_seqnum_jbod_fp = true;
5011 else
5012 instance->use_seqnum_jbod_fp = false;
5013}
5014
5015/**
adam radfordcd50ba82010-12-21 10:23:23 -08005016 * megasas_init_fw - Initializes the FW
5017 * @instance: Adapter soft state
5018 *
5019 * This is the main function for initializing firmware
5020 */
5021
5022static int megasas_init_fw(struct megasas_instance *instance)
5023{
5024 u32 max_sectors_1;
5025 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305026 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005027 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005028 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305029 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005030 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305031 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005032 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305033 struct fusion_context *fusion;
5034
5035 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005036
5037 /* Find first memory bar */
5038 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5039 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
Yinghai Lue7f85162016-08-05 23:37:34 -07005040 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005041 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005042 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005043 return -EBUSY;
5044 }
5045
Ben Collins11f8a7b2013-09-13 12:46:44 -04005046 base_addr = pci_resource_start(instance->pdev, instance->bar);
5047 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005048
5049 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005050 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005051 goto fail_ioremap;
5052 }
5053
5054 reg_set = instance->reg_set;
5055
5056 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08005057 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005058 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005059 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305060 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305061 case PCI_DEVICE_ID_LSI_INTRUDER:
5062 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305063 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5064 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005065 instance->instancet = &megasas_instance_template_fusion;
5066 break;
adam radfordcd50ba82010-12-21 10:23:23 -08005067 case PCI_DEVICE_ID_LSI_SAS1078R:
5068 case PCI_DEVICE_ID_LSI_SAS1078DE:
5069 instance->instancet = &megasas_instance_template_ppc;
5070 break;
5071 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5072 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5073 instance->instancet = &megasas_instance_template_gen2;
5074 break;
5075 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5076 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5077 instance->instancet = &megasas_instance_template_skinny;
5078 break;
5079 case PCI_DEVICE_ID_LSI_SAS1064R:
5080 case PCI_DEVICE_ID_DELL_PERC5:
5081 default:
5082 instance->instancet = &megasas_instance_template_xscale;
5083 break;
5084 }
5085
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305086 if (megasas_transition_to_ready(instance, 0)) {
5087 atomic_set(&instance->fw_reset_no_pci_access, 1);
5088 instance->instancet->adp_reset
5089 (instance, instance->reg_set);
5090 atomic_set(&instance->fw_reset_no_pci_access, 0);
5091 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005092 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305093 __func__);
5094
5095 /*waitting for about 30 second before retry*/
5096 ssleep(30);
5097
5098 if (megasas_transition_to_ready(instance, 0))
5099 goto fail_ready_state;
5100 }
adam radfordcd50ba82010-12-21 10:23:23 -08005101
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305102 /*
5103 * MSI-X host index 0 is common for all adapter.
5104 * It is used for all MPT based Adapters.
5105 */
5106 instance->reply_post_host_index_addr[0] =
Christoph Hellwig8a232bb2015-04-23 16:32:39 +05305107 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305108 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5109
adam radford3f1abce2011-05-11 18:33:47 -07005110 /* Check if MSI-X is supported while in ready state */
5111 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5112 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005113 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305114 scratch_pad_2 = readl
5115 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005116 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305117 if (fusion) {
5118 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5119 instance->msix_vectors = (scratch_pad_2
5120 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5121 fw_msix_count = instance->msix_vectors;
5122 } else { /* Invader series supports more than 8 MSI-x vectors*/
5123 instance->msix_vectors = ((scratch_pad_2
5124 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5125 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sumit Saxena179ac142016-01-28 21:04:28 +05305126 if (rdpq_enable)
5127 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5128 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305129 fw_msix_count = instance->msix_vectors;
5130 /* Save 1-15 reply post index address to local memory
5131 * Index 0 is already saved from reg offset
5132 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5133 */
5134 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5135 instance->reply_post_host_index_addr[loop] =
5136 (u32 __iomem *)
5137 ((u8 __iomem *)instance->reg_set +
5138 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5139 + (loop * 0x10));
5140 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305141 }
5142 if (msix_vectors)
5143 instance->msix_vectors = min(msix_vectors,
5144 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305145 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005146 instance->msix_vectors = 1;
5147 /* Don't bother allocating more MSI-X vectors than cpus */
5148 instance->msix_vectors = min(instance->msix_vectors,
5149 (unsigned int)num_online_cpus());
5150 for (i = 0; i < instance->msix_vectors; i++)
5151 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005152 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
5153 1, instance->msix_vectors);
Jiang Liuc12de882014-11-03 20:44:20 +08005154 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005155 instance->msix_vectors = i;
5156 else
adam radfordc8e858f2011-10-08 18:15:13 -07005157 instance->msix_vectors = 0;
5158 }
adam radford3f1abce2011-05-11 18:33:47 -07005159
Tomas Henzl258c3af2015-06-02 16:09:46 +05305160 dev_info(&instance->pdev->dev,
5161 "firmware supports msix\t: (%d)", fw_msix_count);
5162 dev_info(&instance->pdev->dev,
5163 "current msix/online cpus\t: (%d/%d)\n",
5164 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305165 dev_info(&instance->pdev->dev,
5166 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305167
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305168 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5169 (unsigned long)instance);
5170
Tomas Henzl258c3af2015-06-02 16:09:46 +05305171 if (instance->msix_vectors ?
5172 megasas_setup_irqs_msix(instance, 1) :
5173 megasas_setup_irqs_ioapic(instance))
5174 goto fail_setup_irqs;
5175
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305176 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5177 GFP_KERNEL);
5178 if (instance->ctrl_info == NULL)
5179 goto fail_init_adapter;
5180
5181 /*
5182 * Below are default value for legacy Firmware.
5183 * non-fusion based controllers
5184 */
5185 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5186 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005187 /* Get operational params, sge flags, send init cmd to controller */
5188 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005189 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005190
Tomas Henzl258c3af2015-06-02 16:09:46 +05305191
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305192 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005193
Andy Lutomirski13f30772016-05-03 10:24:31 -07005194 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005195
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305196 megasas_setup_jbod_map(instance);
5197
bo yang39a98552010-09-22 22:36:29 -04005198 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005199 * the following function will get the PD LIST.
5200 */
5201 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005202 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005203 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005204 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305205 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005206 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005207
Yang, Bobdc6fb82009-12-06 08:30:19 -07005208 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07005209 if (megasas_ld_list_query(instance,
5210 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5211 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07005212
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005213 /*
5214 * Compute the max allowed sectors per IO: The controller info has two
5215 * limits on max sectors. Driver should use the minimum of these two.
5216 *
5217 * 1 << stripe_sz_ops.min = max sectors per strip
5218 *
5219 * Note that older firmwares ( < FW ver 30) didn't report information
5220 * to calculate max_sectors_1. So the number ended up as zero always.
5221 */
bo yang14faea92007-11-09 04:14:00 -05005222 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305223 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005224
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305225 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5226 le16_to_cpu(ctrl_info->max_strips_per_io);
5227 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005228
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005229 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305230
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305231 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5232 instance->passive = ctrl_info->cluster.passive;
5233 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305234 instance->UnevenSpanSupport =
5235 ctrl_info->adapterOperations2.supportUnevenSpans;
5236 if (instance->UnevenSpanSupport) {
5237 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305238 if (MR_ValidateMapInfo(instance))
5239 fusion->fast_path_io = 1;
5240 else
5241 fusion->fast_path_io = 0;
5242
5243 }
5244 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305245 instance->requestorId = ctrl_info->iov.requestorId;
5246 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5247 if (!ctrl_info->adapterOperations2.activePassive)
5248 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305249
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305250 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5251 instance->PlasmaFW111 ? "1.11" : "new");
5252
5253 if (instance->PlasmaFW111) {
5254 iovPtr = (struct IOV_111 *)
5255 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5256 instance->requestorId = iovPtr->requestorId;
5257 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305258 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305259 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5260 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305261 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305262
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305263 instance->crash_dump_fw_support =
5264 ctrl_info->adapterOperations3.supportCrashDump;
5265 instance->crash_dump_drv_support =
5266 (instance->crash_dump_fw_support &&
5267 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305268 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305269 megasas_set_crash_dump_params(instance,
5270 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005271
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305272 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305273 if (instance->crash_dump_buf)
5274 pci_free_consistent(instance->pdev,
5275 CRASH_DMA_BUF_SIZE,
5276 instance->crash_dump_buf,
5277 instance->crash_dump_h);
5278 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005279 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305280
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305281
5282 dev_info(&instance->pdev->dev,
5283 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5284 le16_to_cpu(ctrl_info->pci.vendor_id),
5285 le16_to_cpu(ctrl_info->pci.device_id),
5286 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5287 le16_to_cpu(ctrl_info->pci.sub_device_id));
5288 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5289 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305290 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5291 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305292 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5293 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305294
5295
bo yang14faea92007-11-09 04:14:00 -05005296 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305297 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005298 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5299 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005300
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305301 /* Check for valid throttlequeuedepth module parameter */
5302 if (throttlequeuedepth &&
5303 throttlequeuedepth <= instance->max_scsi_cmds)
5304 instance->throttlequeuedepth = throttlequeuedepth;
5305 else
5306 instance->throttlequeuedepth =
5307 MEGASAS_THROTTLE_QUEUE_DEPTH;
5308
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305309 if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
5310 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5311
5312 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5313 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005314
adam radford229fe472014-03-10 02:51:56 -07005315 /* Launch SR-IOV heartbeat timer */
5316 if (instance->requestorId) {
5317 if (!megasas_sriov_start_heartbeat(instance, 1))
5318 megasas_start_timer(instance,
5319 &instance->sriov_heartbeat_timer,
5320 megasas_sriov_heartbeat_handler,
5321 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5322 else
5323 instance->skip_heartbeat_timer_del = 1;
5324 }
5325
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005326 return 0;
5327
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305328fail_get_pd_list:
5329 instance->instancet->disable_intr(instance);
adam radfordeb1b1232011-02-24 20:55:56 -08005330fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305331 megasas_destroy_irqs(instance);
5332fail_setup_irqs:
5333 if (instance->msix_vectors)
5334 pci_disable_msix(instance->pdev);
5335 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005336fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305337 kfree(instance->ctrl_info);
5338 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005339 iounmap(instance->reg_set);
5340
5341 fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005342 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005343
5344 return -EINVAL;
5345}
5346
5347/**
5348 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005349 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005350 */
5351static void megasas_release_mfi(struct megasas_instance *instance)
5352{
adam radford9c915a82010-12-21 13:34:31 -08005353 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005354
adam radford9c915a82010-12-21 13:34:31 -08005355 if (instance->reply_queue)
5356 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005357 instance->reply_queue, instance->reply_queue_h);
5358
5359 megasas_free_cmds(instance);
5360
5361 iounmap(instance->reg_set);
5362
Yinghai Lue7f85162016-08-05 23:37:34 -07005363 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005364}
5365
5366/**
5367 * megasas_get_seq_num - Gets latest event sequence numbers
5368 * @instance: Adapter soft state
5369 * @eli: FW event log sequence numbers information
5370 *
5371 * FW maintains a log of all events in a non-volatile area. Upper layers would
5372 * usually find out the latest sequence number of the events, the seq number at
5373 * the boot etc. They would "read" all the events below the latest seq number
5374 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5375 * number), they would subsribe to AEN (asynchronous event notification) and
5376 * wait for the events to happen.
5377 */
5378static int
5379megasas_get_seq_num(struct megasas_instance *instance,
5380 struct megasas_evt_log_info *eli)
5381{
5382 struct megasas_cmd *cmd;
5383 struct megasas_dcmd_frame *dcmd;
5384 struct megasas_evt_log_info *el_info;
5385 dma_addr_t el_info_h = 0;
5386
5387 cmd = megasas_get_cmd(instance);
5388
5389 if (!cmd) {
5390 return -ENOMEM;
5391 }
5392
5393 dcmd = &cmd->frame->dcmd;
5394 el_info = pci_alloc_consistent(instance->pdev,
5395 sizeof(struct megasas_evt_log_info),
5396 &el_info_h);
5397
5398 if (!el_info) {
5399 megasas_return_cmd(instance, cmd);
5400 return -ENOMEM;
5401 }
5402
5403 memset(el_info, 0, sizeof(*el_info));
5404 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5405
5406 dcmd->cmd = MFI_CMD_DCMD;
5407 dcmd->cmd_status = 0x0;
5408 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305409 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005410 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005411 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305412 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5413 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5414 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5415 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005416
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305417 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5418 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305419 /*
5420 * Copy the data back into callers buffer
5421 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305422 eli->newest_seq_num = el_info->newest_seq_num;
5423 eli->oldest_seq_num = el_info->oldest_seq_num;
5424 eli->clear_seq_num = el_info->clear_seq_num;
5425 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5426 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305427 } else
5428 dev_err(&instance->pdev->dev, "DCMD failed "
5429 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005430
5431 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5432 el_info, el_info_h);
5433
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305434 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005435
5436 return 0;
5437}
5438
5439/**
5440 * megasas_register_aen - Registers for asynchronous event notification
5441 * @instance: Adapter soft state
5442 * @seq_num: The starting sequence number
5443 * @class_locale: Class of the event
5444 *
5445 * This function subscribes for AEN for events beyond the @seq_num. It requests
5446 * to be notified if and only if the event is of type @class_locale
5447 */
5448static int
5449megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5450 u32 class_locale_word)
5451{
5452 int ret_val;
5453 struct megasas_cmd *cmd;
5454 struct megasas_dcmd_frame *dcmd;
5455 union megasas_evt_class_locale curr_aen;
5456 union megasas_evt_class_locale prev_aen;
5457
5458 /*
5459 * If there an AEN pending already (aen_cmd), check if the
5460 * class_locale of that pending AEN is inclusive of the new
5461 * AEN request we currently have. If it is, then we don't have
5462 * to do anything. In other words, whichever events the current
5463 * AEN request is subscribing to, have already been subscribed
5464 * to.
5465 *
5466 * If the old_cmd is _not_ inclusive, then we have to abort
5467 * that command, form a class_locale that is superset of both
5468 * old and current and re-issue to the FW
5469 */
5470
5471 curr_aen.word = class_locale_word;
5472
5473 if (instance->aen_cmd) {
5474
Christoph Hellwiga9555532015-04-23 16:34:24 +05305475 prev_aen.word =
5476 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005477
5478 /*
5479 * A class whose enum value is smaller is inclusive of all
5480 * higher values. If a PROGRESS (= -1) was previously
5481 * registered, then a new registration requests for higher
5482 * classes need not be sent to FW. They are automatically
5483 * included.
5484 *
5485 * Locale numbers don't have such hierarchy. They are bitmap
5486 * values
5487 */
5488 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305489 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005490 curr_aen.members.locale)) {
5491 /*
5492 * Previously issued event registration includes
5493 * current request. Nothing to do.
5494 */
5495 return 0;
5496 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305497 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005498
5499 if (prev_aen.members.class < curr_aen.members.class)
5500 curr_aen.members.class = prev_aen.members.class;
5501
5502 instance->aen_cmd->abort_aen = 1;
5503 ret_val = megasas_issue_blocked_abort_cmd(instance,
5504 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305505 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005506
5507 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005508 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005509 "previous AEN command\n");
5510 return ret_val;
5511 }
5512 }
5513 }
5514
5515 cmd = megasas_get_cmd(instance);
5516
5517 if (!cmd)
5518 return -ENOMEM;
5519
5520 dcmd = &cmd->frame->dcmd;
5521
5522 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5523
5524 /*
5525 * Prepare DCMD for aen registration
5526 */
5527 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5528
5529 dcmd->cmd = MFI_CMD_DCMD;
5530 dcmd->cmd_status = 0x0;
5531 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305532 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005533 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005534 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305535 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5536 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5537 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005538 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305539 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5540 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5541 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005542
Yang, Bof4c9a132009-10-06 14:43:28 -06005543 if (instance->aen_cmd != NULL) {
5544 megasas_return_cmd(instance, cmd);
5545 return 0;
5546 }
5547
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005548 /*
5549 * Store reference to the cmd used to register for AEN. When an
5550 * application wants us to register for AEN, we have to abort this
5551 * cmd and re-register with a new EVENT LOCALE supplied by that app
5552 */
5553 instance->aen_cmd = cmd;
5554
5555 /*
5556 * Issue the aen registration frame
5557 */
adam radford9c915a82010-12-21 13:34:31 -08005558 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005559
5560 return 0;
5561}
5562
5563/**
5564 * megasas_start_aen - Subscribes to AEN during driver load time
5565 * @instance: Adapter soft state
5566 */
5567static int megasas_start_aen(struct megasas_instance *instance)
5568{
5569 struct megasas_evt_log_info eli;
5570 union megasas_evt_class_locale class_locale;
5571
5572 /*
5573 * Get the latest sequence number from FW
5574 */
5575 memset(&eli, 0, sizeof(eli));
5576
5577 if (megasas_get_seq_num(instance, &eli))
5578 return -1;
5579
5580 /*
5581 * Register AEN with FW for latest sequence number plus 1
5582 */
5583 class_locale.members.reserved = 0;
5584 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5585 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5586
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305587 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305588 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305589 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005590}
5591
5592/**
5593 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5594 * @instance: Adapter soft state
5595 */
5596static int megasas_io_attach(struct megasas_instance *instance)
5597{
5598 struct Scsi_Host *host = instance->host;
5599
5600 /*
5601 * Export parameters required by SCSI mid-layer
5602 */
5603 host->irq = instance->pdev->irq;
5604 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305605 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005606 host->this_id = instance->init_id;
5607 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005608
5609 if (instance->fw_support_ieee)
5610 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5611
Yang, Bo1fd10682010-10-12 07:18:50 -06005612 /*
5613 * Check if the module parameter value for max_sectors can be used
5614 */
5615 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5616 instance->max_sectors_per_req = max_sectors;
5617 else {
5618 if (max_sectors) {
5619 if (((instance->pdev->device ==
5620 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5621 (instance->pdev->device ==
5622 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5623 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5624 instance->max_sectors_per_req = max_sectors;
5625 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005626 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005627 "and <= %d (or < 1MB for GEN2 controller)\n",
5628 instance->max_sectors_per_req);
5629 }
5630 }
5631 }
5632
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005633 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005634 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005635 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5636 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5637 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005638 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005639
5640 /*
5641 * Notify the mid-layer about the new controller
5642 */
5643 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305644 dev_err(&instance->pdev->dev,
5645 "Failed to add host from %s %d\n",
5646 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005647 return -ENODEV;
5648 }
5649
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005650 return 0;
5651}
5652
bo yang31ea7082007-11-07 12:09:50 -05005653static int
5654megasas_set_dma_mask(struct pci_dev *pdev)
5655{
5656 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005657 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005658 */
5659 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005660 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005661
Yang Hongyang284901a2009-04-06 19:01:15 -07005662 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005663 goto fail_set_dma_mask;
5664 }
5665 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005666 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005667 goto fail_set_dma_mask;
5668 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305669 /*
5670 * Ensure that all data structures are allocated in 32-bit
5671 * memory.
5672 */
5673 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5674 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5675 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5676 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5677 dev_info(&pdev->dev, "set 32bit DMA mask"
5678 "and 32 bit consistent mask\n");
5679 else
5680 goto fail_set_dma_mask;
5681 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305682
bo yang31ea7082007-11-07 12:09:50 -05005683 return 0;
5684
5685fail_set_dma_mask:
5686 return 1;
5687}
5688
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005689/**
5690 * megasas_probe_one - PCI hotplug entry point
5691 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005692 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005693 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005694static int megasas_probe_one(struct pci_dev *pdev,
5695 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005696{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305697 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005698 struct Scsi_Host *host;
5699 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005700 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305701 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005702
5703 /* Reset MSI-X in the kdump kernel */
5704 if (reset_devices) {
5705 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5706 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005707 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005708 &control);
5709 if (control & PCI_MSIX_FLAGS_ENABLE) {
5710 dev_info(&pdev->dev, "resetting MSI-X\n");
5711 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005712 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005713 control &
5714 ~PCI_MSIX_FLAGS_ENABLE);
5715 }
5716 }
5717 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005718
5719 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005720 * PCI prepping: enable device set bus mastering and dma mask
5721 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005722 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005723
5724 if (rval) {
5725 return rval;
5726 }
5727
5728 pci_set_master(pdev);
5729
bo yang31ea7082007-11-07 12:09:50 -05005730 if (megasas_set_dma_mask(pdev))
5731 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005732
5733 host = scsi_host_alloc(&megasas_template,
5734 sizeof(struct megasas_instance));
5735
5736 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005737 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005738 goto fail_alloc_instance;
5739 }
5740
5741 instance = (struct megasas_instance *)host->hostdata;
5742 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005743 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08005744 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005745
adam radford9c915a82010-12-21 13:34:31 -08005746 switch (instance->pdev->device) {
5747 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005748 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005749 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305750 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305751 case PCI_DEVICE_ID_LSI_INTRUDER:
5752 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305753 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5754 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005755 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305756 instance->ctrl_context_pages =
5757 get_order(sizeof(struct fusion_context));
5758 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5759 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005760 if (!instance->ctrl_context) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005761 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005762 "memory for Fusion context info\n");
5763 goto fail_alloc_dma_buf;
5764 }
5765 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305766 memset(fusion, 0,
5767 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305768 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5769 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5770 fusion->adapter_type = THUNDERBOLT_SERIES;
5771 else
5772 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08005773 }
5774 break;
5775 default: /* For all other supported controllers */
5776
5777 instance->producer =
5778 pci_alloc_consistent(pdev, sizeof(u32),
5779 &instance->producer_h);
5780 instance->consumer =
5781 pci_alloc_consistent(pdev, sizeof(u32),
5782 &instance->consumer_h);
5783
5784 if (!instance->producer || !instance->consumer) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005785 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
adam radford9c915a82010-12-21 13:34:31 -08005786 "memory for producer, consumer\n");
5787 goto fail_alloc_dma_buf;
5788 }
5789
5790 *instance->producer = 0;
5791 *instance->consumer = 0;
5792 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005793 }
5794
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305795 /* Crash dump feature related initialisation*/
5796 instance->drv_buf_index = 0;
5797 instance->drv_buf_alloc = 0;
5798 instance->crash_dump_fw_support = 0;
5799 instance->crash_dump_app_support = 0;
5800 instance->fw_crash_state = UNAVAILABLE;
5801 spin_lock_init(&instance->crashdump_lock);
5802 instance->crash_dump_buf = NULL;
5803
Yang, Boc3518832009-10-06 14:18:02 -06005804 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005805 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005806 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005807 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05305808 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305809 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005810
5811 instance->evt_detail = pci_alloc_consistent(pdev,
5812 sizeof(struct
5813 megasas_evt_detail),
5814 &instance->evt_detail_h);
5815
5816 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005817 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005818 "event detail structure\n");
5819 goto fail_alloc_dma_buf;
5820 }
5821
Sumit Saxenac3e385a2016-04-15 00:23:30 -07005822 if (!reset_devices) {
5823 instance->system_info_buf = pci_zalloc_consistent(pdev,
5824 sizeof(struct MR_DRV_SYSTEM_INFO),
5825 &instance->system_info_h);
5826 if (!instance->system_info_buf)
5827 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
Sumit Saxena2216c302016-01-28 21:04:26 +05305828
Sumit Saxenac3e385a2016-04-15 00:23:30 -07005829 instance->pd_info = pci_alloc_consistent(pdev,
5830 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
5831
5832 if (!instance->pd_info)
5833 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
5834
5835 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5836 CRASH_DMA_BUF_SIZE,
5837 &instance->crash_dump_h);
5838 if (!instance->crash_dump_buf)
5839 dev_err(&pdev->dev, "Can't allocate Firmware "
5840 "crash dump DMA buffer\n");
5841 }
Sumit Saxena2216c302016-01-28 21:04:26 +05305842
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005843 /*
5844 * Initialize locks and queues
5845 */
5846 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005847 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005848
Sumant Patroe4a082c2006-05-30 12:03:37 -07005849 atomic_set(&instance->fw_outstanding,0);
5850
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005851 init_waitqueue_head(&instance->int_cmd_wait_q);
5852 init_waitqueue_head(&instance->abort_cmd_wait_q);
5853
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305854 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005855 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005856 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005857
adam radford9c915a82010-12-21 13:34:31 -08005858 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05305859 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005860
5861 /*
5862 * Initialize PCI related and misc parameters
5863 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005864 instance->host = host;
5865 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5866 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305867 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005868
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305869
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005870 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305871 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005872 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005873
Sumant Patro658dced2006-10-03 13:09:14 -07005874 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005875 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005876 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005877 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005878 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305879 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005880
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305881 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005882 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305883 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5884 } else
adam radford9c915a82010-12-21 13:34:31 -08005885 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005886
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005887 /*
adam radford0a770662011-02-24 20:56:12 -08005888 * Initialize MFI Firmware
5889 */
5890 if (megasas_init_fw(instance))
5891 goto fail_init_mfi;
5892
adam radford229fe472014-03-10 02:51:56 -07005893 if (instance->requestorId) {
5894 if (instance->PlasmaFW111) {
5895 instance->vf_affiliation_111 =
5896 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5897 &instance->vf_affiliation_111_h);
5898 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005899 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005900 "memory for VF affiliation buffer\n");
5901 } else {
5902 instance->vf_affiliation =
5903 pci_alloc_consistent(pdev,
5904 (MAX_LOGICAL_DRIVES + 1) *
5905 sizeof(struct MR_LD_VF_AFFILIATION),
5906 &instance->vf_affiliation_h);
5907 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005908 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005909 "memory for VF affiliation buffer\n");
5910 }
5911 }
5912
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005913 /*
5914 * Store instance in PCI softstate
5915 */
5916 pci_set_drvdata(pdev, instance);
5917
5918 /*
5919 * Add this controller to megasas_mgmt_info structure so that it
5920 * can be exported to management applications
5921 */
5922 megasas_mgmt_info.count++;
5923 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5924 megasas_mgmt_info.max_index++;
5925
5926 /*
adam radford541f90b2011-05-11 18:34:29 -07005927 * Register with SCSI mid-layer
5928 */
5929 if (megasas_io_attach(instance))
5930 goto fail_io_attach;
5931
5932 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305933 /*
5934 * Trigger SCSI to scan our drives
5935 */
5936 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005937
5938 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005939 * Initiate AEN (Asynchronous Event Notification)
5940 */
5941 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005942 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005943 goto fail_start_aen;
5944 }
5945
Adam Radford9ea81f82014-07-09 15:17:57 -07005946 /* Get current SR-IOV LD/VF affiliation */
5947 if (instance->requestorId)
5948 megasas_get_ld_vf_affiliation(instance, 1);
5949
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005950 return 0;
5951
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005952fail_start_aen:
5953fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005954 megasas_mgmt_info.count--;
5955 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5956 megasas_mgmt_info.max_index--;
5957
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305958 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305959 megasas_destroy_irqs(instance);
5960
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305961 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08005962 megasas_release_fusion(instance);
5963 else
5964 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005965 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005966 pci_disable_msix(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305967fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005968fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005969 if (instance->evt_detail)
5970 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5971 instance->evt_detail,
5972 instance->evt_detail_h);
5973
Sumit Saxena2216c302016-01-28 21:04:26 +05305974 if (instance->pd_info)
5975 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
5976 instance->pd_info,
5977 instance->pd_info_h);
adam radfordeb1b1232011-02-24 20:55:56 -08005978 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005979 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5980 instance->producer_h);
5981 if (instance->consumer)
5982 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5983 instance->consumer_h);
5984 scsi_host_put(host);
5985
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005986fail_alloc_instance:
5987fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005988 pci_disable_device(pdev);
5989
5990 return -ENODEV;
5991}
5992
5993/**
5994 * megasas_flush_cache - Requests FW to flush all its caches
5995 * @instance: Adapter soft state
5996 */
5997static void megasas_flush_cache(struct megasas_instance *instance)
5998{
5999 struct megasas_cmd *cmd;
6000 struct megasas_dcmd_frame *dcmd;
6001
Sumit Saxena8a01a412016-01-28 21:04:32 +05306002 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006003 return;
6004
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006005 cmd = megasas_get_cmd(instance);
6006
6007 if (!cmd)
6008 return;
6009
6010 dcmd = &cmd->frame->dcmd;
6011
6012 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6013
6014 dcmd->cmd = MFI_CMD_DCMD;
6015 dcmd->cmd_status = 0x0;
6016 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306017 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006018 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006019 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006020 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306021 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006022 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6023
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306024 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6025 != DCMD_SUCCESS) {
6026 dev_err(&instance->pdev->dev,
6027 "return from %s %d\n", __func__, __LINE__);
6028 return;
6029 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006030
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306031 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006032}
6033
6034/**
6035 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6036 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006037 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006038 */
bo yang31ea7082007-11-07 12:09:50 -05006039static void megasas_shutdown_controller(struct megasas_instance *instance,
6040 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006041{
6042 struct megasas_cmd *cmd;
6043 struct megasas_dcmd_frame *dcmd;
6044
Sumit Saxena8a01a412016-01-28 21:04:32 +05306045 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006046 return;
6047
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006048 cmd = megasas_get_cmd(instance);
6049
6050 if (!cmd)
6051 return;
6052
6053 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306054 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306055 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006056 if (instance->map_update_cmd)
6057 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306058 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306059 if (instance->jbod_seq_cmd)
6060 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306061 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306062
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006063 dcmd = &cmd->frame->dcmd;
6064
6065 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6066
6067 dcmd->cmd = MFI_CMD_DCMD;
6068 dcmd->cmd_status = 0x0;
6069 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306070 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006071 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006072 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006073 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306074 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006075
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306076 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6077 != DCMD_SUCCESS) {
6078 dev_err(&instance->pdev->dev,
6079 "return from %s %d\n", __func__, __LINE__);
6080 return;
6081 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006082
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306083 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006084}
6085
Jiri Slaby33139b22008-05-01 17:56:02 +02006086#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006087/**
bo yangad84db22007-11-09 04:40:16 -05006088 * megasas_suspend - driver suspend entry point
6089 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006090 * @state: PCI power state to suspend routine
6091 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006092static int
bo yang31ea7082007-11-07 12:09:50 -05006093megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6094{
6095 struct Scsi_Host *host;
6096 struct megasas_instance *instance;
6097
6098 instance = pci_get_drvdata(pdev);
6099 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006100 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006101
adam radford229fe472014-03-10 02:51:56 -07006102 /* Shutdown SR-IOV heartbeat timer */
6103 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6104 del_timer_sync(&instance->sriov_heartbeat_timer);
6105
bo yang31ea7082007-11-07 12:09:50 -05006106 megasas_flush_cache(instance);
6107 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006108
6109 /* cancel the delayed work if this work still in queue */
6110 if (instance->ev != NULL) {
6111 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006112 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006113 instance->ev = NULL;
6114 }
6115
bo yang31ea7082007-11-07 12:09:50 -05006116 tasklet_kill(&instance->isr_tasklet);
6117
6118 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306119 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006120
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306121 megasas_destroy_irqs(instance);
6122
adam radfordc8e858f2011-10-08 18:15:13 -07006123 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08006124 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006125
6126 pci_save_state(pdev);
6127 pci_disable_device(pdev);
6128
6129 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6130
6131 return 0;
6132}
6133
6134/**
6135 * megasas_resume- driver resume entry point
6136 * @pdev: PCI device structure
6137 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006138static int
bo yang31ea7082007-11-07 12:09:50 -05006139megasas_resume(struct pci_dev *pdev)
6140{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306141 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006142 struct Scsi_Host *host;
6143 struct megasas_instance *instance;
6144
6145 instance = pci_get_drvdata(pdev);
6146 host = instance->host;
6147 pci_set_power_state(pdev, PCI_D0);
6148 pci_enable_wake(pdev, PCI_D0, 0);
6149 pci_restore_state(pdev);
6150
6151 /*
6152 * PCI prepping: enable device set bus mastering and dma mask
6153 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006154 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006155
6156 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006157 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006158 return rval;
6159 }
6160
6161 pci_set_master(pdev);
6162
6163 if (megasas_set_dma_mask(pdev))
6164 goto fail_set_dma_mask;
6165
6166 /*
6167 * Initialize MFI Firmware
6168 */
6169
bo yang31ea7082007-11-07 12:09:50 -05006170 atomic_set(&instance->fw_outstanding, 0);
6171
6172 /*
6173 * We expect the FW state to be READY
6174 */
adam radford058a8fa2011-10-08 18:14:27 -07006175 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006176 goto fail_ready_state;
6177
adam radford3f1abce2011-05-11 18:33:47 -07006178 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02006179 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02006180 pci_enable_msix_exact(instance->pdev, instance->msixentry,
6181 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02006182 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006183
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306184 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006185 megasas_reset_reply_desc(instance);
6186 if (megasas_ioc_init_fusion(instance)) {
6187 megasas_free_cmds(instance);
6188 megasas_free_cmds_fusion(instance);
6189 goto fail_init_mfi;
6190 }
6191 if (!megasas_get_map_info(instance))
6192 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306193 } else {
adam radford9c915a82010-12-21 13:34:31 -08006194 *instance->producer = 0;
6195 *instance->consumer = 0;
6196 if (megasas_issue_init_mfi(instance))
6197 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006198 }
bo yang31ea7082007-11-07 12:09:50 -05006199
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
6714 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6715 if (!ioc)
6716 return -ENOMEM;
6717
6718 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6719 error = -EFAULT;
6720 goto out_kfree_ioc;
6721 }
6722
6723 instance = megasas_lookup_instance(ioc->host_no);
6724 if (!instance) {
6725 error = -ENODEV;
6726 goto out_kfree_ioc;
6727 }
6728
adam radford229fe472014-03-10 02:51:56 -07006729 /* Adjust ioctl wait time for VF mode */
6730 if (instance->requestorId)
6731 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6732
6733 /* Block ioctls in VF mode */
6734 if (instance->requestorId && !allow_vf_ioctls) {
6735 error = -ENODEV;
6736 goto out_kfree_ioc;
6737 }
6738
Sumit Saxena8a01a412016-01-28 21:04:32 +05306739 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006740 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006741 error = -ENODEV;
6742 goto out_kfree_ioc;
6743 }
6744
6745 if (instance->unload == 1) {
6746 error = -ENODEV;
6747 goto out_kfree_ioc;
6748 }
6749
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006750 if (down_interruptible(&instance->ioctl_sem)) {
6751 error = -ERESTARTSYS;
6752 goto out_kfree_ioc;
6753 }
bo yang39a98552010-09-22 22:36:29 -04006754
6755 for (i = 0; i < wait_time; i++) {
6756
6757 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306758 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006759 spin_unlock_irqrestore(&instance->hba_lock, flags);
6760 break;
6761 }
6762 spin_unlock_irqrestore(&instance->hba_lock, flags);
6763
6764 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006765 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04006766 "for controller reset to finish\n");
6767 }
6768
6769 msleep(1000);
6770 }
6771
6772 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306773 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006774 spin_unlock_irqrestore(&instance->hba_lock, flags);
6775
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006776 dev_err(&instance->pdev->dev, "timed out while"
bo yang39a98552010-09-22 22:36:29 -04006777 "waiting for HBA to recover\n");
6778 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006779 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006780 }
6781 spin_unlock_irqrestore(&instance->hba_lock, flags);
6782
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006783 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006784out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006785 up(&instance->ioctl_sem);
6786
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006787out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006788 kfree(ioc);
6789 return error;
6790}
6791
6792static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6793{
6794 struct megasas_instance *instance;
6795 struct megasas_aen aen;
6796 int error;
bo yang39a98552010-09-22 22:36:29 -04006797 int i;
6798 unsigned long flags;
6799 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006800
6801 if (file->private_data != file) {
6802 printk(KERN_DEBUG "megasas: fasync_helper was not "
6803 "called first\n");
6804 return -EINVAL;
6805 }
6806
6807 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6808 return -EFAULT;
6809
6810 instance = megasas_lookup_instance(aen.host_no);
6811
6812 if (!instance)
6813 return -ENODEV;
6814
Sumit Saxena8a01a412016-01-28 21:04:32 +05306815 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04006816 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006817 }
6818
6819 if (instance->unload == 1) {
6820 return -ENODEV;
6821 }
6822
bo yang39a98552010-09-22 22:36:29 -04006823 for (i = 0; i < wait_time; i++) {
6824
6825 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306826 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006827 spin_unlock_irqrestore(&instance->hba_lock,
6828 flags);
6829 break;
6830 }
6831
6832 spin_unlock_irqrestore(&instance->hba_lock, flags);
6833
6834 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006835 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04006836 "controller reset to finish\n");
6837 }
6838
6839 msleep(1000);
6840 }
6841
6842 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306843 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006844 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006845 dev_err(&instance->pdev->dev, "timed out while waiting"
6846 "for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04006847 return -ENODEV;
6848 }
6849 spin_unlock_irqrestore(&instance->hba_lock, flags);
6850
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306851 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006852 error = megasas_register_aen(instance, aen.seq_num,
6853 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306854 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006855 return error;
6856}
6857
6858/**
6859 * megasas_mgmt_ioctl - char node ioctl entry point
6860 */
6861static long
6862megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6863{
6864 switch (cmd) {
6865 case MEGASAS_IOC_FIRMWARE:
6866 return megasas_mgmt_ioctl_fw(file, arg);
6867
6868 case MEGASAS_IOC_GET_AEN:
6869 return megasas_mgmt_ioctl_aen(file, arg);
6870 }
6871
6872 return -ENOTTY;
6873}
6874
6875#ifdef CONFIG_COMPAT
6876static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6877{
6878 struct compat_megasas_iocpacket __user *cioc =
6879 (struct compat_megasas_iocpacket __user *)arg;
6880 struct megasas_iocpacket __user *ioc =
6881 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6882 int i;
6883 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006884 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306885 u32 local_sense_off;
6886 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306887 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006888
Jeff Garzik83aabc12006-10-04 06:34:03 -04006889 if (clear_user(ioc, sizeof(*ioc)))
6890 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006891
6892 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6893 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6894 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6895 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6896 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6897 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6898 return -EFAULT;
6899
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006900 /*
6901 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6902 * sense_len is not null, so prepare the 64bit value under
6903 * the same condition.
6904 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306905 if (get_user(local_sense_off, &ioc->sense_off) ||
6906 get_user(local_sense_len, &ioc->sense_len) ||
6907 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306908 return -EFAULT;
6909
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306910 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006911 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306912 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006913 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306914 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006915 if (get_user(ptr, sense_cioc_ptr) ||
6916 put_user(compat_ptr(ptr), sense_ioc_ptr))
6917 return -EFAULT;
6918 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006919
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006920 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006921 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6922 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6923 copy_in_user(&ioc->sgl[i].iov_len,
6924 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6925 return -EFAULT;
6926 }
6927
6928 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6929
6930 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6931 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6932 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6933 return -EFAULT;
6934 }
6935 return error;
6936}
6937
6938static long
6939megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6940 unsigned long arg)
6941{
6942 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006943 case MEGASAS_IOC_FIRMWARE32:
6944 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006945 case MEGASAS_IOC_GET_AEN:
6946 return megasas_mgmt_ioctl_aen(file, arg);
6947 }
6948
6949 return -ENOTTY;
6950}
6951#endif
6952
6953/*
6954 * File operations structure for management interface
6955 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006956static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006957 .owner = THIS_MODULE,
6958 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006959 .fasync = megasas_mgmt_fasync,
6960 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006961 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006962#ifdef CONFIG_COMPAT
6963 .compat_ioctl = megasas_mgmt_compat_ioctl,
6964#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006965 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006966};
6967
6968/*
6969 * PCI hotplug support registration structure
6970 */
6971static struct pci_driver megasas_pci_driver = {
6972
6973 .name = "megaraid_sas",
6974 .id_table = megasas_pci_table,
6975 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006976 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006977 .suspend = megasas_suspend,
6978 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006979 .shutdown = megasas_shutdown,
6980};
6981
6982/*
6983 * Sysfs driver attributes
6984 */
6985static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6986{
6987 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6988 MEGASAS_VERSION);
6989}
6990
6991static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6992
6993static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306994megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6995{
6996 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6997 MEGASAS_RELDATE);
6998}
6999
7000static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
7001
7002static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06007003megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
7004{
7005 return sprintf(buf, "%u\n", support_poll_for_event);
7006}
7007
7008static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
7009 megasas_sysfs_show_support_poll_for_event, NULL);
7010
Yang, Bo837f5fe2010-10-11 06:59:20 -06007011 static ssize_t
7012megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
7013{
7014 return sprintf(buf, "%u\n", support_device_change);
7015}
7016
7017static DRIVER_ATTR(support_device_change, S_IRUGO,
7018 megasas_sysfs_show_support_device_change, NULL);
7019
Yang, Bo72c4fd32009-10-06 14:20:59 -06007020static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07007021megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
7022{
bo yangad84db22007-11-09 04:40:16 -05007023 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007024}
7025
7026static ssize_t
7027megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
7028{
7029 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007030
7031 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007032 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7033 retval = -EINVAL;
7034 }
7035 return retval;
7036}
7037
Joe Malicki66dca9b2008-08-14 17:14:48 -04007038static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05007039 megasas_sysfs_set_dbg_lvl);
7040
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007041static void
7042megasas_aen_polling(struct work_struct *work)
7043{
7044 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007045 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007046 struct megasas_instance *instance = ev->instance;
7047 union megasas_evt_class_locale class_locale;
7048 struct Scsi_Host *host;
7049 struct scsi_device *sdev1;
7050 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007051 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007052 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007053 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007054 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307055 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007056
7057 if (!instance) {
7058 printk(KERN_ERR "invalid instance!\n");
7059 kfree(ev);
7060 return;
7061 }
adam radford229fe472014-03-10 02:51:56 -07007062
7063 /* Adjust event workqueue thread wait time for VF mode */
7064 if (instance->requestorId)
7065 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7066
7067 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307068 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007069
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007070 instance->ev = NULL;
7071 host = instance->host;
7072 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307073 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007074
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307075 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307076
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007077 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307078 case MR_EVT_PD_REMOVED:
7079 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307080 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307081 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007082 break;
7083
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307084 case MR_EVT_LD_OFFLINE:
7085 case MR_EVT_CFG_CLEARED:
7086 case MR_EVT_LD_DELETED:
7087 case MR_EVT_LD_CREATED:
7088 if (!instance->requestorId ||
7089 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7090 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007091
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307092 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307093 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007094
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307095 break;
7096
7097 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7098 case MR_EVT_FOREIGN_CFG_IMPORTED:
7099 case MR_EVT_LD_STATE_CHANGE:
7100 dcmd_ret = megasas_get_pd_list(instance);
7101
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307102 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307103 break;
7104
7105 if (!instance->requestorId ||
7106 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7107 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7108
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307109 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307110 break;
7111
7112 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7113 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7114 instance->host->host_no);
7115 break;
7116
7117 case MR_EVT_CTRL_PROP_CHANGED:
7118 dcmd_ret = megasas_get_ctrl_info(instance);
7119 break;
7120 default:
7121 doscan = 0;
7122 break;
7123 }
7124 } else {
7125 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7126 mutex_unlock(&instance->reset_mutex);
7127 kfree(ev);
7128 return;
7129 }
7130
7131 mutex_unlock(&instance->reset_mutex);
7132
7133 if (doscan & SCAN_PD_CHANNEL) {
7134 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7135 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7136 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007137 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307138 if (instance->pd_list[pd_index].driveState ==
7139 MR_PD_STATE_SYSTEM) {
7140 if (!sdev1)
7141 scsi_add_device(host, i, j, 0);
7142 else
Yang, Boc9786842009-12-06 08:39:25 -07007143 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007144 } else {
7145 if (sdev1) {
7146 scsi_remove_device(sdev1);
7147 scsi_device_put(sdev1);
7148 }
7149 }
Yang, Boc9786842009-12-06 08:39:25 -07007150 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007151 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007152 }
7153
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307154 if (doscan & SCAN_VD_CHANNEL) {
7155 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7156 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7157 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7158 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7159 if (instance->ld_ids[ld_index] != 0xff) {
7160 if (!sdev1)
7161 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7162 else
7163 scsi_device_put(sdev1);
7164 } else {
7165 if (sdev1) {
7166 scsi_remove_device(sdev1);
7167 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007168 }
7169 }
7170 }
7171 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007172 }
7173
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307174 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307175 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7176 else
7177 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007178
7179 /* Register AEN with FW for latest sequence number plus 1 */
7180 class_locale.members.reserved = 0;
7181 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7182 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307183
7184 if (instance->aen_cmd != NULL) {
7185 kfree(ev);
7186 return;
7187 }
7188
7189 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007190 error = megasas_register_aen(instance, seq_num,
7191 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007192 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307193 dev_err(&instance->pdev->dev,
7194 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007195
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307196 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007197 kfree(ev);
7198}
7199
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007200/**
7201 * megasas_init - Driver load entry point
7202 */
7203static int __init megasas_init(void)
7204{
7205 int rval;
7206
7207 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007208 * Booted in kdump kernel, minimize memory footprints by
7209 * disabling few features
7210 */
7211 if (reset_devices) {
7212 msix_vectors = 1;
7213 rdpq_enable = 0;
7214 dual_qdepth_disable = 1;
7215 }
7216
7217 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007218 * Announce driver version and other information
7219 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307220 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007221
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007222 spin_lock_init(&poll_aen_lock);
7223
Yang, Bo72c4fd32009-10-06 14:20:59 -06007224 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007225 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007226
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007227 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7228
7229 /*
7230 * Register character device node
7231 */
7232 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7233
7234 if (rval < 0) {
7235 printk(KERN_DEBUG "megasas: failed to open device node\n");
7236 return rval;
7237 }
7238
7239 megasas_mgmt_majorno = rval;
7240
7241 /*
7242 * Register ourselves as PCI hotplug module
7243 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007244 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007245
7246 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007247 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007248 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007249 }
7250
Jeff Garzik83aabc12006-10-04 06:34:03 -04007251 rval = driver_create_file(&megasas_pci_driver.driver,
7252 &driver_attr_version);
7253 if (rval)
7254 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007255
7256 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307257 &driver_attr_release_date);
7258 if (rval)
7259 goto err_dcf_rel_date;
7260
7261 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007262 &driver_attr_support_poll_for_event);
7263 if (rval)
7264 goto err_dcf_support_poll_for_event;
7265
Jeff Garzik83aabc12006-10-04 06:34:03 -04007266 rval = driver_create_file(&megasas_pci_driver.driver,
7267 &driver_attr_dbg_lvl);
7268 if (rval)
7269 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007270 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007271 &driver_attr_support_device_change);
7272 if (rval)
7273 goto err_dcf_support_device_change;
7274
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007275 return rval;
bo yangad84db22007-11-09 04:40:16 -05007276
Yang, Bo837f5fe2010-10-11 06:59:20 -06007277err_dcf_support_device_change:
7278 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007279 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007280err_dcf_dbg_lvl:
7281 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007282 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007283err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307284 driver_remove_file(&megasas_pci_driver.driver,
7285 &driver_attr_release_date);
7286err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007287 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7288err_dcf_attr_ver:
7289 pci_unregister_driver(&megasas_pci_driver);
7290err_pcidrv:
7291 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007292 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007293}
7294
7295/**
7296 * megasas_exit - Driver unload entry point
7297 */
7298static void __exit megasas_exit(void)
7299{
Sumant Patro658dced2006-10-03 13:09:14 -07007300 driver_remove_file(&megasas_pci_driver.driver,
7301 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007302 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007303 &driver_attr_support_poll_for_event);
7304 driver_remove_file(&megasas_pci_driver.driver,
7305 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307306 driver_remove_file(&megasas_pci_driver.driver,
7307 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007308 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007309
7310 pci_unregister_driver(&megasas_pci_driver);
7311 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7312}
7313
7314module_init(megasas_init);
7315module_exit(megasas_exit);