blob: b41bbea3667fe52aee63edd3fa8479c967c31cdf [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
adam radford3f1530c2010-12-14 18:51:48 -08002 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006 *
adam radford3f1530c2010-12-14 18:51:48 -08007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040011 *
adam radford3f1530c2010-12-14 18:51:48 -080012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040016 *
adam radford3f1530c2010-12-14 18:51:48 -080017 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040019 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053020 * Authors: Avago Technologies
adam radford3f1530c2010-12-14 18:51:48 -080021 * Sreenivas Bagalkote
22 * Sumant Patro
23 * Bo Yang
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford3f1530c2010-12-14 18:51:48 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040038#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
43#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010046#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040047#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080048#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010049#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060050#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040051
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070056#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080057#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040058#include "megaraid_sas.h"
59
bo yangad84db22007-11-09 04:40:16 -050060/*
Yang, Bo1fd10682010-10-12 07:18:50 -060061 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
63 */
64static unsigned int max_sectors;
65module_param_named(max_sectors, max_sectors, int, 0);
66MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
68
adam radford80d9da92010-12-21 10:17:40 -080069static int msix_disable;
70module_param(msix_disable, int, S_IRUGO);
71MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
72
adam radford079eadd2012-10-01 19:26:59 -070073static unsigned int msix_vectors;
74module_param(msix_vectors, int, S_IRUGO);
75MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
76
adam radford229fe472014-03-10 02:51:56 -070077static int allow_vf_ioctls;
78module_param(allow_vf_ioctls, int, S_IRUGO);
79MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
80
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053081static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070082module_param(throttlequeuedepth, int, S_IRUGO);
83MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
85
Sumit Saxenae3d178c2016-01-28 21:04:34 +053086unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070087module_param(resetwaittime, int, S_IRUGO);
88MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
90
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053091int smp_affinity_enable = 1;
92module_param(smp_affinity_enable, int, S_IRUGO);
93MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
94
Sumit Saxena179ac142016-01-28 21:04:28 +053095int rdpq_enable = 1;
96module_param(rdpq_enable, int, S_IRUGO);
97MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
98
Sumit Saxena308ec452016-01-28 21:04:30 +053099unsigned int dual_qdepth_disable;
100module_param(dual_qdepth_disable, int, S_IRUGO);
101MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
102
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530103unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
104module_param(scmd_timeout, int, S_IRUGO);
105MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
106
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400107MODULE_LICENSE("GPL");
108MODULE_VERSION(MEGASAS_VERSION);
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +0530109MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
110MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400111
adam radford058a8fa2011-10-08 18:14:27 -0700112int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400113static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700114static int megasas_ld_list_query(struct megasas_instance *instance,
115 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400116static int megasas_issue_init_mfi(struct megasas_instance *instance);
117static int megasas_register_aen(struct megasas_instance *instance,
118 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800119static void megasas_get_pd_info(struct megasas_instance *instance,
120 struct scsi_device *sdev);
Shivasharan S96188a82017-02-10 00:59:11 -0800121static int megasas_get_target_prop(struct megasas_instance *instance,
122 struct scsi_device *sdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400123/*
124 * PCI ID table for all supported controllers
125 */
126static struct pci_device_id megasas_pci_table[] = {
127
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
129 /* xscale IOP */
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
131 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
133 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
135 /* gen2*/
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
137 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
139 /* skinny*/
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
141 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
143 /* xscale IOP, vega */
144 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
145 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
147 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
149 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
151 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
153 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
155 /* Intruder */
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
157 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
159 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500160 /* VENTURA */
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200166 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400167};
168
169MODULE_DEVICE_TABLE(pci, megasas_pci_table);
170
171static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700172struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400173static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100174static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400175
Yang, Boc3518832009-10-06 14:18:02 -0600176static int megasas_poll_wait_aen;
177static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600178static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800179u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600180static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700181
Yang, Boc3518832009-10-06 14:18:02 -0600182/* define lock for aen poll */
183spinlock_t poll_aen_lock;
184
adam radford9c915a82010-12-21 13:34:31 -0800185void
bo yang7343eb62007-11-09 04:35:44 -0500186megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
187 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800188static u32
189megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
190static int
191megasas_adp_reset_gen2(struct megasas_instance *instance,
192 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800193static irqreturn_t megasas_isr(int irq, void *devp);
194static u32
195megasas_init_adapter_mfi(struct megasas_instance *instance);
196u32
197megasas_build_and_issue_cmd(struct megasas_instance *instance,
198 struct scsi_cmnd *scmd);
199static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800200int
adam radford229fe472014-03-10 02:51:56 -0700201wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
202 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800203void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700204static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
205 int initial);
adam radfordcd50ba82010-12-21 10:23:23 -0800206
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800207void
adam radfordcd50ba82010-12-21 10:23:23 -0800208megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
209{
210 instance->instancet->fire_cmd(instance,
211 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800212 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800213}
bo yang7343eb62007-11-09 04:35:44 -0500214
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400215/**
216 * megasas_get_cmd - Get a command from the free pool
217 * @instance: Adapter soft state
218 *
219 * Returns a free command from the pool
220 */
adam radford9c915a82010-12-21 13:34:31 -0800221struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400222 *instance)
223{
224 unsigned long flags;
225 struct megasas_cmd *cmd = NULL;
226
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530227 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400228
229 if (!list_empty(&instance->cmd_pool)) {
230 cmd = list_entry((&instance->cmd_pool)->next,
231 struct megasas_cmd, list);
232 list_del_init(&cmd->list);
233 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500234 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400235 }
236
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530237 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400238 return cmd;
239}
240
241/**
242 * megasas_return_cmd - Return a cmd to free command pool
243 * @instance: Adapter soft state
244 * @cmd: Command packet to be returned to free command pool
245 */
adam radford9c915a82010-12-21 13:34:31 -0800246inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400247megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
248{
249 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530250 u32 blk_tags;
251 struct megasas_cmd_fusion *cmd_fusion;
252 struct fusion_context *fusion = instance->ctrl_context;
253
254 /* This flag is used only for fusion adapter.
255 * Wait for Interrupt for Polled mode DCMD
256 */
257 if (cmd->flags & DRV_DCMD_POLLED_MODE)
258 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400259
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530260 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400261
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530262 if (fusion) {
263 blk_tags = instance->max_scsi_cmds + cmd->index;
264 cmd_fusion = fusion->cmd_list[blk_tags];
265 megasas_return_cmd_fusion(instance, cmd_fusion);
266 }
267 cmd->scmd = NULL;
268 cmd->frame_count = 0;
269 cmd->flags = 0;
270 if (!fusion && reset_devices)
271 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
272 list_add(&cmd->list, (&instance->cmd_pool)->next);
273
274 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
275
276}
Sumant Patro1341c932006-01-25 12:02:40 -0800277
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530278static const char *
279format_timestamp(uint32_t timestamp)
280{
281 static char buffer[32];
282
283 if ((timestamp & 0xff000000) == 0xff000000)
284 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
285 0x00ffffff);
286 else
287 snprintf(buffer, sizeof(buffer), "%us", timestamp);
288 return buffer;
289}
290
291static const char *
292format_class(int8_t class)
293{
294 static char buffer[6];
295
296 switch (class) {
297 case MFI_EVT_CLASS_DEBUG:
298 return "debug";
299 case MFI_EVT_CLASS_PROGRESS:
300 return "progress";
301 case MFI_EVT_CLASS_INFO:
302 return "info";
303 case MFI_EVT_CLASS_WARNING:
304 return "WARN";
305 case MFI_EVT_CLASS_CRITICAL:
306 return "CRIT";
307 case MFI_EVT_CLASS_FATAL:
308 return "FATAL";
309 case MFI_EVT_CLASS_DEAD:
310 return "DEAD";
311 default:
312 snprintf(buffer, sizeof(buffer), "%d", class);
313 return buffer;
314 }
315}
316
317/**
318 * megasas_decode_evt: Decode FW AEN event and print critical event
319 * for information.
320 * @instance: Adapter soft state
321 */
322static void
323megasas_decode_evt(struct megasas_instance *instance)
324{
325 struct megasas_evt_detail *evt_detail = instance->evt_detail;
326 union megasas_evt_class_locale class_locale;
327 class_locale.word = le32_to_cpu(evt_detail->cl.word);
328
329 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
330 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
331 le32_to_cpu(evt_detail->seq_num),
332 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
333 (class_locale.members.locale),
334 format_class(class_locale.members.class),
335 evt_detail->description);
336}
337
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400338/**
adam radford0d490162010-12-14 19:17:17 -0800339* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800340* (deviceid : 1064R, PERC5) controllers
341*/
342
343/**
344 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400345 * @regs: MFI register set
346 */
347static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530348megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400349{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530350 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500351
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530352 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400353 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400354
355 /* Dummy readl to force pci flush */
356 readl(&regs->outbound_intr_mask);
357}
358
359/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700360 * megasas_disable_intr_xscale -Disables interrupt
361 * @regs: MFI register set
362 */
363static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530364megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700365{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530366 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700367 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500368
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530369 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700370 writel(mask, &regs->outbound_intr_mask);
371 /* Dummy readl to force pci flush */
372 readl(&regs->outbound_intr_mask);
373}
374
375/**
Sumant Patro1341c932006-01-25 12:02:40 -0800376 * megasas_read_fw_status_reg_xscale - returns the current FW status value
377 * @regs: MFI register set
378 */
379static u32
380megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
381{
382 return readl(&(regs)->outbound_msg_0);
383}
384/**
385 * megasas_clear_interrupt_xscale - Check & clear interrupt
386 * @regs: MFI register set
387 */
adam radford0d490162010-12-14 19:17:17 -0800388static int
Sumant Patro1341c932006-01-25 12:02:40 -0800389megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
390{
391 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400392 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500393
Sumant Patro1341c932006-01-25 12:02:40 -0800394 /*
395 * Check if it is our interrupt
396 */
397 status = readl(&regs->outbound_intr_status);
398
bo yang39a98552010-09-22 22:36:29 -0400399 if (status & MFI_OB_INTR_STATUS_MASK)
400 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
401 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
402 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800403
404 /*
405 * Clear the interrupt by writing back the same value
406 */
bo yang39a98552010-09-22 22:36:29 -0400407 if (mfiStatus)
408 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800409
Yang, Bo06f579d2008-08-10 12:42:37 -0700410 /* Dummy readl to force pci flush */
411 readl(&regs->outbound_intr_status);
412
bo yang39a98552010-09-22 22:36:29 -0400413 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800414}
415
416/**
417 * megasas_fire_cmd_xscale - Sends command to the FW
418 * @frame_phys_addr : Physical address of cmd
419 * @frame_count : Number of frames for the command
420 * @regs : MFI register set
421 */
adam radford0d490162010-12-14 19:17:17 -0800422static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600423megasas_fire_cmd_xscale(struct megasas_instance *instance,
424 dma_addr_t frame_phys_addr,
425 u32 frame_count,
426 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800427{
bo yang39a98552010-09-22 22:36:29 -0400428 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500429
bo yang39a98552010-09-22 22:36:29 -0400430 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800431 writel((frame_phys_addr >> 3)|(frame_count),
432 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400433 spin_unlock_irqrestore(&instance->hba_lock, flags);
434}
435
436/**
437 * megasas_adp_reset_xscale - For controller reset
438 * @regs: MFI register set
439 */
440static int
441megasas_adp_reset_xscale(struct megasas_instance *instance,
442 struct megasas_register_set __iomem *regs)
443{
444 u32 i;
445 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500446
bo yang39a98552010-09-22 22:36:29 -0400447 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
448
449 for (i = 0; i < 3; i++)
450 msleep(1000); /* sleep for 3 secs */
451 pcidata = 0;
452 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500453 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400454 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500455 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400456 pcidata &= ~0x2;
457 pci_write_config_dword(instance->pdev,
458 MFI_1068_PCSR_OFFSET, pcidata);
459
460 for (i = 0; i < 2; i++)
461 msleep(1000); /* need to wait 2 secs again */
462
463 pcidata = 0;
464 pci_read_config_dword(instance->pdev,
465 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500466 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400467 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500468 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400469 pcidata = 0;
470 pci_write_config_dword(instance->pdev,
471 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
472 }
473 }
474 return 0;
475}
476
477/**
478 * megasas_check_reset_xscale - For controller reset check
479 * @regs: MFI register set
480 */
481static int
482megasas_check_reset_xscale(struct megasas_instance *instance,
483 struct megasas_register_set __iomem *regs)
484{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530485 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530486 (le32_to_cpu(*instance->consumer) ==
487 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400488 return 1;
bo yang39a98552010-09-22 22:36:29 -0400489 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800490}
491
492static struct megasas_instance_template megasas_instance_template_xscale = {
493
494 .fire_cmd = megasas_fire_cmd_xscale,
495 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700496 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800497 .clear_intr = megasas_clear_intr_xscale,
498 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400499 .adp_reset = megasas_adp_reset_xscale,
500 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800501 .service_isr = megasas_isr,
502 .tasklet = megasas_complete_cmd_dpc,
503 .init_adapter = megasas_init_adapter_mfi,
504 .build_and_issue_cmd = megasas_build_and_issue_cmd,
505 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800506};
507
508/**
adam radford0d490162010-12-14 19:17:17 -0800509* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800510* to xscale (deviceid : 1064R, PERC5) controllers
511*/
512
513/**
adam radford0d490162010-12-14 19:17:17 -0800514* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500515* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800516*/
517
518/**
519 * megasas_enable_intr_ppc - Enables interrupts
520 * @regs: MFI register set
521 */
522static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530523megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800524{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530525 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500526
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530527 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800528 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800529
bo yang39a98552010-09-22 22:36:29 -0400530 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800531
532 /* Dummy readl to force pci flush */
533 readl(&regs->outbound_intr_mask);
534}
535
536/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700537 * megasas_disable_intr_ppc - Disable interrupt
538 * @regs: MFI register set
539 */
540static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530541megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700542{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530543 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700544 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500545
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530546 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700547 writel(mask, &regs->outbound_intr_mask);
548 /* Dummy readl to force pci flush */
549 readl(&regs->outbound_intr_mask);
550}
551
552/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800553 * megasas_read_fw_status_reg_ppc - returns the current FW status value
554 * @regs: MFI register set
555 */
556static u32
557megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
558{
559 return readl(&(regs)->outbound_scratch_pad);
560}
561
562/**
563 * megasas_clear_interrupt_ppc - Check & clear interrupt
564 * @regs: MFI register set
565 */
adam radford0d490162010-12-14 19:17:17 -0800566static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800567megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
568{
adam radford3cc6851f92011-05-11 18:34:52 -0700569 u32 status, mfiStatus = 0;
570
Sumant Patrof9876f02006-02-03 15:34:35 -0800571 /*
572 * Check if it is our interrupt
573 */
574 status = readl(&regs->outbound_intr_status);
575
adam radford3cc6851f92011-05-11 18:34:52 -0700576 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
577 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
578
579 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
580 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800581
582 /*
583 * Clear the interrupt by writing back the same value
584 */
585 writel(status, &regs->outbound_doorbell_clear);
586
Yang, Bo06f579d2008-08-10 12:42:37 -0700587 /* Dummy readl to force pci flush */
588 readl(&regs->outbound_doorbell_clear);
589
adam radford3cc6851f92011-05-11 18:34:52 -0700590 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800591}
adam radford3cc6851f92011-05-11 18:34:52 -0700592
Sumant Patrof9876f02006-02-03 15:34:35 -0800593/**
594 * megasas_fire_cmd_ppc - Sends command to the FW
595 * @frame_phys_addr : Physical address of cmd
596 * @frame_count : Number of frames for the command
597 * @regs : MFI register set
598 */
adam radford0d490162010-12-14 19:17:17 -0800599static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600600megasas_fire_cmd_ppc(struct megasas_instance *instance,
601 dma_addr_t frame_phys_addr,
602 u32 frame_count,
603 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800604{
bo yang39a98552010-09-22 22:36:29 -0400605 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500606
bo yang39a98552010-09-22 22:36:29 -0400607 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800608 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800609 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400610 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800611}
612
bo yang39a98552010-09-22 22:36:29 -0400613/**
bo yang39a98552010-09-22 22:36:29 -0400614 * megasas_check_reset_ppc - For controller reset check
615 * @regs: MFI register set
616 */
617static int
618megasas_check_reset_ppc(struct megasas_instance *instance,
619 struct megasas_register_set __iomem *regs)
620{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530621 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700622 return 1;
623
bo yang39a98552010-09-22 22:36:29 -0400624 return 0;
625}
adam radford3cc6851f92011-05-11 18:34:52 -0700626
Sumant Patrof9876f02006-02-03 15:34:35 -0800627static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800628
Sumant Patrof9876f02006-02-03 15:34:35 -0800629 .fire_cmd = megasas_fire_cmd_ppc,
630 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700631 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800632 .clear_intr = megasas_clear_intr_ppc,
633 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700634 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400635 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800636 .service_isr = megasas_isr,
637 .tasklet = megasas_complete_cmd_dpc,
638 .init_adapter = megasas_init_adapter_mfi,
639 .build_and_issue_cmd = megasas_build_and_issue_cmd,
640 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800641};
642
643/**
Yang, Bo87911122009-10-06 14:31:54 -0600644 * megasas_enable_intr_skinny - Enables interrupts
645 * @regs: MFI register set
646 */
647static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530648megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600649{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530650 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500651
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530652 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600653 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
654
655 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
656
657 /* Dummy readl to force pci flush */
658 readl(&regs->outbound_intr_mask);
659}
660
661/**
662 * megasas_disable_intr_skinny - Disables interrupt
663 * @regs: MFI register set
664 */
665static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530666megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600667{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530668 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600669 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500670
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530671 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600672 writel(mask, &regs->outbound_intr_mask);
673 /* Dummy readl to force pci flush */
674 readl(&regs->outbound_intr_mask);
675}
676
677/**
678 * megasas_read_fw_status_reg_skinny - returns the current FW status value
679 * @regs: MFI register set
680 */
681static u32
682megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
683{
684 return readl(&(regs)->outbound_scratch_pad);
685}
686
687/**
688 * megasas_clear_interrupt_skinny - Check & clear interrupt
689 * @regs: MFI register set
690 */
691static int
692megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
693{
694 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800695 u32 mfiStatus = 0;
696
Yang, Bo87911122009-10-06 14:31:54 -0600697 /*
698 * Check if it is our interrupt
699 */
700 status = readl(&regs->outbound_intr_status);
701
702 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400703 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600704 }
705
706 /*
adam radfordebf054b2011-02-24 20:57:15 -0800707 * Check if it is our interrupt
708 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600709 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800710 MFI_STATE_FAULT) {
711 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
712 } else
713 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
714
715 /*
Yang, Bo87911122009-10-06 14:31:54 -0600716 * Clear the interrupt by writing back the same value
717 */
718 writel(status, &regs->outbound_intr_status);
719
720 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500721 * dummy read to flush PCI
722 */
Yang, Bo87911122009-10-06 14:31:54 -0600723 readl(&regs->outbound_intr_status);
724
adam radfordebf054b2011-02-24 20:57:15 -0800725 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600726}
727
728/**
729 * megasas_fire_cmd_skinny - Sends command to the FW
730 * @frame_phys_addr : Physical address of cmd
731 * @frame_count : Number of frames for the command
732 * @regs : MFI register set
733 */
734static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600735megasas_fire_cmd_skinny(struct megasas_instance *instance,
736 dma_addr_t frame_phys_addr,
737 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600738 struct megasas_register_set __iomem *regs)
739{
Yang, Bo0c79e682009-10-06 14:47:35 -0600740 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500741
bo yang39a98552010-09-22 22:36:29 -0400742 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530743 writel(upper_32_bits(frame_phys_addr),
744 &(regs)->inbound_high_queue_port);
745 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
746 &(regs)->inbound_low_queue_port);
Tomas Henzlb99dbe52016-02-01 15:12:04 +0100747 mmiowb();
bo yang39a98552010-09-22 22:36:29 -0400748 spin_unlock_irqrestore(&instance->hba_lock, flags);
749}
750
751/**
bo yang39a98552010-09-22 22:36:29 -0400752 * megasas_check_reset_skinny - For controller reset check
753 * @regs: MFI register set
754 */
755static int
756megasas_check_reset_skinny(struct megasas_instance *instance,
757 struct megasas_register_set __iomem *regs)
758{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530759 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700760 return 1;
761
bo yang39a98552010-09-22 22:36:29 -0400762 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600763}
764
765static struct megasas_instance_template megasas_instance_template_skinny = {
766
767 .fire_cmd = megasas_fire_cmd_skinny,
768 .enable_intr = megasas_enable_intr_skinny,
769 .disable_intr = megasas_disable_intr_skinny,
770 .clear_intr = megasas_clear_intr_skinny,
771 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800772 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400773 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800774 .service_isr = megasas_isr,
775 .tasklet = megasas_complete_cmd_dpc,
776 .init_adapter = megasas_init_adapter_mfi,
777 .build_and_issue_cmd = megasas_build_and_issue_cmd,
778 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600779};
780
781
782/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700783* The following functions are defined for gen2 (deviceid : 0x78 0x79)
784* controllers
785*/
786
787/**
788 * megasas_enable_intr_gen2 - Enables interrupts
789 * @regs: MFI register set
790 */
791static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530792megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700793{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530794 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500795
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530796 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700797 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
798
799 /* write ~0x00000005 (4 & 1) to the intr mask*/
800 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
801
802 /* Dummy readl to force pci flush */
803 readl(&regs->outbound_intr_mask);
804}
805
806/**
807 * megasas_disable_intr_gen2 - Disables interrupt
808 * @regs: MFI register set
809 */
810static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530811megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700812{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530813 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700814 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500815
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530816 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700817 writel(mask, &regs->outbound_intr_mask);
818 /* Dummy readl to force pci flush */
819 readl(&regs->outbound_intr_mask);
820}
821
822/**
823 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
824 * @regs: MFI register set
825 */
826static u32
827megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
828{
829 return readl(&(regs)->outbound_scratch_pad);
830}
831
832/**
833 * megasas_clear_interrupt_gen2 - Check & clear interrupt
834 * @regs: MFI register set
835 */
836static int
837megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
838{
839 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400840 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500841
Yang, Bo6610a6b2008-08-10 12:42:38 -0700842 /*
843 * Check if it is our interrupt
844 */
845 status = readl(&regs->outbound_intr_status);
846
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530847 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400848 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
849 }
850 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
851 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
852 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700853
854 /*
855 * Clear the interrupt by writing back the same value
856 */
bo yang39a98552010-09-22 22:36:29 -0400857 if (mfiStatus)
858 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700859
860 /* Dummy readl to force pci flush */
861 readl(&regs->outbound_intr_status);
862
bo yang39a98552010-09-22 22:36:29 -0400863 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700864}
865/**
866 * megasas_fire_cmd_gen2 - Sends command to the FW
867 * @frame_phys_addr : Physical address of cmd
868 * @frame_count : Number of frames for the command
869 * @regs : MFI register set
870 */
871static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600872megasas_fire_cmd_gen2(struct megasas_instance *instance,
873 dma_addr_t frame_phys_addr,
874 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700875 struct megasas_register_set __iomem *regs)
876{
bo yang39a98552010-09-22 22:36:29 -0400877 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500878
bo yang39a98552010-09-22 22:36:29 -0400879 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700880 writel((frame_phys_addr | (frame_count<<1))|1,
881 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400882 spin_unlock_irqrestore(&instance->hba_lock, flags);
883}
884
885/**
886 * megasas_adp_reset_gen2 - For controller reset
887 * @regs: MFI register set
888 */
889static int
890megasas_adp_reset_gen2(struct megasas_instance *instance,
891 struct megasas_register_set __iomem *reg_set)
892{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500893 u32 retry = 0 ;
894 u32 HostDiag;
895 u32 __iomem *seq_offset = &reg_set->seq_offset;
896 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400897
adam radfordebf054b2011-02-24 20:57:15 -0800898 if (instance->instancet == &megasas_instance_template_skinny) {
899 seq_offset = &reg_set->fusion_seq_offset;
900 hostdiag_offset = &reg_set->fusion_host_diag;
901 }
902
903 writel(0, seq_offset);
904 writel(4, seq_offset);
905 writel(0xb, seq_offset);
906 writel(2, seq_offset);
907 writel(7, seq_offset);
908 writel(0xd, seq_offset);
909
bo yang39a98552010-09-22 22:36:29 -0400910 msleep(1000);
911
adam radfordebf054b2011-02-24 20:57:15 -0800912 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400913
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500914 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400915 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800916 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500917 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400918 retry, HostDiag);
919
920 if (retry++ >= 100)
921 return 1;
922
923 }
924
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500925 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400926
adam radfordebf054b2011-02-24 20:57:15 -0800927 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400928
929 ssleep(10);
930
adam radfordebf054b2011-02-24 20:57:15 -0800931 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500932 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400933 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800934 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500935 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400936 retry, HostDiag);
937
938 if (retry++ >= 1000)
939 return 1;
940
941 }
942 return 0;
943}
944
945/**
946 * megasas_check_reset_gen2 - For controller reset check
947 * @regs: MFI register set
948 */
949static int
950megasas_check_reset_gen2(struct megasas_instance *instance,
951 struct megasas_register_set __iomem *regs)
952{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530953 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600954 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600955
bo yang39a98552010-09-22 22:36:29 -0400956 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700957}
958
959static struct megasas_instance_template megasas_instance_template_gen2 = {
960
961 .fire_cmd = megasas_fire_cmd_gen2,
962 .enable_intr = megasas_enable_intr_gen2,
963 .disable_intr = megasas_disable_intr_gen2,
964 .clear_intr = megasas_clear_intr_gen2,
965 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400966 .adp_reset = megasas_adp_reset_gen2,
967 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800968 .service_isr = megasas_isr,
969 .tasklet = megasas_complete_cmd_dpc,
970 .init_adapter = megasas_init_adapter_mfi,
971 .build_and_issue_cmd = megasas_build_and_issue_cmd,
972 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700973};
974
975/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800976* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400977* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800978*/
979
adam radford9c915a82010-12-21 13:34:31 -0800980/*
981 * Template added for TB (Fusion)
982 */
983extern struct megasas_instance_template megasas_instance_template_fusion;
984
Sumant Patrof9876f02006-02-03 15:34:35 -0800985/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400986 * megasas_issue_polled - Issues a polling command
987 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800988 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530990 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991 */
adam radford9c915a82010-12-21 13:34:31 -0800992int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400993megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
994{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400995 struct megasas_header *frame_hdr = &cmd->frame->hdr;
996
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530997 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530998 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400999
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001000 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301001 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1002 __func__, __LINE__);
1003 return DCMD_NOT_FIRED;
1004 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001005
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001006 instance->instancet->issue_dcmd(instance, cmd);
1007
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301008 return wait_and_poll(instance, cmd, instance->requestorId ?
1009 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001010}
1011
1012/**
1013 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1014 * @instance: Adapter soft state
1015 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301016 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001017 *
1018 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001019 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001020 * Used to issue ioctl commands.
1021 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301022int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001023megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301024 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001025{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301026 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301027 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001029 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301030 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1031 __func__, __LINE__);
1032 return DCMD_NOT_FIRED;
1033 }
1034
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001035 instance->instancet->issue_dcmd(instance, cmd);
1036
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301037 if (timeout) {
1038 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301039 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301040 if (!ret) {
1041 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1042 __func__, __LINE__);
1043 return DCMD_TIMEOUT;
1044 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301045 } else
1046 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301047 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001048
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301049 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301050 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001051}
1052
1053/**
1054 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1055 * @instance: Adapter soft state
1056 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301057 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001058 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301059 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001060 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001061 * cmd and waits for return status.
1062 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001063 */
1064static int
1065megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301066 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001067{
1068 struct megasas_cmd *cmd;
1069 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301070 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001071
1072 cmd = megasas_get_cmd(instance);
1073
1074 if (!cmd)
1075 return -1;
1076
1077 abort_fr = &cmd->frame->abort;
1078
1079 /*
1080 * Prepare and issue the abort frame
1081 */
1082 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301083 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301084 abort_fr->flags = cpu_to_le16(0);
1085 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1086 abort_fr->abort_mfi_phys_addr_lo =
1087 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1088 abort_fr->abort_mfi_phys_addr_hi =
1089 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001090
1091 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301092 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001093
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001094 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301095 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1096 __func__, __LINE__);
1097 return DCMD_NOT_FIRED;
1098 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001099
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001100 instance->instancet->issue_dcmd(instance, cmd);
1101
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301102 if (timeout) {
1103 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301104 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301105 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301106 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1107 __func__, __LINE__);
1108 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301109 }
1110 } else
1111 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301112 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301113
bo yang39a98552010-09-22 22:36:29 -04001114 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001115
1116 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301117 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1118 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001119}
1120
1121/**
1122 * megasas_make_sgl32 - Prepares 32-bit SGL
1123 * @instance: Adapter soft state
1124 * @scp: SCSI command from the mid-layer
1125 * @mfi_sgl: SGL to be filled in
1126 *
1127 * If successful, this function returns the number of SG elements. Otherwise,
1128 * it returnes -1.
1129 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001130static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001131megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1132 union megasas_sgl *mfi_sgl)
1133{
1134 int i;
1135 int sge_count;
1136 struct scatterlist *os_sgl;
1137
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001138 sge_count = scsi_dma_map(scp);
1139 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001140
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001141 if (sge_count) {
1142 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301143 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1144 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001145 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001146 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001147 return sge_count;
1148}
1149
1150/**
1151 * megasas_make_sgl64 - Prepares 64-bit SGL
1152 * @instance: Adapter soft state
1153 * @scp: SCSI command from the mid-layer
1154 * @mfi_sgl: SGL to be filled in
1155 *
1156 * If successful, this function returns the number of SG elements. Otherwise,
1157 * it returnes -1.
1158 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001159static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001160megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1161 union megasas_sgl *mfi_sgl)
1162{
1163 int i;
1164 int sge_count;
1165 struct scatterlist *os_sgl;
1166
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001167 sge_count = scsi_dma_map(scp);
1168 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001169
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001170 if (sge_count) {
1171 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301172 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1173 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001174 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001175 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001176 return sge_count;
1177}
1178
Yang, Bof4c9a132009-10-06 14:43:28 -06001179/**
1180 * megasas_make_sgl_skinny - Prepares IEEE SGL
1181 * @instance: Adapter soft state
1182 * @scp: SCSI command from the mid-layer
1183 * @mfi_sgl: SGL to be filled in
1184 *
1185 * If successful, this function returns the number of SG elements. Otherwise,
1186 * it returnes -1.
1187 */
1188static int
1189megasas_make_sgl_skinny(struct megasas_instance *instance,
1190 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1191{
1192 int i;
1193 int sge_count;
1194 struct scatterlist *os_sgl;
1195
1196 sge_count = scsi_dma_map(scp);
1197
1198 if (sge_count) {
1199 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301200 mfi_sgl->sge_skinny[i].length =
1201 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001202 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301203 cpu_to_le64(sg_dma_address(os_sgl));
1204 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001205 }
1206 }
1207 return sge_count;
1208}
1209
Sumant Patrob1df99d2006-10-03 12:40:47 -07001210 /**
1211 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001212 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001213 * @sge_count : number of sg elements
1214 *
1215 * Returns the number of frames required for numnber of sge's (sge_count)
1216 */
1217
Yang, Bof4c9a132009-10-06 14:43:28 -06001218static u32 megasas_get_frame_count(struct megasas_instance *instance,
1219 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001220{
1221 int num_cnt;
1222 int sge_bytes;
1223 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001224 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001225
1226 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1227 sizeof(struct megasas_sge32);
1228
Yang, Bof4c9a132009-10-06 14:43:28 -06001229 if (instance->flag_ieee) {
1230 sge_sz = sizeof(struct megasas_sge_skinny);
1231 }
1232
Sumant Patrob1df99d2006-10-03 12:40:47 -07001233 /*
bo yangd532dbe2008-03-17 03:36:43 -04001234 * Main frame can contain 2 SGEs for 64-bit SGLs and
1235 * 3 SGEs for 32-bit SGLs for ldio &
1236 * 1 SGEs for 64-bit SGLs and
1237 * 2 SGEs for 32-bit SGLs for pthru frame
1238 */
1239 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001240 if (instance->flag_ieee == 1) {
1241 num_cnt = sge_count - 1;
1242 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001243 num_cnt = sge_count - 1;
1244 else
1245 num_cnt = sge_count - 2;
1246 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001247 if (instance->flag_ieee == 1) {
1248 num_cnt = sge_count - 1;
1249 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001250 num_cnt = sge_count - 2;
1251 else
1252 num_cnt = sge_count - 3;
1253 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001254
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001255 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001256 sge_bytes = sge_sz * num_cnt;
1257
1258 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1259 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1260 }
1261 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001262 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001263
1264 if (frame_count > 7)
1265 frame_count = 8;
1266 return frame_count;
1267}
1268
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001269/**
1270 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1271 * @instance: Adapter soft state
1272 * @scp: SCSI command
1273 * @cmd: Command to be prepared in
1274 *
1275 * This function prepares CDB commands. These are typcially pass-through
1276 * commands to the devices.
1277 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001278static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001279megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1280 struct megasas_cmd *cmd)
1281{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001282 u32 is_logical;
1283 u32 device_id;
1284 u16 flags = 0;
1285 struct megasas_pthru_frame *pthru;
1286
Shivasharan S3cabd162017-02-10 00:59:05 -08001287 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301288 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001289 pthru = (struct megasas_pthru_frame *)cmd->frame;
1290
1291 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1292 flags = MFI_FRAME_DIR_WRITE;
1293 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1294 flags = MFI_FRAME_DIR_READ;
1295 else if (scp->sc_data_direction == PCI_DMA_NONE)
1296 flags = MFI_FRAME_DIR_NONE;
1297
Yang, Bof4c9a132009-10-06 14:43:28 -06001298 if (instance->flag_ieee == 1) {
1299 flags |= MFI_FRAME_IEEE;
1300 }
1301
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001302 /*
1303 * Prepare the DCDB frame
1304 */
1305 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1306 pthru->cmd_status = 0x0;
1307 pthru->scsi_status = 0x0;
1308 pthru->target_id = device_id;
1309 pthru->lun = scp->device->lun;
1310 pthru->cdb_len = scp->cmd_len;
1311 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001312 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301313 pthru->flags = cpu_to_le16(flags);
1314 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001315
1316 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1317
1318 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001319 * If the command is for the tape device, set the
1320 * pthru timeout to the os layer timeout value.
1321 */
Yang, Bo8d568252009-10-06 14:12:21 -06001322 if (scp->device->type == TYPE_TAPE) {
1323 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301324 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001325 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301326 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001327 }
1328
1329 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001330 * Construct SGL
1331 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001332 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301333 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001334 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1335 &pthru->sgl);
1336 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301337 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001338 pthru->sge_count = megasas_make_sgl64(instance, scp,
1339 &pthru->sgl);
1340 } else
1341 pthru->sge_count = megasas_make_sgl32(instance, scp,
1342 &pthru->sgl);
1343
Yang, Bobdc6fb82009-12-06 08:30:19 -07001344 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001345 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001346 pthru->sge_count);
1347 return 0;
1348 }
1349
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001350 /*
1351 * Sense info specific
1352 */
1353 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301354 pthru->sense_buf_phys_addr_hi =
1355 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1356 pthru->sense_buf_phys_addr_lo =
1357 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001358
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001359 /*
1360 * Compute the total number of frames this command consumes. FW uses
1361 * this number to pull sufficient number of frames from host memory.
1362 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001363 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001364 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001365
1366 return cmd->frame_count;
1367}
1368
1369/**
1370 * megasas_build_ldio - Prepares IOs to logical devices
1371 * @instance: Adapter soft state
1372 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001373 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001374 *
1375 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1376 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001377static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001378megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1379 struct megasas_cmd *cmd)
1380{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001381 u32 device_id;
1382 u8 sc = scp->cmnd[0];
1383 u16 flags = 0;
1384 struct megasas_io_frame *ldio;
1385
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301386 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001387 ldio = (struct megasas_io_frame *)cmd->frame;
1388
1389 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1390 flags = MFI_FRAME_DIR_WRITE;
1391 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1392 flags = MFI_FRAME_DIR_READ;
1393
Yang, Bof4c9a132009-10-06 14:43:28 -06001394 if (instance->flag_ieee == 1) {
1395 flags |= MFI_FRAME_IEEE;
1396 }
1397
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001398 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001399 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001400 */
1401 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1402 ldio->cmd_status = 0x0;
1403 ldio->scsi_status = 0x0;
1404 ldio->target_id = device_id;
1405 ldio->timeout = 0;
1406 ldio->reserved_0 = 0;
1407 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301408 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001409 ldio->start_lba_hi = 0;
1410 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1411
1412 /*
1413 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1414 */
1415 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301416 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1417 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1418 ((u32) scp->cmnd[2] << 8) |
1419 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001420
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301421 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001422 }
1423
1424 /*
1425 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1426 */
1427 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301428 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1429 ((u32) scp->cmnd[7] << 8));
1430 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1431 ((u32) scp->cmnd[3] << 16) |
1432 ((u32) scp->cmnd[4] << 8) |
1433 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001434 }
1435
1436 /*
1437 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1438 */
1439 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301440 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1441 ((u32) scp->cmnd[7] << 16) |
1442 ((u32) scp->cmnd[8] << 8) |
1443 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001444
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301445 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1446 ((u32) scp->cmnd[3] << 16) |
1447 ((u32) scp->cmnd[4] << 8) |
1448 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001449 }
1450
1451 /*
1452 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1453 */
1454 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301455 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1456 ((u32) scp->cmnd[11] << 16) |
1457 ((u32) scp->cmnd[12] << 8) |
1458 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001459
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301460 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1461 ((u32) scp->cmnd[7] << 16) |
1462 ((u32) scp->cmnd[8] << 8) |
1463 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301465 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1466 ((u32) scp->cmnd[3] << 16) |
1467 ((u32) scp->cmnd[4] << 8) |
1468 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001469
1470 }
1471
1472 /*
1473 * Construct SGL
1474 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001475 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301476 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001477 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1478 &ldio->sgl);
1479 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301480 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001481 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1482 } else
1483 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1484
Yang, Bobdc6fb82009-12-06 08:30:19 -07001485 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001486 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001487 ldio->sge_count);
1488 return 0;
1489 }
1490
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001491 /*
1492 * Sense info specific
1493 */
1494 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1495 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301496 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001497
Sumant Patrob1df99d2006-10-03 12:40:47 -07001498 /*
1499 * Compute the total number of frames this command consumes. FW uses
1500 * this number to pull sufficient number of frames from host memory.
1501 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001502 cmd->frame_count = megasas_get_frame_count(instance,
1503 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001504
1505 return cmd->frame_count;
1506}
1507
1508/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301509 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1510 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001511 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001512 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001513 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301514inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001515{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301516 int ret;
1517
Sumant Patrocb59aa62006-01-25 11:53:25 -08001518 switch (cmd->cmnd[0]) {
1519 case READ_10:
1520 case WRITE_10:
1521 case READ_12:
1522 case WRITE_12:
1523 case READ_6:
1524 case WRITE_6:
1525 case READ_16:
1526 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001527 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301528 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1529 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001530 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001531 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301532 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001533 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301534 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001535}
1536
Sumant Patro658dced2006-10-03 13:09:14 -07001537 /**
1538 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001539 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001540 * @instance: Adapter soft state
1541 */
1542static inline void
1543megasas_dump_pending_frames(struct megasas_instance *instance)
1544{
1545 struct megasas_cmd *cmd;
1546 int i,n;
1547 union megasas_sgl *mfi_sgl;
1548 struct megasas_io_frame *ldio;
1549 struct megasas_pthru_frame *pthru;
1550 u32 sgcount;
1551 u32 max_cmd = instance->max_fw_cmds;
1552
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001553 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1554 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 -07001555 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001556 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 -07001557 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001558 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 -07001559
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001560 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001561 for (i = 0; i < max_cmd; i++) {
1562 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001563 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001564 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001565 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 +05301566 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001567 ldio = (struct megasas_io_frame *)cmd->frame;
1568 mfi_sgl = &ldio->sgl;
1569 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001570 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 +05301571 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1572 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1573 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1574 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001575 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001576 pthru = (struct megasas_pthru_frame *) cmd->frame;
1577 mfi_sgl = &pthru->sgl;
1578 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001579 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 +05301580 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1581 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1582 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1583 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001584 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001585 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1586 for (n = 0; n < sgcount; n++) {
1587 if (IS_DMA64)
1588 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1589 le32_to_cpu(mfi_sgl->sge64[n].length),
1590 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1591 else
1592 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1593 le32_to_cpu(mfi_sgl->sge32[n].length),
1594 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001595 }
1596 }
Sumant Patro658dced2006-10-03 13:09:14 -07001597 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001598 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001599 for (i = 0; i < max_cmd; i++) {
1600
1601 cmd = instance->cmd_list[i];
1602
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001603 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001604 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001605 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001606 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001607}
1608
adam radfordcd50ba82010-12-21 10:23:23 -08001609u32
1610megasas_build_and_issue_cmd(struct megasas_instance *instance,
1611 struct scsi_cmnd *scmd)
1612{
1613 struct megasas_cmd *cmd;
1614 u32 frame_count;
1615
1616 cmd = megasas_get_cmd(instance);
1617 if (!cmd)
1618 return SCSI_MLQUEUE_HOST_BUSY;
1619
1620 /*
1621 * Logical drive command
1622 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301623 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001624 frame_count = megasas_build_ldio(instance, scmd, cmd);
1625 else
1626 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1627
1628 if (!frame_count)
1629 goto out_return_cmd;
1630
1631 cmd->scmd = scmd;
1632 scmd->SCp.ptr = (char *)cmd;
1633
1634 /*
1635 * Issue the command to the FW
1636 */
1637 atomic_inc(&instance->fw_outstanding);
1638
1639 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1640 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001641
1642 return 0;
1643out_return_cmd:
1644 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301645 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001646}
1647
1648
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001649/**
1650 * megasas_queue_command - Queue entry point
1651 * @scmd: SCSI command to be queued
1652 * @done: Callback entry point
1653 */
1654static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301655megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001656{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001657 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301658 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001659
1660 instance = (struct megasas_instance *)
1661 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001662
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301663 if (instance->unload == 1) {
1664 scmd->result = DID_NO_CONNECT << 16;
1665 scmd->scsi_done(scmd);
1666 return 0;
1667 }
1668
bo yang39a98552010-09-22 22:36:29 -04001669 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001670 return SCSI_MLQUEUE_HOST_BUSY;
1671
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301672
adam radford229fe472014-03-10 02:51:56 -07001673 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301674 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001675 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001676 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001677 return SCSI_MLQUEUE_HOST_BUSY;
1678 } else {
adam radford229fe472014-03-10 02:51:56 -07001679 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301680 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001681 return 0;
1682 }
1683 }
1684
Sumit Saxena8a01a412016-01-28 21:04:32 +05301685 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001686 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301687 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301688 return 0;
1689 }
1690
Sumit Saxena18365b12016-01-28 21:04:25 +05301691 mr_device_priv_data = scmd->device->hostdata;
1692 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301693 scmd->result = DID_NO_CONNECT << 16;
1694 scmd->scsi_done(scmd);
1695 return 0;
1696 }
1697
Sumit Saxena8a01a412016-01-28 21:04:32 +05301698 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001699 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001700
Sumit Saxena8a01a412016-01-28 21:04:32 +05301701 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301702 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301703
bo yang39a98552010-09-22 22:36:29 -04001704
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001705 scmd->result = 0;
1706
Shivasharan S3cabd162017-02-10 00:59:05 -08001707 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301708 (scmd->device->id >= instance->fw_supported_vd_count ||
1709 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001710 scmd->result = DID_BAD_TARGET << 16;
1711 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001712 }
1713
Shivasharan S3cabd162017-02-10 00:59:05 -08001714 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1715 MEGASAS_IS_LOGICAL(scmd->device) &&
1716 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001717 scmd->result = DID_OK << 16;
1718 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001719 }
1720
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301721 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001722
Sumant Patrocb59aa62006-01-25 11:53:25 -08001723 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301724 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001725 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001726}
1727
Yang, Bo044833b2009-10-06 14:33:06 -06001728static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1729{
1730 int i;
1731
1732 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1733
1734 if ((megasas_mgmt_info.instance[i]) &&
1735 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1736 return megasas_mgmt_info.instance[i];
1737 }
1738
1739 return NULL;
1740}
1741
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301742/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001743* megasas_set_dynamic_target_properties -
1744* Device property set by driver may not be static and it is required to be
1745* updated after OCR
1746*
1747* set tm_capable.
1748* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301749*
1750* @sdev: OS provided scsi device
1751*
1752* Returns void
1753*/
Shivasharan S15dd0382017-02-10 00:59:10 -08001754void megasas_set_dynamic_target_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301755{
Shivasharan S15dd0382017-02-10 00:59:10 -08001756 u16 pd_index = 0, ld;
1757 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301758 struct megasas_instance *instance;
1759 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301760 struct MR_PRIV_DEVICE *mr_device_priv_data;
1761 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301762 struct MR_LD_RAID *raid;
1763 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1764
1765 instance = megasas_lookup_instance(sdev->host->host_no);
1766 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301767 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301768
Shivasharan Sed981b82017-02-10 00:59:06 -08001769 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301770 return;
1771
Shivasharan Sed981b82017-02-10 00:59:06 -08001772 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301773 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1774 + sdev->id;
1775 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1776 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001777 if (ld >= instance->fw_supported_vd_count)
1778 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301779 raid = MR_LdRaidGet(ld, local_map_ptr);
1780
1781 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001782 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001783
Sumit Saxena18365b12016-01-28 21:04:25 +05301784 mr_device_priv_data->is_tm_capable =
1785 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001786 } else if (instance->use_seqnum_jbod_fp) {
1787 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001788 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001789 pd_sync = (void *)fusion->pd_seq_sync
1790 [(instance->pd_seq_map_id - 1) & 1];
1791 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001792 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301793 }
1794}
1795
Shivasharan S15dd0382017-02-10 00:59:10 -08001796/*
1797 * megasas_set_nvme_device_properties -
1798 * set nomerges=2
1799 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1800 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1801 *
1802 * MR firmware provides value in KB. Caller of this function converts
1803 * kb into bytes.
1804 *
1805 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1806 * MR firmware provides value 128 as (32 * 4K) = 128K.
1807 *
1808 * @sdev: scsi device
1809 * @max_io_size: maximum io transfer size
1810 *
1811 */
1812static inline void
1813megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301814{
Sumit Saxena2216c302016-01-28 21:04:26 +05301815 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001816 u32 mr_nvme_pg_size;
1817
1818 instance = (struct megasas_instance *)sdev->host->hostdata;
1819 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1820 MR_DEFAULT_NVME_PAGE_SIZE);
1821
1822 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1823
1824 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1825 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1826}
1827
1828
1829/*
1830 * megasas_set_static_target_properties -
1831 * Device property set by driver are static and it is not required to be
1832 * updated after OCR.
1833 *
1834 * set io timeout
1835 * set device queue depth
1836 * set nvme device properties. see - megasas_set_nvme_device_properties
1837 *
1838 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001839 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001840 */
Shivasharan S96188a82017-02-10 00:59:11 -08001841static void megasas_set_static_target_properties(struct scsi_device *sdev,
1842 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001843{
1844 u16 target_index = 0;
1845 u8 interface_type;
1846 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1847 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001848 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001849 struct megasas_instance *instance;
1850 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301851
1852 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001853 mr_device_priv_data = sdev->hostdata;
1854 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301855
Shivasharan S15dd0382017-02-10 00:59:10 -08001856 /*
1857 * The RAID firmware may require extended timeouts.
1858 */
1859 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301860
Shivasharan S15dd0382017-02-10 00:59:10 -08001861 target_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05301862
Shivasharan S15dd0382017-02-10 00:59:10 -08001863 switch (interface_type) {
1864 case SAS_PD:
1865 device_qd = MEGASAS_SAS_QD;
1866 break;
1867 case SATA_PD:
1868 device_qd = MEGASAS_SATA_QD;
1869 break;
1870 case NVME_PD:
1871 device_qd = MEGASAS_NVME_QD;
1872 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301873 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001874
Shivasharan S96188a82017-02-10 00:59:11 -08001875 if (is_target_prop) {
1876 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1877 if (tgt_device_qd &&
1878 (tgt_device_qd <= instance->host->can_queue))
1879 device_qd = tgt_device_qd;
1880
1881 /* max_io_size_kb will be set to non zero for
1882 * nvme based vd and syspd.
1883 */
1884 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1885 }
1886
Shivasharan S15dd0382017-02-10 00:59:10 -08001887 if (instance->nvme_page_size && max_io_size_kb)
1888 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1889
1890 scsi_change_queue_depth(sdev, device_qd);
1891
Sumit Saxena2216c302016-01-28 21:04:26 +05301892}
1893
Sumit Saxena18365b12016-01-28 21:04:25 +05301894
Christoph Hellwig147aab62006-02-17 12:13:48 +01001895static int megasas_slave_configure(struct scsi_device *sdev)
1896{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301897 u16 pd_index = 0;
1898 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08001899 int ret_target_prop = DCMD_FAILED;
1900 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301901
1902 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001903 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001904 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301905 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1906 sdev->id;
1907 if (instance->pd_list[pd_index].driveState !=
1908 MR_PD_STATE_SYSTEM)
1909 return -ENXIO;
1910 }
1911 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301912
Shivasharan S15dd0382017-02-10 00:59:10 -08001913 mutex_lock(&instance->hba_mutex);
1914 /* Send DCMD to Firmware and cache the information */
1915 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
1916 megasas_get_pd_info(instance, sdev);
1917
Shivasharan S96188a82017-02-10 00:59:11 -08001918 /* Some ventura firmware may not have instance->nvme_page_size set.
1919 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
1920 */
1921 if ((instance->tgt_prop) && (instance->nvme_page_size))
1922 ret_target_prop = megasas_get_target_prop(instance, sdev);
1923
1924 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
1925 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08001926
1927 mutex_unlock(&instance->hba_mutex);
1928
1929 /* This sdev property may change post OCR */
1930 megasas_set_dynamic_target_properties(sdev);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301931
Yang, Bo044833b2009-10-06 14:33:06 -06001932 return 0;
1933}
1934
1935static int megasas_slave_alloc(struct scsi_device *sdev)
1936{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001937 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001938 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301939 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001940
Yang, Bo044833b2009-10-06 14:33:06 -06001941 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08001942 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06001943 /*
1944 * Open the OS scan to the SYSTEM PD
1945 */
1946 pd_index =
1947 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1948 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08001949 if ((instance->pd_list_not_supported ||
1950 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301951 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301952 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001953 }
1954 return -ENXIO;
1955 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301956
1957scan_target:
1958 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1959 GFP_KERNEL);
1960 if (!mr_device_priv_data)
1961 return -ENOMEM;
1962 sdev->hostdata = mr_device_priv_data;
Christoph Hellwig147aab62006-02-17 12:13:48 +01001963 return 0;
1964}
1965
Sumit Saxena18365b12016-01-28 21:04:25 +05301966static void megasas_slave_destroy(struct scsi_device *sdev)
1967{
1968 kfree(sdev->hostdata);
1969 sdev->hostdata = NULL;
1970}
1971
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301972/*
1973* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1974* kill adapter
1975* @instance: Adapter soft state
1976*
1977*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301978static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301979{
1980 int i;
1981 struct megasas_cmd *cmd_mfi;
1982 struct megasas_cmd_fusion *cmd_fusion;
1983 struct fusion_context *fusion = instance->ctrl_context;
1984
1985 /* Find all outstanding ioctls */
1986 if (fusion) {
1987 for (i = 0; i < instance->max_fw_cmds; i++) {
1988 cmd_fusion = fusion->cmd_list[i];
1989 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1990 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1991 if (cmd_mfi->sync_cmd &&
1992 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1993 megasas_complete_cmd(instance,
1994 cmd_mfi, DID_OK);
1995 }
1996 }
1997 } else {
1998 for (i = 0; i < instance->max_fw_cmds; i++) {
1999 cmd_mfi = instance->cmd_list[i];
2000 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2001 MFI_CMD_ABORT)
2002 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2003 }
2004 }
2005}
2006
2007
adam radford9c915a82010-12-21 13:34:31 -08002008void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002009{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302010 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302011 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302012 /* Wait 1 second to ensure IO or ioctls in build have posted */
2013 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002014 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302015 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302016 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002017 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07002018 /* Flush */
2019 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302020 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002021 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002022 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302023 writel(MFI_STOP_ADP,
2024 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002025 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302026 /* Complete outstanding ioctls when adapter is killed */
2027 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002028}
2029
2030 /**
2031 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2032 * restored to max value
2033 * @instance: Adapter soft state
2034 *
2035 */
2036void
2037megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2038{
2039 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302040
adam radford9c915a82010-12-21 13:34:31 -08002041 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002042 && time_after(jiffies, instance->last_time + 5 * HZ)
2043 && atomic_read(&instance->fw_outstanding) <
2044 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002045
2046 spin_lock_irqsave(instance->host->host_lock, flags);
2047 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002048
Sumit Saxena308ec452016-01-28 21:04:30 +05302049 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002050 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002051 }
2052}
2053
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002054/**
bo yang7343eb62007-11-09 04:35:44 -05002055 * megasas_complete_cmd_dpc - Returns FW's controller structure
2056 * @instance_addr: Address of adapter soft state
2057 *
2058 * Tasklet to complete cmds
2059 */
2060static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2061{
2062 u32 producer;
2063 u32 consumer;
2064 u32 context;
2065 struct megasas_cmd *cmd;
2066 struct megasas_instance *instance =
2067 (struct megasas_instance *)instance_addr;
2068 unsigned long flags;
2069
2070 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302071 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002072 return;
2073
2074 spin_lock_irqsave(&instance->completion_lock, flags);
2075
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302076 producer = le32_to_cpu(*instance->producer);
2077 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002078
2079 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302080 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002081 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002082 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002083 context);
2084 BUG();
2085 }
bo yang7343eb62007-11-09 04:35:44 -05002086
2087 cmd = instance->cmd_list[context];
2088
2089 megasas_complete_cmd(instance, cmd, DID_OK);
2090
2091 consumer++;
2092 if (consumer == (instance->max_fw_cmds + 1)) {
2093 consumer = 0;
2094 }
2095 }
2096
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302097 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002098
2099 spin_unlock_irqrestore(&instance->completion_lock, flags);
2100
2101 /*
2102 * Check if we can restore can_queue
2103 */
adam radford9c915a82010-12-21 13:34:31 -08002104 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002105}
2106
adam radford229fe472014-03-10 02:51:56 -07002107/**
2108 * megasas_start_timer - Initializes a timer object
2109 * @instance: Adapter soft state
2110 * @timer: timer object to be initialized
2111 * @fn: timer function
2112 * @interval: time interval between timer function call
2113 *
2114 */
2115void megasas_start_timer(struct megasas_instance *instance,
2116 struct timer_list *timer,
2117 void *fn, unsigned long interval)
2118{
2119 init_timer(timer);
2120 timer->expires = jiffies + interval;
2121 timer->data = (unsigned long)instance;
2122 timer->function = fn;
2123 add_timer(timer);
2124}
2125
Yang, Bo707e09b2010-10-12 07:20:27 -06002126static void
2127megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2128
2129static void
2130process_fw_state_change_wq(struct work_struct *work);
2131
2132void megasas_do_ocr(struct megasas_instance *instance)
2133{
2134 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2135 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2136 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302137 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002138 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302139 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302140 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002141 instance->issuepend_done = 0;
2142
2143 atomic_set(&instance->fw_outstanding, 0);
2144 megasas_internal_reset_defer_cmds(instance);
2145 process_fw_state_change_wq(&instance->work_init);
2146}
2147
Adam Radford4cbfea82014-07-09 15:17:56 -07002148static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2149 int initial)
adam radford229fe472014-03-10 02:51:56 -07002150{
2151 struct megasas_cmd *cmd;
2152 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002153 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002154 dma_addr_t new_affiliation_111_h;
2155 int ld, retval = 0;
2156 u8 thisVf;
2157
2158 cmd = megasas_get_cmd(instance);
2159
2160 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002161 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2162 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002163 instance->host->host_no);
2164 return -ENOMEM;
2165 }
2166
2167 dcmd = &cmd->frame->dcmd;
2168
Adam Radford4cbfea82014-07-09 15:17:56 -07002169 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002170 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2171 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002172 megasas_return_cmd(instance, cmd);
2173 return -ENOMEM;
2174 }
2175
2176 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002177 memset(instance->vf_affiliation_111, 0,
2178 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002179 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002180 new_affiliation_111 =
2181 pci_alloc_consistent(instance->pdev,
2182 sizeof(struct MR_LD_VF_AFFILIATION_111),
2183 &new_affiliation_111_h);
2184 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002185 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2186 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002187 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002188 megasas_return_cmd(instance, cmd);
2189 return -ENOMEM;
2190 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002191 memset(new_affiliation_111, 0,
2192 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002193 }
2194
2195 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2196
2197 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302198 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002199 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302200 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002201 dcmd->timeout = 0;
2202 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302203 dcmd->data_xfer_len =
2204 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2205 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002206
Adam Radford4cbfea82014-07-09 15:17:56 -07002207 if (initial)
2208 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302209 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002210 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302211 dcmd->sgl.sge32[0].phys_addr =
2212 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002213
Christoph Hellwig2213a462015-04-23 16:33:54 +05302214 dcmd->sgl.sge32[0].length = cpu_to_le32(
2215 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002216
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002217 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002218 "scsi%d\n", instance->host->host_no);
2219
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302220 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002221 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2222 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002223 dcmd->cmd_status, instance->host->host_no);
2224 retval = 1; /* Do a scan if we couldn't get affiliation */
2225 goto out;
2226 }
2227
2228 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002229 thisVf = new_affiliation_111->thisVf;
2230 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2231 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2232 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002233 dev_warn(&instance->pdev->dev, "SR-IOV: "
2234 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002235 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002236 memcpy(instance->vf_affiliation_111,
2237 new_affiliation_111,
2238 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002239 retval = 1;
2240 goto out;
2241 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002242 }
2243out:
2244 if (new_affiliation_111) {
2245 pci_free_consistent(instance->pdev,
2246 sizeof(struct MR_LD_VF_AFFILIATION_111),
2247 new_affiliation_111,
2248 new_affiliation_111_h);
2249 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302250
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302251 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002252
2253 return retval;
2254}
2255
2256static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2257 int initial)
2258{
2259 struct megasas_cmd *cmd;
2260 struct megasas_dcmd_frame *dcmd;
2261 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2262 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2263 dma_addr_t new_affiliation_h;
2264 int i, j, retval = 0, found = 0, doscan = 0;
2265 u8 thisVf;
2266
2267 cmd = megasas_get_cmd(instance);
2268
2269 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002270 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2271 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002272 instance->host->host_no);
2273 return -ENOMEM;
2274 }
2275
2276 dcmd = &cmd->frame->dcmd;
2277
2278 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002279 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2280 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002281 megasas_return_cmd(instance, cmd);
2282 return -ENOMEM;
2283 }
2284
2285 if (initial)
2286 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2287 sizeof(struct MR_LD_VF_AFFILIATION));
2288 else {
2289 new_affiliation =
2290 pci_alloc_consistent(instance->pdev,
2291 (MAX_LOGICAL_DRIVES + 1) *
2292 sizeof(struct MR_LD_VF_AFFILIATION),
2293 &new_affiliation_h);
2294 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002295 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2296 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002297 instance->host->host_no);
2298 megasas_return_cmd(instance, cmd);
2299 return -ENOMEM;
2300 }
2301 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2302 sizeof(struct MR_LD_VF_AFFILIATION));
2303 }
2304
2305 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2306
2307 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302308 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002309 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302310 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002311 dcmd->timeout = 0;
2312 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302313 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2314 sizeof(struct MR_LD_VF_AFFILIATION));
2315 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002316
2317 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302318 dcmd->sgl.sge32[0].phys_addr =
2319 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002320 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302321 dcmd->sgl.sge32[0].phys_addr =
2322 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002323
Christoph Hellwig2213a462015-04-23 16:33:54 +05302324 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2325 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002326
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002327 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002328 "scsi%d\n", instance->host->host_no);
2329
Adam Radford4cbfea82014-07-09 15:17:56 -07002330
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302331 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002332 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2333 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002334 dcmd->cmd_status, instance->host->host_no);
2335 retval = 1; /* Do a scan if we couldn't get affiliation */
2336 goto out;
2337 }
2338
2339 if (!initial) {
2340 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002341 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2342 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002343 instance->host->host_no);
2344 retval = 1;
2345 goto out;
2346 }
2347 newmap = new_affiliation->map;
2348 savedmap = instance->vf_affiliation->map;
2349 thisVf = new_affiliation->thisVf;
2350 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2351 found = 0;
2352 for (j = 0; j < instance->vf_affiliation->ldCount;
2353 j++) {
2354 if (newmap->ref.targetId ==
2355 savedmap->ref.targetId) {
2356 found = 1;
2357 if (newmap->policy[thisVf] !=
2358 savedmap->policy[thisVf]) {
2359 doscan = 1;
2360 goto out;
2361 }
adam radford229fe472014-03-10 02:51:56 -07002362 }
2363 savedmap = (struct MR_LD_VF_MAP *)
2364 ((unsigned char *)savedmap +
2365 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002366 }
2367 if (!found && newmap->policy[thisVf] !=
2368 MR_LD_ACCESS_HIDDEN) {
2369 doscan = 1;
2370 goto out;
2371 }
2372 newmap = (struct MR_LD_VF_MAP *)
2373 ((unsigned char *)newmap + newmap->size);
2374 }
2375
2376 newmap = new_affiliation->map;
2377 savedmap = instance->vf_affiliation->map;
2378
2379 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2380 found = 0;
2381 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2382 if (savedmap->ref.targetId ==
2383 newmap->ref.targetId) {
2384 found = 1;
2385 if (savedmap->policy[thisVf] !=
2386 newmap->policy[thisVf]) {
2387 doscan = 1;
2388 goto out;
2389 }
2390 }
adam radford229fe472014-03-10 02:51:56 -07002391 newmap = (struct MR_LD_VF_MAP *)
2392 ((unsigned char *)newmap +
2393 newmap->size);
2394 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002395 if (!found && savedmap->policy[thisVf] !=
2396 MR_LD_ACCESS_HIDDEN) {
2397 doscan = 1;
2398 goto out;
2399 }
2400 savedmap = (struct MR_LD_VF_MAP *)
2401 ((unsigned char *)savedmap +
2402 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002403 }
2404 }
2405out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002406 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002407 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2408 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002409 memcpy(instance->vf_affiliation, new_affiliation,
2410 new_affiliation->size);
2411 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002412 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002413
2414 if (new_affiliation)
2415 pci_free_consistent(instance->pdev,
2416 (MAX_LOGICAL_DRIVES + 1) *
2417 sizeof(struct MR_LD_VF_AFFILIATION),
2418 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302419 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002420
2421 return retval;
2422}
2423
Adam Radford4cbfea82014-07-09 15:17:56 -07002424/* This function will get the current SR-IOV LD/VF affiliation */
2425static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2426 int initial)
2427{
2428 int retval;
2429
2430 if (instance->PlasmaFW111)
2431 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2432 else
2433 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2434 return retval;
2435}
2436
adam radford229fe472014-03-10 02:51:56 -07002437/* This function will tell FW to start the SR-IOV heartbeat */
2438int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2439 int initial)
2440{
2441 struct megasas_cmd *cmd;
2442 struct megasas_dcmd_frame *dcmd;
2443 int retval = 0;
2444
2445 cmd = megasas_get_cmd(instance);
2446
2447 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002448 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2449 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002450 instance->host->host_no);
2451 return -ENOMEM;
2452 }
2453
2454 dcmd = &cmd->frame->dcmd;
2455
2456 if (initial) {
2457 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002458 pci_zalloc_consistent(instance->pdev,
2459 sizeof(struct MR_CTRL_HB_HOST_MEM),
2460 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002461 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002462 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2463 " memory for heartbeat host memory for scsi%d\n",
2464 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002465 retval = -ENOMEM;
2466 goto out;
2467 }
adam radford229fe472014-03-10 02:51:56 -07002468 }
2469
2470 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2471
Christoph Hellwig2213a462015-04-23 16:33:54 +05302472 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002473 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302474 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002475 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302476 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002477 dcmd->timeout = 0;
2478 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302479 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2480 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2481 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2482 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002483
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002484 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002485 instance->host->host_no);
2486
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302487 if (instance->ctrl_context && !instance->mask_interrupts)
2488 retval = megasas_issue_blocked_cmd(instance, cmd,
2489 MEGASAS_ROUTINE_WAIT_TIME_VF);
2490 else
2491 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002492
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302493 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302494 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2495 "_MEM_ALLOC DCMD %s for scsi%d\n",
2496 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2497 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002498 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002499 }
2500
2501out:
2502 megasas_return_cmd(instance, cmd);
2503
2504 return retval;
2505}
2506
2507/* Handler for SR-IOV heartbeat */
2508void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2509{
2510 struct megasas_instance *instance =
2511 (struct megasas_instance *)instance_addr;
2512
2513 if (instance->hb_host_mem->HB.fwCounter !=
2514 instance->hb_host_mem->HB.driverCounter) {
2515 instance->hb_host_mem->HB.driverCounter =
2516 instance->hb_host_mem->HB.fwCounter;
2517 mod_timer(&instance->sriov_heartbeat_timer,
2518 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2519 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002520 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002521 "completed for scsi%d\n", instance->host->host_no);
2522 schedule_work(&instance->work_init);
2523 }
2524}
2525
bo yang7343eb62007-11-09 04:35:44 -05002526/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002527 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2528 * @instance: Adapter soft state
2529 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002530 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002531 * complete all its outstanding commands. Returns error if one or more IOs
2532 * are pending after this time period. It also marks the controller dead.
2533 */
2534static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2535{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302536 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002537 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002538 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002539 unsigned long flags;
2540 struct list_head clist_local;
2541 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002542 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002543
Sumit Saxenaccc75072016-01-28 21:04:33 +05302544 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2545 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2546 __func__, __LINE__);
2547 return FAILED;
2548 }
bo yang39a98552010-09-22 22:36:29 -04002549
Sumit Saxena8a01a412016-01-28 21:04:32 +05302550 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002551
2552 INIT_LIST_HEAD(&clist_local);
2553 spin_lock_irqsave(&instance->hba_lock, flags);
2554 list_splice_init(&instance->internal_reset_pending_q,
2555 &clist_local);
2556 spin_unlock_irqrestore(&instance->hba_lock, flags);
2557
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002558 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002559 for (i = 0; i < wait_time; i++) {
2560 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302561 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002562 break;
2563 }
2564
Sumit Saxena8a01a412016-01-28 21:04:32 +05302565 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002566 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302567 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002568 return FAILED;
2569 }
2570
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002571 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002572 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002573 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002574 struct megasas_cmd, list);
2575 list_del_init(&reset_cmd->list);
2576 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002577 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002578 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002579 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002580 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002581
2582 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2583 megasas_return_cmd(instance, reset_cmd);
2584 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002585 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002586 "reset queue\n",
2587 reset_cmd);
2588
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302589 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002590 instance->instancet->fire_cmd(instance,
2591 reset_cmd->frame_phys_addr,
2592 0, instance->reg_set);
2593 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002594 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002595 "cmds lst\n",
2596 reset_cmd);
2597 }
2598 reset_index++;
2599 }
2600
2601 return SUCCESS;
2602 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002603
adam radfordc007b8b2012-07-17 18:20:24 -07002604 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302605 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002606
2607 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002608 break;
2609
2610 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002611 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002612 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002613 /*
2614 * Call cmd completion routine. Cmd to be
2615 * be completed directly without depending on isr.
2616 */
2617 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002618 }
2619
2620 msleep(1000);
2621 }
2622
Yang, Bo707e09b2010-10-12 07:20:27 -06002623 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302624 outstanding = atomic_read(&instance->fw_outstanding);
2625 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002626
Sumit Saxenaccc75072016-01-28 21:04:33 +05302627 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2628 goto no_outstanding;
2629
2630 if (instance->disableOnlineCtrlReset)
2631 goto kill_hba_and_failed;
2632 do {
2633 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2634 dev_info(&instance->pdev->dev,
2635 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2636 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2637 if (i == 3)
2638 goto kill_hba_and_failed;
2639 megasas_do_ocr(instance);
2640
2641 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2642 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2643 __func__, __LINE__);
2644 return FAILED;
2645 }
2646 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2647 __func__, __LINE__);
2648
2649 for (sl = 0; sl < 10; sl++)
2650 msleep(500);
2651
2652 outstanding = atomic_read(&instance->fw_outstanding);
2653
2654 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2655 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2656 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002657 }
2658 i++;
2659 } while (i <= 3);
2660
Sumit Saxenaccc75072016-01-28 21:04:33 +05302661no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002662
Sumit Saxenaccc75072016-01-28 21:04:33 +05302663 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2664 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002665 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302666
2667kill_hba_and_failed:
2668
2669 /* Reset not supported, kill adapter */
2670 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2671 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2672 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2673 atomic_read(&instance->fw_outstanding));
2674 megasas_dump_pending_frames(instance);
2675 megaraid_sas_kill_hba(instance);
2676
2677 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002678}
2679
2680/**
2681 * megasas_generic_reset - Generic reset routine
2682 * @scmd: Mid-layer SCSI command
2683 *
2684 * This routine implements a generic reset handler for device, bus and host
2685 * reset requests. Device, bus and host specific reset handlers can use this
2686 * function after they do their specific tasks.
2687 */
2688static int megasas_generic_reset(struct scsi_cmnd *scmd)
2689{
2690 int ret_val;
2691 struct megasas_instance *instance;
2692
2693 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2694
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002695 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2696 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002697
Sumit Saxena8a01a412016-01-28 21:04:32 +05302698 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002699 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002700 return FAILED;
2701 }
2702
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002703 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002704 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002705 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002706 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002707 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002708
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002709 return ret_val;
2710}
2711
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002712/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002713 * megasas_reset_timer - quiesce the adapter if required
2714 * @scmd: scsi cmnd
2715 *
2716 * Sets the FW busy flag and reduces the host->can_queue if the
2717 * cmd has not been completed within the timeout period.
2718 */
2719static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002720blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002721{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002722 struct megasas_instance *instance;
2723 unsigned long flags;
2724
2725 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302726 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002727 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002728 }
2729
adam radfordf575c5d2011-10-13 16:01:12 -07002730 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002731 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2732 /* FW is busy, throttle IO */
2733 spin_lock_irqsave(instance->host->host_lock, flags);
2734
adam radfordc5daa6a2012-07-17 18:20:03 -07002735 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002736 instance->last_time = jiffies;
2737 instance->flag |= MEGASAS_FW_BUSY;
2738
2739 spin_unlock_irqrestore(instance->host->host_lock, flags);
2740 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002741 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002742}
2743
2744/**
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002745 * megasas_dump_frame - This function will dump MPT/MFI frame
2746 */
2747static inline void
2748megasas_dump_frame(void *mpi_request, int sz)
2749{
2750 int i;
2751 __le32 *mfp = (__le32 *)mpi_request;
2752
2753 printk(KERN_INFO "IO request frame:\n\t");
2754 for (i = 0; i < sz; i++) {
2755 if (i && ((i % 8) == 0))
2756 printk("\n\t");
2757 printk("%08x ", le32_to_cpu(mfp[i]));
2758 }
2759 printk("\n");
2760}
2761
2762/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002763 * megasas_reset_bus_host - Bus & host reset handler entry point
2764 */
2765static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2766{
2767 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002768 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002769
adam radford9c915a82010-12-21 13:34:31 -08002770 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002771
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002772 scmd_printk(KERN_INFO, scmd,
2773 "Controller reset is requested due to IO timeout\n"
2774 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2775 " SCSI host busy: %d\t FW outstanding: %d\n",
2776 scmd, scmd->device->host->shost_state,
2777 atomic_read((atomic_t *)&scmd->device->host->host_busy),
2778 atomic_read(&instance->fw_outstanding));
2779
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002780 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002781 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002782 */
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002783 if (instance->ctrl_context) {
2784 struct megasas_cmd_fusion *cmd;
2785 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2786 if (cmd)
2787 megasas_dump_frame(cmd->io_request,
2788 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
2789 ret = megasas_reset_fusion(scmd->device->host,
2790 SCSIIO_TIMEOUT_OCR);
2791 } else
adam radford9c915a82010-12-21 13:34:31 -08002792 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002793
2794 return ret;
2795}
2796
2797/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002798 * megasas_task_abort - Issues task abort request to firmware
2799 * (supported only for fusion adapters)
2800 * @scmd: SCSI command pointer
2801 */
2802static int megasas_task_abort(struct scsi_cmnd *scmd)
2803{
2804 int ret;
2805 struct megasas_instance *instance;
2806
2807 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2808
2809 if (instance->ctrl_context)
2810 ret = megasas_task_abort_fusion(scmd);
2811 else {
2812 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2813 ret = FAILED;
2814 }
2815
2816 return ret;
2817}
2818
2819/**
2820 * megasas_reset_target: Issues target reset request to firmware
2821 * (supported only for fusion adapters)
2822 * @scmd: SCSI command pointer
2823 */
2824static int megasas_reset_target(struct scsi_cmnd *scmd)
2825{
2826 int ret;
2827 struct megasas_instance *instance;
2828
2829 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2830
2831 if (instance->ctrl_context)
2832 ret = megasas_reset_target_fusion(scmd);
2833 else {
2834 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2835 ret = FAILED;
2836 }
2837
2838 return ret;
2839}
2840
2841/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002842 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002843 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002844 * @bdev: block device
2845 * @capacity: drive capacity
2846 * @geom: geometry parameters
2847 */
2848static int
2849megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2850 sector_t capacity, int geom[])
2851{
2852 int heads;
2853 int sectors;
2854 sector_t cylinders;
2855 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002856
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002857 /* Default heads (64) & sectors (32) */
2858 heads = 64;
2859 sectors = 32;
2860
2861 tmp = heads * sectors;
2862 cylinders = capacity;
2863
2864 sector_div(cylinders, tmp);
2865
2866 /*
2867 * Handle extended translation size for logical drives > 1Gb
2868 */
2869
2870 if (capacity >= 0x200000) {
2871 heads = 255;
2872 sectors = 63;
2873 tmp = heads*sectors;
2874 cylinders = capacity;
2875 sector_div(cylinders, tmp);
2876 }
2877
2878 geom[0] = heads;
2879 geom[1] = sectors;
2880 geom[2] = cylinders;
2881
2882 return 0;
2883}
2884
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002885static void megasas_aen_polling(struct work_struct *work);
2886
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002887/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002888 * megasas_service_aen - Processes an event notification
2889 * @instance: Adapter soft state
2890 * @cmd: AEN command completed by the ISR
2891 *
2892 * For AEN, driver sends a command down to FW that is held by the FW till an
2893 * event occurs. When an event of interest occurs, FW completes the command
2894 * that it was previously holding.
2895 *
2896 * This routines sends SIGIO signal to processes that have registered with the
2897 * driver for AEN.
2898 */
2899static void
2900megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2901{
Yang, Boc3518832009-10-06 14:18:02 -06002902 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002903
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002904 /*
2905 * Don't signal app if it is just an aborted previously registered aen
2906 */
Yang, Boc3518832009-10-06 14:18:02 -06002907 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2908 spin_lock_irqsave(&poll_aen_lock, flags);
2909 megasas_poll_wait_aen = 1;
2910 spin_unlock_irqrestore(&poll_aen_lock, flags);
2911 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002912 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002913 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002914 else
2915 cmd->abort_aen = 0;
2916
2917 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302918
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302919 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002920
bo yang39a98552010-09-22 22:36:29 -04002921 if ((instance->unload == 0) &&
2922 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002923 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002924
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002925 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2926 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002927 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002928 } else {
2929 ev->instance = instance;
2930 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002931 INIT_DELAYED_WORK(&ev->hotplug_work,
2932 megasas_aen_polling);
2933 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002934 }
2935 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002936}
2937
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302938static ssize_t
2939megasas_fw_crash_buffer_store(struct device *cdev,
2940 struct device_attribute *attr, const char *buf, size_t count)
2941{
2942 struct Scsi_Host *shost = class_to_shost(cdev);
2943 struct megasas_instance *instance =
2944 (struct megasas_instance *) shost->hostdata;
2945 int val = 0;
2946 unsigned long flags;
2947
2948 if (kstrtoint(buf, 0, &val) != 0)
2949 return -EINVAL;
2950
2951 spin_lock_irqsave(&instance->crashdump_lock, flags);
2952 instance->fw_crash_buffer_offset = val;
2953 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2954 return strlen(buf);
2955}
2956
2957static ssize_t
2958megasas_fw_crash_buffer_show(struct device *cdev,
2959 struct device_attribute *attr, char *buf)
2960{
2961 struct Scsi_Host *shost = class_to_shost(cdev);
2962 struct megasas_instance *instance =
2963 (struct megasas_instance *) shost->hostdata;
2964 u32 size;
2965 unsigned long buff_addr;
2966 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2967 unsigned long src_addr;
2968 unsigned long flags;
2969 u32 buff_offset;
2970
2971 spin_lock_irqsave(&instance->crashdump_lock, flags);
2972 buff_offset = instance->fw_crash_buffer_offset;
2973 if (!instance->crash_dump_buf &&
2974 !((instance->fw_crash_state == AVAILABLE) ||
2975 (instance->fw_crash_state == COPYING))) {
2976 dev_err(&instance->pdev->dev,
2977 "Firmware crash dump is not available\n");
2978 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2979 return -EINVAL;
2980 }
2981
2982 buff_addr = (unsigned long) buf;
2983
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002984 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302985 dev_err(&instance->pdev->dev,
2986 "Firmware crash dump offset is out of range\n");
2987 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2988 return 0;
2989 }
2990
2991 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2992 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2993
2994 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2995 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002996 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302997 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2998
2999 return size;
3000}
3001
3002static ssize_t
3003megasas_fw_crash_buffer_size_show(struct device *cdev,
3004 struct device_attribute *attr, char *buf)
3005{
3006 struct Scsi_Host *shost = class_to_shost(cdev);
3007 struct megasas_instance *instance =
3008 (struct megasas_instance *) shost->hostdata;
3009
3010 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3011 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3012}
3013
3014static ssize_t
3015megasas_fw_crash_state_store(struct device *cdev,
3016 struct device_attribute *attr, const char *buf, size_t count)
3017{
3018 struct Scsi_Host *shost = class_to_shost(cdev);
3019 struct megasas_instance *instance =
3020 (struct megasas_instance *) shost->hostdata;
3021 int val = 0;
3022 unsigned long flags;
3023
3024 if (kstrtoint(buf, 0, &val) != 0)
3025 return -EINVAL;
3026
3027 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3028 dev_err(&instance->pdev->dev, "application updates invalid "
3029 "firmware crash state\n");
3030 return -EINVAL;
3031 }
3032
3033 instance->fw_crash_state = val;
3034
3035 if ((val == COPIED) || (val == COPY_ERROR)) {
3036 spin_lock_irqsave(&instance->crashdump_lock, flags);
3037 megasas_free_host_crash_buffer(instance);
3038 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3039 if (val == COPY_ERROR)
3040 dev_info(&instance->pdev->dev, "application failed to "
3041 "copy Firmware crash dump\n");
3042 else
3043 dev_info(&instance->pdev->dev, "Firmware crash dump "
3044 "copied successfully\n");
3045 }
3046 return strlen(buf);
3047}
3048
3049static ssize_t
3050megasas_fw_crash_state_show(struct device *cdev,
3051 struct device_attribute *attr, char *buf)
3052{
3053 struct Scsi_Host *shost = class_to_shost(cdev);
3054 struct megasas_instance *instance =
3055 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003056
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303057 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3058}
3059
3060static ssize_t
3061megasas_page_size_show(struct device *cdev,
3062 struct device_attribute *attr, char *buf)
3063{
3064 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3065}
3066
Sumit Saxena308ec452016-01-28 21:04:30 +05303067static ssize_t
3068megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3069 char *buf)
3070{
3071 struct Scsi_Host *shost = class_to_shost(cdev);
3072 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3073
3074 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3075}
3076
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303077static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3078 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3079static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3080 megasas_fw_crash_buffer_size_show, NULL);
3081static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3082 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3083static DEVICE_ATTR(page_size, S_IRUGO,
3084 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05303085static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3086 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303087
3088struct device_attribute *megaraid_host_attrs[] = {
3089 &dev_attr_fw_crash_buffer_size,
3090 &dev_attr_fw_crash_buffer,
3091 &dev_attr_fw_crash_state,
3092 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303093 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303094 NULL,
3095};
3096
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003097/*
3098 * Scsi host template for megaraid_sas driver
3099 */
3100static struct scsi_host_template megasas_template = {
3101
3102 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303103 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003104 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003105 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003106 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303107 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003108 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003109 .eh_target_reset_handler = megasas_reset_target,
3110 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003111 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003112 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303113 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003114 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003115 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003116 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003117 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003118};
3119
3120/**
3121 * megasas_complete_int_cmd - Completes an internal command
3122 * @instance: Adapter soft state
3123 * @cmd: Command to be completed
3124 *
3125 * The megasas_issue_blocked_cmd() function waits for a command to complete
3126 * after it issues a command. This function wakes up that waiting routine by
3127 * calling wake_up() on the wait queue.
3128 */
3129static void
3130megasas_complete_int_cmd(struct megasas_instance *instance,
3131 struct megasas_cmd *cmd)
3132{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303133 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003134 wake_up(&instance->int_cmd_wait_q);
3135}
3136
3137/**
3138 * megasas_complete_abort - Completes aborting a command
3139 * @instance: Adapter soft state
3140 * @cmd: Cmd that was issued to abort another cmd
3141 *
adam radford0d490162010-12-14 19:17:17 -08003142 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3143 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003144 * wakes up all functions waiting on the same wait queue.
3145 */
3146static void
3147megasas_complete_abort(struct megasas_instance *instance,
3148 struct megasas_cmd *cmd)
3149{
3150 if (cmd->sync_cmd) {
3151 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303152 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003153 wake_up(&instance->abort_cmd_wait_q);
3154 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003155}
3156
3157/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003158 * megasas_complete_cmd - Completes a command
3159 * @instance: Adapter soft state
3160 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003161 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003162 * SCSI mid-layer instead of the value returned
3163 * by the FW. This should be used if caller wants
3164 * an alternate status (as in the case of aborted
3165 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003166 */
adam radford9c915a82010-12-21 13:34:31 -08003167void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003168megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3169 u8 alt_status)
3170{
3171 int exception = 0;
3172 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003173 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003174 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303175 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003176
bo yang39a98552010-09-22 22:36:29 -04003177 /* flag for the retry reset */
3178 cmd->retry_for_fw_reset = 0;
3179
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003180 if (cmd->scmd)
3181 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003182
3183 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003184 case MFI_CMD_INVALID:
3185 /* Some older 1068 controller FW may keep a pended
3186 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3187 when booting the kdump kernel. Ignore this command to
3188 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003189 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3190 "completed\n");
3191 dev_warn(&instance->pdev->dev, "If you have a controller "
3192 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003193 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003194 case MFI_CMD_PD_SCSI_IO:
3195 case MFI_CMD_LD_SCSI_IO:
3196
3197 /*
3198 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3199 * issued either through an IO path or an IOCTL path. If it
3200 * was via IOCTL, we will send it to internal completion.
3201 */
3202 if (cmd->sync_cmd) {
3203 cmd->sync_cmd = 0;
3204 megasas_complete_int_cmd(instance, cmd);
3205 break;
3206 }
3207
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003208 case MFI_CMD_LD_READ:
3209 case MFI_CMD_LD_WRITE:
3210
3211 if (alt_status) {
3212 cmd->scmd->result = alt_status << 16;
3213 exception = 1;
3214 }
3215
3216 if (exception) {
3217
Sumant Patroe4a082c2006-05-30 12:03:37 -07003218 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003219
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003220 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003221 cmd->scmd->scsi_done(cmd->scmd);
3222 megasas_return_cmd(instance, cmd);
3223
3224 break;
3225 }
3226
3227 switch (hdr->cmd_status) {
3228
3229 case MFI_STAT_OK:
3230 cmd->scmd->result = DID_OK << 16;
3231 break;
3232
3233 case MFI_STAT_SCSI_IO_FAILED:
3234 case MFI_STAT_LD_INIT_IN_PROGRESS:
3235 cmd->scmd->result =
3236 (DID_ERROR << 16) | hdr->scsi_status;
3237 break;
3238
3239 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3240
3241 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3242
3243 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3244 memset(cmd->scmd->sense_buffer, 0,
3245 SCSI_SENSE_BUFFERSIZE);
3246 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3247 hdr->sense_len);
3248
3249 cmd->scmd->result |= DRIVER_SENSE << 24;
3250 }
3251
3252 break;
3253
3254 case MFI_STAT_LD_OFFLINE:
3255 case MFI_STAT_DEVICE_NOT_FOUND:
3256 cmd->scmd->result = DID_BAD_TARGET << 16;
3257 break;
3258
3259 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003260 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003261 hdr->cmd_status);
3262 cmd->scmd->result = DID_ERROR << 16;
3263 break;
3264 }
3265
Sumant Patroe4a082c2006-05-30 12:03:37 -07003266 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003267
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003268 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003269 cmd->scmd->scsi_done(cmd->scmd);
3270 megasas_return_cmd(instance, cmd);
3271
3272 break;
3273
3274 case MFI_CMD_SMP:
3275 case MFI_CMD_STP:
3276 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303277 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003278 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303279 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3280 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303281 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003282 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303283 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003284 if (cmd->frame->hdr.cmd_status != 0) {
3285 if (cmd->frame->hdr.cmd_status !=
3286 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003287 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003288 cmd->frame->hdr.cmd_status);
3289 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303290 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003291 spin_unlock_irqrestore(
3292 instance->host->host_lock,
3293 flags);
3294 break;
3295 }
3296 } else
3297 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303298 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303299
3300 /*
3301 * Set fast path IO to ZERO.
3302 * Validate Map will set proper value.
3303 * Meanwhile all IOs will go as LD IO.
3304 */
3305 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003306 fusion->fast_path_io = 1;
3307 else
3308 fusion->fast_path_io = 0;
3309 megasas_sync_map_info(instance);
3310 spin_unlock_irqrestore(instance->host->host_lock,
3311 flags);
3312 break;
3313 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303314 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3315 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003316 spin_lock_irqsave(&poll_aen_lock, flags);
3317 megasas_poll_wait_aen = 0;
3318 spin_unlock_irqrestore(&poll_aen_lock, flags);
3319 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003320
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303321 /* FW has an updated PD sequence */
3322 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3323 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3324
3325 spin_lock_irqsave(instance->host->host_lock, flags);
3326 status = cmd->frame->hdr.cmd_status;
3327 instance->jbod_seq_cmd = NULL;
3328 megasas_return_cmd(instance, cmd);
3329
3330 if (status == MFI_STAT_OK) {
3331 instance->pd_seq_map_id++;
3332 /* Re-register a pd sync seq num cmd */
3333 if (megasas_sync_pd_seq_num(instance, true))
3334 instance->use_seqnum_jbod_fp = false;
3335 } else
3336 instance->use_seqnum_jbod_fp = false;
3337
3338 spin_unlock_irqrestore(instance->host->host_lock, flags);
3339 break;
3340 }
3341
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003342 /*
3343 * See if got an event notification
3344 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303345 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003346 megasas_service_aen(instance, cmd);
3347 else
3348 megasas_complete_int_cmd(instance, cmd);
3349
3350 break;
3351
3352 case MFI_CMD_ABORT:
3353 /*
3354 * Cmd issued to abort another cmd returned
3355 */
3356 megasas_complete_abort(instance, cmd);
3357 break;
3358
3359 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003360 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003361 hdr->cmd);
3362 break;
3363 }
3364}
3365
3366/**
bo yang39a98552010-09-22 22:36:29 -04003367 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003368 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003369 * @instance: Adapter soft state
3370 */
3371static inline void
3372megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3373{
3374 struct megasas_cmd *cmd;
3375 struct list_head clist_local;
3376 union megasas_evt_class_locale class_locale;
3377 unsigned long flags;
3378 u32 seq_num;
3379
3380 INIT_LIST_HEAD(&clist_local);
3381 spin_lock_irqsave(&instance->hba_lock, flags);
3382 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3383 spin_unlock_irqrestore(&instance->hba_lock, flags);
3384
3385 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003386 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003387 struct megasas_cmd, list);
3388 list_del_init(&cmd->list);
3389
3390 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003391 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3392 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003393 cmd, cmd->scmd, cmd->sync_cmd);
3394
3395 cmd->retry_for_fw_reset++;
3396
3397 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003398 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003399 "was tried multiple times during reset."
3400 "Shutting down the HBA\n",
3401 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303402 instance->instancet->disable_intr(instance);
3403 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003404 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003405 return;
3406 }
3407 }
3408
3409 if (cmd->sync_cmd == 1) {
3410 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003411 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003412 "cmd attached to internal command!\n");
3413 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003414 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003415 "on the internal reset queue,"
3416 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303417 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003418 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003419 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003420 0, instance->reg_set);
3421 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003422 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003423 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003424 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003425
3426 atomic_inc(&instance->fw_outstanding);
3427 instance->instancet->fire_cmd(instance,
3428 cmd->frame_phys_addr,
3429 cmd->frame_count-1, instance->reg_set);
3430 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003431 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003432 "internal reset defer list while re-issue!!\n",
3433 cmd);
3434 }
3435 }
3436
3437 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003438 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003439 megasas_return_cmd(instance, instance->aen_cmd);
3440
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003441 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003442 }
3443
3444 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003445 * Initiate AEN (Asynchronous Event Notification)
3446 */
bo yang39a98552010-09-22 22:36:29 -04003447 seq_num = instance->last_seq_num;
3448 class_locale.members.reserved = 0;
3449 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3450 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3451
3452 megasas_register_aen(instance, seq_num, class_locale.word);
3453}
3454
3455/**
3456 * Move the internal reset pending commands to a deferred queue.
3457 *
3458 * We move the commands pending at internal reset time to a
3459 * pending queue. This queue would be flushed after successful
3460 * completion of the internal reset sequence. if the internal reset
3461 * did not complete in time, the kernel reset handler would flush
3462 * these commands.
3463 **/
3464static void
3465megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3466{
3467 struct megasas_cmd *cmd;
3468 int i;
3469 u32 max_cmd = instance->max_fw_cmds;
3470 u32 defer_index;
3471 unsigned long flags;
3472
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003473 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303474 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003475 for (i = 0; i < max_cmd; i++) {
3476 cmd = instance->cmd_list[i];
3477 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003478 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003479 "on the defer queue as internal\n",
3480 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3481
3482 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003483 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003484 " moving this cmd:%p, %d %p, it was"
3485 "discovered on some list?\n",
3486 cmd, cmd->sync_cmd, cmd->scmd);
3487
3488 list_del_init(&cmd->list);
3489 }
3490 defer_index++;
3491 list_add_tail(&cmd->list,
3492 &instance->internal_reset_pending_q);
3493 }
3494 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303495 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003496}
3497
3498
3499static void
3500process_fw_state_change_wq(struct work_struct *work)
3501{
3502 struct megasas_instance *instance =
3503 container_of(work, struct megasas_instance, work_init);
3504 u32 wait;
3505 unsigned long flags;
3506
Sumit Saxena8a01a412016-01-28 21:04:32 +05303507 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003508 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303509 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003510 return ;
3511 }
3512
Sumit Saxena8a01a412016-01-28 21:04:32 +05303513 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003514 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003515 "state, restarting it...\n");
3516
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303517 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003518 atomic_set(&instance->fw_outstanding, 0);
3519
3520 atomic_set(&instance->fw_reset_no_pci_access, 1);
3521 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003522 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003523
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003524 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003525 "initiating next stage...\n");
3526
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003527 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003528 "state 2 starting...\n");
3529
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003530 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003531 for (wait = 0; wait < 30; wait++) {
3532 msleep(1000);
3533 }
3534
adam radford058a8fa2011-10-08 18:14:27 -07003535 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003536 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003537
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303538 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003539 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003540 return ;
3541 }
3542
3543 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3544 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3545 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3546 ) {
3547 *instance->consumer = *instance->producer;
3548 } else {
3549 *instance->consumer = 0;
3550 *instance->producer = 0;
3551 }
3552
3553 megasas_issue_init_mfi(instance);
3554
3555 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303556 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003557 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303558 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003559
3560 megasas_issue_pending_cmds_again(instance);
3561 instance->issuepend_done = 1;
3562 }
bo yang39a98552010-09-22 22:36:29 -04003563}
3564
3565/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003566 * megasas_deplete_reply_queue - Processes all completed commands
3567 * @instance: Adapter soft state
3568 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003569 * SCSI mid-layer instead of the status
3570 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003571 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003572 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003573static int
bo yang39a98552010-09-22 22:36:29 -04003574megasas_deplete_reply_queue(struct megasas_instance *instance,
3575 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003576{
bo yang39a98552010-09-22 22:36:29 -04003577 u32 mfiStatus;
3578 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003579
bo yang39a98552010-09-22 22:36:29 -04003580 if ((mfiStatus = instance->instancet->check_reset(instance,
3581 instance->reg_set)) == 1) {
3582 return IRQ_HANDLED;
3583 }
3584
3585 if ((mfiStatus = instance->instancet->clear_intr(
3586 instance->reg_set)
3587 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003588 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003589 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003590 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003591 }
3592
3593 instance->mfiStatus = mfiStatus;
3594
3595 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3596 fw_state = instance->instancet->read_fw_status_reg(
3597 instance->reg_set) & MFI_STATE_MASK;
3598
3599 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003600 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003601 fw_state);
3602 }
3603
3604 if ((fw_state == MFI_STATE_FAULT) &&
3605 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003606 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003607
3608 if ((instance->pdev->device ==
3609 PCI_DEVICE_ID_LSI_SAS1064R) ||
3610 (instance->pdev->device ==
3611 PCI_DEVICE_ID_DELL_PERC5) ||
3612 (instance->pdev->device ==
3613 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3614
3615 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303616 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003617 }
3618
3619
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303620 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303621 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003622 instance->issuepend_done = 0;
3623
3624 atomic_set(&instance->fw_outstanding, 0);
3625 megasas_internal_reset_defer_cmds(instance);
3626
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003627 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303628 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003629
3630 schedule_work(&instance->work_init);
3631 return IRQ_HANDLED;
3632
3633 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003634 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003635 fw_state, instance->disableOnlineCtrlReset);
3636 }
3637 }
3638
Sumant Patro5d018ad2006-10-03 13:13:18 -07003639 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003640 return IRQ_HANDLED;
3641}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003642/**
3643 * megasas_isr - isr entry point
3644 */
David Howells7d12e782006-10-05 14:55:46 +01003645static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003646{
adam radfordc8e858f2011-10-08 18:15:13 -07003647 struct megasas_irq_context *irq_context = devp;
3648 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003649 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003650 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003651
adam radfordc8e858f2011-10-08 18:15:13 -07003652 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003653 return IRQ_HANDLED;
3654
bo yang39a98552010-09-22 22:36:29 -04003655 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003656 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003657 spin_unlock_irqrestore(&instance->hba_lock, flags);
3658
3659 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003660}
3661
3662/**
3663 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003664 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003665 *
3666 * During the initialization, FW passes can potentially be in any one of
3667 * several possible states. If the FW in operational, waiting-for-handshake
3668 * states, driver must take steps to bring it to ready state. Otherwise, it
3669 * has to wait for the ready state.
3670 */
adam radford9c915a82010-12-21 13:34:31 -08003671int
adam radford058a8fa2011-10-08 18:14:27 -07003672megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003673{
3674 int i;
3675 u8 max_wait;
3676 u32 fw_state;
3677 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003678 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003679
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003680 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3681 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003682
Sumant Patroe3bbff92006-10-03 12:28:49 -07003683 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003684 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003685 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003686
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003687 while (fw_state != MFI_STATE_READY) {
3688
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003689 switch (fw_state) {
3690
3691 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003692 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003693 if (ocr) {
3694 max_wait = MEGASAS_RESET_WAIT_TIME;
3695 cur_state = MFI_STATE_FAULT;
3696 break;
3697 } else
3698 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003699
3700 case MFI_STATE_WAIT_HANDSHAKE:
3701 /*
3702 * Set the CLR bit in inbound doorbell
3703 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003704 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003705 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3706 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003707 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303708 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003709 writel(
3710 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003711 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303712 else
Yang, Bo87911122009-10-06 14:31:54 -06003713 writel(
3714 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3715 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003716
Yang, Bo7218df62009-10-06 14:52:20 -06003717 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003718 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3719 break;
3720
Sumant Patroe3bbff92006-10-03 12:28:49 -07003721 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003722 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003723 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3724 (instance->pdev->device ==
3725 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303726 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003727 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003728 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303729 else
Yang, Bo87911122009-10-06 14:31:54 -06003730 writel(MFI_INIT_HOTPLUG,
3731 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003732
Yang, Bo7218df62009-10-06 14:52:20 -06003733 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003734 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3735 break;
3736
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003737 case MFI_STATE_OPERATIONAL:
3738 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003739 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003740 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303741 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003742 if ((instance->pdev->device ==
3743 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3744 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003745 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303746 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003747 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003748 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303749
3750 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003751 for (i = 0; i < (10 * 1000); i += 20) {
3752 if (readl(
3753 &instance->
3754 reg_set->
3755 doorbell) & 1)
3756 msleep(20);
3757 else
3758 break;
3759 }
3760 }
Yang, Bo87911122009-10-06 14:31:54 -06003761 } else
3762 writel(MFI_RESET_FLAGS,
3763 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003764
Yang, Bo7218df62009-10-06 14:52:20 -06003765 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003766 cur_state = MFI_STATE_OPERATIONAL;
3767 break;
3768
3769 case MFI_STATE_UNDEFINED:
3770 /*
3771 * This state should not last for more than 2 seconds
3772 */
Yang, Bo7218df62009-10-06 14:52:20 -06003773 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003774 cur_state = MFI_STATE_UNDEFINED;
3775 break;
3776
3777 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003778 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003779 cur_state = MFI_STATE_BB_INIT;
3780 break;
3781
3782 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003783 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003784 cur_state = MFI_STATE_FW_INIT;
3785 break;
3786
3787 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003788 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003789 cur_state = MFI_STATE_FW_INIT_2;
3790 break;
3791
3792 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003793 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003794 cur_state = MFI_STATE_DEVICE_SCAN;
3795 break;
3796
3797 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003798 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003799 cur_state = MFI_STATE_FLUSH_CACHE;
3800 break;
3801
3802 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003803 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003804 fw_state);
3805 return -ENODEV;
3806 }
3807
3808 /*
3809 * The cur_state should not last for more than max_wait secs
3810 */
3811 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003812 curr_abs_state = instance->instancet->
3813 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003814
Yang, Bo7218df62009-10-06 14:52:20 -06003815 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003816 msleep(1);
3817 } else
3818 break;
3819 }
3820
3821 /*
3822 * Return error if fw_state hasn't changed after max_wait
3823 */
Yang, Bo7218df62009-10-06 14:52:20 -06003824 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003825 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003826 "in %d secs\n", fw_state, max_wait);
3827 return -ENODEV;
3828 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003829
3830 abs_state = curr_abs_state;
3831 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003832 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003833 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003834
3835 return 0;
3836}
3837
3838/**
3839 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3840 * @instance: Adapter soft state
3841 */
3842static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3843{
3844 int i;
adam radford9c915a82010-12-21 13:34:31 -08003845 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003846 struct megasas_cmd *cmd;
3847
3848 if (!instance->frame_dma_pool)
3849 return;
3850
3851 /*
3852 * Return all frames to pool
3853 */
3854 for (i = 0; i < max_cmd; i++) {
3855
3856 cmd = instance->cmd_list[i];
3857
3858 if (cmd->frame)
3859 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3860 cmd->frame_phys_addr);
3861
3862 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003863 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003864 cmd->sense_phys_addr);
3865 }
3866
3867 /*
3868 * Now destroy the pool itself
3869 */
3870 pci_pool_destroy(instance->frame_dma_pool);
3871 pci_pool_destroy(instance->sense_dma_pool);
3872
3873 instance->frame_dma_pool = NULL;
3874 instance->sense_dma_pool = NULL;
3875}
3876
3877/**
3878 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3879 * @instance: Adapter soft state
3880 *
3881 * Each command packet has an embedded DMA memory buffer that is used for
3882 * filling MFI frame and the SG list that immediately follows the frame. This
3883 * function creates those DMA memory buffers for each command packet by using
3884 * PCI pool facility.
3885 */
3886static int megasas_create_frame_pool(struct megasas_instance *instance)
3887{
3888 int i;
3889 u32 max_cmd;
3890 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003891 u32 total_sz;
3892 u32 frame_count;
3893 struct megasas_cmd *cmd;
3894
adam radford9c915a82010-12-21 13:34:31 -08003895 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003896
3897 /*
3898 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3899 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3900 */
3901 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3902 sizeof(struct megasas_sge32);
3903
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003904 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003905 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003906
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003907 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303908 * For MFI controllers.
3909 * max_num_sge = 60
3910 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3911 * Total 960 byte (15 MFI frame of 64 byte)
3912 *
3913 * Fusion adapter require only 3 extra frame.
3914 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3915 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3916 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003917 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303918 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003919 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3920 /*
3921 * Use DMA pool facility provided by PCI layer
3922 */
3923 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303924 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003925
3926 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003927 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003928 return -ENOMEM;
3929 }
3930
3931 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3932 instance->pdev, 128, 4, 0);
3933
3934 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003935 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003936
3937 pci_pool_destroy(instance->frame_dma_pool);
3938 instance->frame_dma_pool = NULL;
3939
3940 return -ENOMEM;
3941 }
3942
3943 /*
3944 * Allocate and attach a frame to each of the commands in cmd_list.
3945 * By making cmd->index as the context instead of the &cmd, we can
3946 * always use 32bit context regardless of the architecture
3947 */
3948 for (i = 0; i < max_cmd; i++) {
3949
3950 cmd = instance->cmd_list[i];
3951
3952 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3953 GFP_KERNEL, &cmd->frame_phys_addr);
3954
3955 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3956 GFP_KERNEL, &cmd->sense_phys_addr);
3957
3958 /*
3959 * megasas_teardown_frame_pool() takes care of freeing
3960 * whatever has been allocated
3961 */
3962 if (!cmd->frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003963 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003964 megasas_teardown_frame_pool(instance);
3965 return -ENOMEM;
3966 }
3967
Yang, Bo707e09b2010-10-12 07:20:27 -06003968 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303969 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003970 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303971 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003972 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003973 }
3974
3975 return 0;
3976}
3977
3978/**
3979 * megasas_free_cmds - Free all the cmds in the free cmd pool
3980 * @instance: Adapter soft state
3981 */
adam radford9c915a82010-12-21 13:34:31 -08003982void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003983{
3984 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003985
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003986 /* First free the MFI frame pool */
3987 megasas_teardown_frame_pool(instance);
3988
3989 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003990 for (i = 0; i < instance->max_mfi_cmds; i++)
3991
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003992 kfree(instance->cmd_list[i]);
3993
3994 /* Free the cmd_list buffer itself */
3995 kfree(instance->cmd_list);
3996 instance->cmd_list = NULL;
3997
3998 INIT_LIST_HEAD(&instance->cmd_pool);
3999}
4000
4001/**
4002 * megasas_alloc_cmds - Allocates the command packets
4003 * @instance: Adapter soft state
4004 *
4005 * Each command that is issued to the FW, whether IO commands from the OS or
4006 * internal commands like IOCTLs, are wrapped in local data structure called
4007 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4008 * the FW.
4009 *
4010 * Each frame has a 32-bit field called context (tag). This context is used
4011 * to get back the megasas_cmd from the frame when a frame gets completed in
4012 * the ISR. Typically the address of the megasas_cmd itself would be used as
4013 * the context. But we wanted to keep the differences between 32 and 64 bit
4014 * systems to the mininum. We always use 32 bit integers for the context. In
4015 * this driver, the 32 bit values are the indices into an array cmd_list.
4016 * This array is used only to look up the megasas_cmd given the context. The
4017 * free commands themselves are maintained in a linked list called cmd_pool.
4018 */
adam radford9c915a82010-12-21 13:34:31 -08004019int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004020{
4021 int i;
4022 int j;
4023 u32 max_cmd;
4024 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304025 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004026
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304027 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08004028 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004029
4030 /*
4031 * instance->cmd_list is an array of struct megasas_cmd pointers.
4032 * Allocate the dynamic array first and then allocate individual
4033 * commands.
4034 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004035 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004036
4037 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004038 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004039 return -ENOMEM;
4040 }
4041
adam radford9c915a82010-12-21 13:34:31 -08004042 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004043
4044 for (i = 0; i < max_cmd; i++) {
4045 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4046 GFP_KERNEL);
4047
4048 if (!instance->cmd_list[i]) {
4049
4050 for (j = 0; j < i; j++)
4051 kfree(instance->cmd_list[j]);
4052
4053 kfree(instance->cmd_list);
4054 instance->cmd_list = NULL;
4055
4056 return -ENOMEM;
4057 }
4058 }
4059
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004060 for (i = 0; i < max_cmd; i++) {
4061 cmd = instance->cmd_list[i];
4062 memset(cmd, 0, sizeof(struct megasas_cmd));
4063 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004064 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004065 cmd->instance = instance;
4066
4067 list_add_tail(&cmd->list, &instance->cmd_pool);
4068 }
4069
4070 /*
4071 * Create a frame pool and assign one frame to each cmd
4072 */
4073 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004074 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004075 megasas_free_cmds(instance);
4076 }
4077
4078 return 0;
4079}
4080
Yang, Bo81e403c2009-10-06 14:27:54 -06004081/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304082 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4083 * @instance: Adapter soft state
4084 *
4085 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4086 * or FW is not under OCR.
4087 */
4088inline int
4089dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4090
4091 if (!instance->ctrl_context)
4092 return KILL_ADAPTER;
4093 else if (instance->unload ||
4094 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4095 return IGNORE_TIMEOUT;
4096 else
4097 return INITIATE_OCR;
4098}
4099
Shivasharan S15dd0382017-02-10 00:59:10 -08004100static void
4101megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304102{
4103 int ret;
4104 struct megasas_cmd *cmd;
4105 struct megasas_dcmd_frame *dcmd;
4106
Shivasharan S15dd0382017-02-10 00:59:10 -08004107 struct MR_PRIV_DEVICE *mr_device_priv_data;
4108 u16 device_id = 0;
4109
4110 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304111 cmd = megasas_get_cmd(instance);
4112
4113 if (!cmd) {
4114 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004115 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304116 }
4117
4118 dcmd = &cmd->frame->dcmd;
4119
4120 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4121 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4122
4123 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4124 dcmd->cmd = MFI_CMD_DCMD;
4125 dcmd->cmd_status = 0xFF;
4126 dcmd->sge_count = 1;
4127 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4128 dcmd->timeout = 0;
4129 dcmd->pad_0 = 0;
4130 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4131 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4132 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4133 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4134
4135 if (instance->ctrl_context && !instance->mask_interrupts)
4136 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4137 else
4138 ret = megasas_issue_polled(instance, cmd);
4139
4140 switch (ret) {
4141 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004142 mr_device_priv_data = sdev->hostdata;
4143 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4144 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304145 instance->pd_info->state.ddf.pdType.intf;
4146 break;
4147
4148 case DCMD_TIMEOUT:
4149
4150 switch (dcmd_timeout_ocr_possible(instance)) {
4151 case INITIATE_OCR:
4152 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4153 megasas_reset_fusion(instance->host,
4154 MFI_IO_TIMEOUT_OCR);
4155 break;
4156 case KILL_ADAPTER:
4157 megaraid_sas_kill_hba(instance);
4158 break;
4159 case IGNORE_TIMEOUT:
4160 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4161 __func__, __LINE__);
4162 break;
4163 }
4164
4165 break;
4166 }
4167
4168 if (ret != DCMD_TIMEOUT)
4169 megasas_return_cmd(instance, cmd);
4170
Shivasharan S15dd0382017-02-10 00:59:10 -08004171 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304172}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304173/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004174 * megasas_get_pd_list_info - Returns FW's pd_list structure
4175 * @instance: Adapter soft state
4176 * @pd_list: pd_list structure
4177 *
4178 * Issues an internal command (DCMD) to get the FW's controller PD
4179 * list structure. This information is mainly used to find out SYSTEM
4180 * supported by the FW.
4181 */
4182static int
4183megasas_get_pd_list(struct megasas_instance *instance)
4184{
4185 int ret = 0, pd_index = 0;
4186 struct megasas_cmd *cmd;
4187 struct megasas_dcmd_frame *dcmd;
4188 struct MR_PD_LIST *ci;
4189 struct MR_PD_ADDRESS *pd_addr;
4190 dma_addr_t ci_h = 0;
4191
Sumit Saxenad9083162016-07-08 03:30:16 -07004192 if (instance->pd_list_not_supported) {
4193 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4194 "not supported by firmware\n");
4195 return ret;
4196 }
4197
Yang, Bo81e403c2009-10-06 14:27:54 -06004198 cmd = megasas_get_cmd(instance);
4199
4200 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004201 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004202 return -ENOMEM;
4203 }
4204
4205 dcmd = &cmd->frame->dcmd;
4206
4207 ci = pci_alloc_consistent(instance->pdev,
4208 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4209
4210 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004211 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004212 megasas_return_cmd(instance, cmd);
4213 return -ENOMEM;
4214 }
4215
4216 memset(ci, 0, sizeof(*ci));
4217 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4218
4219 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4220 dcmd->mbox.b[1] = 0;
4221 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304222 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004223 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304224 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004225 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004226 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304227 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4228 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4229 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4230 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004231
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304232 if (instance->ctrl_context && !instance->mask_interrupts)
4233 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304234 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304235 else
4236 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004237
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304238 switch (ret) {
4239 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004240 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4241 "failed/not supported by firmware\n");
4242
4243 if (instance->ctrl_context)
4244 megaraid_sas_kill_hba(instance);
4245 else
4246 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304247 break;
4248 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004249
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304250 switch (dcmd_timeout_ocr_possible(instance)) {
4251 case INITIATE_OCR:
4252 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4253 /*
4254 * DCMD failed from AEN path.
4255 * AEN path already hold reset_mutex to avoid PCI access
4256 * while OCR is in progress.
4257 */
4258 mutex_unlock(&instance->reset_mutex);
4259 megasas_reset_fusion(instance->host,
4260 MFI_IO_TIMEOUT_OCR);
4261 mutex_lock(&instance->reset_mutex);
4262 break;
4263 case KILL_ADAPTER:
4264 megaraid_sas_kill_hba(instance);
4265 break;
4266 case IGNORE_TIMEOUT:
4267 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4268 __func__, __LINE__);
4269 break;
4270 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004271
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304272 break;
4273
4274 case DCMD_SUCCESS:
4275 pd_addr = ci->addr;
4276
4277 if ((le32_to_cpu(ci->count) >
4278 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4279 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004280
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304281 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304282 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004283
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304284 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304285 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304286 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304287 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304288 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304289 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304290 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004291 pd_addr++;
4292 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304293
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304294 memcpy(instance->pd_list, instance->local_pd_list,
4295 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304296 break;
4297
Yang, Bo81e403c2009-10-06 14:27:54 -06004298 }
4299
4300 pci_free_consistent(instance->pdev,
4301 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4302 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304303
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304304 if (ret != DCMD_TIMEOUT)
4305 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004306
4307 return ret;
4308}
4309
Yang, Bobdc6fb82009-12-06 08:30:19 -07004310/*
4311 * megasas_get_ld_list_info - Returns FW's ld_list structure
4312 * @instance: Adapter soft state
4313 * @ld_list: ld_list structure
4314 *
4315 * Issues an internal command (DCMD) to get the FW's controller PD
4316 * list structure. This information is mainly used to find out SYSTEM
4317 * supported by the FW.
4318 */
4319static int
4320megasas_get_ld_list(struct megasas_instance *instance)
4321{
4322 int ret = 0, ld_index = 0, ids = 0;
4323 struct megasas_cmd *cmd;
4324 struct megasas_dcmd_frame *dcmd;
4325 struct MR_LD_LIST *ci;
4326 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304327 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004328
4329 cmd = megasas_get_cmd(instance);
4330
4331 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004332 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004333 return -ENOMEM;
4334 }
4335
4336 dcmd = &cmd->frame->dcmd;
4337
4338 ci = pci_alloc_consistent(instance->pdev,
4339 sizeof(struct MR_LD_LIST),
4340 &ci_h);
4341
4342 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004343 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004344 megasas_return_cmd(instance, cmd);
4345 return -ENOMEM;
4346 }
4347
4348 memset(ci, 0, sizeof(*ci));
4349 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4350
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304351 if (instance->supportmax256vd)
4352 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004353 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304354 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004355 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304356 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004357 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304358 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4359 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4360 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4361 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004362 dcmd->pad_0 = 0;
4363
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304364 if (instance->ctrl_context && !instance->mask_interrupts)
4365 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304366 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304367 else
4368 ret = megasas_issue_polled(instance, cmd);
4369
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304370 ld_count = le32_to_cpu(ci->ldCount);
4371
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304372 switch (ret) {
4373 case DCMD_FAILED:
4374 megaraid_sas_kill_hba(instance);
4375 break;
4376 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004377
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304378 switch (dcmd_timeout_ocr_possible(instance)) {
4379 case INITIATE_OCR:
4380 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4381 /*
4382 * DCMD failed from AEN path.
4383 * AEN path already hold reset_mutex to avoid PCI access
4384 * while OCR is in progress.
4385 */
4386 mutex_unlock(&instance->reset_mutex);
4387 megasas_reset_fusion(instance->host,
4388 MFI_IO_TIMEOUT_OCR);
4389 mutex_lock(&instance->reset_mutex);
4390 break;
4391 case KILL_ADAPTER:
4392 megaraid_sas_kill_hba(instance);
4393 break;
4394 case IGNORE_TIMEOUT:
4395 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4396 __func__, __LINE__);
4397 break;
4398 }
4399
4400 break;
4401
4402 case DCMD_SUCCESS:
4403 if (ld_count > instance->fw_supported_vd_count)
4404 break;
4405
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304406 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004407
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304408 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004409 if (ci->ldList[ld_index].state != 0) {
4410 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304411 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004412 }
4413 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304414
4415 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004416 }
4417
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304418 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004419
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304420 if (ret != DCMD_TIMEOUT)
4421 megasas_return_cmd(instance, cmd);
4422
Yang, Bobdc6fb82009-12-06 08:30:19 -07004423 return ret;
4424}
4425
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004426/**
adam radford21c9e162013-09-06 15:27:14 -07004427 * megasas_ld_list_query - Returns FW's ld_list structure
4428 * @instance: Adapter soft state
4429 * @ld_list: ld_list structure
4430 *
4431 * Issues an internal command (DCMD) to get the FW's controller PD
4432 * list structure. This information is mainly used to find out SYSTEM
4433 * supported by the FW.
4434 */
4435static int
4436megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4437{
4438 int ret = 0, ld_index = 0, ids = 0;
4439 struct megasas_cmd *cmd;
4440 struct megasas_dcmd_frame *dcmd;
4441 struct MR_LD_TARGETID_LIST *ci;
4442 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304443 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004444
4445 cmd = megasas_get_cmd(instance);
4446
4447 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004448 dev_warn(&instance->pdev->dev,
4449 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004450 return -ENOMEM;
4451 }
4452
4453 dcmd = &cmd->frame->dcmd;
4454
4455 ci = pci_alloc_consistent(instance->pdev,
4456 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4457
4458 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004459 dev_warn(&instance->pdev->dev,
4460 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004461 megasas_return_cmd(instance, cmd);
4462 return -ENOMEM;
4463 }
4464
4465 memset(ci, 0, sizeof(*ci));
4466 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4467
4468 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304469 if (instance->supportmax256vd)
4470 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004471
4472 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304473 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004474 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304475 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004476 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304477 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4478 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4479 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4480 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004481 dcmd->pad_0 = 0;
4482
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304483 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304484 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304485 else
4486 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004487
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304488 switch (ret) {
4489 case DCMD_FAILED:
4490 dev_info(&instance->pdev->dev,
4491 "DCMD not supported by firmware - %s %d\n",
4492 __func__, __LINE__);
4493 ret = megasas_get_ld_list(instance);
4494 break;
4495 case DCMD_TIMEOUT:
4496 switch (dcmd_timeout_ocr_possible(instance)) {
4497 case INITIATE_OCR:
4498 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4499 /*
4500 * DCMD failed from AEN path.
4501 * AEN path already hold reset_mutex to avoid PCI access
4502 * while OCR is in progress.
4503 */
4504 mutex_unlock(&instance->reset_mutex);
4505 megasas_reset_fusion(instance->host,
4506 MFI_IO_TIMEOUT_OCR);
4507 mutex_lock(&instance->reset_mutex);
4508 break;
4509 case KILL_ADAPTER:
4510 megaraid_sas_kill_hba(instance);
4511 break;
4512 case IGNORE_TIMEOUT:
4513 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4514 __func__, __LINE__);
4515 break;
4516 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304517
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304518 break;
4519 case DCMD_SUCCESS:
4520 tgtid_count = le32_to_cpu(ci->count);
4521
4522 if ((tgtid_count > (instance->fw_supported_vd_count)))
4523 break;
4524
adam radford21c9e162013-09-06 15:27:14 -07004525 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304526 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004527 ids = ci->targetId[ld_index];
4528 instance->ld_ids[ids] = ci->targetId[ld_index];
4529 }
4530
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304531 break;
adam radford21c9e162013-09-06 15:27:14 -07004532 }
4533
4534 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304535 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004536
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304537 if (ret != DCMD_TIMEOUT)
4538 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004539
4540 return ret;
4541}
4542
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304543/*
4544 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4545 * instance : Controller's instance
4546*/
4547static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4548{
4549 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004550 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304551
4552 fusion = instance->ctrl_context;
4553 /* For MFI based controllers return dummy success */
4554 if (!fusion)
4555 return;
4556
4557 instance->supportmax256vd =
4558 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4559 /* Below is additional check to address future FW enhancement */
4560 if (instance->ctrl_info->max_lds > 64)
4561 instance->supportmax256vd = 1;
4562
4563 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4564 * MEGASAS_MAX_DEV_PER_CHANNEL;
4565 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4566 * MEGASAS_MAX_DEV_PER_CHANNEL;
4567 if (instance->supportmax256vd) {
4568 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4569 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4570 } else {
4571 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4572 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4573 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304574
4575 dev_info(&instance->pdev->dev,
4576 "firmware type\t: %s\n",
4577 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4578 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304579
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004580 if (instance->max_raid_mapsize) {
4581 ventura_map_sz = instance->max_raid_mapsize *
4582 MR_MIN_MAP_SIZE; /* 64k */
4583 fusion->current_map_sz = ventura_map_sz;
4584 fusion->max_map_sz = ventura_map_sz;
4585 } else {
4586 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4587 (sizeof(struct MR_LD_SPAN_MAP) *
4588 (instance->fw_supported_vd_count - 1));
4589 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304590
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004591 fusion->max_map_sz =
4592 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304593
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004594 if (instance->supportmax256vd)
4595 fusion->current_map_sz = fusion->new_map_sz;
4596 else
4597 fusion->current_map_sz = fusion->old_map_sz;
4598 }
4599 /* irrespective of FW raid maps, driver raid map is constant */
4600 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304601
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004602#if VD_EXT_DEBUG
4603 dev_info(&instance->pdev->dev, "instance->max_raid_mapsize 0x%x\n ",
4604 instance->max_raid_mapsize);
4605 dev_info(&instance->pdev->dev, "new_map_sz = 0x%x, old_map_sz = 0x%x\n",
4606 fusion->new_map_sz, fusion->old_map_sz);
4607 dev_info(&instance->pdev->dev, "ventura_map_sz = 0x%x, current_map_sz = 0x%x\n",
4608 ventura_map_sz, fusion->current_map_sz);
4609 dev_info(&instance->pdev->dev, "fusion->drv_map_sz =0x%x, size of driver raid map 0x%lx\n",
4610 fusion->drv_map_sz, sizeof(struct MR_DRV_RAID_MAP_ALL));
4611#endif
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304612}
4613
adam radford21c9e162013-09-06 15:27:14 -07004614/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004615 * megasas_get_controller_info - Returns FW's controller structure
4616 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004617 *
4618 * Issues an internal command (DCMD) to get the FW's controller structure.
4619 * This information is mainly used to find out the maximum IO transfer per
4620 * command supported by the FW.
4621 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304622int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304623megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004624{
4625 int ret = 0;
4626 struct megasas_cmd *cmd;
4627 struct megasas_dcmd_frame *dcmd;
4628 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304629 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004630 dma_addr_t ci_h = 0;
4631
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304632 ctrl_info = instance->ctrl_info;
4633
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004634 cmd = megasas_get_cmd(instance);
4635
4636 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004637 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004638 return -ENOMEM;
4639 }
4640
4641 dcmd = &cmd->frame->dcmd;
4642
4643 ci = pci_alloc_consistent(instance->pdev,
4644 sizeof(struct megasas_ctrl_info), &ci_h);
4645
4646 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004647 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004648 megasas_return_cmd(instance, cmd);
4649 return -ENOMEM;
4650 }
4651
4652 memset(ci, 0, sizeof(*ci));
4653 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4654
4655 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304656 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004657 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304658 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004659 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004660 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304661 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4662 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4663 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4664 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304665 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004666
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304667 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304668 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304669 else
4670 ret = megasas_issue_polled(instance, cmd);
4671
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304672 switch (ret) {
4673 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004674 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304675 /* Save required controller information in
4676 * CPU endianness format.
4677 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304678 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4679 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4680 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004681 le16_to_cpus((u16 *)&ctrl_info->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304682
4683 /* Update the latest Ext VD info.
4684 * From Init path, store current firmware details.
4685 * From OCR path, detect any firmware properties changes.
4686 * in case of Firmware upgrade without system reboot.
4687 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304688 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304689 instance->use_seqnum_jbod_fp =
4690 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004691 instance->support_morethan256jbod =
4692 ctrl_info->adapter_operations4.support_pd_map_target_id;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304693
4694 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304695 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4696 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304697 "controller type\t: %s(%dMB)\n",
4698 instance->is_imr ? "iMR" : "MR",
4699 le16_to_cpu(ctrl_info->memory_size));
4700
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304701 instance->disableOnlineCtrlReset =
4702 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304703 instance->secure_jbod_support =
4704 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304705 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4706 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304707 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4708 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304709 break;
4710
4711 case DCMD_TIMEOUT:
4712 switch (dcmd_timeout_ocr_possible(instance)) {
4713 case INITIATE_OCR:
4714 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4715 megasas_reset_fusion(instance->host,
4716 MFI_IO_TIMEOUT_OCR);
4717 break;
4718 case KILL_ADAPTER:
4719 megaraid_sas_kill_hba(instance);
4720 break;
4721 case IGNORE_TIMEOUT:
4722 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4723 __func__, __LINE__);
4724 break;
4725 }
4726 case DCMD_FAILED:
4727 megaraid_sas_kill_hba(instance);
4728 break;
4729
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304730 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004731
4732 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4733 ci, ci_h);
4734
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304735 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304736
4737
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004738 return ret;
4739}
4740
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304741/*
4742 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4743 * to firmware
4744 *
4745 * @instance: Adapter soft state
4746 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4747 MR_CRASH_BUF_TURN_OFF = 0
4748 MR_CRASH_BUF_TURN_ON = 1
4749 * @return 0 on success non-zero on failure.
4750 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4751 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4752 * that driver supports crash dump feature. This DCMD will be sent only if
4753 * crash dump feature is supported by the FW.
4754 *
4755 */
4756int megasas_set_crash_dump_params(struct megasas_instance *instance,
4757 u8 crash_buf_state)
4758{
4759 int ret = 0;
4760 struct megasas_cmd *cmd;
4761 struct megasas_dcmd_frame *dcmd;
4762
4763 cmd = megasas_get_cmd(instance);
4764
4765 if (!cmd) {
4766 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4767 return -ENOMEM;
4768 }
4769
4770
4771 dcmd = &cmd->frame->dcmd;
4772
4773 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4774 dcmd->mbox.b[0] = crash_buf_state;
4775 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304776 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304777 dcmd->sge_count = 1;
4778 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4779 dcmd->timeout = 0;
4780 dcmd->pad_0 = 0;
4781 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4782 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4783 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4784 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4785
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304786 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304787 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304788 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304789 ret = megasas_issue_polled(instance, cmd);
4790
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304791 if (ret == DCMD_TIMEOUT) {
4792 switch (dcmd_timeout_ocr_possible(instance)) {
4793 case INITIATE_OCR:
4794 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4795 megasas_reset_fusion(instance->host,
4796 MFI_IO_TIMEOUT_OCR);
4797 break;
4798 case KILL_ADAPTER:
4799 megaraid_sas_kill_hba(instance);
4800 break;
4801 case IGNORE_TIMEOUT:
4802 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4803 __func__, __LINE__);
4804 break;
4805 }
4806 } else
4807 megasas_return_cmd(instance, cmd);
4808
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304809 return ret;
4810}
4811
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004812/**
bo yang31ea7082007-11-07 12:09:50 -05004813 * megasas_issue_init_mfi - Initializes the FW
4814 * @instance: Adapter soft state
4815 *
4816 * Issues the INIT MFI cmd
4817 */
4818static int
4819megasas_issue_init_mfi(struct megasas_instance *instance)
4820{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304821 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004822 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004823 struct megasas_init_frame *init_frame;
4824 struct megasas_init_queue_info *initq_info;
4825 dma_addr_t init_frame_h;
4826 dma_addr_t initq_info_h;
4827
4828 /*
4829 * Prepare a init frame. Note the init frame points to queue info
4830 * structure. Each frame has SGL allocated after first 64 bytes. For
4831 * this frame - since we don't need any SGL - we use SGL's space as
4832 * queue info structure
4833 *
4834 * We will not get a NULL command below. We just created the pool.
4835 */
4836 cmd = megasas_get_cmd(instance);
4837
4838 init_frame = (struct megasas_init_frame *)cmd->frame;
4839 initq_info = (struct megasas_init_queue_info *)
4840 ((unsigned long)init_frame + 64);
4841
4842 init_frame_h = cmd->frame_phys_addr;
4843 initq_info_h = init_frame_h + 64;
4844
4845 context = init_frame->context;
4846 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4847 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4848 init_frame->context = context;
4849
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304850 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4851 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004852
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304853 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4854 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004855
4856 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304857 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304858 init_frame->queue_info_new_phys_addr_lo =
4859 cpu_to_le32(lower_32_bits(initq_info_h));
4860 init_frame->queue_info_new_phys_addr_hi =
4861 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004862
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304863 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004864
4865 /*
4866 * disable the intr before firing the init frame to FW
4867 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304868 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004869
4870 /*
4871 * Issue the init frame in polled mode
4872 */
4873
4874 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004875 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004876 megasas_return_cmd(instance, cmd);
4877 goto fail_fw_init;
4878 }
4879
4880 megasas_return_cmd(instance, cmd);
4881
4882 return 0;
4883
4884fail_fw_init:
4885 return -EINVAL;
4886}
4887
adam radfordcd50ba82010-12-21 10:23:23 -08004888static u32
4889megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004890{
adam radfordcd50ba82010-12-21 10:23:23 -08004891 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004892 u32 context_sz;
4893 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004894
4895 reg_set = instance->reg_set;
4896
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004897 /*
4898 * Get various operational parameters from status register
4899 */
Sumant Patro1341c932006-01-25 12:02:40 -08004900 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004901 /*
4902 * Reduce the max supported cmds by 1. This is to ensure that the
4903 * reply_q_sz (1 more than the max cmd that driver may send)
4904 * does not exceed max cmds that the FW can support
4905 */
4906 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004907 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004908 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004909 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004910 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304911 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4912 * are reserved for IOCTL + driver's internal DCMDs.
4913 */
4914 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4915 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4916 instance->max_scsi_cmds = (instance->max_fw_cmds -
4917 MEGASAS_SKINNY_INT_CMDS);
4918 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4919 } else {
4920 instance->max_scsi_cmds = (instance->max_fw_cmds -
4921 MEGASAS_INT_CMDS);
4922 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4923 }
4924
Sumit Saxena308ec452016-01-28 21:04:30 +05304925 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304926 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004927 * Create a pool of commands
4928 */
4929 if (megasas_alloc_cmds(instance))
4930 goto fail_alloc_cmds;
4931
4932 /*
4933 * Allocate memory for reply queue. Length of reply queue should
4934 * be _one_ more than the maximum commands handled by the firmware.
4935 *
4936 * Note: When FW completes commands, it places corresponding contex
4937 * values in this circular reply queue. This circular queue is a fairly
4938 * typical producer-consumer queue. FW is the producer (of completed
4939 * commands) and the driver is the consumer.
4940 */
4941 context_sz = sizeof(u32);
4942 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4943
4944 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4945 reply_q_sz,
4946 &instance->reply_queue_h);
4947
4948 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004949 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004950 goto fail_reply_queue;
4951 }
4952
bo yang31ea7082007-11-07 12:09:50 -05004953 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004954 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004955
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304956 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304957 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4958 "Fail from %s %d\n", instance->unique_id,
4959 __func__, __LINE__);
4960 goto fail_fw_init;
4961 }
4962
bo yang39a98552010-09-22 22:36:29 -04004963 instance->fw_support_ieee = 0;
4964 instance->fw_support_ieee =
4965 (instance->instancet->read_fw_status_reg(reg_set) &
4966 0x04000000);
4967
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004968 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004969 instance->fw_support_ieee);
4970
4971 if (instance->fw_support_ieee)
4972 instance->flag_ieee = 1;
4973
adam radfordcd50ba82010-12-21 10:23:23 -08004974 return 0;
4975
4976fail_fw_init:
4977
4978 pci_free_consistent(instance->pdev, reply_q_sz,
4979 instance->reply_queue, instance->reply_queue_h);
4980fail_reply_queue:
4981 megasas_free_cmds(instance);
4982
4983fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004984 return 1;
4985}
4986
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304987/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01004988 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304989 * @instance: Adapter soft state
4990 *
4991 * Do not enable interrupt, only setup ISRs.
4992 *
4993 * Return 0 on success.
4994 */
4995static int
4996megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4997{
4998 struct pci_dev *pdev;
4999
5000 pdev = instance->pdev;
5001 instance->irq_context[0].instance = instance;
5002 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005003 if (request_irq(pci_irq_vector(pdev, 0),
5004 instance->instancet->service_isr, IRQF_SHARED,
5005 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305006 dev_err(&instance->pdev->dev,
5007 "Failed to register IRQ from %s %d\n",
5008 __func__, __LINE__);
5009 return -1;
5010 }
5011 return 0;
5012}
5013
5014/**
5015 * megasas_setup_irqs_msix - register MSI-x interrupts.
5016 * @instance: Adapter soft state
5017 * @is_probe: Driver probe check
5018 *
5019 * Do not enable interrupt, only setup ISRs.
5020 *
5021 * Return 0 on success.
5022 */
5023static int
5024megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5025{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005026 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305027 struct pci_dev *pdev;
5028
5029 pdev = instance->pdev;
5030
5031 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305032 for (i = 0; i < instance->msix_vectors; i++) {
5033 instance->irq_context[i].instance = instance;
5034 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005035 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305036 instance->instancet->service_isr, 0, "megasas",
5037 &instance->irq_context[i])) {
5038 dev_err(&instance->pdev->dev,
5039 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005040 for (j = 0; j < i; j++)
5041 free_irq(pci_irq_vector(pdev, j),
5042 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305043 /* Retry irq register for IO_APIC*/
5044 instance->msix_vectors = 0;
5045 if (is_probe)
5046 return megasas_setup_irqs_ioapic(instance);
5047 else
5048 return -1;
5049 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305050 }
5051 return 0;
5052}
5053
5054/*
5055 * megasas_destroy_irqs- unregister interrupts.
5056 * @instance: Adapter soft state
5057 * return: void
5058 */
5059static void
5060megasas_destroy_irqs(struct megasas_instance *instance) {
5061
5062 int i;
5063
5064 if (instance->msix_vectors)
5065 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005066 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305067 &instance->irq_context[i]);
5068 }
5069 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005070 free_irq(pci_irq_vector(instance->pdev, 0),
5071 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305072}
5073
adam radfordcd50ba82010-12-21 10:23:23 -08005074/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305075 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5076 * @instance: Adapter soft state
5077 * @is_probe: Driver probe check
5078 *
5079 * Return 0 on success.
5080 */
5081void
5082megasas_setup_jbod_map(struct megasas_instance *instance)
5083{
5084 int i;
5085 struct fusion_context *fusion = instance->ctrl_context;
5086 u32 pd_seq_map_sz;
5087
5088 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5089 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5090
5091 if (reset_devices || !fusion ||
5092 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
5093 dev_info(&instance->pdev->dev,
5094 "Jbod map is not supported %s %d\n",
5095 __func__, __LINE__);
5096 instance->use_seqnum_jbod_fp = false;
5097 return;
5098 }
5099
5100 if (fusion->pd_seq_sync[0])
5101 goto skip_alloc;
5102
5103 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5104 fusion->pd_seq_sync[i] = dma_alloc_coherent
5105 (&instance->pdev->dev, pd_seq_map_sz,
5106 &fusion->pd_seq_phys[i], GFP_KERNEL);
5107 if (!fusion->pd_seq_sync[i]) {
5108 dev_err(&instance->pdev->dev,
5109 "Failed to allocate memory from %s %d\n",
5110 __func__, __LINE__);
5111 if (i == 1) {
5112 dma_free_coherent(&instance->pdev->dev,
5113 pd_seq_map_sz, fusion->pd_seq_sync[0],
5114 fusion->pd_seq_phys[0]);
5115 fusion->pd_seq_sync[0] = NULL;
5116 }
5117 instance->use_seqnum_jbod_fp = false;
5118 return;
5119 }
5120 }
5121
5122skip_alloc:
5123 if (!megasas_sync_pd_seq_num(instance, false) &&
5124 !megasas_sync_pd_seq_num(instance, true))
5125 instance->use_seqnum_jbod_fp = true;
5126 else
5127 instance->use_seqnum_jbod_fp = false;
5128}
5129
5130/**
adam radfordcd50ba82010-12-21 10:23:23 -08005131 * megasas_init_fw - Initializes the FW
5132 * @instance: Adapter soft state
5133 *
5134 * This is the main function for initializing firmware
5135 */
5136
5137static int megasas_init_fw(struct megasas_instance *instance)
5138{
5139 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005140 u32 max_sectors_2, tmp_sectors, msix_enable;
5141 u32 scratch_pad_2, scratch_pad_3, scratch_pad_4;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005142 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005143 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305144 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005145 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005146 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005147 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305148 struct fusion_context *fusion;
5149
5150 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005151
5152 /* Find first memory bar */
5153 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005154 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005155 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005156 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005157 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005158 return -EBUSY;
5159 }
5160
Ben Collins11f8a7b2013-09-13 12:46:44 -04005161 base_addr = pci_resource_start(instance->pdev, instance->bar);
5162 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005163
5164 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005165 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005166 goto fail_ioremap;
5167 }
5168
5169 reg_set = instance->reg_set;
5170
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005171 if (fusion)
adam radford9c915a82010-12-21 13:34:31 -08005172 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005173 else {
5174 switch (instance->pdev->device) {
5175 case PCI_DEVICE_ID_LSI_SAS1078R:
5176 case PCI_DEVICE_ID_LSI_SAS1078DE:
5177 instance->instancet = &megasas_instance_template_ppc;
5178 break;
5179 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5180 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5181 instance->instancet = &megasas_instance_template_gen2;
5182 break;
5183 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5184 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5185 instance->instancet = &megasas_instance_template_skinny;
5186 break;
5187 case PCI_DEVICE_ID_LSI_SAS1064R:
5188 case PCI_DEVICE_ID_DELL_PERC5:
5189 default:
5190 instance->instancet = &megasas_instance_template_xscale;
5191 instance->pd_list_not_supported = 1;
5192 break;
5193 }
adam radfordcd50ba82010-12-21 10:23:23 -08005194 }
5195
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305196 if (megasas_transition_to_ready(instance, 0)) {
5197 atomic_set(&instance->fw_reset_no_pci_access, 1);
5198 instance->instancet->adp_reset
5199 (instance, instance->reg_set);
5200 atomic_set(&instance->fw_reset_no_pci_access, 0);
5201 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005202 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305203 __func__);
5204
5205 /*waitting for about 30 second before retry*/
5206 ssleep(30);
5207
5208 if (megasas_transition_to_ready(instance, 0))
5209 goto fail_ready_state;
5210 }
adam radfordcd50ba82010-12-21 10:23:23 -08005211
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005212 if (instance->is_ventura) {
5213 scratch_pad_3 =
5214 readl(&instance->reg_set->outbound_scratch_pad_3);
5215#if VD_EXT_DEBUG
5216 dev_info(&instance->pdev->dev, "scratch_pad3 0x%x\n",
5217 scratch_pad_3);
5218#endif
5219 instance->max_raid_mapsize = ((scratch_pad_3 >>
5220 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5221 MR_MAX_RAID_MAP_SIZE_MASK);
5222 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305223
adam radford3f1abce2011-05-11 18:33:47 -07005224 /* Check if MSI-X is supported while in ready state */
5225 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5226 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005227 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005228 int irq_flags = PCI_IRQ_MSIX;
5229
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305230 scratch_pad_2 = readl
5231 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005232 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305233 if (fusion) {
5234 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5235 instance->msix_vectors = (scratch_pad_2
5236 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5237 fw_msix_count = instance->msix_vectors;
5238 } else { /* Invader series supports more than 8 MSI-x vectors*/
5239 instance->msix_vectors = ((scratch_pad_2
5240 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5241 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005242 if (instance->msix_vectors > 16)
5243 instance->msix_combined = true;
5244
Sumit Saxena179ac142016-01-28 21:04:28 +05305245 if (rdpq_enable)
5246 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5247 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305248 fw_msix_count = instance->msix_vectors;
5249 /* Save 1-15 reply post index address to local memory
5250 * Index 0 is already saved from reg offset
5251 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5252 */
5253 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5254 instance->reply_post_host_index_addr[loop] =
5255 (u32 __iomem *)
5256 ((u8 __iomem *)instance->reg_set +
5257 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5258 + (loop * 0x10));
5259 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305260 }
5261 if (msix_vectors)
5262 instance->msix_vectors = min(msix_vectors,
5263 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305264 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005265 instance->msix_vectors = 1;
5266 /* Don't bother allocating more MSI-X vectors than cpus */
5267 instance->msix_vectors = min(instance->msix_vectors,
5268 (unsigned int)num_online_cpus());
Hannes Reineckefad119b2016-12-02 12:52:23 +01005269 if (smp_affinity_enable)
5270 irq_flags |= PCI_IRQ_AFFINITY;
5271 i = pci_alloc_irq_vectors(instance->pdev, 1,
5272 instance->msix_vectors, irq_flags);
Jiang Liuc12de882014-11-03 20:44:20 +08005273 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005274 instance->msix_vectors = i;
5275 else
adam radfordc8e858f2011-10-08 18:15:13 -07005276 instance->msix_vectors = 0;
5277 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005278 /*
5279 * MSI-X host index 0 is common for all adapter.
5280 * It is used for all MPT based Adapters.
5281 */
5282 if (instance->msix_combined) {
5283 instance->reply_post_host_index_addr[0] =
5284 (u32 *)((u8 *)instance->reg_set +
5285 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5286 } else {
5287 instance->reply_post_host_index_addr[0] =
5288 (u32 *)((u8 *)instance->reg_set +
5289 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5290 }
5291
Hannes Reineckefad119b2016-12-02 12:52:23 +01005292 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5293 if (i < 0)
5294 goto fail_setup_irqs;
adam radford3f1abce2011-05-11 18:33:47 -07005295
Tomas Henzl258c3af2015-06-02 16:09:46 +05305296 dev_info(&instance->pdev->dev,
5297 "firmware supports msix\t: (%d)", fw_msix_count);
5298 dev_info(&instance->pdev->dev,
5299 "current msix/online cpus\t: (%d/%d)\n",
5300 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305301 dev_info(&instance->pdev->dev,
5302 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305303
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305304 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5305 (unsigned long)instance);
5306
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305307 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5308 GFP_KERNEL);
5309 if (instance->ctrl_info == NULL)
5310 goto fail_init_adapter;
5311
5312 /*
5313 * Below are default value for legacy Firmware.
5314 * non-fusion based controllers
5315 */
5316 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5317 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005318 /* Get operational params, sge flags, send init cmd to controller */
5319 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005320 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005321
Shivasharan S15dd0382017-02-10 00:59:10 -08005322 if (instance->is_ventura) {
5323 scratch_pad_4 =
5324 readl(&instance->reg_set->outbound_scratch_pad_4);
5325 if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
5326 MR_DEFAULT_NVME_PAGE_SHIFT)
5327 instance->nvme_page_size =
5328 (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
5329
5330 dev_info(&instance->pdev->dev,
5331 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5332 }
5333
Tomas Henzl18103ef2016-11-01 17:32:02 +01005334 if (instance->msix_vectors ?
5335 megasas_setup_irqs_msix(instance, 1) :
5336 megasas_setup_irqs_ioapic(instance))
5337 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305338
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305339 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005340
Andy Lutomirski13f30772016-05-03 10:24:31 -07005341 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005342
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305343 megasas_setup_jbod_map(instance);
5344
bo yang39a98552010-09-22 22:36:29 -04005345 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005346 * the following function will get the PD LIST.
5347 */
5348 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005349 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005350 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005351 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305352 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005353 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005354
Yang, Bobdc6fb82009-12-06 08:30:19 -07005355 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005356
5357 /* stream detection initialization */
5358 if (instance->is_ventura) {
5359 fusion->stream_detect_by_ld =
5360 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5361 * MAX_LOGICAL_DRIVES_EXT,
5362 GFP_KERNEL);
5363 if (!fusion->stream_detect_by_ld) {
5364 dev_err(&instance->pdev->dev,
5365 "unable to allocate stream detection for pool of LDs\n");
5366 goto fail_get_ld_pd_list;
5367 }
5368 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5369 fusion->stream_detect_by_ld[i] =
5370 kmalloc(sizeof(struct LD_STREAM_DETECT),
5371 GFP_KERNEL);
5372 if (!fusion->stream_detect_by_ld[i]) {
5373 dev_err(&instance->pdev->dev,
5374 "unable to allocate stream detect by LD\n ");
5375 for (j = 0; j < i; ++j)
5376 kfree(fusion->stream_detect_by_ld[j]);
5377 kfree(fusion->stream_detect_by_ld);
5378 fusion->stream_detect_by_ld = NULL;
5379 goto fail_get_ld_pd_list;
5380 }
5381 fusion->stream_detect_by_ld[i]->mru_bit_map
5382 = MR_STREAM_BITMAP;
5383 }
5384 }
5385
adam radford21c9e162013-09-06 15:27:14 -07005386 if (megasas_ld_list_query(instance,
5387 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5388 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07005389
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005390 /*
5391 * Compute the max allowed sectors per IO: The controller info has two
5392 * limits on max sectors. Driver should use the minimum of these two.
5393 *
5394 * 1 << stripe_sz_ops.min = max sectors per strip
5395 *
5396 * Note that older firmwares ( < FW ver 30) didn't report information
5397 * to calculate max_sectors_1. So the number ended up as zero always.
5398 */
bo yang14faea92007-11-09 04:14:00 -05005399 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305400 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005401
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305402 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5403 le16_to_cpu(ctrl_info->max_strips_per_io);
5404 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005405
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005406 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305407
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305408 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5409 instance->passive = ctrl_info->cluster.passive;
5410 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305411 instance->UnevenSpanSupport =
5412 ctrl_info->adapterOperations2.supportUnevenSpans;
5413 if (instance->UnevenSpanSupport) {
5414 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305415 if (MR_ValidateMapInfo(instance))
5416 fusion->fast_path_io = 1;
5417 else
5418 fusion->fast_path_io = 0;
5419
5420 }
5421 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305422 instance->requestorId = ctrl_info->iov.requestorId;
5423 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5424 if (!ctrl_info->adapterOperations2.activePassive)
5425 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305426
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305427 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5428 instance->PlasmaFW111 ? "1.11" : "new");
5429
5430 if (instance->PlasmaFW111) {
5431 iovPtr = (struct IOV_111 *)
5432 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5433 instance->requestorId = iovPtr->requestorId;
5434 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305435 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305436 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5437 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305438 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305439
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305440 instance->crash_dump_fw_support =
5441 ctrl_info->adapterOperations3.supportCrashDump;
5442 instance->crash_dump_drv_support =
5443 (instance->crash_dump_fw_support &&
5444 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305445 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305446 megasas_set_crash_dump_params(instance,
5447 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005448
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305449 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305450 if (instance->crash_dump_buf)
5451 pci_free_consistent(instance->pdev,
5452 CRASH_DMA_BUF_SIZE,
5453 instance->crash_dump_buf,
5454 instance->crash_dump_h);
5455 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005456 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305457
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305458
5459 dev_info(&instance->pdev->dev,
5460 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5461 le16_to_cpu(ctrl_info->pci.vendor_id),
5462 le16_to_cpu(ctrl_info->pci.device_id),
5463 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5464 le16_to_cpu(ctrl_info->pci.sub_device_id));
5465 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5466 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305467 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5468 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305469 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5470 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305471
5472
bo yang14faea92007-11-09 04:14:00 -05005473 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305474 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005475 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5476 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005477
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305478 /* Check for valid throttlequeuedepth module parameter */
5479 if (throttlequeuedepth &&
5480 throttlequeuedepth <= instance->max_scsi_cmds)
5481 instance->throttlequeuedepth = throttlequeuedepth;
5482 else
5483 instance->throttlequeuedepth =
5484 MEGASAS_THROTTLE_QUEUE_DEPTH;
5485
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305486 if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
5487 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5488
5489 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5490 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005491
adam radford229fe472014-03-10 02:51:56 -07005492 /* Launch SR-IOV heartbeat timer */
5493 if (instance->requestorId) {
5494 if (!megasas_sriov_start_heartbeat(instance, 1))
5495 megasas_start_timer(instance,
5496 &instance->sriov_heartbeat_timer,
5497 megasas_sriov_heartbeat_handler,
5498 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5499 else
5500 instance->skip_heartbeat_timer_del = 1;
5501 }
5502
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005503 return 0;
5504
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005505fail_get_ld_pd_list:
5506 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305507fail_get_pd_list:
5508 instance->instancet->disable_intr(instance);
Tomas Henzl18103ef2016-11-01 17:32:02 +01005509fail_init_adapter:
Hannes Reineckefad119b2016-12-02 12:52:23 +01005510 megasas_destroy_irqs(instance);
5511fail_setup_irqs:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305512 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005513 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305514 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005515fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305516 kfree(instance->ctrl_info);
5517 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005518 iounmap(instance->reg_set);
5519
5520 fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005521 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005522
5523 return -EINVAL;
5524}
5525
5526/**
5527 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005528 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005529 */
5530static void megasas_release_mfi(struct megasas_instance *instance)
5531{
adam radford9c915a82010-12-21 13:34:31 -08005532 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005533
adam radford9c915a82010-12-21 13:34:31 -08005534 if (instance->reply_queue)
5535 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005536 instance->reply_queue, instance->reply_queue_h);
5537
5538 megasas_free_cmds(instance);
5539
5540 iounmap(instance->reg_set);
5541
Yinghai Lue7f85162016-08-05 23:37:34 -07005542 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005543}
5544
5545/**
5546 * megasas_get_seq_num - Gets latest event sequence numbers
5547 * @instance: Adapter soft state
5548 * @eli: FW event log sequence numbers information
5549 *
5550 * FW maintains a log of all events in a non-volatile area. Upper layers would
5551 * usually find out the latest sequence number of the events, the seq number at
5552 * the boot etc. They would "read" all the events below the latest seq number
5553 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5554 * number), they would subsribe to AEN (asynchronous event notification) and
5555 * wait for the events to happen.
5556 */
5557static int
5558megasas_get_seq_num(struct megasas_instance *instance,
5559 struct megasas_evt_log_info *eli)
5560{
5561 struct megasas_cmd *cmd;
5562 struct megasas_dcmd_frame *dcmd;
5563 struct megasas_evt_log_info *el_info;
5564 dma_addr_t el_info_h = 0;
5565
5566 cmd = megasas_get_cmd(instance);
5567
5568 if (!cmd) {
5569 return -ENOMEM;
5570 }
5571
5572 dcmd = &cmd->frame->dcmd;
5573 el_info = pci_alloc_consistent(instance->pdev,
5574 sizeof(struct megasas_evt_log_info),
5575 &el_info_h);
5576
5577 if (!el_info) {
5578 megasas_return_cmd(instance, cmd);
5579 return -ENOMEM;
5580 }
5581
5582 memset(el_info, 0, sizeof(*el_info));
5583 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5584
5585 dcmd->cmd = MFI_CMD_DCMD;
5586 dcmd->cmd_status = 0x0;
5587 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305588 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005589 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005590 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305591 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5592 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5593 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5594 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005595
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305596 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5597 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305598 /*
5599 * Copy the data back into callers buffer
5600 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305601 eli->newest_seq_num = el_info->newest_seq_num;
5602 eli->oldest_seq_num = el_info->oldest_seq_num;
5603 eli->clear_seq_num = el_info->clear_seq_num;
5604 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5605 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305606 } else
5607 dev_err(&instance->pdev->dev, "DCMD failed "
5608 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005609
5610 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5611 el_info, el_info_h);
5612
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305613 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005614
5615 return 0;
5616}
5617
5618/**
5619 * megasas_register_aen - Registers for asynchronous event notification
5620 * @instance: Adapter soft state
5621 * @seq_num: The starting sequence number
5622 * @class_locale: Class of the event
5623 *
5624 * This function subscribes for AEN for events beyond the @seq_num. It requests
5625 * to be notified if and only if the event is of type @class_locale
5626 */
5627static int
5628megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5629 u32 class_locale_word)
5630{
5631 int ret_val;
5632 struct megasas_cmd *cmd;
5633 struct megasas_dcmd_frame *dcmd;
5634 union megasas_evt_class_locale curr_aen;
5635 union megasas_evt_class_locale prev_aen;
5636
5637 /*
5638 * If there an AEN pending already (aen_cmd), check if the
5639 * class_locale of that pending AEN is inclusive of the new
5640 * AEN request we currently have. If it is, then we don't have
5641 * to do anything. In other words, whichever events the current
5642 * AEN request is subscribing to, have already been subscribed
5643 * to.
5644 *
5645 * If the old_cmd is _not_ inclusive, then we have to abort
5646 * that command, form a class_locale that is superset of both
5647 * old and current and re-issue to the FW
5648 */
5649
5650 curr_aen.word = class_locale_word;
5651
5652 if (instance->aen_cmd) {
5653
Christoph Hellwiga9555532015-04-23 16:34:24 +05305654 prev_aen.word =
5655 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005656
5657 /*
5658 * A class whose enum value is smaller is inclusive of all
5659 * higher values. If a PROGRESS (= -1) was previously
5660 * registered, then a new registration requests for higher
5661 * classes need not be sent to FW. They are automatically
5662 * included.
5663 *
5664 * Locale numbers don't have such hierarchy. They are bitmap
5665 * values
5666 */
5667 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305668 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005669 curr_aen.members.locale)) {
5670 /*
5671 * Previously issued event registration includes
5672 * current request. Nothing to do.
5673 */
5674 return 0;
5675 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305676 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005677
5678 if (prev_aen.members.class < curr_aen.members.class)
5679 curr_aen.members.class = prev_aen.members.class;
5680
5681 instance->aen_cmd->abort_aen = 1;
5682 ret_val = megasas_issue_blocked_abort_cmd(instance,
5683 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305684 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005685
5686 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005687 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005688 "previous AEN command\n");
5689 return ret_val;
5690 }
5691 }
5692 }
5693
5694 cmd = megasas_get_cmd(instance);
5695
5696 if (!cmd)
5697 return -ENOMEM;
5698
5699 dcmd = &cmd->frame->dcmd;
5700
5701 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5702
5703 /*
5704 * Prepare DCMD for aen registration
5705 */
5706 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5707
5708 dcmd->cmd = MFI_CMD_DCMD;
5709 dcmd->cmd_status = 0x0;
5710 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305711 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005712 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005713 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305714 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5715 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5716 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005717 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305718 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5719 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5720 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005721
Yang, Bof4c9a132009-10-06 14:43:28 -06005722 if (instance->aen_cmd != NULL) {
5723 megasas_return_cmd(instance, cmd);
5724 return 0;
5725 }
5726
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005727 /*
5728 * Store reference to the cmd used to register for AEN. When an
5729 * application wants us to register for AEN, we have to abort this
5730 * cmd and re-register with a new EVENT LOCALE supplied by that app
5731 */
5732 instance->aen_cmd = cmd;
5733
5734 /*
5735 * Issue the aen registration frame
5736 */
adam radford9c915a82010-12-21 13:34:31 -08005737 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005738
5739 return 0;
5740}
5741
Shivasharan S96188a82017-02-10 00:59:11 -08005742/* megasas_get_target_prop - Send DCMD with below details to firmware.
5743 *
5744 * This DCMD will fetch few properties of LD/system PD defined
5745 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
5746 *
5747 * DCMD send by drivers whenever new target is added to the OS.
5748 *
5749 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
5750 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
5751 * 0 = system PD, 1 = LD.
5752 * dcmd.mbox.s[1] - TargetID for LD/system PD.
5753 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
5754 *
5755 * @instance: Adapter soft state
5756 * @sdev: OS provided scsi device
5757 *
5758 * Returns 0 on success non-zero on failure.
5759 */
5760static int
5761megasas_get_target_prop(struct megasas_instance *instance,
5762 struct scsi_device *sdev)
5763{
5764 int ret;
5765 struct megasas_cmd *cmd;
5766 struct megasas_dcmd_frame *dcmd;
5767 u16 targetId = (sdev->channel % 2) + sdev->id;
5768
5769 cmd = megasas_get_cmd(instance);
5770
5771 if (!cmd) {
5772 dev_err(&instance->pdev->dev,
5773 "Failed to get cmd %s\n", __func__);
5774 return -ENOMEM;
5775 }
5776
5777 dcmd = &cmd->frame->dcmd;
5778
5779 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
5780 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5781 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
5782
5783 dcmd->mbox.s[1] = cpu_to_le16(targetId);
5784 dcmd->cmd = MFI_CMD_DCMD;
5785 dcmd->cmd_status = 0xFF;
5786 dcmd->sge_count = 1;
5787 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5788 dcmd->timeout = 0;
5789 dcmd->pad_0 = 0;
5790 dcmd->data_xfer_len =
5791 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5792 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
5793 dcmd->sgl.sge32[0].phys_addr =
5794 cpu_to_le32(instance->tgt_prop_h);
5795 dcmd->sgl.sge32[0].length =
5796 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5797
5798 if (instance->ctrl_context && !instance->mask_interrupts)
5799 ret = megasas_issue_blocked_cmd(instance,
5800 cmd, MFI_IO_TIMEOUT_SECS);
5801 else
5802 ret = megasas_issue_polled(instance, cmd);
5803
5804 switch (ret) {
5805 case DCMD_TIMEOUT:
5806 switch (dcmd_timeout_ocr_possible(instance)) {
5807 case INITIATE_OCR:
5808 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5809 megasas_reset_fusion(instance->host,
5810 MFI_IO_TIMEOUT_OCR);
5811 break;
5812 case KILL_ADAPTER:
5813 megaraid_sas_kill_hba(instance);
5814 break;
5815 case IGNORE_TIMEOUT:
5816 dev_info(&instance->pdev->dev,
5817 "Ignore DCMD timeout: %s %d\n",
5818 __func__, __LINE__);
5819 break;
5820 }
5821 break;
5822
5823 default:
5824 megasas_return_cmd(instance, cmd);
5825 }
5826 if (ret != DCMD_SUCCESS)
5827 dev_err(&instance->pdev->dev,
5828 "return from %s %d return value %d\n",
5829 __func__, __LINE__, ret);
5830
5831 return ret;
5832}
5833
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005834/**
5835 * megasas_start_aen - Subscribes to AEN during driver load time
5836 * @instance: Adapter soft state
5837 */
5838static int megasas_start_aen(struct megasas_instance *instance)
5839{
5840 struct megasas_evt_log_info eli;
5841 union megasas_evt_class_locale class_locale;
5842
5843 /*
5844 * Get the latest sequence number from FW
5845 */
5846 memset(&eli, 0, sizeof(eli));
5847
5848 if (megasas_get_seq_num(instance, &eli))
5849 return -1;
5850
5851 /*
5852 * Register AEN with FW for latest sequence number plus 1
5853 */
5854 class_locale.members.reserved = 0;
5855 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5856 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5857
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305858 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305859 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305860 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005861}
5862
5863/**
5864 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5865 * @instance: Adapter soft state
5866 */
5867static int megasas_io_attach(struct megasas_instance *instance)
5868{
5869 struct Scsi_Host *host = instance->host;
5870
5871 /*
5872 * Export parameters required by SCSI mid-layer
5873 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005874 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305875 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005876 host->this_id = instance->init_id;
5877 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005878
5879 if (instance->fw_support_ieee)
5880 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5881
Yang, Bo1fd10682010-10-12 07:18:50 -06005882 /*
5883 * Check if the module parameter value for max_sectors can be used
5884 */
5885 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5886 instance->max_sectors_per_req = max_sectors;
5887 else {
5888 if (max_sectors) {
5889 if (((instance->pdev->device ==
5890 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5891 (instance->pdev->device ==
5892 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5893 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5894 instance->max_sectors_per_req = max_sectors;
5895 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005896 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005897 "and <= %d (or < 1MB for GEN2 controller)\n",
5898 instance->max_sectors_per_req);
5899 }
5900 }
5901 }
5902
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005903 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005904 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005905 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5906 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5907 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005908 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005909
5910 /*
5911 * Notify the mid-layer about the new controller
5912 */
5913 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305914 dev_err(&instance->pdev->dev,
5915 "Failed to add host from %s %d\n",
5916 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005917 return -ENODEV;
5918 }
5919
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005920 return 0;
5921}
5922
bo yang31ea7082007-11-07 12:09:50 -05005923static int
5924megasas_set_dma_mask(struct pci_dev *pdev)
5925{
5926 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005927 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005928 */
5929 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005930 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005931
Yang Hongyang284901a2009-04-06 19:01:15 -07005932 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005933 goto fail_set_dma_mask;
5934 }
5935 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005936 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005937 goto fail_set_dma_mask;
5938 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305939 /*
5940 * Ensure that all data structures are allocated in 32-bit
5941 * memory.
5942 */
5943 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5944 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5945 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5946 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5947 dev_info(&pdev->dev, "set 32bit DMA mask"
5948 "and 32 bit consistent mask\n");
5949 else
5950 goto fail_set_dma_mask;
5951 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305952
bo yang31ea7082007-11-07 12:09:50 -05005953 return 0;
5954
5955fail_set_dma_mask:
5956 return 1;
5957}
5958
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005959/**
5960 * megasas_probe_one - PCI hotplug entry point
5961 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005962 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005963 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005964static int megasas_probe_one(struct pci_dev *pdev,
5965 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005966{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305967 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005968 struct Scsi_Host *host;
5969 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005970 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305971 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005972
5973 /* Reset MSI-X in the kdump kernel */
5974 if (reset_devices) {
5975 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5976 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005977 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005978 &control);
5979 if (control & PCI_MSIX_FLAGS_ENABLE) {
5980 dev_info(&pdev->dev, "resetting MSI-X\n");
5981 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005982 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005983 control &
5984 ~PCI_MSIX_FLAGS_ENABLE);
5985 }
5986 }
5987 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005988
5989 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005990 * PCI prepping: enable device set bus mastering and dma mask
5991 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005992 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005993
5994 if (rval) {
5995 return rval;
5996 }
5997
5998 pci_set_master(pdev);
5999
bo yang31ea7082007-11-07 12:09:50 -05006000 if (megasas_set_dma_mask(pdev))
6001 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006002
6003 host = scsi_host_alloc(&megasas_template,
6004 sizeof(struct megasas_instance));
6005
6006 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006007 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006008 goto fail_alloc_instance;
6009 }
6010
6011 instance = (struct megasas_instance *)host->hostdata;
6012 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006013 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08006014 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006015
adam radford9c915a82010-12-21 13:34:31 -08006016 switch (instance->pdev->device) {
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -05006017 case PCI_DEVICE_ID_LSI_VENTURA:
6018 case PCI_DEVICE_ID_LSI_HARPOON:
6019 case PCI_DEVICE_ID_LSI_TOMCAT:
6020 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6021 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6022 instance->is_ventura = true;
adam radford9c915a82010-12-21 13:34:31 -08006023 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07006024 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07006025 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05306026 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05306027 case PCI_DEVICE_ID_LSI_INTRUDER:
6028 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05306029 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6030 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08006031 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306032 instance->ctrl_context_pages =
6033 get_order(sizeof(struct fusion_context));
6034 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
6035 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08006036 if (!instance->ctrl_context) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006037 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08006038 "memory for Fusion context info\n");
6039 goto fail_alloc_dma_buf;
6040 }
6041 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05306042 memset(fusion, 0,
6043 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306044 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
6045 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
6046 fusion->adapter_type = THUNDERBOLT_SERIES;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05006047 else if (instance->is_ventura)
6048 fusion->adapter_type = VENTURA_SERIES;
6049 else
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306050 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08006051 }
6052 break;
6053 default: /* For all other supported controllers */
6054
6055 instance->producer =
6056 pci_alloc_consistent(pdev, sizeof(u32),
6057 &instance->producer_h);
6058 instance->consumer =
6059 pci_alloc_consistent(pdev, sizeof(u32),
6060 &instance->consumer_h);
6061
6062 if (!instance->producer || !instance->consumer) {
Colin Ian King4086eae52016-09-12 13:42:12 +01006063 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08006064 "memory for producer, consumer\n");
6065 goto fail_alloc_dma_buf;
6066 }
6067
6068 *instance->producer = 0;
6069 *instance->consumer = 0;
6070 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006071 }
6072
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306073 /* Crash dump feature related initialisation*/
6074 instance->drv_buf_index = 0;
6075 instance->drv_buf_alloc = 0;
6076 instance->crash_dump_fw_support = 0;
6077 instance->crash_dump_app_support = 0;
6078 instance->fw_crash_state = UNAVAILABLE;
6079 spin_lock_init(&instance->crashdump_lock);
6080 instance->crash_dump_buf = NULL;
6081
Yang, Boc3518832009-10-06 14:18:02 -06006082 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06006083 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006084 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04006085 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05306086 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306087 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006088
6089 instance->evt_detail = pci_alloc_consistent(pdev,
6090 sizeof(struct
6091 megasas_evt_detail),
6092 &instance->evt_detail_h);
6093
6094 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006095 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006096 "event detail structure\n");
6097 goto fail_alloc_dma_buf;
6098 }
6099
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006100 if (!reset_devices) {
6101 instance->system_info_buf = pci_zalloc_consistent(pdev,
6102 sizeof(struct MR_DRV_SYSTEM_INFO),
6103 &instance->system_info_h);
6104 if (!instance->system_info_buf)
6105 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
Sumit Saxena2216c302016-01-28 21:04:26 +05306106
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006107 instance->pd_info = pci_alloc_consistent(pdev,
6108 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
6109
Shivasharan S96188a82017-02-10 00:59:11 -08006110 instance->pd_info = pci_alloc_consistent(pdev,
6111 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
6112 instance->tgt_prop = pci_alloc_consistent(pdev,
6113 sizeof(struct MR_TARGET_PROPERTIES), &instance->tgt_prop_h);
6114
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006115 if (!instance->pd_info)
6116 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
6117
Shivasharan S96188a82017-02-10 00:59:11 -08006118 if (!instance->tgt_prop)
6119 dev_err(&instance->pdev->dev, "Failed to alloc mem for tgt_prop\n");
6120
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006121 instance->crash_dump_buf = pci_alloc_consistent(pdev,
6122 CRASH_DMA_BUF_SIZE,
6123 &instance->crash_dump_h);
6124 if (!instance->crash_dump_buf)
6125 dev_err(&pdev->dev, "Can't allocate Firmware "
6126 "crash dump DMA buffer\n");
6127 }
Sumit Saxena2216c302016-01-28 21:04:26 +05306128
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006129 /*
6130 * Initialize locks and queues
6131 */
6132 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04006133 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006134
Sumant Patroe4a082c2006-05-30 12:03:37 -07006135 atomic_set(&instance->fw_outstanding,0);
6136
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006137 init_waitqueue_head(&instance->int_cmd_wait_q);
6138 init_waitqueue_head(&instance->abort_cmd_wait_q);
6139
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306140 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04006141 spin_lock_init(&instance->hba_lock);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006142 spin_lock_init(&instance->stream_lock);
bo yang7343eb62007-11-09 04:35:44 -05006143 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006144
adam radford9c915a82010-12-21 13:34:31 -08006145 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05306146 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006147
6148 /*
6149 * Initialize PCI related and misc parameters
6150 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006151 instance->host = host;
6152 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6153 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306154 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006155
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306156
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006157 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306158 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06006159 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006160
Sumant Patro658dced2006-10-03 13:09:14 -07006161 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006162 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06006163 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006164 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04006165 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05306166 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04006167
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306168 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006169 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306170 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
6171 } else
adam radford9c915a82010-12-21 13:34:31 -08006172 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07006173
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006174 /*
adam radford0a770662011-02-24 20:56:12 -08006175 * Initialize MFI Firmware
6176 */
6177 if (megasas_init_fw(instance))
6178 goto fail_init_mfi;
6179
adam radford229fe472014-03-10 02:51:56 -07006180 if (instance->requestorId) {
6181 if (instance->PlasmaFW111) {
6182 instance->vf_affiliation_111 =
6183 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
6184 &instance->vf_affiliation_111_h);
6185 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006186 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006187 "memory for VF affiliation buffer\n");
6188 } else {
6189 instance->vf_affiliation =
6190 pci_alloc_consistent(pdev,
6191 (MAX_LOGICAL_DRIVES + 1) *
6192 sizeof(struct MR_LD_VF_AFFILIATION),
6193 &instance->vf_affiliation_h);
6194 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006195 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006196 "memory for VF affiliation buffer\n");
6197 }
6198 }
6199
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006200 /*
6201 * Store instance in PCI softstate
6202 */
6203 pci_set_drvdata(pdev, instance);
6204
6205 /*
6206 * Add this controller to megasas_mgmt_info structure so that it
6207 * can be exported to management applications
6208 */
6209 megasas_mgmt_info.count++;
6210 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6211 megasas_mgmt_info.max_index++;
6212
6213 /*
adam radford541f90b2011-05-11 18:34:29 -07006214 * Register with SCSI mid-layer
6215 */
6216 if (megasas_io_attach(instance))
6217 goto fail_io_attach;
6218
6219 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306220 /*
6221 * Trigger SCSI to scan our drives
6222 */
6223 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07006224
6225 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006226 * Initiate AEN (Asynchronous Event Notification)
6227 */
6228 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006229 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006230 goto fail_start_aen;
6231 }
6232
Adam Radford9ea81f82014-07-09 15:17:57 -07006233 /* Get current SR-IOV LD/VF affiliation */
6234 if (instance->requestorId)
6235 megasas_get_ld_vf_affiliation(instance, 1);
6236
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006237 return 0;
6238
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006239fail_start_aen:
6240fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006241 megasas_mgmt_info.count--;
6242 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
6243 megasas_mgmt_info.max_index--;
6244
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306245 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306246 megasas_destroy_irqs(instance);
6247
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306248 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08006249 megasas_release_fusion(instance);
6250 else
6251 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006252 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006253 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306254fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006255fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006256 if (instance->evt_detail)
6257 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6258 instance->evt_detail,
6259 instance->evt_detail_h);
6260
Sumit Saxena2216c302016-01-28 21:04:26 +05306261 if (instance->pd_info)
6262 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6263 instance->pd_info,
6264 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006265 if (instance->tgt_prop)
6266 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6267 instance->tgt_prop,
6268 instance->tgt_prop_h);
adam radfordeb1b1232011-02-24 20:55:56 -08006269 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006270 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6271 instance->producer_h);
6272 if (instance->consumer)
6273 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6274 instance->consumer_h);
6275 scsi_host_put(host);
6276
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006277fail_alloc_instance:
6278fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006279 pci_disable_device(pdev);
6280
6281 return -ENODEV;
6282}
6283
6284/**
6285 * megasas_flush_cache - Requests FW to flush all its caches
6286 * @instance: Adapter soft state
6287 */
6288static void megasas_flush_cache(struct megasas_instance *instance)
6289{
6290 struct megasas_cmd *cmd;
6291 struct megasas_dcmd_frame *dcmd;
6292
Sumit Saxena8a01a412016-01-28 21:04:32 +05306293 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006294 return;
6295
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006296 cmd = megasas_get_cmd(instance);
6297
6298 if (!cmd)
6299 return;
6300
6301 dcmd = &cmd->frame->dcmd;
6302
6303 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6304
6305 dcmd->cmd = MFI_CMD_DCMD;
6306 dcmd->cmd_status = 0x0;
6307 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306308 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006309 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006310 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006311 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306312 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006313 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6314
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306315 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6316 != DCMD_SUCCESS) {
6317 dev_err(&instance->pdev->dev,
6318 "return from %s %d\n", __func__, __LINE__);
6319 return;
6320 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006321
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306322 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006323}
6324
6325/**
6326 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6327 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006328 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006329 */
bo yang31ea7082007-11-07 12:09:50 -05006330static void megasas_shutdown_controller(struct megasas_instance *instance,
6331 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006332{
6333 struct megasas_cmd *cmd;
6334 struct megasas_dcmd_frame *dcmd;
6335
Sumit Saxena8a01a412016-01-28 21:04:32 +05306336 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006337 return;
6338
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006339 cmd = megasas_get_cmd(instance);
6340
6341 if (!cmd)
6342 return;
6343
6344 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306345 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306346 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006347 if (instance->map_update_cmd)
6348 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306349 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306350 if (instance->jbod_seq_cmd)
6351 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306352 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306353
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006354 dcmd = &cmd->frame->dcmd;
6355
6356 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6357
6358 dcmd->cmd = MFI_CMD_DCMD;
6359 dcmd->cmd_status = 0x0;
6360 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306361 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006362 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006363 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006364 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306365 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006366
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306367 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6368 != DCMD_SUCCESS) {
6369 dev_err(&instance->pdev->dev,
6370 "return from %s %d\n", __func__, __LINE__);
6371 return;
6372 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006373
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306374 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006375}
6376
Jiri Slaby33139b22008-05-01 17:56:02 +02006377#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006378/**
bo yangad84db22007-11-09 04:40:16 -05006379 * megasas_suspend - driver suspend entry point
6380 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006381 * @state: PCI power state to suspend routine
6382 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006383static int
bo yang31ea7082007-11-07 12:09:50 -05006384megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6385{
6386 struct Scsi_Host *host;
6387 struct megasas_instance *instance;
6388
6389 instance = pci_get_drvdata(pdev);
6390 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006391 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006392
adam radford229fe472014-03-10 02:51:56 -07006393 /* Shutdown SR-IOV heartbeat timer */
6394 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6395 del_timer_sync(&instance->sriov_heartbeat_timer);
6396
bo yang31ea7082007-11-07 12:09:50 -05006397 megasas_flush_cache(instance);
6398 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006399
6400 /* cancel the delayed work if this work still in queue */
6401 if (instance->ev != NULL) {
6402 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006403 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006404 instance->ev = NULL;
6405 }
6406
bo yang31ea7082007-11-07 12:09:50 -05006407 tasklet_kill(&instance->isr_tasklet);
6408
6409 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306410 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006411
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306412 megasas_destroy_irqs(instance);
6413
adam radfordc8e858f2011-10-08 18:15:13 -07006414 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006415 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006416
6417 pci_save_state(pdev);
6418 pci_disable_device(pdev);
6419
6420 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6421
6422 return 0;
6423}
6424
6425/**
6426 * megasas_resume- driver resume entry point
6427 * @pdev: PCI device structure
6428 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006429static int
bo yang31ea7082007-11-07 12:09:50 -05006430megasas_resume(struct pci_dev *pdev)
6431{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306432 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006433 struct Scsi_Host *host;
6434 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01006435 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05006436
6437 instance = pci_get_drvdata(pdev);
6438 host = instance->host;
6439 pci_set_power_state(pdev, PCI_D0);
6440 pci_enable_wake(pdev, PCI_D0, 0);
6441 pci_restore_state(pdev);
6442
6443 /*
6444 * PCI prepping: enable device set bus mastering and dma mask
6445 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006446 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006447
6448 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006449 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006450 return rval;
6451 }
6452
6453 pci_set_master(pdev);
6454
6455 if (megasas_set_dma_mask(pdev))
6456 goto fail_set_dma_mask;
6457
6458 /*
6459 * Initialize MFI Firmware
6460 */
6461
bo yang31ea7082007-11-07 12:09:50 -05006462 atomic_set(&instance->fw_outstanding, 0);
6463
6464 /*
6465 * We expect the FW state to be READY
6466 */
adam radford058a8fa2011-10-08 18:14:27 -07006467 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006468 goto fail_ready_state;
6469
adam radford3f1abce2011-05-11 18:33:47 -07006470 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01006471 if (instance->msix_vectors) {
6472 irq_flags = PCI_IRQ_MSIX;
6473 if (smp_affinity_enable)
6474 irq_flags |= PCI_IRQ_AFFINITY;
6475 }
6476 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6477 instance->msix_vectors ?
6478 instance->msix_vectors : 1, irq_flags);
6479 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02006480 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006481
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306482 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006483 megasas_reset_reply_desc(instance);
6484 if (megasas_ioc_init_fusion(instance)) {
6485 megasas_free_cmds(instance);
6486 megasas_free_cmds_fusion(instance);
6487 goto fail_init_mfi;
6488 }
6489 if (!megasas_get_map_info(instance))
6490 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306491 } else {
adam radford9c915a82010-12-21 13:34:31 -08006492 *instance->producer = 0;
6493 *instance->consumer = 0;
6494 if (megasas_issue_init_mfi(instance))
6495 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006496 }
bo yang31ea7082007-11-07 12:09:50 -05006497
adam radford9c915a82010-12-21 13:34:31 -08006498 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6499 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006500
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306501 if (instance->msix_vectors ?
6502 megasas_setup_irqs_msix(instance, 0) :
6503 megasas_setup_irqs_ioapic(instance))
6504 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006505
adam radford229fe472014-03-10 02:51:56 -07006506 /* Re-launch SR-IOV heartbeat timer */
6507 if (instance->requestorId) {
6508 if (!megasas_sriov_start_heartbeat(instance, 0))
6509 megasas_start_timer(instance,
6510 &instance->sriov_heartbeat_timer,
6511 megasas_sriov_heartbeat_handler,
6512 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306513 else {
adam radford229fe472014-03-10 02:51:56 -07006514 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306515 goto fail_init_mfi;
6516 }
adam radford229fe472014-03-10 02:51:56 -07006517 }
6518
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306519 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306520 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006521 instance->unload = 0;
6522
adam radford541f90b2011-05-11 18:34:29 -07006523 /*
6524 * Initiate AEN (Asynchronous Event Notification)
6525 */
6526 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006527 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006528
bo yang31ea7082007-11-07 12:09:50 -05006529 return 0;
6530
bo yang31ea7082007-11-07 12:09:50 -05006531fail_init_mfi:
6532 if (instance->evt_detail)
6533 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6534 instance->evt_detail,
6535 instance->evt_detail_h);
6536
Sumit Saxena2216c302016-01-28 21:04:26 +05306537 if (instance->pd_info)
6538 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6539 instance->pd_info,
6540 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006541 if (instance->tgt_prop)
6542 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6543 instance->tgt_prop,
6544 instance->tgt_prop_h);
bo yang31ea7082007-11-07 12:09:50 -05006545 if (instance->producer)
6546 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6547 instance->producer_h);
6548 if (instance->consumer)
6549 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6550 instance->consumer_h);
6551 scsi_host_put(host);
6552
6553fail_set_dma_mask:
6554fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006555fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006556
6557 pci_disable_device(pdev);
6558
6559 return -ENODEV;
6560}
Jiri Slaby33139b22008-05-01 17:56:02 +02006561#else
6562#define megasas_suspend NULL
6563#define megasas_resume NULL
6564#endif
bo yang31ea7082007-11-07 12:09:50 -05006565
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006566static inline int
6567megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6568{
6569 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6570 int i;
6571
6572 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6573 return 1;
6574
6575 for (i = 0; i < wait_time; i++) {
6576 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6577 break;
6578
6579 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6580 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6581
6582 msleep(1000);
6583 }
6584
6585 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6586 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6587 __func__);
6588 return 1;
6589 }
6590
6591 return 0;
6592}
6593
bo yang31ea7082007-11-07 12:09:50 -05006594/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006595 * megasas_detach_one - PCI hot"un"plug entry point
6596 * @pdev: PCI device structure
6597 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006598static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006599{
6600 int i;
6601 struct Scsi_Host *host;
6602 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006603 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306604 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006605
6606 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006607 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006608 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006609 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006610
adam radford229fe472014-03-10 02:51:56 -07006611 /* Shutdown SR-IOV heartbeat timer */
6612 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6613 del_timer_sync(&instance->sriov_heartbeat_timer);
6614
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306615 if (instance->fw_crash_state != UNAVAILABLE)
6616 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006617 scsi_remove_host(instance->host);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006618
6619 if (megasas_wait_for_adapter_operational(instance))
6620 goto skip_firing_dcmds;
6621
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006622 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006623 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006624
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006625skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006626 /* cancel the delayed work if this work still in queue*/
6627 if (instance->ev != NULL) {
6628 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006629 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006630 instance->ev = NULL;
6631 }
6632
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306633 /* cancel all wait events */
6634 wake_up_all(&instance->int_cmd_wait_q);
6635
Sumant Patro5d018ad2006-10-03 13:13:18 -07006636 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006637
6638 /*
6639 * Take the instance off the instance array. Note that we will not
6640 * decrement the max_index. We let this array be sparse array
6641 */
6642 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6643 if (megasas_mgmt_info.instance[i] == instance) {
6644 megasas_mgmt_info.count--;
6645 megasas_mgmt_info.instance[i] = NULL;
6646
6647 break;
6648 }
6649 }
6650
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306651 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006652
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306653 megasas_destroy_irqs(instance);
6654
adam radfordc8e858f2011-10-08 18:15:13 -07006655 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006656 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006657
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006658 if (instance->is_ventura) {
6659 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6660 kfree(fusion->stream_detect_by_ld[i]);
6661 kfree(fusion->stream_detect_by_ld);
6662 fusion->stream_detect_by_ld = NULL;
6663 }
6664
6665
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306666 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006667 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306668 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6669 (sizeof(struct MR_PD_CFG_SEQ) *
6670 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306671 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006672 if (fusion->ld_map[i])
6673 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306674 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006675 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306676 fusion->ld_map_phys[i]);
6677 if (fusion->ld_drv_map[i])
6678 free_pages((ulong)fusion->ld_drv_map[i],
6679 fusion->drv_map_pages);
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006680 if (fusion->pd_seq_sync[i])
6681 dma_free_coherent(&instance->pdev->dev,
6682 pd_seq_map_sz,
6683 fusion->pd_seq_sync[i],
6684 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306685 }
6686 free_pages((ulong)instance->ctrl_context,
6687 instance->ctrl_context_pages);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306688 } else {
adam radford9c915a82010-12-21 13:34:31 -08006689 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006690 pci_free_consistent(pdev, sizeof(u32),
6691 instance->producer,
6692 instance->producer_h);
6693 pci_free_consistent(pdev, sizeof(u32),
6694 instance->consumer,
6695 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08006696 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006697
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306698 kfree(instance->ctrl_info);
6699
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05306700 if (instance->evt_detail)
6701 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6702 instance->evt_detail, instance->evt_detail_h);
Sumit Saxena2216c302016-01-28 21:04:26 +05306703 if (instance->pd_info)
6704 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6705 instance->pd_info,
6706 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006707 if (instance->tgt_prop)
6708 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6709 instance->tgt_prop,
6710 instance->tgt_prop_h);
adam radford229fe472014-03-10 02:51:56 -07006711 if (instance->vf_affiliation)
6712 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6713 sizeof(struct MR_LD_VF_AFFILIATION),
6714 instance->vf_affiliation,
6715 instance->vf_affiliation_h);
6716
6717 if (instance->vf_affiliation_111)
6718 pci_free_consistent(pdev,
6719 sizeof(struct MR_LD_VF_AFFILIATION_111),
6720 instance->vf_affiliation_111,
6721 instance->vf_affiliation_111_h);
6722
6723 if (instance->hb_host_mem)
6724 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6725 instance->hb_host_mem,
6726 instance->hb_host_mem_h);
6727
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306728 if (instance->crash_dump_buf)
6729 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6730 instance->crash_dump_buf, instance->crash_dump_h);
6731
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306732 if (instance->system_info_buf)
6733 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6734 instance->system_info_buf, instance->system_info_h);
6735
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006736 scsi_host_put(host);
6737
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006738 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006739}
6740
6741/**
6742 * megasas_shutdown - Shutdown entry point
6743 * @device: Generic device structure
6744 */
6745static void megasas_shutdown(struct pci_dev *pdev)
6746{
6747 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006748
Yang, Bo0c79e682009-10-06 14:47:35 -06006749 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006750
6751 if (megasas_wait_for_adapter_operational(instance))
6752 goto skip_firing_dcmds;
6753
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006754 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006755 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006756
6757skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306758 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306759 megasas_destroy_irqs(instance);
6760
adam radfordc8e858f2011-10-08 18:15:13 -07006761 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006762 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006763}
6764
6765/**
6766 * megasas_mgmt_open - char node "open" entry point
6767 */
6768static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6769{
6770 /*
6771 * Allow only those users with admin rights
6772 */
6773 if (!capable(CAP_SYS_ADMIN))
6774 return -EACCES;
6775
6776 return 0;
6777}
6778
6779/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006780 * megasas_mgmt_fasync - Async notifier registration from applications
6781 *
6782 * This function adds the calling process to a driver global queue. When an
6783 * event occurs, SIGIO will be sent to all processes in this queue.
6784 */
6785static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6786{
6787 int rc;
6788
Arjan van de Ven0b950672006-01-11 13:16:10 +01006789 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006790
6791 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6792
Arjan van de Ven0b950672006-01-11 13:16:10 +01006793 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006794
6795 if (rc >= 0) {
6796 /* For sanity check when we get ioctl */
6797 filep->private_data = filep;
6798 return 0;
6799 }
6800
6801 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6802
6803 return rc;
6804}
6805
6806/**
Yang, Boc3518832009-10-06 14:18:02 -06006807 * megasas_mgmt_poll - char node "poll" entry point
6808 * */
6809static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6810{
6811 unsigned int mask;
6812 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006813
Yang, Boc3518832009-10-06 14:18:02 -06006814 poll_wait(file, &megasas_poll_wait, wait);
6815 spin_lock_irqsave(&poll_aen_lock, flags);
6816 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006817 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006818 else
6819 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306820 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006821 spin_unlock_irqrestore(&poll_aen_lock, flags);
6822 return mask;
6823}
6824
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306825/*
6826 * megasas_set_crash_dump_params_ioctl:
6827 * Send CRASH_DUMP_MODE DCMD to all controllers
6828 * @cmd: MFI command frame
6829 */
6830
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006831static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306832{
6833 struct megasas_instance *local_instance;
6834 int i, error = 0;
6835 int crash_support;
6836
6837 crash_support = cmd->frame->dcmd.mbox.w[0];
6838
6839 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6840 local_instance = megasas_mgmt_info.instance[i];
6841 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05306842 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306843 MEGASAS_HBA_OPERATIONAL) &&
6844 !megasas_set_crash_dump_params(local_instance,
6845 crash_support)) {
6846 local_instance->crash_dump_app_support =
6847 crash_support;
6848 dev_info(&local_instance->pdev->dev,
6849 "Application firmware crash "
6850 "dump mode set success\n");
6851 error = 0;
6852 } else {
6853 dev_info(&local_instance->pdev->dev,
6854 "Application firmware crash "
6855 "dump mode set failed\n");
6856 error = -1;
6857 }
6858 }
6859 }
6860 return error;
6861}
6862
Yang, Boc3518832009-10-06 14:18:02 -06006863/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006864 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6865 * @instance: Adapter soft state
6866 * @argp: User's ioctl packet
6867 */
6868static int
6869megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6870 struct megasas_iocpacket __user * user_ioc,
6871 struct megasas_iocpacket *ioc)
6872{
6873 struct megasas_sge32 *kern_sge32;
6874 struct megasas_cmd *cmd;
6875 void *kbuff_arr[MAX_IOCTL_SGE];
6876 dma_addr_t buf_handle = 0;
6877 int error = 0, i;
6878 void *sense = NULL;
6879 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006880 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006881
6882 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6883
6884 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006885 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006886 ioc->sge_count, MAX_IOCTL_SGE);
6887 return -EINVAL;
6888 }
6889
6890 cmd = megasas_get_cmd(instance);
6891 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006892 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006893 return -ENOMEM;
6894 }
6895
6896 /*
6897 * User's IOCTL packet has 2 frames (maximum). Copy those two
6898 * frames into our cmd's frames. cmd->frame's context will get
6899 * overwritten when we copy from user's frames. So set that value
6900 * alone separately
6901 */
6902 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306903 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006904 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306905 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6906 MFI_FRAME_SGL64 |
6907 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006908
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306909 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6910 error = megasas_set_crash_dump_params_ioctl(cmd);
6911 megasas_return_cmd(instance, cmd);
6912 return error;
6913 }
6914
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006915 /*
6916 * The management interface between applications and the fw uses
6917 * MFI frames. E.g, RAID configuration changes, LD property changes
6918 * etc are accomplishes through different kinds of MFI frames. The
6919 * driver needs to care only about substituting user buffers with
6920 * kernel buffers in SGLs. The location of SGL is embedded in the
6921 * struct iocpacket itself.
6922 */
6923 kern_sge32 = (struct megasas_sge32 *)
6924 ((unsigned long)cmd->frame + ioc->sgl_off);
6925
6926 /*
6927 * For each user buffer, create a mirror buffer and copy in
6928 */
6929 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006930 if (!ioc->sgl[i].iov_len)
6931 continue;
6932
Sumant Patro9f35fa82007-02-14 12:55:45 -08006933 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006934 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006935 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006936 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006937 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6938 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006939 error = -ENOMEM;
6940 goto out;
6941 }
6942
6943 /*
6944 * We don't change the dma_coherent_mask, so
6945 * pci_alloc_consistent only returns 32bit addresses
6946 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306947 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6948 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006949
6950 /*
6951 * We created a kernel buffer corresponding to the
6952 * user buffer. Now copy in from the user buffer
6953 */
6954 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6955 (u32) (ioc->sgl[i].iov_len))) {
6956 error = -EFAULT;
6957 goto out;
6958 }
6959 }
6960
6961 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006962 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6963 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006964 if (!sense) {
6965 error = -ENOMEM;
6966 goto out;
6967 }
6968
6969 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006970 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306971 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006972 }
6973
6974 /*
6975 * Set the sync_cmd flag so that the ISR knows not to complete this
6976 * cmd to the SCSI mid-layer
6977 */
6978 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306979 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6980 cmd->sync_cmd = 0;
6981 dev_err(&instance->pdev->dev,
6982 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6983 __func__, __LINE__, cmd->frame->dcmd.opcode,
6984 cmd->cmd_status_drv);
6985 return -EBUSY;
6986 }
6987
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006988 cmd->sync_cmd = 0;
6989
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306990 if (instance->unload == 1) {
6991 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6992 "don't submit data to application\n");
6993 goto out;
6994 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006995 /*
6996 * copy out the kernel buffers to user buffers
6997 */
6998 for (i = 0; i < ioc->sge_count; i++) {
6999 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7000 ioc->sgl[i].iov_len)) {
7001 error = -EFAULT;
7002 goto out;
7003 }
7004 }
7005
7006 /*
7007 * copy out the sense
7008 */
7009 if (ioc->sense_len) {
7010 /*
bo yangb70a41e2008-03-18 03:13:06 -04007011 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007012 * sense buffer address
7013 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007014 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7015 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007016
bo yangb70a41e2008-03-18 03:13:06 -04007017 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
7018 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007019 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007020 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007021 error = -EFAULT;
7022 goto out;
7023 }
7024 }
7025
7026 /*
7027 * copy the status codes returned by the fw
7028 */
7029 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7030 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007031 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007032 error = -EFAULT;
7033 }
7034
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007035out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007036 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007037 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007038 sense, sense_handle);
7039 }
7040
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007041 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007042 if (kbuff_arr[i]) {
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007043 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307044 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007045 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307046 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307047 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007048 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007049 }
7050
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307051 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007052 return error;
7053}
7054
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007055static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7056{
7057 struct megasas_iocpacket __user *user_ioc =
7058 (struct megasas_iocpacket __user *)arg;
7059 struct megasas_iocpacket *ioc;
7060 struct megasas_instance *instance;
7061 int error;
bo yang39a98552010-09-22 22:36:29 -04007062 int i;
7063 unsigned long flags;
7064 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007065
Markus Elfring709ab232016-08-21 10:39:04 +02007066 ioc = memdup_user(user_ioc, sizeof(*ioc));
7067 if (IS_ERR(ioc))
7068 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007069
7070 instance = megasas_lookup_instance(ioc->host_no);
7071 if (!instance) {
7072 error = -ENODEV;
7073 goto out_kfree_ioc;
7074 }
7075
adam radford229fe472014-03-10 02:51:56 -07007076 /* Adjust ioctl wait time for VF mode */
7077 if (instance->requestorId)
7078 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7079
7080 /* Block ioctls in VF mode */
7081 if (instance->requestorId && !allow_vf_ioctls) {
7082 error = -ENODEV;
7083 goto out_kfree_ioc;
7084 }
7085
Sumit Saxena8a01a412016-01-28 21:04:32 +05307086 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007087 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007088 error = -ENODEV;
7089 goto out_kfree_ioc;
7090 }
7091
7092 if (instance->unload == 1) {
7093 error = -ENODEV;
7094 goto out_kfree_ioc;
7095 }
7096
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007097 if (down_interruptible(&instance->ioctl_sem)) {
7098 error = -ERESTARTSYS;
7099 goto out_kfree_ioc;
7100 }
bo yang39a98552010-09-22 22:36:29 -04007101
7102 for (i = 0; i < wait_time; i++) {
7103
7104 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307105 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007106 spin_unlock_irqrestore(&instance->hba_lock, flags);
7107 break;
7108 }
7109 spin_unlock_irqrestore(&instance->hba_lock, flags);
7110
7111 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007112 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04007113 "for controller reset to finish\n");
7114 }
7115
7116 msleep(1000);
7117 }
7118
7119 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307120 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007121 spin_unlock_irqrestore(&instance->hba_lock, flags);
7122
Colin Ian King644da3c2016-11-12 16:25:24 +00007123 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007124 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007125 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007126 }
7127 spin_unlock_irqrestore(&instance->hba_lock, flags);
7128
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007129 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007130out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007131 up(&instance->ioctl_sem);
7132
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007133out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007134 kfree(ioc);
7135 return error;
7136}
7137
7138static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7139{
7140 struct megasas_instance *instance;
7141 struct megasas_aen aen;
7142 int error;
bo yang39a98552010-09-22 22:36:29 -04007143 int i;
7144 unsigned long flags;
7145 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007146
7147 if (file->private_data != file) {
7148 printk(KERN_DEBUG "megasas: fasync_helper was not "
7149 "called first\n");
7150 return -EINVAL;
7151 }
7152
7153 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7154 return -EFAULT;
7155
7156 instance = megasas_lookup_instance(aen.host_no);
7157
7158 if (!instance)
7159 return -ENODEV;
7160
Sumit Saxena8a01a412016-01-28 21:04:32 +05307161 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007162 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007163 }
7164
7165 if (instance->unload == 1) {
7166 return -ENODEV;
7167 }
7168
bo yang39a98552010-09-22 22:36:29 -04007169 for (i = 0; i < wait_time; i++) {
7170
7171 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307172 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007173 spin_unlock_irqrestore(&instance->hba_lock,
7174 flags);
7175 break;
7176 }
7177
7178 spin_unlock_irqrestore(&instance->hba_lock, flags);
7179
7180 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007181 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04007182 "controller reset to finish\n");
7183 }
7184
7185 msleep(1000);
7186 }
7187
7188 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307189 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007190 spin_unlock_irqrestore(&instance->hba_lock, flags);
Colin Ian King644da3c2016-11-12 16:25:24 +00007191 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007192 return -ENODEV;
7193 }
7194 spin_unlock_irqrestore(&instance->hba_lock, flags);
7195
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307196 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007197 error = megasas_register_aen(instance, aen.seq_num,
7198 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307199 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007200 return error;
7201}
7202
7203/**
7204 * megasas_mgmt_ioctl - char node ioctl entry point
7205 */
7206static long
7207megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7208{
7209 switch (cmd) {
7210 case MEGASAS_IOC_FIRMWARE:
7211 return megasas_mgmt_ioctl_fw(file, arg);
7212
7213 case MEGASAS_IOC_GET_AEN:
7214 return megasas_mgmt_ioctl_aen(file, arg);
7215 }
7216
7217 return -ENOTTY;
7218}
7219
7220#ifdef CONFIG_COMPAT
7221static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7222{
7223 struct compat_megasas_iocpacket __user *cioc =
7224 (struct compat_megasas_iocpacket __user *)arg;
7225 struct megasas_iocpacket __user *ioc =
7226 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7227 int i;
7228 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007229 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307230 u32 local_sense_off;
7231 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307232 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007233
Jeff Garzik83aabc12006-10-04 06:34:03 -04007234 if (clear_user(ioc, sizeof(*ioc)))
7235 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007236
7237 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7238 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7239 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7240 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7241 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7242 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7243 return -EFAULT;
7244
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007245 /*
7246 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7247 * sense_len is not null, so prepare the 64bit value under
7248 * the same condition.
7249 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307250 if (get_user(local_sense_off, &ioc->sense_off) ||
7251 get_user(local_sense_len, &ioc->sense_len) ||
7252 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307253 return -EFAULT;
7254
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307255 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007256 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307257 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007258 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307259 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007260 if (get_user(ptr, sense_cioc_ptr) ||
7261 put_user(compat_ptr(ptr), sense_ioc_ptr))
7262 return -EFAULT;
7263 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007264
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007265 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007266 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7267 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7268 copy_in_user(&ioc->sgl[i].iov_len,
7269 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7270 return -EFAULT;
7271 }
7272
7273 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7274
7275 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7276 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7277 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7278 return -EFAULT;
7279 }
7280 return error;
7281}
7282
7283static long
7284megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7285 unsigned long arg)
7286{
7287 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007288 case MEGASAS_IOC_FIRMWARE32:
7289 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007290 case MEGASAS_IOC_GET_AEN:
7291 return megasas_mgmt_ioctl_aen(file, arg);
7292 }
7293
7294 return -ENOTTY;
7295}
7296#endif
7297
7298/*
7299 * File operations structure for management interface
7300 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007301static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007302 .owner = THIS_MODULE,
7303 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007304 .fasync = megasas_mgmt_fasync,
7305 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06007306 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007307#ifdef CONFIG_COMPAT
7308 .compat_ioctl = megasas_mgmt_compat_ioctl,
7309#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02007310 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007311};
7312
7313/*
7314 * PCI hotplug support registration structure
7315 */
7316static struct pci_driver megasas_pci_driver = {
7317
7318 .name = "megaraid_sas",
7319 .id_table = megasas_pci_table,
7320 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007321 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05007322 .suspend = megasas_suspend,
7323 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007324 .shutdown = megasas_shutdown,
7325};
7326
7327/*
7328 * Sysfs driver attributes
7329 */
7330static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
7331{
7332 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7333 MEGASAS_VERSION);
7334}
7335
7336static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
7337
7338static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307339megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
7340{
7341 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7342 MEGASAS_RELDATE);
7343}
7344
7345static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
7346
7347static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06007348megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
7349{
7350 return sprintf(buf, "%u\n", support_poll_for_event);
7351}
7352
7353static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
7354 megasas_sysfs_show_support_poll_for_event, NULL);
7355
Yang, Bo837f5fe2010-10-11 06:59:20 -06007356 static ssize_t
7357megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
7358{
7359 return sprintf(buf, "%u\n", support_device_change);
7360}
7361
7362static DRIVER_ATTR(support_device_change, S_IRUGO,
7363 megasas_sysfs_show_support_device_change, NULL);
7364
Yang, Bo72c4fd32009-10-06 14:20:59 -06007365static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07007366megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
7367{
bo yangad84db22007-11-09 04:40:16 -05007368 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007369}
7370
7371static ssize_t
7372megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
7373{
7374 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007375
7376 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007377 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7378 retval = -EINVAL;
7379 }
7380 return retval;
7381}
7382
Joe Malicki66dca9b2008-08-14 17:14:48 -04007383static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05007384 megasas_sysfs_set_dbg_lvl);
7385
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007386static void
7387megasas_aen_polling(struct work_struct *work)
7388{
7389 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007390 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007391 struct megasas_instance *instance = ev->instance;
7392 union megasas_evt_class_locale class_locale;
7393 struct Scsi_Host *host;
7394 struct scsi_device *sdev1;
7395 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007396 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007397 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007398 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007399 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307400 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007401
7402 if (!instance) {
7403 printk(KERN_ERR "invalid instance!\n");
7404 kfree(ev);
7405 return;
7406 }
adam radford229fe472014-03-10 02:51:56 -07007407
7408 /* Adjust event workqueue thread wait time for VF mode */
7409 if (instance->requestorId)
7410 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7411
7412 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307413 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007414
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007415 instance->ev = NULL;
7416 host = instance->host;
7417 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307418 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007419
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307420 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307421
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007422 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307423 case MR_EVT_PD_REMOVED:
7424 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307425 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307426 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007427 break;
7428
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307429 case MR_EVT_LD_OFFLINE:
7430 case MR_EVT_CFG_CLEARED:
7431 case MR_EVT_LD_DELETED:
7432 case MR_EVT_LD_CREATED:
7433 if (!instance->requestorId ||
7434 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7435 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007436
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307437 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307438 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007439
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307440 break;
7441
7442 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7443 case MR_EVT_FOREIGN_CFG_IMPORTED:
7444 case MR_EVT_LD_STATE_CHANGE:
7445 dcmd_ret = megasas_get_pd_list(instance);
7446
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307447 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307448 break;
7449
7450 if (!instance->requestorId ||
7451 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7452 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7453
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307454 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307455 break;
7456
7457 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7458 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7459 instance->host->host_no);
7460 break;
7461
7462 case MR_EVT_CTRL_PROP_CHANGED:
7463 dcmd_ret = megasas_get_ctrl_info(instance);
7464 break;
7465 default:
7466 doscan = 0;
7467 break;
7468 }
7469 } else {
7470 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7471 mutex_unlock(&instance->reset_mutex);
7472 kfree(ev);
7473 return;
7474 }
7475
7476 mutex_unlock(&instance->reset_mutex);
7477
7478 if (doscan & SCAN_PD_CHANNEL) {
7479 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7480 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7481 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007482 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307483 if (instance->pd_list[pd_index].driveState ==
7484 MR_PD_STATE_SYSTEM) {
7485 if (!sdev1)
7486 scsi_add_device(host, i, j, 0);
7487 else
Yang, Boc9786842009-12-06 08:39:25 -07007488 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007489 } else {
7490 if (sdev1) {
7491 scsi_remove_device(sdev1);
7492 scsi_device_put(sdev1);
7493 }
7494 }
Yang, Boc9786842009-12-06 08:39:25 -07007495 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007496 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007497 }
7498
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307499 if (doscan & SCAN_VD_CHANNEL) {
7500 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7501 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7502 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7503 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7504 if (instance->ld_ids[ld_index] != 0xff) {
7505 if (!sdev1)
7506 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7507 else
7508 scsi_device_put(sdev1);
7509 } else {
7510 if (sdev1) {
7511 scsi_remove_device(sdev1);
7512 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007513 }
7514 }
7515 }
7516 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007517 }
7518
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307519 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307520 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7521 else
7522 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007523
7524 /* Register AEN with FW for latest sequence number plus 1 */
7525 class_locale.members.reserved = 0;
7526 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7527 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307528
7529 if (instance->aen_cmd != NULL) {
7530 kfree(ev);
7531 return;
7532 }
7533
7534 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007535 error = megasas_register_aen(instance, seq_num,
7536 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007537 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307538 dev_err(&instance->pdev->dev,
7539 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007540
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307541 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007542 kfree(ev);
7543}
7544
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007545/**
7546 * megasas_init - Driver load entry point
7547 */
7548static int __init megasas_init(void)
7549{
7550 int rval;
7551
7552 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007553 * Booted in kdump kernel, minimize memory footprints by
7554 * disabling few features
7555 */
7556 if (reset_devices) {
7557 msix_vectors = 1;
7558 rdpq_enable = 0;
7559 dual_qdepth_disable = 1;
7560 }
7561
7562 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007563 * Announce driver version and other information
7564 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307565 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007566
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007567 spin_lock_init(&poll_aen_lock);
7568
Yang, Bo72c4fd32009-10-06 14:20:59 -06007569 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007570 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007571
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007572 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7573
7574 /*
7575 * Register character device node
7576 */
7577 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7578
7579 if (rval < 0) {
7580 printk(KERN_DEBUG "megasas: failed to open device node\n");
7581 return rval;
7582 }
7583
7584 megasas_mgmt_majorno = rval;
7585
7586 /*
7587 * Register ourselves as PCI hotplug module
7588 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007589 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007590
7591 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007592 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007593 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007594 }
7595
Jeff Garzik83aabc12006-10-04 06:34:03 -04007596 rval = driver_create_file(&megasas_pci_driver.driver,
7597 &driver_attr_version);
7598 if (rval)
7599 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007600
7601 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307602 &driver_attr_release_date);
7603 if (rval)
7604 goto err_dcf_rel_date;
7605
7606 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007607 &driver_attr_support_poll_for_event);
7608 if (rval)
7609 goto err_dcf_support_poll_for_event;
7610
Jeff Garzik83aabc12006-10-04 06:34:03 -04007611 rval = driver_create_file(&megasas_pci_driver.driver,
7612 &driver_attr_dbg_lvl);
7613 if (rval)
7614 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007615 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007616 &driver_attr_support_device_change);
7617 if (rval)
7618 goto err_dcf_support_device_change;
7619
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007620 return rval;
bo yangad84db22007-11-09 04:40:16 -05007621
Yang, Bo837f5fe2010-10-11 06:59:20 -06007622err_dcf_support_device_change:
7623 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007624 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007625err_dcf_dbg_lvl:
7626 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007627 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007628err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307629 driver_remove_file(&megasas_pci_driver.driver,
7630 &driver_attr_release_date);
7631err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007632 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7633err_dcf_attr_ver:
7634 pci_unregister_driver(&megasas_pci_driver);
7635err_pcidrv:
7636 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007637 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007638}
7639
7640/**
7641 * megasas_exit - Driver unload entry point
7642 */
7643static void __exit megasas_exit(void)
7644{
Sumant Patro658dced2006-10-03 13:09:14 -07007645 driver_remove_file(&megasas_pci_driver.driver,
7646 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007647 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007648 &driver_attr_support_poll_for_event);
7649 driver_remove_file(&megasas_pci_driver.driver,
7650 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307651 driver_remove_file(&megasas_pci_driver.driver,
7652 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007653 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007654
7655 pci_unregister_driver(&megasas_pci_driver);
7656 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7657}
7658
7659module_init(megasas_init);
7660module_exit(megasas_exit);