blob: 5202c2fd72ac3f2eb5ae2228302e233b9b94b45f [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
adam radford3f1530c2010-12-14 18:51:48 -08002 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006 *
adam radford3f1530c2010-12-14 18:51:48 -08007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040011 *
adam radford3f1530c2010-12-14 18:51:48 -080012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040016 *
adam radford3f1530c2010-12-14 18:51:48 -080017 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040019 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053020 * Authors: Avago Technologies
adam radford3f1530c2010-12-14 18:51:48 -080021 * Sreenivas Bagalkote
22 * Sumant Patro
23 * Bo Yang
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford3f1530c2010-12-14 18:51:48 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040038#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
43#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Shivasharan S318aaef2017-02-10 00:59:22 -080046#include <asm/unaligned.h>
Al Viro43399232005-10-04 17:36:04 +010047#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040048#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080049#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010050#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060051#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040052
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070057#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080058#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040059#include "megaraid_sas.h"
60
bo yangad84db22007-11-09 04:40:16 -050061/*
Yang, Bo1fd10682010-10-12 07:18:50 -060062 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
64 */
65static unsigned int max_sectors;
66module_param_named(max_sectors, max_sectors, int, 0);
67MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
69
adam radford80d9da92010-12-21 10:17:40 -080070static int msix_disable;
71module_param(msix_disable, int, S_IRUGO);
72MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73
adam radford079eadd2012-10-01 19:26:59 -070074static unsigned int msix_vectors;
75module_param(msix_vectors, int, S_IRUGO);
76MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77
adam radford229fe472014-03-10 02:51:56 -070078static int allow_vf_ioctls;
79module_param(allow_vf_ioctls, int, S_IRUGO);
80MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053082static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070083module_param(throttlequeuedepth, int, S_IRUGO);
84MODULE_PARM_DESC(throttlequeuedepth,
85 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86
Sumit Saxenae3d178c2016-01-28 21:04:34 +053087unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070088module_param(resetwaittime, int, S_IRUGO);
89MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
90 "before resetting adapter. Default: 180");
91
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053092int smp_affinity_enable = 1;
93module_param(smp_affinity_enable, int, S_IRUGO);
94MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
95
Sumit Saxena179ac142016-01-28 21:04:28 +053096int rdpq_enable = 1;
97module_param(rdpq_enable, int, S_IRUGO);
98MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
99
Sumit Saxena308ec452016-01-28 21:04:30 +0530100unsigned int dual_qdepth_disable;
101module_param(dual_qdepth_disable, int, S_IRUGO);
102MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
103
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530104unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
105module_param(scmd_timeout, int, S_IRUGO);
106MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
107
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400108MODULE_LICENSE("GPL");
109MODULE_VERSION(MEGASAS_VERSION);
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +0530110MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
111MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400112
adam radford058a8fa2011-10-08 18:14:27 -0700113int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400114static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700115static int megasas_ld_list_query(struct megasas_instance *instance,
116 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400117static int megasas_issue_init_mfi(struct megasas_instance *instance);
118static int megasas_register_aen(struct megasas_instance *instance,
119 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800120static void megasas_get_pd_info(struct megasas_instance *instance,
121 struct scsi_device *sdev);
Shivasharan S96188a82017-02-10 00:59:11 -0800122static int megasas_get_target_prop(struct megasas_instance *instance,
123 struct scsi_device *sdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400124/*
125 * PCI ID table for all supported controllers
126 */
127static struct pci_device_id megasas_pci_table[] = {
128
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200129 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
130 /* xscale IOP */
131 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
132 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400133 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
134 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700135 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
136 /* gen2*/
137 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
138 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600139 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
140 /* skinny*/
141 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
142 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200143 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
144 /* xscale IOP, vega */
145 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
146 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800147 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
148 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700149 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
150 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700151 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
152 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530153 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
154 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530155 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
156 /* Intruder */
157 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
158 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530159 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500161 /* VENTURA */
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200167 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400168};
169
170MODULE_DEVICE_TABLE(pci, megasas_pci_table);
171
172static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700173struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400174static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100175static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400176
Yang, Boc3518832009-10-06 14:18:02 -0600177static int megasas_poll_wait_aen;
178static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600179static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800180u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600181static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700182
Yang, Boc3518832009-10-06 14:18:02 -0600183/* define lock for aen poll */
184spinlock_t poll_aen_lock;
185
adam radford9c915a82010-12-21 13:34:31 -0800186void
bo yang7343eb62007-11-09 04:35:44 -0500187megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
188 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800189static u32
190megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
191static int
192megasas_adp_reset_gen2(struct megasas_instance *instance,
193 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800194static irqreturn_t megasas_isr(int irq, void *devp);
195static u32
196megasas_init_adapter_mfi(struct megasas_instance *instance);
197u32
198megasas_build_and_issue_cmd(struct megasas_instance *instance,
199 struct scsi_cmnd *scmd);
200static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800201int
adam radford229fe472014-03-10 02:51:56 -0700202wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
203 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800204void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700205static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
206 int initial);
adam radfordcd50ba82010-12-21 10:23:23 -0800207
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800208void
adam radfordcd50ba82010-12-21 10:23:23 -0800209megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
210{
211 instance->instancet->fire_cmd(instance,
212 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800213 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800214}
bo yang7343eb62007-11-09 04:35:44 -0500215
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400216/**
217 * megasas_get_cmd - Get a command from the free pool
218 * @instance: Adapter soft state
219 *
220 * Returns a free command from the pool
221 */
adam radford9c915a82010-12-21 13:34:31 -0800222struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400223 *instance)
224{
225 unsigned long flags;
226 struct megasas_cmd *cmd = NULL;
227
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530228 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400229
230 if (!list_empty(&instance->cmd_pool)) {
231 cmd = list_entry((&instance->cmd_pool)->next,
232 struct megasas_cmd, list);
233 list_del_init(&cmd->list);
234 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500235 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400236 }
237
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530238 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400239 return cmd;
240}
241
242/**
243 * megasas_return_cmd - Return a cmd to free command pool
244 * @instance: Adapter soft state
245 * @cmd: Command packet to be returned to free command pool
246 */
Andi Kleen68b43742017-05-08 15:58:53 -0700247void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400248megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
249{
250 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530251 u32 blk_tags;
252 struct megasas_cmd_fusion *cmd_fusion;
253 struct fusion_context *fusion = instance->ctrl_context;
254
255 /* This flag is used only for fusion adapter.
256 * Wait for Interrupt for Polled mode DCMD
257 */
258 if (cmd->flags & DRV_DCMD_POLLED_MODE)
259 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400260
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530261 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400262
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530263 if (fusion) {
264 blk_tags = instance->max_scsi_cmds + cmd->index;
265 cmd_fusion = fusion->cmd_list[blk_tags];
266 megasas_return_cmd_fusion(instance, cmd_fusion);
267 }
268 cmd->scmd = NULL;
269 cmd->frame_count = 0;
270 cmd->flags = 0;
Shivasharan S21c34002017-02-10 00:59:28 -0800271 memset(cmd->frame, 0, instance->mfi_frame_size);
272 cmd->frame->io.context = cpu_to_le32(cmd->index);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530273 if (!fusion && reset_devices)
274 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
275 list_add(&cmd->list, (&instance->cmd_pool)->next);
276
277 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
278
279}
Sumant Patro1341c932006-01-25 12:02:40 -0800280
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530281static const char *
282format_timestamp(uint32_t timestamp)
283{
284 static char buffer[32];
285
286 if ((timestamp & 0xff000000) == 0xff000000)
287 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
288 0x00ffffff);
289 else
290 snprintf(buffer, sizeof(buffer), "%us", timestamp);
291 return buffer;
292}
293
294static const char *
295format_class(int8_t class)
296{
297 static char buffer[6];
298
299 switch (class) {
300 case MFI_EVT_CLASS_DEBUG:
301 return "debug";
302 case MFI_EVT_CLASS_PROGRESS:
303 return "progress";
304 case MFI_EVT_CLASS_INFO:
305 return "info";
306 case MFI_EVT_CLASS_WARNING:
307 return "WARN";
308 case MFI_EVT_CLASS_CRITICAL:
309 return "CRIT";
310 case MFI_EVT_CLASS_FATAL:
311 return "FATAL";
312 case MFI_EVT_CLASS_DEAD:
313 return "DEAD";
314 default:
315 snprintf(buffer, sizeof(buffer), "%d", class);
316 return buffer;
317 }
318}
319
320/**
321 * megasas_decode_evt: Decode FW AEN event and print critical event
322 * for information.
323 * @instance: Adapter soft state
324 */
325static void
326megasas_decode_evt(struct megasas_instance *instance)
327{
328 struct megasas_evt_detail *evt_detail = instance->evt_detail;
329 union megasas_evt_class_locale class_locale;
330 class_locale.word = le32_to_cpu(evt_detail->cl.word);
331
332 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
333 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
334 le32_to_cpu(evt_detail->seq_num),
335 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
336 (class_locale.members.locale),
337 format_class(class_locale.members.class),
338 evt_detail->description);
339}
340
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400341/**
adam radford0d490162010-12-14 19:17:17 -0800342* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800343* (deviceid : 1064R, PERC5) controllers
344*/
345
346/**
347 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400348 * @regs: MFI register set
349 */
350static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530351megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400352{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530353 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500354
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530355 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400356 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400357
358 /* Dummy readl to force pci flush */
359 readl(&regs->outbound_intr_mask);
360}
361
362/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700363 * megasas_disable_intr_xscale -Disables interrupt
364 * @regs: MFI register set
365 */
366static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530367megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700368{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530369 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700370 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500371
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530372 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700373 writel(mask, &regs->outbound_intr_mask);
374 /* Dummy readl to force pci flush */
375 readl(&regs->outbound_intr_mask);
376}
377
378/**
Sumant Patro1341c932006-01-25 12:02:40 -0800379 * megasas_read_fw_status_reg_xscale - returns the current FW status value
380 * @regs: MFI register set
381 */
382static u32
383megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
384{
385 return readl(&(regs)->outbound_msg_0);
386}
387/**
388 * megasas_clear_interrupt_xscale - Check & clear interrupt
389 * @regs: MFI register set
390 */
adam radford0d490162010-12-14 19:17:17 -0800391static int
Sumant Patro1341c932006-01-25 12:02:40 -0800392megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
393{
394 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400395 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500396
Sumant Patro1341c932006-01-25 12:02:40 -0800397 /*
398 * Check if it is our interrupt
399 */
400 status = readl(&regs->outbound_intr_status);
401
bo yang39a98552010-09-22 22:36:29 -0400402 if (status & MFI_OB_INTR_STATUS_MASK)
403 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
404 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
405 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800406
407 /*
408 * Clear the interrupt by writing back the same value
409 */
bo yang39a98552010-09-22 22:36:29 -0400410 if (mfiStatus)
411 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800412
Yang, Bo06f579d2008-08-10 12:42:37 -0700413 /* Dummy readl to force pci flush */
414 readl(&regs->outbound_intr_status);
415
bo yang39a98552010-09-22 22:36:29 -0400416 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800417}
418
419/**
420 * megasas_fire_cmd_xscale - Sends command to the FW
421 * @frame_phys_addr : Physical address of cmd
422 * @frame_count : Number of frames for the command
423 * @regs : MFI register set
424 */
adam radford0d490162010-12-14 19:17:17 -0800425static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600426megasas_fire_cmd_xscale(struct megasas_instance *instance,
427 dma_addr_t frame_phys_addr,
428 u32 frame_count,
429 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800430{
bo yang39a98552010-09-22 22:36:29 -0400431 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500432
bo yang39a98552010-09-22 22:36:29 -0400433 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800434 writel((frame_phys_addr >> 3)|(frame_count),
435 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400436 spin_unlock_irqrestore(&instance->hba_lock, flags);
437}
438
439/**
440 * megasas_adp_reset_xscale - For controller reset
441 * @regs: MFI register set
442 */
443static int
444megasas_adp_reset_xscale(struct megasas_instance *instance,
445 struct megasas_register_set __iomem *regs)
446{
447 u32 i;
448 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500449
bo yang39a98552010-09-22 22:36:29 -0400450 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
451
452 for (i = 0; i < 3; i++)
453 msleep(1000); /* sleep for 3 secs */
454 pcidata = 0;
455 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500456 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400457 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500458 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400459 pcidata &= ~0x2;
460 pci_write_config_dword(instance->pdev,
461 MFI_1068_PCSR_OFFSET, pcidata);
462
463 for (i = 0; i < 2; i++)
464 msleep(1000); /* need to wait 2 secs again */
465
466 pcidata = 0;
467 pci_read_config_dword(instance->pdev,
468 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500469 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400470 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500471 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400472 pcidata = 0;
473 pci_write_config_dword(instance->pdev,
474 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
475 }
476 }
477 return 0;
478}
479
480/**
481 * megasas_check_reset_xscale - For controller reset check
482 * @regs: MFI register set
483 */
484static int
485megasas_check_reset_xscale(struct megasas_instance *instance,
486 struct megasas_register_set __iomem *regs)
487{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530488 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530489 (le32_to_cpu(*instance->consumer) ==
490 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400491 return 1;
bo yang39a98552010-09-22 22:36:29 -0400492 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800493}
494
495static struct megasas_instance_template megasas_instance_template_xscale = {
496
497 .fire_cmd = megasas_fire_cmd_xscale,
498 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700499 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800500 .clear_intr = megasas_clear_intr_xscale,
501 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400502 .adp_reset = megasas_adp_reset_xscale,
503 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800504 .service_isr = megasas_isr,
505 .tasklet = megasas_complete_cmd_dpc,
506 .init_adapter = megasas_init_adapter_mfi,
507 .build_and_issue_cmd = megasas_build_and_issue_cmd,
508 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800509};
510
511/**
adam radford0d490162010-12-14 19:17:17 -0800512* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800513* to xscale (deviceid : 1064R, PERC5) controllers
514*/
515
516/**
adam radford0d490162010-12-14 19:17:17 -0800517* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500518* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800519*/
520
521/**
522 * megasas_enable_intr_ppc - Enables interrupts
523 * @regs: MFI register set
524 */
525static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530526megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800527{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530528 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500529
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530530 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800531 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800532
bo yang39a98552010-09-22 22:36:29 -0400533 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800534
535 /* Dummy readl to force pci flush */
536 readl(&regs->outbound_intr_mask);
537}
538
539/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700540 * megasas_disable_intr_ppc - Disable interrupt
541 * @regs: MFI register set
542 */
543static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530544megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700545{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530546 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700547 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500548
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530549 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700550 writel(mask, &regs->outbound_intr_mask);
551 /* Dummy readl to force pci flush */
552 readl(&regs->outbound_intr_mask);
553}
554
555/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800556 * megasas_read_fw_status_reg_ppc - returns the current FW status value
557 * @regs: MFI register set
558 */
559static u32
560megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
561{
562 return readl(&(regs)->outbound_scratch_pad);
563}
564
565/**
566 * megasas_clear_interrupt_ppc - Check & clear interrupt
567 * @regs: MFI register set
568 */
adam radford0d490162010-12-14 19:17:17 -0800569static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800570megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
571{
adam radford3cc6851f92011-05-11 18:34:52 -0700572 u32 status, mfiStatus = 0;
573
Sumant Patrof9876f02006-02-03 15:34:35 -0800574 /*
575 * Check if it is our interrupt
576 */
577 status = readl(&regs->outbound_intr_status);
578
adam radford3cc6851f92011-05-11 18:34:52 -0700579 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
580 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
581
582 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
583 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800584
585 /*
586 * Clear the interrupt by writing back the same value
587 */
588 writel(status, &regs->outbound_doorbell_clear);
589
Yang, Bo06f579d2008-08-10 12:42:37 -0700590 /* Dummy readl to force pci flush */
591 readl(&regs->outbound_doorbell_clear);
592
adam radford3cc6851f92011-05-11 18:34:52 -0700593 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800594}
adam radford3cc6851f92011-05-11 18:34:52 -0700595
Sumant Patrof9876f02006-02-03 15:34:35 -0800596/**
597 * megasas_fire_cmd_ppc - Sends command to the FW
598 * @frame_phys_addr : Physical address of cmd
599 * @frame_count : Number of frames for the command
600 * @regs : MFI register set
601 */
adam radford0d490162010-12-14 19:17:17 -0800602static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600603megasas_fire_cmd_ppc(struct megasas_instance *instance,
604 dma_addr_t frame_phys_addr,
605 u32 frame_count,
606 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800607{
bo yang39a98552010-09-22 22:36:29 -0400608 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500609
bo yang39a98552010-09-22 22:36:29 -0400610 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800611 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800612 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400613 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800614}
615
bo yang39a98552010-09-22 22:36:29 -0400616/**
bo yang39a98552010-09-22 22:36:29 -0400617 * megasas_check_reset_ppc - For controller reset check
618 * @regs: MFI register set
619 */
620static int
621megasas_check_reset_ppc(struct megasas_instance *instance,
622 struct megasas_register_set __iomem *regs)
623{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530624 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700625 return 1;
626
bo yang39a98552010-09-22 22:36:29 -0400627 return 0;
628}
adam radford3cc6851f92011-05-11 18:34:52 -0700629
Sumant Patrof9876f02006-02-03 15:34:35 -0800630static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800631
Sumant Patrof9876f02006-02-03 15:34:35 -0800632 .fire_cmd = megasas_fire_cmd_ppc,
633 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700634 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800635 .clear_intr = megasas_clear_intr_ppc,
636 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700637 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400638 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800639 .service_isr = megasas_isr,
640 .tasklet = megasas_complete_cmd_dpc,
641 .init_adapter = megasas_init_adapter_mfi,
642 .build_and_issue_cmd = megasas_build_and_issue_cmd,
643 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800644};
645
646/**
Yang, Bo87911122009-10-06 14:31:54 -0600647 * megasas_enable_intr_skinny - Enables interrupts
648 * @regs: MFI register set
649 */
650static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530651megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600652{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530653 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500654
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530655 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600656 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
657
658 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
659
660 /* Dummy readl to force pci flush */
661 readl(&regs->outbound_intr_mask);
662}
663
664/**
665 * megasas_disable_intr_skinny - Disables interrupt
666 * @regs: MFI register set
667 */
668static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530669megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600670{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530671 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600672 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500673
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530674 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600675 writel(mask, &regs->outbound_intr_mask);
676 /* Dummy readl to force pci flush */
677 readl(&regs->outbound_intr_mask);
678}
679
680/**
681 * megasas_read_fw_status_reg_skinny - returns the current FW status value
682 * @regs: MFI register set
683 */
684static u32
685megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
686{
687 return readl(&(regs)->outbound_scratch_pad);
688}
689
690/**
691 * megasas_clear_interrupt_skinny - Check & clear interrupt
692 * @regs: MFI register set
693 */
694static int
695megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
696{
697 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800698 u32 mfiStatus = 0;
699
Yang, Bo87911122009-10-06 14:31:54 -0600700 /*
701 * Check if it is our interrupt
702 */
703 status = readl(&regs->outbound_intr_status);
704
705 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400706 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600707 }
708
709 /*
adam radfordebf054b2011-02-24 20:57:15 -0800710 * Check if it is our interrupt
711 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600712 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800713 MFI_STATE_FAULT) {
714 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
715 } else
716 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
717
718 /*
Yang, Bo87911122009-10-06 14:31:54 -0600719 * Clear the interrupt by writing back the same value
720 */
721 writel(status, &regs->outbound_intr_status);
722
723 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500724 * dummy read to flush PCI
725 */
Yang, Bo87911122009-10-06 14:31:54 -0600726 readl(&regs->outbound_intr_status);
727
adam radfordebf054b2011-02-24 20:57:15 -0800728 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600729}
730
731/**
732 * megasas_fire_cmd_skinny - Sends command to the FW
733 * @frame_phys_addr : Physical address of cmd
734 * @frame_count : Number of frames for the command
735 * @regs : MFI register set
736 */
737static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600738megasas_fire_cmd_skinny(struct megasas_instance *instance,
739 dma_addr_t frame_phys_addr,
740 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600741 struct megasas_register_set __iomem *regs)
742{
Yang, Bo0c79e682009-10-06 14:47:35 -0600743 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500744
bo yang39a98552010-09-22 22:36:29 -0400745 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530746 writel(upper_32_bits(frame_phys_addr),
747 &(regs)->inbound_high_queue_port);
748 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
749 &(regs)->inbound_low_queue_port);
Tomas Henzlb99dbe52016-02-01 15:12:04 +0100750 mmiowb();
bo yang39a98552010-09-22 22:36:29 -0400751 spin_unlock_irqrestore(&instance->hba_lock, flags);
752}
753
754/**
bo yang39a98552010-09-22 22:36:29 -0400755 * megasas_check_reset_skinny - For controller reset check
756 * @regs: MFI register set
757 */
758static int
759megasas_check_reset_skinny(struct megasas_instance *instance,
760 struct megasas_register_set __iomem *regs)
761{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530762 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700763 return 1;
764
bo yang39a98552010-09-22 22:36:29 -0400765 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600766}
767
768static struct megasas_instance_template megasas_instance_template_skinny = {
769
770 .fire_cmd = megasas_fire_cmd_skinny,
771 .enable_intr = megasas_enable_intr_skinny,
772 .disable_intr = megasas_disable_intr_skinny,
773 .clear_intr = megasas_clear_intr_skinny,
774 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800775 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400776 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800777 .service_isr = megasas_isr,
778 .tasklet = megasas_complete_cmd_dpc,
779 .init_adapter = megasas_init_adapter_mfi,
780 .build_and_issue_cmd = megasas_build_and_issue_cmd,
781 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600782};
783
784
785/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700786* The following functions are defined for gen2 (deviceid : 0x78 0x79)
787* controllers
788*/
789
790/**
791 * megasas_enable_intr_gen2 - Enables interrupts
792 * @regs: MFI register set
793 */
794static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530795megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700796{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530797 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500798
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530799 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700800 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
801
802 /* write ~0x00000005 (4 & 1) to the intr mask*/
803 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
804
805 /* Dummy readl to force pci flush */
806 readl(&regs->outbound_intr_mask);
807}
808
809/**
810 * megasas_disable_intr_gen2 - Disables interrupt
811 * @regs: MFI register set
812 */
813static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530814megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700815{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530816 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700817 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500818
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530819 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700820 writel(mask, &regs->outbound_intr_mask);
821 /* Dummy readl to force pci flush */
822 readl(&regs->outbound_intr_mask);
823}
824
825/**
826 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
827 * @regs: MFI register set
828 */
829static u32
830megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
831{
832 return readl(&(regs)->outbound_scratch_pad);
833}
834
835/**
836 * megasas_clear_interrupt_gen2 - Check & clear interrupt
837 * @regs: MFI register set
838 */
839static int
840megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
841{
842 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400843 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500844
Yang, Bo6610a6b2008-08-10 12:42:38 -0700845 /*
846 * Check if it is our interrupt
847 */
848 status = readl(&regs->outbound_intr_status);
849
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530850 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400851 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
852 }
853 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
854 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
855 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700856
857 /*
858 * Clear the interrupt by writing back the same value
859 */
bo yang39a98552010-09-22 22:36:29 -0400860 if (mfiStatus)
861 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700862
863 /* Dummy readl to force pci flush */
864 readl(&regs->outbound_intr_status);
865
bo yang39a98552010-09-22 22:36:29 -0400866 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700867}
868/**
869 * megasas_fire_cmd_gen2 - Sends command to the FW
870 * @frame_phys_addr : Physical address of cmd
871 * @frame_count : Number of frames for the command
872 * @regs : MFI register set
873 */
874static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600875megasas_fire_cmd_gen2(struct megasas_instance *instance,
876 dma_addr_t frame_phys_addr,
877 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700878 struct megasas_register_set __iomem *regs)
879{
bo yang39a98552010-09-22 22:36:29 -0400880 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500881
bo yang39a98552010-09-22 22:36:29 -0400882 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700883 writel((frame_phys_addr | (frame_count<<1))|1,
884 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400885 spin_unlock_irqrestore(&instance->hba_lock, flags);
886}
887
888/**
889 * megasas_adp_reset_gen2 - For controller reset
890 * @regs: MFI register set
891 */
892static int
893megasas_adp_reset_gen2(struct megasas_instance *instance,
894 struct megasas_register_set __iomem *reg_set)
895{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500896 u32 retry = 0 ;
897 u32 HostDiag;
898 u32 __iomem *seq_offset = &reg_set->seq_offset;
899 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400900
adam radfordebf054b2011-02-24 20:57:15 -0800901 if (instance->instancet == &megasas_instance_template_skinny) {
902 seq_offset = &reg_set->fusion_seq_offset;
903 hostdiag_offset = &reg_set->fusion_host_diag;
904 }
905
906 writel(0, seq_offset);
907 writel(4, seq_offset);
908 writel(0xb, seq_offset);
909 writel(2, seq_offset);
910 writel(7, seq_offset);
911 writel(0xd, seq_offset);
912
bo yang39a98552010-09-22 22:36:29 -0400913 msleep(1000);
914
adam radfordebf054b2011-02-24 20:57:15 -0800915 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400916
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500917 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400918 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800919 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500920 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400921 retry, HostDiag);
922
923 if (retry++ >= 100)
924 return 1;
925
926 }
927
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500928 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400929
adam radfordebf054b2011-02-24 20:57:15 -0800930 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400931
932 ssleep(10);
933
adam radfordebf054b2011-02-24 20:57:15 -0800934 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500935 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400936 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800937 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500938 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400939 retry, HostDiag);
940
941 if (retry++ >= 1000)
942 return 1;
943
944 }
945 return 0;
946}
947
948/**
949 * megasas_check_reset_gen2 - For controller reset check
950 * @regs: MFI register set
951 */
952static int
953megasas_check_reset_gen2(struct megasas_instance *instance,
954 struct megasas_register_set __iomem *regs)
955{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530956 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600957 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600958
bo yang39a98552010-09-22 22:36:29 -0400959 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700960}
961
962static struct megasas_instance_template megasas_instance_template_gen2 = {
963
964 .fire_cmd = megasas_fire_cmd_gen2,
965 .enable_intr = megasas_enable_intr_gen2,
966 .disable_intr = megasas_disable_intr_gen2,
967 .clear_intr = megasas_clear_intr_gen2,
968 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400969 .adp_reset = megasas_adp_reset_gen2,
970 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800971 .service_isr = megasas_isr,
972 .tasklet = megasas_complete_cmd_dpc,
973 .init_adapter = megasas_init_adapter_mfi,
974 .build_and_issue_cmd = megasas_build_and_issue_cmd,
975 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700976};
977
978/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800979* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400980* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800981*/
982
adam radford9c915a82010-12-21 13:34:31 -0800983/*
984 * Template added for TB (Fusion)
985 */
986extern struct megasas_instance_template megasas_instance_template_fusion;
987
Sumant Patrof9876f02006-02-03 15:34:35 -0800988/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989 * megasas_issue_polled - Issues a polling command
990 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800991 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400992 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530993 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400994 */
adam radford9c915a82010-12-21 13:34:31 -0800995int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
997{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998 struct megasas_header *frame_hdr = &cmd->frame->hdr;
999
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301000 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301001 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001002
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001003 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301004 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1005 __func__, __LINE__);
1006 return DCMD_NOT_FIRED;
1007 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001008
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001009 instance->instancet->issue_dcmd(instance, cmd);
1010
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301011 return wait_and_poll(instance, cmd, instance->requestorId ?
1012 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001013}
1014
1015/**
1016 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1017 * @instance: Adapter soft state
1018 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301019 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001020 *
1021 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001022 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001023 * Used to issue ioctl commands.
1024 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301025int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001026megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301027 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301029 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301030 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001031
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001032 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301033 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1034 __func__, __LINE__);
1035 return DCMD_NOT_FIRED;
1036 }
1037
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001038 instance->instancet->issue_dcmd(instance, cmd);
1039
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301040 if (timeout) {
1041 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301042 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301043 if (!ret) {
1044 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1045 __func__, __LINE__);
1046 return DCMD_TIMEOUT;
1047 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301048 } else
1049 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301050 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001051
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301052 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301053 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001054}
1055
1056/**
1057 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1058 * @instance: Adapter soft state
1059 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301060 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001061 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301062 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001063 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001064 * cmd and waits for return status.
1065 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001066 */
1067static int
1068megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301069 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001070{
1071 struct megasas_cmd *cmd;
1072 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301073 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001074
1075 cmd = megasas_get_cmd(instance);
1076
1077 if (!cmd)
1078 return -1;
1079
1080 abort_fr = &cmd->frame->abort;
1081
1082 /*
1083 * Prepare and issue the abort frame
1084 */
1085 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301086 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301087 abort_fr->flags = cpu_to_le16(0);
1088 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1089 abort_fr->abort_mfi_phys_addr_lo =
1090 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1091 abort_fr->abort_mfi_phys_addr_hi =
1092 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001093
1094 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301095 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001096
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001097 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301098 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1099 __func__, __LINE__);
1100 return DCMD_NOT_FIRED;
1101 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001102
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001103 instance->instancet->issue_dcmd(instance, cmd);
1104
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301105 if (timeout) {
1106 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301107 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301108 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301109 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1110 __func__, __LINE__);
1111 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301112 }
1113 } else
1114 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301115 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301116
bo yang39a98552010-09-22 22:36:29 -04001117 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001118
1119 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301120 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1121 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001122}
1123
1124/**
1125 * megasas_make_sgl32 - Prepares 32-bit SGL
1126 * @instance: Adapter soft state
1127 * @scp: SCSI command from the mid-layer
1128 * @mfi_sgl: SGL to be filled in
1129 *
1130 * If successful, this function returns the number of SG elements. Otherwise,
1131 * it returnes -1.
1132 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001133static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001134megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1135 union megasas_sgl *mfi_sgl)
1136{
1137 int i;
1138 int sge_count;
1139 struct scatterlist *os_sgl;
1140
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001141 sge_count = scsi_dma_map(scp);
1142 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001143
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001144 if (sge_count) {
1145 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301146 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1147 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001148 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001149 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001150 return sge_count;
1151}
1152
1153/**
1154 * megasas_make_sgl64 - Prepares 64-bit SGL
1155 * @instance: Adapter soft state
1156 * @scp: SCSI command from the mid-layer
1157 * @mfi_sgl: SGL to be filled in
1158 *
1159 * If successful, this function returns the number of SG elements. Otherwise,
1160 * it returnes -1.
1161 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001162static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001163megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1164 union megasas_sgl *mfi_sgl)
1165{
1166 int i;
1167 int sge_count;
1168 struct scatterlist *os_sgl;
1169
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001170 sge_count = scsi_dma_map(scp);
1171 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001172
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001173 if (sge_count) {
1174 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301175 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1176 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001177 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001178 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001179 return sge_count;
1180}
1181
Yang, Bof4c9a132009-10-06 14:43:28 -06001182/**
1183 * megasas_make_sgl_skinny - Prepares IEEE SGL
1184 * @instance: Adapter soft state
1185 * @scp: SCSI command from the mid-layer
1186 * @mfi_sgl: SGL to be filled in
1187 *
1188 * If successful, this function returns the number of SG elements. Otherwise,
1189 * it returnes -1.
1190 */
1191static int
1192megasas_make_sgl_skinny(struct megasas_instance *instance,
1193 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1194{
1195 int i;
1196 int sge_count;
1197 struct scatterlist *os_sgl;
1198
1199 sge_count = scsi_dma_map(scp);
1200
1201 if (sge_count) {
1202 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301203 mfi_sgl->sge_skinny[i].length =
1204 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001205 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301206 cpu_to_le64(sg_dma_address(os_sgl));
1207 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001208 }
1209 }
1210 return sge_count;
1211}
1212
Sumant Patrob1df99d2006-10-03 12:40:47 -07001213 /**
1214 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001215 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001216 * @sge_count : number of sg elements
1217 *
1218 * Returns the number of frames required for numnber of sge's (sge_count)
1219 */
1220
Yang, Bof4c9a132009-10-06 14:43:28 -06001221static u32 megasas_get_frame_count(struct megasas_instance *instance,
1222 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001223{
1224 int num_cnt;
1225 int sge_bytes;
1226 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001227 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001228
1229 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1230 sizeof(struct megasas_sge32);
1231
Yang, Bof4c9a132009-10-06 14:43:28 -06001232 if (instance->flag_ieee) {
1233 sge_sz = sizeof(struct megasas_sge_skinny);
1234 }
1235
Sumant Patrob1df99d2006-10-03 12:40:47 -07001236 /*
bo yangd532dbe2008-03-17 03:36:43 -04001237 * Main frame can contain 2 SGEs for 64-bit SGLs and
1238 * 3 SGEs for 32-bit SGLs for ldio &
1239 * 1 SGEs for 64-bit SGLs and
1240 * 2 SGEs for 32-bit SGLs for pthru frame
1241 */
1242 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001243 if (instance->flag_ieee == 1) {
1244 num_cnt = sge_count - 1;
1245 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001246 num_cnt = sge_count - 1;
1247 else
1248 num_cnt = sge_count - 2;
1249 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001250 if (instance->flag_ieee == 1) {
1251 num_cnt = sge_count - 1;
1252 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001253 num_cnt = sge_count - 2;
1254 else
1255 num_cnt = sge_count - 3;
1256 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001257
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001258 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001259 sge_bytes = sge_sz * num_cnt;
1260
1261 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1262 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1263 }
1264 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001265 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001266
1267 if (frame_count > 7)
1268 frame_count = 8;
1269 return frame_count;
1270}
1271
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001272/**
1273 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1274 * @instance: Adapter soft state
1275 * @scp: SCSI command
1276 * @cmd: Command to be prepared in
1277 *
1278 * This function prepares CDB commands. These are typcially pass-through
1279 * commands to the devices.
1280 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001281static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001282megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1283 struct megasas_cmd *cmd)
1284{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001285 u32 is_logical;
1286 u32 device_id;
1287 u16 flags = 0;
1288 struct megasas_pthru_frame *pthru;
1289
Shivasharan S3cabd162017-02-10 00:59:05 -08001290 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301291 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001292 pthru = (struct megasas_pthru_frame *)cmd->frame;
1293
1294 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1295 flags = MFI_FRAME_DIR_WRITE;
1296 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1297 flags = MFI_FRAME_DIR_READ;
1298 else if (scp->sc_data_direction == PCI_DMA_NONE)
1299 flags = MFI_FRAME_DIR_NONE;
1300
Yang, Bof4c9a132009-10-06 14:43:28 -06001301 if (instance->flag_ieee == 1) {
1302 flags |= MFI_FRAME_IEEE;
1303 }
1304
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001305 /*
1306 * Prepare the DCDB frame
1307 */
1308 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1309 pthru->cmd_status = 0x0;
1310 pthru->scsi_status = 0x0;
1311 pthru->target_id = device_id;
1312 pthru->lun = scp->device->lun;
1313 pthru->cdb_len = scp->cmd_len;
1314 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001315 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301316 pthru->flags = cpu_to_le16(flags);
1317 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001318
1319 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1320
1321 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001322 * If the command is for the tape device, set the
1323 * pthru timeout to the os layer timeout value.
1324 */
Yang, Bo8d568252009-10-06 14:12:21 -06001325 if (scp->device->type == TYPE_TAPE) {
1326 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301327 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001328 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301329 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001330 }
1331
1332 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001333 * Construct SGL
1334 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001335 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301336 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001337 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1338 &pthru->sgl);
1339 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301340 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001341 pthru->sge_count = megasas_make_sgl64(instance, scp,
1342 &pthru->sgl);
1343 } else
1344 pthru->sge_count = megasas_make_sgl32(instance, scp,
1345 &pthru->sgl);
1346
Yang, Bobdc6fb82009-12-06 08:30:19 -07001347 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001348 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001349 pthru->sge_count);
1350 return 0;
1351 }
1352
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001353 /*
1354 * Sense info specific
1355 */
1356 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301357 pthru->sense_buf_phys_addr_hi =
1358 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1359 pthru->sense_buf_phys_addr_lo =
1360 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001361
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001362 /*
1363 * Compute the total number of frames this command consumes. FW uses
1364 * this number to pull sufficient number of frames from host memory.
1365 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001366 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001367 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001368
1369 return cmd->frame_count;
1370}
1371
1372/**
1373 * megasas_build_ldio - Prepares IOs to logical devices
1374 * @instance: Adapter soft state
1375 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001376 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001377 *
1378 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1379 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001380static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001381megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1382 struct megasas_cmd *cmd)
1383{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001384 u32 device_id;
1385 u8 sc = scp->cmnd[0];
1386 u16 flags = 0;
1387 struct megasas_io_frame *ldio;
1388
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301389 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001390 ldio = (struct megasas_io_frame *)cmd->frame;
1391
1392 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1393 flags = MFI_FRAME_DIR_WRITE;
1394 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1395 flags = MFI_FRAME_DIR_READ;
1396
Yang, Bof4c9a132009-10-06 14:43:28 -06001397 if (instance->flag_ieee == 1) {
1398 flags |= MFI_FRAME_IEEE;
1399 }
1400
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001401 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001402 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001403 */
1404 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1405 ldio->cmd_status = 0x0;
1406 ldio->scsi_status = 0x0;
1407 ldio->target_id = device_id;
1408 ldio->timeout = 0;
1409 ldio->reserved_0 = 0;
1410 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301411 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001412 ldio->start_lba_hi = 0;
1413 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1414
1415 /*
1416 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1417 */
1418 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301419 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1420 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1421 ((u32) scp->cmnd[2] << 8) |
1422 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301424 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001425 }
1426
1427 /*
1428 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1429 */
1430 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301431 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1432 ((u32) scp->cmnd[7] << 8));
1433 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1434 ((u32) scp->cmnd[3] << 16) |
1435 ((u32) scp->cmnd[4] << 8) |
1436 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001437 }
1438
1439 /*
1440 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1441 */
1442 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301443 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1444 ((u32) scp->cmnd[7] << 16) |
1445 ((u32) scp->cmnd[8] << 8) |
1446 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001447
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301448 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1449 ((u32) scp->cmnd[3] << 16) |
1450 ((u32) scp->cmnd[4] << 8) |
1451 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001452 }
1453
1454 /*
1455 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1456 */
1457 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301458 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1459 ((u32) scp->cmnd[11] << 16) |
1460 ((u32) scp->cmnd[12] << 8) |
1461 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001462
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301463 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1464 ((u32) scp->cmnd[7] << 16) |
1465 ((u32) scp->cmnd[8] << 8) |
1466 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001467
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301468 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1469 ((u32) scp->cmnd[3] << 16) |
1470 ((u32) scp->cmnd[4] << 8) |
1471 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001472
1473 }
1474
1475 /*
1476 * Construct SGL
1477 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001478 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301479 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001480 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1481 &ldio->sgl);
1482 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301483 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001484 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1485 } else
1486 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1487
Yang, Bobdc6fb82009-12-06 08:30:19 -07001488 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001489 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001490 ldio->sge_count);
1491 return 0;
1492 }
1493
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001494 /*
1495 * Sense info specific
1496 */
1497 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1498 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301499 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001500
Sumant Patrob1df99d2006-10-03 12:40:47 -07001501 /*
1502 * Compute the total number of frames this command consumes. FW uses
1503 * this number to pull sufficient number of frames from host memory.
1504 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001505 cmd->frame_count = megasas_get_frame_count(instance,
1506 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001507
1508 return cmd->frame_count;
1509}
1510
1511/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301512 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1513 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001514 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001515 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001516 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301517inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001518{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301519 int ret;
1520
Sumant Patrocb59aa62006-01-25 11:53:25 -08001521 switch (cmd->cmnd[0]) {
1522 case READ_10:
1523 case WRITE_10:
1524 case READ_12:
1525 case WRITE_12:
1526 case READ_6:
1527 case WRITE_6:
1528 case READ_16:
1529 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001530 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301531 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1532 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001533 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001534 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301535 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001536 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301537 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001538}
1539
Sumant Patro658dced2006-10-03 13:09:14 -07001540 /**
1541 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001542 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001543 * @instance: Adapter soft state
1544 */
1545static inline void
1546megasas_dump_pending_frames(struct megasas_instance *instance)
1547{
1548 struct megasas_cmd *cmd;
1549 int i,n;
1550 union megasas_sgl *mfi_sgl;
1551 struct megasas_io_frame *ldio;
1552 struct megasas_pthru_frame *pthru;
1553 u32 sgcount;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001554 u16 max_cmd = instance->max_fw_cmds;
Sumant Patro658dced2006-10-03 13:09:14 -07001555
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001556 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1557 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 -07001558 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001559 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 -07001560 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001561 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 -07001562
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001563 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001564 for (i = 0; i < max_cmd; i++) {
1565 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001566 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001567 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001568 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 +05301569 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001570 ldio = (struct megasas_io_frame *)cmd->frame;
1571 mfi_sgl = &ldio->sgl;
1572 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001573 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 +05301574 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1575 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1576 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1577 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001578 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001579 pthru = (struct megasas_pthru_frame *) cmd->frame;
1580 mfi_sgl = &pthru->sgl;
1581 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001582 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 +05301583 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1584 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1585 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1586 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001587 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001588 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1589 for (n = 0; n < sgcount; n++) {
1590 if (IS_DMA64)
1591 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1592 le32_to_cpu(mfi_sgl->sge64[n].length),
1593 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1594 else
1595 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1596 le32_to_cpu(mfi_sgl->sge32[n].length),
1597 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001598 }
1599 }
Sumant Patro658dced2006-10-03 13:09:14 -07001600 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001601 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001602 for (i = 0; i < max_cmd; i++) {
1603
1604 cmd = instance->cmd_list[i];
1605
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001606 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001607 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001608 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001609 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001610}
1611
adam radfordcd50ba82010-12-21 10:23:23 -08001612u32
1613megasas_build_and_issue_cmd(struct megasas_instance *instance,
1614 struct scsi_cmnd *scmd)
1615{
1616 struct megasas_cmd *cmd;
1617 u32 frame_count;
1618
1619 cmd = megasas_get_cmd(instance);
1620 if (!cmd)
1621 return SCSI_MLQUEUE_HOST_BUSY;
1622
1623 /*
1624 * Logical drive command
1625 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301626 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001627 frame_count = megasas_build_ldio(instance, scmd, cmd);
1628 else
1629 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1630
1631 if (!frame_count)
1632 goto out_return_cmd;
1633
1634 cmd->scmd = scmd;
1635 scmd->SCp.ptr = (char *)cmd;
1636
1637 /*
1638 * Issue the command to the FW
1639 */
1640 atomic_inc(&instance->fw_outstanding);
1641
1642 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1643 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001644
1645 return 0;
1646out_return_cmd:
1647 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301648 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001649}
1650
1651
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001652/**
1653 * megasas_queue_command - Queue entry point
1654 * @scmd: SCSI command to be queued
1655 * @done: Callback entry point
1656 */
1657static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301658megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001659{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001660 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301661 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001662
1663 instance = (struct megasas_instance *)
1664 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001665
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301666 if (instance->unload == 1) {
1667 scmd->result = DID_NO_CONNECT << 16;
1668 scmd->scsi_done(scmd);
1669 return 0;
1670 }
1671
bo yang39a98552010-09-22 22:36:29 -04001672 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001673 return SCSI_MLQUEUE_HOST_BUSY;
1674
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301675
adam radford229fe472014-03-10 02:51:56 -07001676 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301677 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001678 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001679 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001680 return SCSI_MLQUEUE_HOST_BUSY;
1681 } else {
adam radford229fe472014-03-10 02:51:56 -07001682 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301683 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001684 return 0;
1685 }
1686 }
1687
Sumit Saxena8a01a412016-01-28 21:04:32 +05301688 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001689 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301690 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301691 return 0;
1692 }
1693
Sumit Saxena18365b12016-01-28 21:04:25 +05301694 mr_device_priv_data = scmd->device->hostdata;
1695 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301696 scmd->result = DID_NO_CONNECT << 16;
1697 scmd->scsi_done(scmd);
1698 return 0;
1699 }
1700
Sumit Saxena8a01a412016-01-28 21:04:32 +05301701 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001702 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001703
Sumit Saxena8a01a412016-01-28 21:04:32 +05301704 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301705 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301706
bo yang39a98552010-09-22 22:36:29 -04001707
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001708 scmd->result = 0;
1709
Shivasharan S3cabd162017-02-10 00:59:05 -08001710 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301711 (scmd->device->id >= instance->fw_supported_vd_count ||
1712 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001713 scmd->result = DID_BAD_TARGET << 16;
1714 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001715 }
1716
Shivasharan S3cabd162017-02-10 00:59:05 -08001717 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1718 MEGASAS_IS_LOGICAL(scmd->device) &&
1719 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001720 scmd->result = DID_OK << 16;
1721 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001722 }
1723
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301724 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001725
Sumant Patrocb59aa62006-01-25 11:53:25 -08001726 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301727 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001728 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001729}
1730
Yang, Bo044833b2009-10-06 14:33:06 -06001731static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1732{
1733 int i;
1734
1735 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1736
1737 if ((megasas_mgmt_info.instance[i]) &&
1738 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1739 return megasas_mgmt_info.instance[i];
1740 }
1741
1742 return NULL;
1743}
1744
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301745/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001746* megasas_set_dynamic_target_properties -
1747* Device property set by driver may not be static and it is required to be
1748* updated after OCR
1749*
1750* set tm_capable.
1751* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301752*
1753* @sdev: OS provided scsi device
1754*
1755* Returns void
1756*/
Shivasharan S15dd0382017-02-10 00:59:10 -08001757void megasas_set_dynamic_target_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301758{
Shivasharan S15dd0382017-02-10 00:59:10 -08001759 u16 pd_index = 0, ld;
1760 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301761 struct megasas_instance *instance;
1762 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301763 struct MR_PRIV_DEVICE *mr_device_priv_data;
1764 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301765 struct MR_LD_RAID *raid;
1766 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1767
1768 instance = megasas_lookup_instance(sdev->host->host_no);
1769 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301770 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301771
Shivasharan Sed981b82017-02-10 00:59:06 -08001772 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301773 return;
1774
Shivasharan Sed981b82017-02-10 00:59:06 -08001775 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301776 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1777 + sdev->id;
1778 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1779 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001780 if (ld >= instance->fw_supported_vd_count)
1781 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301782 raid = MR_LdRaidGet(ld, local_map_ptr);
1783
1784 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001785 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001786
Sumit Saxena18365b12016-01-28 21:04:25 +05301787 mr_device_priv_data->is_tm_capable =
1788 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001789 } else if (instance->use_seqnum_jbod_fp) {
1790 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001791 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001792 pd_sync = (void *)fusion->pd_seq_sync
1793 [(instance->pd_seq_map_id - 1) & 1];
1794 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001795 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301796 }
1797}
1798
Shivasharan S15dd0382017-02-10 00:59:10 -08001799/*
1800 * megasas_set_nvme_device_properties -
1801 * set nomerges=2
1802 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1803 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1804 *
1805 * MR firmware provides value in KB. Caller of this function converts
1806 * kb into bytes.
1807 *
1808 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1809 * MR firmware provides value 128 as (32 * 4K) = 128K.
1810 *
1811 * @sdev: scsi device
1812 * @max_io_size: maximum io transfer size
1813 *
1814 */
1815static inline void
1816megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301817{
Sumit Saxena2216c302016-01-28 21:04:26 +05301818 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001819 u32 mr_nvme_pg_size;
1820
1821 instance = (struct megasas_instance *)sdev->host->hostdata;
1822 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1823 MR_DEFAULT_NVME_PAGE_SIZE);
1824
1825 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1826
1827 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1828 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1829}
1830
1831
1832/*
1833 * megasas_set_static_target_properties -
1834 * Device property set by driver are static and it is not required to be
1835 * updated after OCR.
1836 *
1837 * set io timeout
1838 * set device queue depth
1839 * set nvme device properties. see - megasas_set_nvme_device_properties
1840 *
1841 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001842 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001843 */
Shivasharan S96188a82017-02-10 00:59:11 -08001844static void megasas_set_static_target_properties(struct scsi_device *sdev,
1845 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001846{
1847 u16 target_index = 0;
1848 u8 interface_type;
1849 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1850 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001851 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001852 struct megasas_instance *instance;
1853 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301854
1855 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001856 mr_device_priv_data = sdev->hostdata;
1857 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301858
Shivasharan S15dd0382017-02-10 00:59:10 -08001859 /*
1860 * The RAID firmware may require extended timeouts.
1861 */
1862 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301863
Shivasharan S15dd0382017-02-10 00:59:10 -08001864 target_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05301865
Shivasharan S15dd0382017-02-10 00:59:10 -08001866 switch (interface_type) {
1867 case SAS_PD:
1868 device_qd = MEGASAS_SAS_QD;
1869 break;
1870 case SATA_PD:
1871 device_qd = MEGASAS_SATA_QD;
1872 break;
1873 case NVME_PD:
1874 device_qd = MEGASAS_NVME_QD;
1875 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301876 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001877
Shivasharan S96188a82017-02-10 00:59:11 -08001878 if (is_target_prop) {
1879 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1880 if (tgt_device_qd &&
1881 (tgt_device_qd <= instance->host->can_queue))
1882 device_qd = tgt_device_qd;
1883
1884 /* max_io_size_kb will be set to non zero for
1885 * nvme based vd and syspd.
1886 */
1887 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1888 }
1889
Shivasharan S15dd0382017-02-10 00:59:10 -08001890 if (instance->nvme_page_size && max_io_size_kb)
1891 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1892
1893 scsi_change_queue_depth(sdev, device_qd);
1894
Sumit Saxena2216c302016-01-28 21:04:26 +05301895}
1896
Sumit Saxena18365b12016-01-28 21:04:25 +05301897
Christoph Hellwig147aab62006-02-17 12:13:48 +01001898static int megasas_slave_configure(struct scsi_device *sdev)
1899{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301900 u16 pd_index = 0;
1901 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08001902 int ret_target_prop = DCMD_FAILED;
1903 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301904
1905 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001906 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001907 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301908 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1909 sdev->id;
1910 if (instance->pd_list[pd_index].driveState !=
1911 MR_PD_STATE_SYSTEM)
1912 return -ENXIO;
1913 }
1914 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301915
Shivasharan S15dd0382017-02-10 00:59:10 -08001916 mutex_lock(&instance->hba_mutex);
1917 /* Send DCMD to Firmware and cache the information */
1918 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
1919 megasas_get_pd_info(instance, sdev);
1920
Shivasharan S96188a82017-02-10 00:59:11 -08001921 /* Some ventura firmware may not have instance->nvme_page_size set.
1922 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
1923 */
1924 if ((instance->tgt_prop) && (instance->nvme_page_size))
1925 ret_target_prop = megasas_get_target_prop(instance, sdev);
1926
1927 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
1928 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08001929
1930 mutex_unlock(&instance->hba_mutex);
1931
1932 /* This sdev property may change post OCR */
1933 megasas_set_dynamic_target_properties(sdev);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301934
Yang, Bo044833b2009-10-06 14:33:06 -06001935 return 0;
1936}
1937
1938static int megasas_slave_alloc(struct scsi_device *sdev)
1939{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001940 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001941 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301942 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001943
Yang, Bo044833b2009-10-06 14:33:06 -06001944 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08001945 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06001946 /*
1947 * Open the OS scan to the SYSTEM PD
1948 */
1949 pd_index =
1950 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1951 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08001952 if ((instance->pd_list_not_supported ||
1953 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301954 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301955 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001956 }
1957 return -ENXIO;
1958 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301959
1960scan_target:
1961 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1962 GFP_KERNEL);
1963 if (!mr_device_priv_data)
1964 return -ENOMEM;
1965 sdev->hostdata = mr_device_priv_data;
Shivasharan S49524b32017-03-10 03:22:13 -08001966
1967 atomic_set(&mr_device_priv_data->r1_ldio_hint,
1968 instance->r1_ldio_hint_default);
Christoph Hellwig147aab62006-02-17 12:13:48 +01001969 return 0;
1970}
1971
Sumit Saxena18365b12016-01-28 21:04:25 +05301972static void megasas_slave_destroy(struct scsi_device *sdev)
1973{
1974 kfree(sdev->hostdata);
1975 sdev->hostdata = NULL;
1976}
1977
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301978/*
1979* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1980* kill adapter
1981* @instance: Adapter soft state
1982*
1983*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301984static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301985{
1986 int i;
1987 struct megasas_cmd *cmd_mfi;
1988 struct megasas_cmd_fusion *cmd_fusion;
1989 struct fusion_context *fusion = instance->ctrl_context;
1990
1991 /* Find all outstanding ioctls */
1992 if (fusion) {
1993 for (i = 0; i < instance->max_fw_cmds; i++) {
1994 cmd_fusion = fusion->cmd_list[i];
1995 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1996 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1997 if (cmd_mfi->sync_cmd &&
1998 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1999 megasas_complete_cmd(instance,
2000 cmd_mfi, DID_OK);
2001 }
2002 }
2003 } else {
2004 for (i = 0; i < instance->max_fw_cmds; i++) {
2005 cmd_mfi = instance->cmd_list[i];
2006 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2007 MFI_CMD_ABORT)
2008 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2009 }
2010 }
2011}
2012
2013
adam radford9c915a82010-12-21 13:34:31 -08002014void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002015{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302016 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302017 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302018 /* Wait 1 second to ensure IO or ioctls in build have posted */
2019 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002020 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302021 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302022 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002023 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07002024 /* Flush */
2025 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302026 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002027 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002028 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302029 writel(MFI_STOP_ADP,
2030 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002031 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302032 /* Complete outstanding ioctls when adapter is killed */
2033 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002034}
2035
2036 /**
2037 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2038 * restored to max value
2039 * @instance: Adapter soft state
2040 *
2041 */
2042void
2043megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2044{
2045 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302046
adam radford9c915a82010-12-21 13:34:31 -08002047 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002048 && time_after(jiffies, instance->last_time + 5 * HZ)
2049 && atomic_read(&instance->fw_outstanding) <
2050 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002051
2052 spin_lock_irqsave(instance->host->host_lock, flags);
2053 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002054
Sumit Saxena308ec452016-01-28 21:04:30 +05302055 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002056 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002057 }
2058}
2059
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002060/**
bo yang7343eb62007-11-09 04:35:44 -05002061 * megasas_complete_cmd_dpc - Returns FW's controller structure
2062 * @instance_addr: Address of adapter soft state
2063 *
2064 * Tasklet to complete cmds
2065 */
2066static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2067{
2068 u32 producer;
2069 u32 consumer;
2070 u32 context;
2071 struct megasas_cmd *cmd;
2072 struct megasas_instance *instance =
2073 (struct megasas_instance *)instance_addr;
2074 unsigned long flags;
2075
2076 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302077 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002078 return;
2079
2080 spin_lock_irqsave(&instance->completion_lock, flags);
2081
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302082 producer = le32_to_cpu(*instance->producer);
2083 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002084
2085 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302086 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002087 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002088 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002089 context);
2090 BUG();
2091 }
bo yang7343eb62007-11-09 04:35:44 -05002092
2093 cmd = instance->cmd_list[context];
2094
2095 megasas_complete_cmd(instance, cmd, DID_OK);
2096
2097 consumer++;
2098 if (consumer == (instance->max_fw_cmds + 1)) {
2099 consumer = 0;
2100 }
2101 }
2102
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302103 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002104
2105 spin_unlock_irqrestore(&instance->completion_lock, flags);
2106
2107 /*
2108 * Check if we can restore can_queue
2109 */
adam radford9c915a82010-12-21 13:34:31 -08002110 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002111}
2112
adam radford229fe472014-03-10 02:51:56 -07002113/**
2114 * megasas_start_timer - Initializes a timer object
2115 * @instance: Adapter soft state
2116 * @timer: timer object to be initialized
2117 * @fn: timer function
2118 * @interval: time interval between timer function call
2119 *
2120 */
2121void megasas_start_timer(struct megasas_instance *instance,
2122 struct timer_list *timer,
2123 void *fn, unsigned long interval)
2124{
2125 init_timer(timer);
2126 timer->expires = jiffies + interval;
2127 timer->data = (unsigned long)instance;
2128 timer->function = fn;
2129 add_timer(timer);
2130}
2131
Yang, Bo707e09b2010-10-12 07:20:27 -06002132static void
2133megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2134
2135static void
2136process_fw_state_change_wq(struct work_struct *work);
2137
2138void megasas_do_ocr(struct megasas_instance *instance)
2139{
2140 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2141 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2142 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302143 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002144 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302145 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302146 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002147 instance->issuepend_done = 0;
2148
2149 atomic_set(&instance->fw_outstanding, 0);
2150 megasas_internal_reset_defer_cmds(instance);
2151 process_fw_state_change_wq(&instance->work_init);
2152}
2153
Adam Radford4cbfea82014-07-09 15:17:56 -07002154static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2155 int initial)
adam radford229fe472014-03-10 02:51:56 -07002156{
2157 struct megasas_cmd *cmd;
2158 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002159 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002160 dma_addr_t new_affiliation_111_h;
2161 int ld, retval = 0;
2162 u8 thisVf;
2163
2164 cmd = megasas_get_cmd(instance);
2165
2166 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002167 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2168 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002169 instance->host->host_no);
2170 return -ENOMEM;
2171 }
2172
2173 dcmd = &cmd->frame->dcmd;
2174
Adam Radford4cbfea82014-07-09 15:17:56 -07002175 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002176 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2177 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002178 megasas_return_cmd(instance, cmd);
2179 return -ENOMEM;
2180 }
2181
2182 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002183 memset(instance->vf_affiliation_111, 0,
2184 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002185 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002186 new_affiliation_111 =
2187 pci_alloc_consistent(instance->pdev,
2188 sizeof(struct MR_LD_VF_AFFILIATION_111),
2189 &new_affiliation_111_h);
2190 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002191 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2192 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002193 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002194 megasas_return_cmd(instance, cmd);
2195 return -ENOMEM;
2196 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002197 memset(new_affiliation_111, 0,
2198 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002199 }
2200
2201 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2202
2203 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302204 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002205 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302206 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002207 dcmd->timeout = 0;
2208 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302209 dcmd->data_xfer_len =
2210 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2211 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002212
Adam Radford4cbfea82014-07-09 15:17:56 -07002213 if (initial)
2214 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302215 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002216 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302217 dcmd->sgl.sge32[0].phys_addr =
2218 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002219
Christoph Hellwig2213a462015-04-23 16:33:54 +05302220 dcmd->sgl.sge32[0].length = cpu_to_le32(
2221 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002222
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002223 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002224 "scsi%d\n", instance->host->host_no);
2225
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302226 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002227 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2228 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002229 dcmd->cmd_status, instance->host->host_no);
2230 retval = 1; /* Do a scan if we couldn't get affiliation */
2231 goto out;
2232 }
2233
2234 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002235 thisVf = new_affiliation_111->thisVf;
2236 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2237 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2238 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002239 dev_warn(&instance->pdev->dev, "SR-IOV: "
2240 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002241 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002242 memcpy(instance->vf_affiliation_111,
2243 new_affiliation_111,
2244 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002245 retval = 1;
2246 goto out;
2247 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002248 }
2249out:
2250 if (new_affiliation_111) {
2251 pci_free_consistent(instance->pdev,
2252 sizeof(struct MR_LD_VF_AFFILIATION_111),
2253 new_affiliation_111,
2254 new_affiliation_111_h);
2255 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302256
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302257 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002258
2259 return retval;
2260}
2261
2262static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2263 int initial)
2264{
2265 struct megasas_cmd *cmd;
2266 struct megasas_dcmd_frame *dcmd;
2267 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2268 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2269 dma_addr_t new_affiliation_h;
2270 int i, j, retval = 0, found = 0, doscan = 0;
2271 u8 thisVf;
2272
2273 cmd = megasas_get_cmd(instance);
2274
2275 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002276 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2277 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002278 instance->host->host_no);
2279 return -ENOMEM;
2280 }
2281
2282 dcmd = &cmd->frame->dcmd;
2283
2284 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002285 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2286 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002287 megasas_return_cmd(instance, cmd);
2288 return -ENOMEM;
2289 }
2290
2291 if (initial)
2292 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2293 sizeof(struct MR_LD_VF_AFFILIATION));
2294 else {
2295 new_affiliation =
2296 pci_alloc_consistent(instance->pdev,
2297 (MAX_LOGICAL_DRIVES + 1) *
2298 sizeof(struct MR_LD_VF_AFFILIATION),
2299 &new_affiliation_h);
2300 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002301 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2302 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002303 instance->host->host_no);
2304 megasas_return_cmd(instance, cmd);
2305 return -ENOMEM;
2306 }
2307 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2308 sizeof(struct MR_LD_VF_AFFILIATION));
2309 }
2310
2311 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2312
2313 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302314 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002315 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302316 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002317 dcmd->timeout = 0;
2318 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302319 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2320 sizeof(struct MR_LD_VF_AFFILIATION));
2321 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002322
2323 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302324 dcmd->sgl.sge32[0].phys_addr =
2325 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002326 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302327 dcmd->sgl.sge32[0].phys_addr =
2328 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002329
Christoph Hellwig2213a462015-04-23 16:33:54 +05302330 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2331 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002332
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002333 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002334 "scsi%d\n", instance->host->host_no);
2335
Adam Radford4cbfea82014-07-09 15:17:56 -07002336
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302337 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002338 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2339 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002340 dcmd->cmd_status, instance->host->host_no);
2341 retval = 1; /* Do a scan if we couldn't get affiliation */
2342 goto out;
2343 }
2344
2345 if (!initial) {
2346 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002347 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2348 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002349 instance->host->host_no);
2350 retval = 1;
2351 goto out;
2352 }
2353 newmap = new_affiliation->map;
2354 savedmap = instance->vf_affiliation->map;
2355 thisVf = new_affiliation->thisVf;
2356 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2357 found = 0;
2358 for (j = 0; j < instance->vf_affiliation->ldCount;
2359 j++) {
2360 if (newmap->ref.targetId ==
2361 savedmap->ref.targetId) {
2362 found = 1;
2363 if (newmap->policy[thisVf] !=
2364 savedmap->policy[thisVf]) {
2365 doscan = 1;
2366 goto out;
2367 }
adam radford229fe472014-03-10 02:51:56 -07002368 }
2369 savedmap = (struct MR_LD_VF_MAP *)
2370 ((unsigned char *)savedmap +
2371 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002372 }
2373 if (!found && newmap->policy[thisVf] !=
2374 MR_LD_ACCESS_HIDDEN) {
2375 doscan = 1;
2376 goto out;
2377 }
2378 newmap = (struct MR_LD_VF_MAP *)
2379 ((unsigned char *)newmap + newmap->size);
2380 }
2381
2382 newmap = new_affiliation->map;
2383 savedmap = instance->vf_affiliation->map;
2384
2385 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2386 found = 0;
2387 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2388 if (savedmap->ref.targetId ==
2389 newmap->ref.targetId) {
2390 found = 1;
2391 if (savedmap->policy[thisVf] !=
2392 newmap->policy[thisVf]) {
2393 doscan = 1;
2394 goto out;
2395 }
2396 }
adam radford229fe472014-03-10 02:51:56 -07002397 newmap = (struct MR_LD_VF_MAP *)
2398 ((unsigned char *)newmap +
2399 newmap->size);
2400 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002401 if (!found && savedmap->policy[thisVf] !=
2402 MR_LD_ACCESS_HIDDEN) {
2403 doscan = 1;
2404 goto out;
2405 }
2406 savedmap = (struct MR_LD_VF_MAP *)
2407 ((unsigned char *)savedmap +
2408 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002409 }
2410 }
2411out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002412 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002413 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2414 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002415 memcpy(instance->vf_affiliation, new_affiliation,
2416 new_affiliation->size);
2417 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002418 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002419
2420 if (new_affiliation)
2421 pci_free_consistent(instance->pdev,
2422 (MAX_LOGICAL_DRIVES + 1) *
2423 sizeof(struct MR_LD_VF_AFFILIATION),
2424 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302425 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002426
2427 return retval;
2428}
2429
Adam Radford4cbfea82014-07-09 15:17:56 -07002430/* This function will get the current SR-IOV LD/VF affiliation */
2431static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2432 int initial)
2433{
2434 int retval;
2435
2436 if (instance->PlasmaFW111)
2437 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2438 else
2439 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2440 return retval;
2441}
2442
adam radford229fe472014-03-10 02:51:56 -07002443/* This function will tell FW to start the SR-IOV heartbeat */
2444int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2445 int initial)
2446{
2447 struct megasas_cmd *cmd;
2448 struct megasas_dcmd_frame *dcmd;
2449 int retval = 0;
2450
2451 cmd = megasas_get_cmd(instance);
2452
2453 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002454 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2455 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002456 instance->host->host_no);
2457 return -ENOMEM;
2458 }
2459
2460 dcmd = &cmd->frame->dcmd;
2461
2462 if (initial) {
2463 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002464 pci_zalloc_consistent(instance->pdev,
2465 sizeof(struct MR_CTRL_HB_HOST_MEM),
2466 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002467 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002468 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2469 " memory for heartbeat host memory for scsi%d\n",
2470 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002471 retval = -ENOMEM;
2472 goto out;
2473 }
adam radford229fe472014-03-10 02:51:56 -07002474 }
2475
2476 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2477
Christoph Hellwig2213a462015-04-23 16:33:54 +05302478 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002479 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302480 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002481 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302482 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002483 dcmd->timeout = 0;
2484 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302485 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2486 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2487 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2488 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002489
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002490 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002491 instance->host->host_no);
2492
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302493 if (instance->ctrl_context && !instance->mask_interrupts)
2494 retval = megasas_issue_blocked_cmd(instance, cmd,
2495 MEGASAS_ROUTINE_WAIT_TIME_VF);
2496 else
2497 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002498
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302499 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302500 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2501 "_MEM_ALLOC DCMD %s for scsi%d\n",
2502 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2503 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002504 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002505 }
2506
2507out:
2508 megasas_return_cmd(instance, cmd);
2509
2510 return retval;
2511}
2512
2513/* Handler for SR-IOV heartbeat */
2514void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2515{
2516 struct megasas_instance *instance =
2517 (struct megasas_instance *)instance_addr;
2518
2519 if (instance->hb_host_mem->HB.fwCounter !=
2520 instance->hb_host_mem->HB.driverCounter) {
2521 instance->hb_host_mem->HB.driverCounter =
2522 instance->hb_host_mem->HB.fwCounter;
2523 mod_timer(&instance->sriov_heartbeat_timer,
2524 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2525 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002526 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002527 "completed for scsi%d\n", instance->host->host_no);
2528 schedule_work(&instance->work_init);
2529 }
2530}
2531
bo yang7343eb62007-11-09 04:35:44 -05002532/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002533 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2534 * @instance: Adapter soft state
2535 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002536 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002537 * complete all its outstanding commands. Returns error if one or more IOs
2538 * are pending after this time period. It also marks the controller dead.
2539 */
2540static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2541{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302542 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002543 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002544 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002545 unsigned long flags;
2546 struct list_head clist_local;
2547 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002548 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002549
Sumit Saxenaccc75072016-01-28 21:04:33 +05302550 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2551 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2552 __func__, __LINE__);
2553 return FAILED;
2554 }
bo yang39a98552010-09-22 22:36:29 -04002555
Sumit Saxena8a01a412016-01-28 21:04:32 +05302556 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002557
2558 INIT_LIST_HEAD(&clist_local);
2559 spin_lock_irqsave(&instance->hba_lock, flags);
2560 list_splice_init(&instance->internal_reset_pending_q,
2561 &clist_local);
2562 spin_unlock_irqrestore(&instance->hba_lock, flags);
2563
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002564 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002565 for (i = 0; i < wait_time; i++) {
2566 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302567 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002568 break;
2569 }
2570
Sumit Saxena8a01a412016-01-28 21:04:32 +05302571 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002572 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302573 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002574 return FAILED;
2575 }
2576
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002577 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002578 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002579 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002580 struct megasas_cmd, list);
2581 list_del_init(&reset_cmd->list);
2582 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002583 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002584 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002585 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002586 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002587
2588 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2589 megasas_return_cmd(instance, reset_cmd);
2590 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002591 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002592 "reset queue\n",
2593 reset_cmd);
2594
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302595 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002596 instance->instancet->fire_cmd(instance,
2597 reset_cmd->frame_phys_addr,
2598 0, instance->reg_set);
2599 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002600 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002601 "cmds lst\n",
2602 reset_cmd);
2603 }
2604 reset_index++;
2605 }
2606
2607 return SUCCESS;
2608 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002609
adam radfordc007b8b2012-07-17 18:20:24 -07002610 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302611 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002612
2613 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002614 break;
2615
2616 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002617 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002618 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002619 /*
2620 * Call cmd completion routine. Cmd to be
2621 * be completed directly without depending on isr.
2622 */
2623 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002624 }
2625
2626 msleep(1000);
2627 }
2628
Yang, Bo707e09b2010-10-12 07:20:27 -06002629 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302630 outstanding = atomic_read(&instance->fw_outstanding);
2631 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002632
Sumit Saxenaccc75072016-01-28 21:04:33 +05302633 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2634 goto no_outstanding;
2635
2636 if (instance->disableOnlineCtrlReset)
2637 goto kill_hba_and_failed;
2638 do {
2639 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2640 dev_info(&instance->pdev->dev,
2641 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2642 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2643 if (i == 3)
2644 goto kill_hba_and_failed;
2645 megasas_do_ocr(instance);
2646
2647 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2648 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2649 __func__, __LINE__);
2650 return FAILED;
2651 }
2652 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2653 __func__, __LINE__);
2654
2655 for (sl = 0; sl < 10; sl++)
2656 msleep(500);
2657
2658 outstanding = atomic_read(&instance->fw_outstanding);
2659
2660 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2661 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2662 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002663 }
2664 i++;
2665 } while (i <= 3);
2666
Sumit Saxenaccc75072016-01-28 21:04:33 +05302667no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002668
Sumit Saxenaccc75072016-01-28 21:04:33 +05302669 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2670 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002671 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302672
2673kill_hba_and_failed:
2674
2675 /* Reset not supported, kill adapter */
2676 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2677 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2678 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2679 atomic_read(&instance->fw_outstanding));
2680 megasas_dump_pending_frames(instance);
2681 megaraid_sas_kill_hba(instance);
2682
2683 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002684}
2685
2686/**
2687 * megasas_generic_reset - Generic reset routine
2688 * @scmd: Mid-layer SCSI command
2689 *
2690 * This routine implements a generic reset handler for device, bus and host
2691 * reset requests. Device, bus and host specific reset handlers can use this
2692 * function after they do their specific tasks.
2693 */
2694static int megasas_generic_reset(struct scsi_cmnd *scmd)
2695{
2696 int ret_val;
2697 struct megasas_instance *instance;
2698
2699 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2700
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002701 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2702 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002703
Sumit Saxena8a01a412016-01-28 21:04:32 +05302704 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002705 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002706 return FAILED;
2707 }
2708
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002709 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002710 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002711 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002712 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002713 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002714
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002715 return ret_val;
2716}
2717
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002718/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002719 * megasas_reset_timer - quiesce the adapter if required
2720 * @scmd: scsi cmnd
2721 *
2722 * Sets the FW busy flag and reduces the host->can_queue if the
2723 * cmd has not been completed within the timeout period.
2724 */
2725static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002726blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002727{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002728 struct megasas_instance *instance;
2729 unsigned long flags;
2730
2731 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302732 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002733 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002734 }
2735
adam radfordf575c5d2011-10-13 16:01:12 -07002736 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002737 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2738 /* FW is busy, throttle IO */
2739 spin_lock_irqsave(instance->host->host_lock, flags);
2740
adam radfordc5daa6a2012-07-17 18:20:03 -07002741 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002742 instance->last_time = jiffies;
2743 instance->flag |= MEGASAS_FW_BUSY;
2744
2745 spin_unlock_irqrestore(instance->host->host_lock, flags);
2746 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002747 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002748}
2749
2750/**
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002751 * megasas_dump_frame - This function will dump MPT/MFI frame
2752 */
2753static inline void
2754megasas_dump_frame(void *mpi_request, int sz)
2755{
2756 int i;
2757 __le32 *mfp = (__le32 *)mpi_request;
2758
2759 printk(KERN_INFO "IO request frame:\n\t");
Dan Carpenter40a4c2c2017-02-14 19:38:55 +03002760 for (i = 0; i < sz / sizeof(__le32); i++) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002761 if (i && ((i % 8) == 0))
2762 printk("\n\t");
2763 printk("%08x ", le32_to_cpu(mfp[i]));
2764 }
2765 printk("\n");
2766}
2767
2768/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002769 * megasas_reset_bus_host - Bus & host reset handler entry point
2770 */
2771static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2772{
2773 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002774 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002775
adam radford9c915a82010-12-21 13:34:31 -08002776 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002777
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002778 scmd_printk(KERN_INFO, scmd,
2779 "Controller reset is requested due to IO timeout\n"
2780 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2781 " SCSI host busy: %d\t FW outstanding: %d\n",
2782 scmd, scmd->device->host->shost_state,
2783 atomic_read((atomic_t *)&scmd->device->host->host_busy),
2784 atomic_read(&instance->fw_outstanding));
2785
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002786 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002787 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002788 */
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002789 if (instance->ctrl_context) {
2790 struct megasas_cmd_fusion *cmd;
2791 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2792 if (cmd)
2793 megasas_dump_frame(cmd->io_request,
2794 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
2795 ret = megasas_reset_fusion(scmd->device->host,
2796 SCSIIO_TIMEOUT_OCR);
2797 } else
adam radford9c915a82010-12-21 13:34:31 -08002798 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799
2800 return ret;
2801}
2802
2803/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002804 * megasas_task_abort - Issues task abort request to firmware
2805 * (supported only for fusion adapters)
2806 * @scmd: SCSI command pointer
2807 */
2808static int megasas_task_abort(struct scsi_cmnd *scmd)
2809{
2810 int ret;
2811 struct megasas_instance *instance;
2812
2813 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2814
2815 if (instance->ctrl_context)
2816 ret = megasas_task_abort_fusion(scmd);
2817 else {
2818 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2819 ret = FAILED;
2820 }
2821
2822 return ret;
2823}
2824
2825/**
2826 * megasas_reset_target: Issues target reset request to firmware
2827 * (supported only for fusion adapters)
2828 * @scmd: SCSI command pointer
2829 */
2830static int megasas_reset_target(struct scsi_cmnd *scmd)
2831{
2832 int ret;
2833 struct megasas_instance *instance;
2834
2835 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2836
2837 if (instance->ctrl_context)
2838 ret = megasas_reset_target_fusion(scmd);
2839 else {
2840 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2841 ret = FAILED;
2842 }
2843
2844 return ret;
2845}
2846
2847/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002848 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002849 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002850 * @bdev: block device
2851 * @capacity: drive capacity
2852 * @geom: geometry parameters
2853 */
2854static int
2855megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2856 sector_t capacity, int geom[])
2857{
2858 int heads;
2859 int sectors;
2860 sector_t cylinders;
2861 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002862
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002863 /* Default heads (64) & sectors (32) */
2864 heads = 64;
2865 sectors = 32;
2866
2867 tmp = heads * sectors;
2868 cylinders = capacity;
2869
2870 sector_div(cylinders, tmp);
2871
2872 /*
2873 * Handle extended translation size for logical drives > 1Gb
2874 */
2875
2876 if (capacity >= 0x200000) {
2877 heads = 255;
2878 sectors = 63;
2879 tmp = heads*sectors;
2880 cylinders = capacity;
2881 sector_div(cylinders, tmp);
2882 }
2883
2884 geom[0] = heads;
2885 geom[1] = sectors;
2886 geom[2] = cylinders;
2887
2888 return 0;
2889}
2890
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002891static void megasas_aen_polling(struct work_struct *work);
2892
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002893/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002894 * megasas_service_aen - Processes an event notification
2895 * @instance: Adapter soft state
2896 * @cmd: AEN command completed by the ISR
2897 *
2898 * For AEN, driver sends a command down to FW that is held by the FW till an
2899 * event occurs. When an event of interest occurs, FW completes the command
2900 * that it was previously holding.
2901 *
2902 * This routines sends SIGIO signal to processes that have registered with the
2903 * driver for AEN.
2904 */
2905static void
2906megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2907{
Yang, Boc3518832009-10-06 14:18:02 -06002908 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002909
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002910 /*
2911 * Don't signal app if it is just an aborted previously registered aen
2912 */
Yang, Boc3518832009-10-06 14:18:02 -06002913 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2914 spin_lock_irqsave(&poll_aen_lock, flags);
2915 megasas_poll_wait_aen = 1;
2916 spin_unlock_irqrestore(&poll_aen_lock, flags);
2917 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002918 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002919 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002920 else
2921 cmd->abort_aen = 0;
2922
2923 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302924
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302925 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002926
bo yang39a98552010-09-22 22:36:29 -04002927 if ((instance->unload == 0) &&
2928 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002929 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002930
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002931 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2932 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002933 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002934 } else {
2935 ev->instance = instance;
2936 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002937 INIT_DELAYED_WORK(&ev->hotplug_work,
2938 megasas_aen_polling);
2939 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002940 }
2941 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002942}
2943
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302944static ssize_t
2945megasas_fw_crash_buffer_store(struct device *cdev,
2946 struct device_attribute *attr, const char *buf, size_t count)
2947{
2948 struct Scsi_Host *shost = class_to_shost(cdev);
2949 struct megasas_instance *instance =
2950 (struct megasas_instance *) shost->hostdata;
2951 int val = 0;
2952 unsigned long flags;
2953
2954 if (kstrtoint(buf, 0, &val) != 0)
2955 return -EINVAL;
2956
2957 spin_lock_irqsave(&instance->crashdump_lock, flags);
2958 instance->fw_crash_buffer_offset = val;
2959 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2960 return strlen(buf);
2961}
2962
2963static ssize_t
2964megasas_fw_crash_buffer_show(struct device *cdev,
2965 struct device_attribute *attr, char *buf)
2966{
2967 struct Scsi_Host *shost = class_to_shost(cdev);
2968 struct megasas_instance *instance =
2969 (struct megasas_instance *) shost->hostdata;
2970 u32 size;
2971 unsigned long buff_addr;
2972 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2973 unsigned long src_addr;
2974 unsigned long flags;
2975 u32 buff_offset;
2976
2977 spin_lock_irqsave(&instance->crashdump_lock, flags);
2978 buff_offset = instance->fw_crash_buffer_offset;
2979 if (!instance->crash_dump_buf &&
2980 !((instance->fw_crash_state == AVAILABLE) ||
2981 (instance->fw_crash_state == COPYING))) {
2982 dev_err(&instance->pdev->dev,
2983 "Firmware crash dump is not available\n");
2984 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2985 return -EINVAL;
2986 }
2987
2988 buff_addr = (unsigned long) buf;
2989
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002990 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302991 dev_err(&instance->pdev->dev,
2992 "Firmware crash dump offset is out of range\n");
2993 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2994 return 0;
2995 }
2996
2997 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2998 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2999
3000 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3001 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003002 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303003 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3004
3005 return size;
3006}
3007
3008static ssize_t
3009megasas_fw_crash_buffer_size_show(struct device *cdev,
3010 struct device_attribute *attr, char *buf)
3011{
3012 struct Scsi_Host *shost = class_to_shost(cdev);
3013 struct megasas_instance *instance =
3014 (struct megasas_instance *) shost->hostdata;
3015
3016 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3017 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3018}
3019
3020static ssize_t
3021megasas_fw_crash_state_store(struct device *cdev,
3022 struct device_attribute *attr, const char *buf, size_t count)
3023{
3024 struct Scsi_Host *shost = class_to_shost(cdev);
3025 struct megasas_instance *instance =
3026 (struct megasas_instance *) shost->hostdata;
3027 int val = 0;
3028 unsigned long flags;
3029
3030 if (kstrtoint(buf, 0, &val) != 0)
3031 return -EINVAL;
3032
3033 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3034 dev_err(&instance->pdev->dev, "application updates invalid "
3035 "firmware crash state\n");
3036 return -EINVAL;
3037 }
3038
3039 instance->fw_crash_state = val;
3040
3041 if ((val == COPIED) || (val == COPY_ERROR)) {
3042 spin_lock_irqsave(&instance->crashdump_lock, flags);
3043 megasas_free_host_crash_buffer(instance);
3044 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3045 if (val == COPY_ERROR)
3046 dev_info(&instance->pdev->dev, "application failed to "
3047 "copy Firmware crash dump\n");
3048 else
3049 dev_info(&instance->pdev->dev, "Firmware crash dump "
3050 "copied successfully\n");
3051 }
3052 return strlen(buf);
3053}
3054
3055static ssize_t
3056megasas_fw_crash_state_show(struct device *cdev,
3057 struct device_attribute *attr, char *buf)
3058{
3059 struct Scsi_Host *shost = class_to_shost(cdev);
3060 struct megasas_instance *instance =
3061 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003062
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303063 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3064}
3065
3066static ssize_t
3067megasas_page_size_show(struct device *cdev,
3068 struct device_attribute *attr, char *buf)
3069{
3070 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3071}
3072
Sumit Saxena308ec452016-01-28 21:04:30 +05303073static ssize_t
3074megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3075 char *buf)
3076{
3077 struct Scsi_Host *shost = class_to_shost(cdev);
3078 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3079
3080 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3081}
3082
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303083static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3084 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3085static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3086 megasas_fw_crash_buffer_size_show, NULL);
3087static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3088 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3089static DEVICE_ATTR(page_size, S_IRUGO,
3090 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05303091static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3092 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303093
3094struct device_attribute *megaraid_host_attrs[] = {
3095 &dev_attr_fw_crash_buffer_size,
3096 &dev_attr_fw_crash_buffer,
3097 &dev_attr_fw_crash_state,
3098 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303099 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303100 NULL,
3101};
3102
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003103/*
3104 * Scsi host template for megaraid_sas driver
3105 */
3106static struct scsi_host_template megasas_template = {
3107
3108 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303109 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003110 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003111 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003112 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303113 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003114 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003115 .eh_target_reset_handler = megasas_reset_target,
3116 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003117 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003118 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303119 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003120 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003121 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003122 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003123 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003124};
3125
3126/**
3127 * megasas_complete_int_cmd - Completes an internal command
3128 * @instance: Adapter soft state
3129 * @cmd: Command to be completed
3130 *
3131 * The megasas_issue_blocked_cmd() function waits for a command to complete
3132 * after it issues a command. This function wakes up that waiting routine by
3133 * calling wake_up() on the wait queue.
3134 */
3135static void
3136megasas_complete_int_cmd(struct megasas_instance *instance,
3137 struct megasas_cmd *cmd)
3138{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303139 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003140 wake_up(&instance->int_cmd_wait_q);
3141}
3142
3143/**
3144 * megasas_complete_abort - Completes aborting a command
3145 * @instance: Adapter soft state
3146 * @cmd: Cmd that was issued to abort another cmd
3147 *
adam radford0d490162010-12-14 19:17:17 -08003148 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3149 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003150 * wakes up all functions waiting on the same wait queue.
3151 */
3152static void
3153megasas_complete_abort(struct megasas_instance *instance,
3154 struct megasas_cmd *cmd)
3155{
3156 if (cmd->sync_cmd) {
3157 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303158 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003159 wake_up(&instance->abort_cmd_wait_q);
3160 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003161}
3162
3163/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003164 * megasas_complete_cmd - Completes a command
3165 * @instance: Adapter soft state
3166 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003167 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003168 * SCSI mid-layer instead of the value returned
3169 * by the FW. This should be used if caller wants
3170 * an alternate status (as in the case of aborted
3171 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003172 */
adam radford9c915a82010-12-21 13:34:31 -08003173void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003174megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3175 u8 alt_status)
3176{
3177 int exception = 0;
3178 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003179 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003180 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303181 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003182
bo yang39a98552010-09-22 22:36:29 -04003183 /* flag for the retry reset */
3184 cmd->retry_for_fw_reset = 0;
3185
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003186 if (cmd->scmd)
3187 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003188
3189 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003190 case MFI_CMD_INVALID:
3191 /* Some older 1068 controller FW may keep a pended
3192 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3193 when booting the kdump kernel. Ignore this command to
3194 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003195 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3196 "completed\n");
3197 dev_warn(&instance->pdev->dev, "If you have a controller "
3198 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003199 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003200 case MFI_CMD_PD_SCSI_IO:
3201 case MFI_CMD_LD_SCSI_IO:
3202
3203 /*
3204 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3205 * issued either through an IO path or an IOCTL path. If it
3206 * was via IOCTL, we will send it to internal completion.
3207 */
3208 if (cmd->sync_cmd) {
3209 cmd->sync_cmd = 0;
3210 megasas_complete_int_cmd(instance, cmd);
3211 break;
3212 }
3213
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003214 case MFI_CMD_LD_READ:
3215 case MFI_CMD_LD_WRITE:
3216
3217 if (alt_status) {
3218 cmd->scmd->result = alt_status << 16;
3219 exception = 1;
3220 }
3221
3222 if (exception) {
3223
Sumant Patroe4a082c2006-05-30 12:03:37 -07003224 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003225
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003226 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003227 cmd->scmd->scsi_done(cmd->scmd);
3228 megasas_return_cmd(instance, cmd);
3229
3230 break;
3231 }
3232
3233 switch (hdr->cmd_status) {
3234
3235 case MFI_STAT_OK:
3236 cmd->scmd->result = DID_OK << 16;
3237 break;
3238
3239 case MFI_STAT_SCSI_IO_FAILED:
3240 case MFI_STAT_LD_INIT_IN_PROGRESS:
3241 cmd->scmd->result =
3242 (DID_ERROR << 16) | hdr->scsi_status;
3243 break;
3244
3245 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3246
3247 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3248
3249 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3250 memset(cmd->scmd->sense_buffer, 0,
3251 SCSI_SENSE_BUFFERSIZE);
3252 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3253 hdr->sense_len);
3254
3255 cmd->scmd->result |= DRIVER_SENSE << 24;
3256 }
3257
3258 break;
3259
3260 case MFI_STAT_LD_OFFLINE:
3261 case MFI_STAT_DEVICE_NOT_FOUND:
3262 cmd->scmd->result = DID_BAD_TARGET << 16;
3263 break;
3264
3265 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003266 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003267 hdr->cmd_status);
3268 cmd->scmd->result = DID_ERROR << 16;
3269 break;
3270 }
3271
Sumant Patroe4a082c2006-05-30 12:03:37 -07003272 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003273
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003274 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003275 cmd->scmd->scsi_done(cmd->scmd);
3276 megasas_return_cmd(instance, cmd);
3277
3278 break;
3279
3280 case MFI_CMD_SMP:
3281 case MFI_CMD_STP:
3282 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303283 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003284 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303285 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3286 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303287 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003288 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303289 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003290 if (cmd->frame->hdr.cmd_status != 0) {
3291 if (cmd->frame->hdr.cmd_status !=
3292 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003293 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003294 cmd->frame->hdr.cmd_status);
3295 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303296 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003297 spin_unlock_irqrestore(
3298 instance->host->host_lock,
3299 flags);
3300 break;
3301 }
3302 } else
3303 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303304 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303305
3306 /*
3307 * Set fast path IO to ZERO.
3308 * Validate Map will set proper value.
3309 * Meanwhile all IOs will go as LD IO.
3310 */
3311 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003312 fusion->fast_path_io = 1;
3313 else
3314 fusion->fast_path_io = 0;
3315 megasas_sync_map_info(instance);
3316 spin_unlock_irqrestore(instance->host->host_lock,
3317 flags);
3318 break;
3319 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303320 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3321 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003322 spin_lock_irqsave(&poll_aen_lock, flags);
3323 megasas_poll_wait_aen = 0;
3324 spin_unlock_irqrestore(&poll_aen_lock, flags);
3325 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003326
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303327 /* FW has an updated PD sequence */
3328 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3329 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3330
3331 spin_lock_irqsave(instance->host->host_lock, flags);
3332 status = cmd->frame->hdr.cmd_status;
3333 instance->jbod_seq_cmd = NULL;
3334 megasas_return_cmd(instance, cmd);
3335
3336 if (status == MFI_STAT_OK) {
3337 instance->pd_seq_map_id++;
3338 /* Re-register a pd sync seq num cmd */
3339 if (megasas_sync_pd_seq_num(instance, true))
3340 instance->use_seqnum_jbod_fp = false;
3341 } else
3342 instance->use_seqnum_jbod_fp = false;
3343
3344 spin_unlock_irqrestore(instance->host->host_lock, flags);
3345 break;
3346 }
3347
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003348 /*
3349 * See if got an event notification
3350 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303351 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003352 megasas_service_aen(instance, cmd);
3353 else
3354 megasas_complete_int_cmd(instance, cmd);
3355
3356 break;
3357
3358 case MFI_CMD_ABORT:
3359 /*
3360 * Cmd issued to abort another cmd returned
3361 */
3362 megasas_complete_abort(instance, cmd);
3363 break;
3364
3365 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003366 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003367 hdr->cmd);
3368 break;
3369 }
3370}
3371
3372/**
bo yang39a98552010-09-22 22:36:29 -04003373 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003374 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003375 * @instance: Adapter soft state
3376 */
3377static inline void
3378megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3379{
3380 struct megasas_cmd *cmd;
3381 struct list_head clist_local;
3382 union megasas_evt_class_locale class_locale;
3383 unsigned long flags;
3384 u32 seq_num;
3385
3386 INIT_LIST_HEAD(&clist_local);
3387 spin_lock_irqsave(&instance->hba_lock, flags);
3388 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3389 spin_unlock_irqrestore(&instance->hba_lock, flags);
3390
3391 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003392 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003393 struct megasas_cmd, list);
3394 list_del_init(&cmd->list);
3395
3396 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003397 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3398 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003399 cmd, cmd->scmd, cmd->sync_cmd);
3400
3401 cmd->retry_for_fw_reset++;
3402
3403 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003404 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003405 "was tried multiple times during reset."
3406 "Shutting down the HBA\n",
3407 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303408 instance->instancet->disable_intr(instance);
3409 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003410 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003411 return;
3412 }
3413 }
3414
3415 if (cmd->sync_cmd == 1) {
3416 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003417 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003418 "cmd attached to internal command!\n");
3419 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003420 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003421 "on the internal reset queue,"
3422 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303423 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003424 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003425 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003426 0, instance->reg_set);
3427 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003428 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003429 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003430 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003431
3432 atomic_inc(&instance->fw_outstanding);
3433 instance->instancet->fire_cmd(instance,
3434 cmd->frame_phys_addr,
3435 cmd->frame_count-1, instance->reg_set);
3436 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003437 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003438 "internal reset defer list while re-issue!!\n",
3439 cmd);
3440 }
3441 }
3442
3443 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003444 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003445 megasas_return_cmd(instance, instance->aen_cmd);
3446
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003447 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003448 }
3449
3450 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003451 * Initiate AEN (Asynchronous Event Notification)
3452 */
bo yang39a98552010-09-22 22:36:29 -04003453 seq_num = instance->last_seq_num;
3454 class_locale.members.reserved = 0;
3455 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3456 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3457
3458 megasas_register_aen(instance, seq_num, class_locale.word);
3459}
3460
3461/**
3462 * Move the internal reset pending commands to a deferred queue.
3463 *
3464 * We move the commands pending at internal reset time to a
3465 * pending queue. This queue would be flushed after successful
3466 * completion of the internal reset sequence. if the internal reset
3467 * did not complete in time, the kernel reset handler would flush
3468 * these commands.
3469 **/
3470static void
3471megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3472{
3473 struct megasas_cmd *cmd;
3474 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003475 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003476 u32 defer_index;
3477 unsigned long flags;
3478
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003479 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303480 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003481 for (i = 0; i < max_cmd; i++) {
3482 cmd = instance->cmd_list[i];
3483 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003484 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003485 "on the defer queue as internal\n",
3486 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3487
3488 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003489 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003490 " moving this cmd:%p, %d %p, it was"
3491 "discovered on some list?\n",
3492 cmd, cmd->sync_cmd, cmd->scmd);
3493
3494 list_del_init(&cmd->list);
3495 }
3496 defer_index++;
3497 list_add_tail(&cmd->list,
3498 &instance->internal_reset_pending_q);
3499 }
3500 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303501 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003502}
3503
3504
3505static void
3506process_fw_state_change_wq(struct work_struct *work)
3507{
3508 struct megasas_instance *instance =
3509 container_of(work, struct megasas_instance, work_init);
3510 u32 wait;
3511 unsigned long flags;
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, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303515 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003516 return ;
3517 }
3518
Sumit Saxena8a01a412016-01-28 21:04:32 +05303519 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003520 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003521 "state, restarting it...\n");
3522
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303523 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003524 atomic_set(&instance->fw_outstanding, 0);
3525
3526 atomic_set(&instance->fw_reset_no_pci_access, 1);
3527 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003528 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003529
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003530 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003531 "initiating next stage...\n");
3532
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003533 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003534 "state 2 starting...\n");
3535
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003536 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003537 for (wait = 0; wait < 30; wait++) {
3538 msleep(1000);
3539 }
3540
adam radford058a8fa2011-10-08 18:14:27 -07003541 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003542 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003543
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303544 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003545 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003546 return ;
3547 }
3548
3549 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3550 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3551 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3552 ) {
3553 *instance->consumer = *instance->producer;
3554 } else {
3555 *instance->consumer = 0;
3556 *instance->producer = 0;
3557 }
3558
3559 megasas_issue_init_mfi(instance);
3560
3561 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303562 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003563 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303564 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003565
3566 megasas_issue_pending_cmds_again(instance);
3567 instance->issuepend_done = 1;
3568 }
bo yang39a98552010-09-22 22:36:29 -04003569}
3570
3571/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003572 * megasas_deplete_reply_queue - Processes all completed commands
3573 * @instance: Adapter soft state
3574 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003575 * SCSI mid-layer instead of the status
3576 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003577 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003578 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003579static int
bo yang39a98552010-09-22 22:36:29 -04003580megasas_deplete_reply_queue(struct megasas_instance *instance,
3581 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003582{
bo yang39a98552010-09-22 22:36:29 -04003583 u32 mfiStatus;
3584 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003585
bo yang39a98552010-09-22 22:36:29 -04003586 if ((mfiStatus = instance->instancet->check_reset(instance,
3587 instance->reg_set)) == 1) {
3588 return IRQ_HANDLED;
3589 }
3590
3591 if ((mfiStatus = instance->instancet->clear_intr(
3592 instance->reg_set)
3593 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003594 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003595 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003596 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003597 }
3598
3599 instance->mfiStatus = mfiStatus;
3600
3601 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3602 fw_state = instance->instancet->read_fw_status_reg(
3603 instance->reg_set) & MFI_STATE_MASK;
3604
3605 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003606 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003607 fw_state);
3608 }
3609
3610 if ((fw_state == MFI_STATE_FAULT) &&
3611 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003612 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003613
3614 if ((instance->pdev->device ==
3615 PCI_DEVICE_ID_LSI_SAS1064R) ||
3616 (instance->pdev->device ==
3617 PCI_DEVICE_ID_DELL_PERC5) ||
3618 (instance->pdev->device ==
3619 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3620
3621 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303622 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003623 }
3624
3625
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303626 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303627 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003628 instance->issuepend_done = 0;
3629
3630 atomic_set(&instance->fw_outstanding, 0);
3631 megasas_internal_reset_defer_cmds(instance);
3632
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003633 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303634 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003635
3636 schedule_work(&instance->work_init);
3637 return IRQ_HANDLED;
3638
3639 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003640 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003641 fw_state, instance->disableOnlineCtrlReset);
3642 }
3643 }
3644
Sumant Patro5d018ad2006-10-03 13:13:18 -07003645 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003646 return IRQ_HANDLED;
3647}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003648/**
3649 * megasas_isr - isr entry point
3650 */
David Howells7d12e782006-10-05 14:55:46 +01003651static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003652{
adam radfordc8e858f2011-10-08 18:15:13 -07003653 struct megasas_irq_context *irq_context = devp;
3654 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003655 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003656 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003657
adam radfordc8e858f2011-10-08 18:15:13 -07003658 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003659 return IRQ_HANDLED;
3660
bo yang39a98552010-09-22 22:36:29 -04003661 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003662 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003663 spin_unlock_irqrestore(&instance->hba_lock, flags);
3664
3665 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003666}
3667
3668/**
3669 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003670 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003671 *
3672 * During the initialization, FW passes can potentially be in any one of
3673 * several possible states. If the FW in operational, waiting-for-handshake
3674 * states, driver must take steps to bring it to ready state. Otherwise, it
3675 * has to wait for the ready state.
3676 */
adam radford9c915a82010-12-21 13:34:31 -08003677int
adam radford058a8fa2011-10-08 18:14:27 -07003678megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003679{
3680 int i;
3681 u8 max_wait;
3682 u32 fw_state;
3683 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003684 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003685
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003686 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3687 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003688
Sumant Patroe3bbff92006-10-03 12:28:49 -07003689 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003690 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003691 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003692
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003693 while (fw_state != MFI_STATE_READY) {
3694
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003695 switch (fw_state) {
3696
3697 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003698 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003699 if (ocr) {
3700 max_wait = MEGASAS_RESET_WAIT_TIME;
3701 cur_state = MFI_STATE_FAULT;
3702 break;
3703 } else
3704 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003705
3706 case MFI_STATE_WAIT_HANDSHAKE:
3707 /*
3708 * Set the CLR bit in inbound doorbell
3709 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003710 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003711 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3712 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003713 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303714 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003715 writel(
3716 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003717 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303718 else
Yang, Bo87911122009-10-06 14:31:54 -06003719 writel(
3720 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3721 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003722
Yang, Bo7218df62009-10-06 14:52:20 -06003723 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003724 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3725 break;
3726
Sumant Patroe3bbff92006-10-03 12:28:49 -07003727 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003728 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003729 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3730 (instance->pdev->device ==
3731 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303732 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003733 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003734 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303735 else
Yang, Bo87911122009-10-06 14:31:54 -06003736 writel(MFI_INIT_HOTPLUG,
3737 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003738
Yang, Bo7218df62009-10-06 14:52:20 -06003739 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003740 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3741 break;
3742
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003743 case MFI_STATE_OPERATIONAL:
3744 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003745 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003746 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303747 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003748 if ((instance->pdev->device ==
3749 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3750 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003751 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303752 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003753 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003754 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303755
3756 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003757 for (i = 0; i < (10 * 1000); i += 20) {
3758 if (readl(
3759 &instance->
3760 reg_set->
3761 doorbell) & 1)
3762 msleep(20);
3763 else
3764 break;
3765 }
3766 }
Yang, Bo87911122009-10-06 14:31:54 -06003767 } else
3768 writel(MFI_RESET_FLAGS,
3769 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003770
Yang, Bo7218df62009-10-06 14:52:20 -06003771 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003772 cur_state = MFI_STATE_OPERATIONAL;
3773 break;
3774
3775 case MFI_STATE_UNDEFINED:
3776 /*
3777 * This state should not last for more than 2 seconds
3778 */
Yang, Bo7218df62009-10-06 14:52:20 -06003779 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003780 cur_state = MFI_STATE_UNDEFINED;
3781 break;
3782
3783 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003784 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003785 cur_state = MFI_STATE_BB_INIT;
3786 break;
3787
3788 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003789 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003790 cur_state = MFI_STATE_FW_INIT;
3791 break;
3792
3793 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003794 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003795 cur_state = MFI_STATE_FW_INIT_2;
3796 break;
3797
3798 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003799 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003800 cur_state = MFI_STATE_DEVICE_SCAN;
3801 break;
3802
3803 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003804 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003805 cur_state = MFI_STATE_FLUSH_CACHE;
3806 break;
3807
3808 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003809 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003810 fw_state);
3811 return -ENODEV;
3812 }
3813
3814 /*
3815 * The cur_state should not last for more than max_wait secs
3816 */
3817 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003818 curr_abs_state = instance->instancet->
3819 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003820
Yang, Bo7218df62009-10-06 14:52:20 -06003821 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003822 msleep(1);
3823 } else
3824 break;
3825 }
3826
3827 /*
3828 * Return error if fw_state hasn't changed after max_wait
3829 */
Yang, Bo7218df62009-10-06 14:52:20 -06003830 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003831 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003832 "in %d secs\n", fw_state, max_wait);
3833 return -ENODEV;
3834 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003835
3836 abs_state = curr_abs_state;
3837 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003838 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003839 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003840
3841 return 0;
3842}
3843
3844/**
3845 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3846 * @instance: Adapter soft state
3847 */
3848static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3849{
3850 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003851 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003852 struct megasas_cmd *cmd;
3853
3854 if (!instance->frame_dma_pool)
3855 return;
3856
3857 /*
3858 * Return all frames to pool
3859 */
3860 for (i = 0; i < max_cmd; i++) {
3861
3862 cmd = instance->cmd_list[i];
3863
3864 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02003865 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003866 cmd->frame_phys_addr);
3867
3868 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02003869 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003870 cmd->sense_phys_addr);
3871 }
3872
3873 /*
3874 * Now destroy the pool itself
3875 */
Romain Perierfc69d862017-07-06 10:13:06 +02003876 dma_pool_destroy(instance->frame_dma_pool);
3877 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003878
3879 instance->frame_dma_pool = NULL;
3880 instance->sense_dma_pool = NULL;
3881}
3882
3883/**
3884 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3885 * @instance: Adapter soft state
3886 *
3887 * Each command packet has an embedded DMA memory buffer that is used for
3888 * filling MFI frame and the SG list that immediately follows the frame. This
3889 * function creates those DMA memory buffers for each command packet by using
3890 * PCI pool facility.
3891 */
3892static int megasas_create_frame_pool(struct megasas_instance *instance)
3893{
3894 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003895 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003896 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003897 u32 frame_count;
3898 struct megasas_cmd *cmd;
3899
adam radford9c915a82010-12-21 13:34:31 -08003900 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003901
3902 /*
3903 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3904 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3905 */
3906 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3907 sizeof(struct megasas_sge32);
3908
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003909 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003910 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003911
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003912 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303913 * For MFI controllers.
3914 * max_num_sge = 60
3915 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3916 * Total 960 byte (15 MFI frame of 64 byte)
3917 *
3918 * Fusion adapter require only 3 extra frame.
3919 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3920 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3921 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003922 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303923 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08003924 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003925 /*
3926 * Use DMA pool facility provided by PCI layer
3927 */
Romain Perierfc69d862017-07-06 10:13:06 +02003928 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
3929 &instance->pdev->dev,
3930 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003931
3932 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003933 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003934 return -ENOMEM;
3935 }
3936
Romain Perierfc69d862017-07-06 10:13:06 +02003937 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
3938 &instance->pdev->dev, 128,
3939 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003940
3941 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003942 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003943
Romain Perierfc69d862017-07-06 10:13:06 +02003944 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003945 instance->frame_dma_pool = NULL;
3946
3947 return -ENOMEM;
3948 }
3949
3950 /*
3951 * Allocate and attach a frame to each of the commands in cmd_list.
3952 * By making cmd->index as the context instead of the &cmd, we can
3953 * always use 32bit context regardless of the architecture
3954 */
3955 for (i = 0; i < max_cmd; i++) {
3956
3957 cmd = instance->cmd_list[i];
3958
Romain Perierfc69d862017-07-06 10:13:06 +02003959 cmd->frame = dma_pool_alloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003960 GFP_KERNEL, &cmd->frame_phys_addr);
3961
Romain Perierfc69d862017-07-06 10:13:06 +02003962 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003963 GFP_KERNEL, &cmd->sense_phys_addr);
3964
3965 /*
3966 * megasas_teardown_frame_pool() takes care of freeing
3967 * whatever has been allocated
3968 */
3969 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02003970 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003971 megasas_teardown_frame_pool(instance);
3972 return -ENOMEM;
3973 }
3974
Shivasharan S21c34002017-02-10 00:59:28 -08003975 memset(cmd->frame, 0, instance->mfi_frame_size);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303976 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003977 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303978 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003979 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003980 }
3981
3982 return 0;
3983}
3984
3985/**
3986 * megasas_free_cmds - Free all the cmds in the free cmd pool
3987 * @instance: Adapter soft state
3988 */
adam radford9c915a82010-12-21 13:34:31 -08003989void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003990{
3991 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003992
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003993 /* First free the MFI frame pool */
3994 megasas_teardown_frame_pool(instance);
3995
3996 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003997 for (i = 0; i < instance->max_mfi_cmds; i++)
3998
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003999 kfree(instance->cmd_list[i]);
4000
4001 /* Free the cmd_list buffer itself */
4002 kfree(instance->cmd_list);
4003 instance->cmd_list = NULL;
4004
4005 INIT_LIST_HEAD(&instance->cmd_pool);
4006}
4007
4008/**
4009 * megasas_alloc_cmds - Allocates the command packets
4010 * @instance: Adapter soft state
4011 *
4012 * Each command that is issued to the FW, whether IO commands from the OS or
4013 * internal commands like IOCTLs, are wrapped in local data structure called
4014 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4015 * the FW.
4016 *
4017 * Each frame has a 32-bit field called context (tag). This context is used
4018 * to get back the megasas_cmd from the frame when a frame gets completed in
4019 * the ISR. Typically the address of the megasas_cmd itself would be used as
4020 * the context. But we wanted to keep the differences between 32 and 64 bit
4021 * systems to the mininum. We always use 32 bit integers for the context. In
4022 * this driver, the 32 bit values are the indices into an array cmd_list.
4023 * This array is used only to look up the megasas_cmd given the context. The
4024 * free commands themselves are maintained in a linked list called cmd_pool.
4025 */
adam radford9c915a82010-12-21 13:34:31 -08004026int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004027{
4028 int i;
4029 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004030 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004031 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304032 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004033
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304034 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08004035 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004036
4037 /*
4038 * instance->cmd_list is an array of struct megasas_cmd pointers.
4039 * Allocate the dynamic array first and then allocate individual
4040 * commands.
4041 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004042 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004043
4044 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004045 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004046 return -ENOMEM;
4047 }
4048
adam radford9c915a82010-12-21 13:34:31 -08004049 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004050
4051 for (i = 0; i < max_cmd; i++) {
4052 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4053 GFP_KERNEL);
4054
4055 if (!instance->cmd_list[i]) {
4056
4057 for (j = 0; j < i; j++)
4058 kfree(instance->cmd_list[j]);
4059
4060 kfree(instance->cmd_list);
4061 instance->cmd_list = NULL;
4062
4063 return -ENOMEM;
4064 }
4065 }
4066
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004067 for (i = 0; i < max_cmd; i++) {
4068 cmd = instance->cmd_list[i];
4069 memset(cmd, 0, sizeof(struct megasas_cmd));
4070 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004071 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004072 cmd->instance = instance;
4073
4074 list_add_tail(&cmd->list, &instance->cmd_pool);
4075 }
4076
4077 /*
4078 * Create a frame pool and assign one frame to each cmd
4079 */
4080 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004081 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004082 megasas_free_cmds(instance);
4083 }
4084
4085 return 0;
4086}
4087
Yang, Bo81e403c2009-10-06 14:27:54 -06004088/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304089 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4090 * @instance: Adapter soft state
4091 *
4092 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4093 * or FW is not under OCR.
4094 */
4095inline int
4096dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4097
4098 if (!instance->ctrl_context)
4099 return KILL_ADAPTER;
4100 else if (instance->unload ||
4101 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4102 return IGNORE_TIMEOUT;
4103 else
4104 return INITIATE_OCR;
4105}
4106
Shivasharan S15dd0382017-02-10 00:59:10 -08004107static void
4108megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304109{
4110 int ret;
4111 struct megasas_cmd *cmd;
4112 struct megasas_dcmd_frame *dcmd;
4113
Shivasharan S15dd0382017-02-10 00:59:10 -08004114 struct MR_PRIV_DEVICE *mr_device_priv_data;
4115 u16 device_id = 0;
4116
4117 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304118 cmd = megasas_get_cmd(instance);
4119
4120 if (!cmd) {
4121 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004122 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304123 }
4124
4125 dcmd = &cmd->frame->dcmd;
4126
4127 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4128 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4129
4130 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4131 dcmd->cmd = MFI_CMD_DCMD;
4132 dcmd->cmd_status = 0xFF;
4133 dcmd->sge_count = 1;
4134 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4135 dcmd->timeout = 0;
4136 dcmd->pad_0 = 0;
4137 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4138 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4139 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4140 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4141
4142 if (instance->ctrl_context && !instance->mask_interrupts)
4143 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4144 else
4145 ret = megasas_issue_polled(instance, cmd);
4146
4147 switch (ret) {
4148 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004149 mr_device_priv_data = sdev->hostdata;
4150 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4151 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304152 instance->pd_info->state.ddf.pdType.intf;
4153 break;
4154
4155 case DCMD_TIMEOUT:
4156
4157 switch (dcmd_timeout_ocr_possible(instance)) {
4158 case INITIATE_OCR:
4159 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4160 megasas_reset_fusion(instance->host,
4161 MFI_IO_TIMEOUT_OCR);
4162 break;
4163 case KILL_ADAPTER:
4164 megaraid_sas_kill_hba(instance);
4165 break;
4166 case IGNORE_TIMEOUT:
4167 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4168 __func__, __LINE__);
4169 break;
4170 }
4171
4172 break;
4173 }
4174
4175 if (ret != DCMD_TIMEOUT)
4176 megasas_return_cmd(instance, cmd);
4177
Shivasharan S15dd0382017-02-10 00:59:10 -08004178 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304179}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304180/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004181 * megasas_get_pd_list_info - Returns FW's pd_list structure
4182 * @instance: Adapter soft state
4183 * @pd_list: pd_list structure
4184 *
4185 * Issues an internal command (DCMD) to get the FW's controller PD
4186 * list structure. This information is mainly used to find out SYSTEM
4187 * supported by the FW.
4188 */
4189static int
4190megasas_get_pd_list(struct megasas_instance *instance)
4191{
4192 int ret = 0, pd_index = 0;
4193 struct megasas_cmd *cmd;
4194 struct megasas_dcmd_frame *dcmd;
4195 struct MR_PD_LIST *ci;
4196 struct MR_PD_ADDRESS *pd_addr;
4197 dma_addr_t ci_h = 0;
4198
Sumit Saxenad9083162016-07-08 03:30:16 -07004199 if (instance->pd_list_not_supported) {
4200 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4201 "not supported by firmware\n");
4202 return ret;
4203 }
4204
Yang, Bo81e403c2009-10-06 14:27:54 -06004205 cmd = megasas_get_cmd(instance);
4206
4207 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004208 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004209 return -ENOMEM;
4210 }
4211
4212 dcmd = &cmd->frame->dcmd;
4213
4214 ci = pci_alloc_consistent(instance->pdev,
4215 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4216
4217 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004218 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004219 megasas_return_cmd(instance, cmd);
4220 return -ENOMEM;
4221 }
4222
4223 memset(ci, 0, sizeof(*ci));
4224 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4225
4226 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4227 dcmd->mbox.b[1] = 0;
4228 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304229 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004230 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304231 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004232 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004233 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304234 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4235 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4236 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4237 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004238
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304239 if (instance->ctrl_context && !instance->mask_interrupts)
4240 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304241 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304242 else
4243 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004244
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304245 switch (ret) {
4246 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004247 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4248 "failed/not supported by firmware\n");
4249
4250 if (instance->ctrl_context)
4251 megaraid_sas_kill_hba(instance);
4252 else
4253 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304254 break;
4255 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004256
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304257 switch (dcmd_timeout_ocr_possible(instance)) {
4258 case INITIATE_OCR:
4259 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4260 /*
4261 * DCMD failed from AEN path.
4262 * AEN path already hold reset_mutex to avoid PCI access
4263 * while OCR is in progress.
4264 */
4265 mutex_unlock(&instance->reset_mutex);
4266 megasas_reset_fusion(instance->host,
4267 MFI_IO_TIMEOUT_OCR);
4268 mutex_lock(&instance->reset_mutex);
4269 break;
4270 case KILL_ADAPTER:
4271 megaraid_sas_kill_hba(instance);
4272 break;
4273 case IGNORE_TIMEOUT:
4274 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4275 __func__, __LINE__);
4276 break;
4277 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004278
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304279 break;
4280
4281 case DCMD_SUCCESS:
4282 pd_addr = ci->addr;
4283
4284 if ((le32_to_cpu(ci->count) >
4285 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4286 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004287
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304288 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304289 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004290
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304291 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304292 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304293 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304294 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304295 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304296 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304297 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004298 pd_addr++;
4299 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304300
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304301 memcpy(instance->pd_list, instance->local_pd_list,
4302 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304303 break;
4304
Yang, Bo81e403c2009-10-06 14:27:54 -06004305 }
4306
4307 pci_free_consistent(instance->pdev,
4308 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4309 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304310
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304311 if (ret != DCMD_TIMEOUT)
4312 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004313
4314 return ret;
4315}
4316
Yang, Bobdc6fb82009-12-06 08:30:19 -07004317/*
4318 * megasas_get_ld_list_info - Returns FW's ld_list structure
4319 * @instance: Adapter soft state
4320 * @ld_list: ld_list structure
4321 *
4322 * Issues an internal command (DCMD) to get the FW's controller PD
4323 * list structure. This information is mainly used to find out SYSTEM
4324 * supported by the FW.
4325 */
4326static int
4327megasas_get_ld_list(struct megasas_instance *instance)
4328{
4329 int ret = 0, ld_index = 0, ids = 0;
4330 struct megasas_cmd *cmd;
4331 struct megasas_dcmd_frame *dcmd;
4332 struct MR_LD_LIST *ci;
4333 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304334 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004335
4336 cmd = megasas_get_cmd(instance);
4337
4338 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004339 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004340 return -ENOMEM;
4341 }
4342
4343 dcmd = &cmd->frame->dcmd;
4344
4345 ci = pci_alloc_consistent(instance->pdev,
4346 sizeof(struct MR_LD_LIST),
4347 &ci_h);
4348
4349 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004350 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004351 megasas_return_cmd(instance, cmd);
4352 return -ENOMEM;
4353 }
4354
4355 memset(ci, 0, sizeof(*ci));
4356 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4357
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304358 if (instance->supportmax256vd)
4359 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004360 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304361 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004362 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304363 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004364 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304365 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4366 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4367 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4368 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004369 dcmd->pad_0 = 0;
4370
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304371 if (instance->ctrl_context && !instance->mask_interrupts)
4372 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304373 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304374 else
4375 ret = megasas_issue_polled(instance, cmd);
4376
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304377 ld_count = le32_to_cpu(ci->ldCount);
4378
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304379 switch (ret) {
4380 case DCMD_FAILED:
4381 megaraid_sas_kill_hba(instance);
4382 break;
4383 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004384
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304385 switch (dcmd_timeout_ocr_possible(instance)) {
4386 case INITIATE_OCR:
4387 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4388 /*
4389 * DCMD failed from AEN path.
4390 * AEN path already hold reset_mutex to avoid PCI access
4391 * while OCR is in progress.
4392 */
4393 mutex_unlock(&instance->reset_mutex);
4394 megasas_reset_fusion(instance->host,
4395 MFI_IO_TIMEOUT_OCR);
4396 mutex_lock(&instance->reset_mutex);
4397 break;
4398 case KILL_ADAPTER:
4399 megaraid_sas_kill_hba(instance);
4400 break;
4401 case IGNORE_TIMEOUT:
4402 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4403 __func__, __LINE__);
4404 break;
4405 }
4406
4407 break;
4408
4409 case DCMD_SUCCESS:
4410 if (ld_count > instance->fw_supported_vd_count)
4411 break;
4412
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304413 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004414
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304415 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004416 if (ci->ldList[ld_index].state != 0) {
4417 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304418 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004419 }
4420 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304421
4422 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004423 }
4424
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304425 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004426
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304427 if (ret != DCMD_TIMEOUT)
4428 megasas_return_cmd(instance, cmd);
4429
Yang, Bobdc6fb82009-12-06 08:30:19 -07004430 return ret;
4431}
4432
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004433/**
adam radford21c9e162013-09-06 15:27:14 -07004434 * megasas_ld_list_query - Returns FW's ld_list structure
4435 * @instance: Adapter soft state
4436 * @ld_list: ld_list structure
4437 *
4438 * Issues an internal command (DCMD) to get the FW's controller PD
4439 * list structure. This information is mainly used to find out SYSTEM
4440 * supported by the FW.
4441 */
4442static int
4443megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4444{
4445 int ret = 0, ld_index = 0, ids = 0;
4446 struct megasas_cmd *cmd;
4447 struct megasas_dcmd_frame *dcmd;
4448 struct MR_LD_TARGETID_LIST *ci;
4449 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304450 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004451
4452 cmd = megasas_get_cmd(instance);
4453
4454 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004455 dev_warn(&instance->pdev->dev,
4456 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004457 return -ENOMEM;
4458 }
4459
4460 dcmd = &cmd->frame->dcmd;
4461
4462 ci = pci_alloc_consistent(instance->pdev,
4463 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4464
4465 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004466 dev_warn(&instance->pdev->dev,
4467 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004468 megasas_return_cmd(instance, cmd);
4469 return -ENOMEM;
4470 }
4471
4472 memset(ci, 0, sizeof(*ci));
4473 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4474
4475 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304476 if (instance->supportmax256vd)
4477 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004478
4479 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304480 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004481 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304482 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004483 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304484 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4485 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4486 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4487 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004488 dcmd->pad_0 = 0;
4489
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304490 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304491 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304492 else
4493 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004494
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304495 switch (ret) {
4496 case DCMD_FAILED:
4497 dev_info(&instance->pdev->dev,
4498 "DCMD not supported by firmware - %s %d\n",
4499 __func__, __LINE__);
4500 ret = megasas_get_ld_list(instance);
4501 break;
4502 case DCMD_TIMEOUT:
4503 switch (dcmd_timeout_ocr_possible(instance)) {
4504 case INITIATE_OCR:
4505 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4506 /*
4507 * DCMD failed from AEN path.
4508 * AEN path already hold reset_mutex to avoid PCI access
4509 * while OCR is in progress.
4510 */
4511 mutex_unlock(&instance->reset_mutex);
4512 megasas_reset_fusion(instance->host,
4513 MFI_IO_TIMEOUT_OCR);
4514 mutex_lock(&instance->reset_mutex);
4515 break;
4516 case KILL_ADAPTER:
4517 megaraid_sas_kill_hba(instance);
4518 break;
4519 case IGNORE_TIMEOUT:
4520 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4521 __func__, __LINE__);
4522 break;
4523 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304524
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304525 break;
4526 case DCMD_SUCCESS:
4527 tgtid_count = le32_to_cpu(ci->count);
4528
4529 if ((tgtid_count > (instance->fw_supported_vd_count)))
4530 break;
4531
adam radford21c9e162013-09-06 15:27:14 -07004532 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304533 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004534 ids = ci->targetId[ld_index];
4535 instance->ld_ids[ids] = ci->targetId[ld_index];
4536 }
4537
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304538 break;
adam radford21c9e162013-09-06 15:27:14 -07004539 }
4540
4541 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304542 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004543
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304544 if (ret != DCMD_TIMEOUT)
4545 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004546
4547 return ret;
4548}
4549
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304550/*
4551 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4552 * instance : Controller's instance
4553*/
4554static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4555{
4556 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004557 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304558
4559 fusion = instance->ctrl_context;
4560 /* For MFI based controllers return dummy success */
4561 if (!fusion)
4562 return;
4563
4564 instance->supportmax256vd =
4565 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4566 /* Below is additional check to address future FW enhancement */
4567 if (instance->ctrl_info->max_lds > 64)
4568 instance->supportmax256vd = 1;
4569
4570 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4571 * MEGASAS_MAX_DEV_PER_CHANNEL;
4572 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4573 * MEGASAS_MAX_DEV_PER_CHANNEL;
4574 if (instance->supportmax256vd) {
4575 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4576 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4577 } else {
4578 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4579 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4580 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304581
4582 dev_info(&instance->pdev->dev,
4583 "firmware type\t: %s\n",
4584 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4585 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304586
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004587 if (instance->max_raid_mapsize) {
4588 ventura_map_sz = instance->max_raid_mapsize *
4589 MR_MIN_MAP_SIZE; /* 64k */
4590 fusion->current_map_sz = ventura_map_sz;
4591 fusion->max_map_sz = ventura_map_sz;
4592 } else {
4593 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4594 (sizeof(struct MR_LD_SPAN_MAP) *
4595 (instance->fw_supported_vd_count - 1));
4596 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304597
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004598 fusion->max_map_sz =
4599 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304600
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004601 if (instance->supportmax256vd)
4602 fusion->current_map_sz = fusion->new_map_sz;
4603 else
4604 fusion->current_map_sz = fusion->old_map_sz;
4605 }
4606 /* irrespective of FW raid maps, driver raid map is constant */
4607 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304608}
4609
adam radford21c9e162013-09-06 15:27:14 -07004610/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004611 * megasas_get_controller_info - Returns FW's controller structure
4612 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004613 *
4614 * Issues an internal command (DCMD) to get the FW's controller structure.
4615 * This information is mainly used to find out the maximum IO transfer per
4616 * command supported by the FW.
4617 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304618int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304619megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004620{
4621 int ret = 0;
4622 struct megasas_cmd *cmd;
4623 struct megasas_dcmd_frame *dcmd;
4624 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304625 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004626 dma_addr_t ci_h = 0;
4627
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304628 ctrl_info = instance->ctrl_info;
4629
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004630 cmd = megasas_get_cmd(instance);
4631
4632 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004633 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004634 return -ENOMEM;
4635 }
4636
4637 dcmd = &cmd->frame->dcmd;
4638
4639 ci = pci_alloc_consistent(instance->pdev,
4640 sizeof(struct megasas_ctrl_info), &ci_h);
4641
4642 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004643 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004644 megasas_return_cmd(instance, cmd);
4645 return -ENOMEM;
4646 }
4647
4648 memset(ci, 0, sizeof(*ci));
4649 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4650
4651 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304652 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004653 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304654 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004655 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004656 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304657 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4658 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4659 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4660 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304661 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004662
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304663 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304664 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304665 else
4666 ret = megasas_issue_polled(instance, cmd);
4667
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304668 switch (ret) {
4669 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004670 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304671 /* Save required controller information in
4672 * CPU endianness format.
4673 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304674 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4675 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4676 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004677 le16_to_cpus((u16 *)&ctrl_info->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304678
4679 /* Update the latest Ext VD info.
4680 * From Init path, store current firmware details.
4681 * From OCR path, detect any firmware properties changes.
4682 * in case of Firmware upgrade without system reboot.
4683 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304684 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304685 instance->use_seqnum_jbod_fp =
4686 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004687 instance->support_morethan256jbod =
4688 ctrl_info->adapter_operations4.support_pd_map_target_id;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304689
4690 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304691 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4692 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304693 "controller type\t: %s(%dMB)\n",
4694 instance->is_imr ? "iMR" : "MR",
4695 le16_to_cpu(ctrl_info->memory_size));
4696
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304697 instance->disableOnlineCtrlReset =
4698 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304699 instance->secure_jbod_support =
4700 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304701 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4702 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304703 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4704 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304705 break;
4706
4707 case DCMD_TIMEOUT:
4708 switch (dcmd_timeout_ocr_possible(instance)) {
4709 case INITIATE_OCR:
4710 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4711 megasas_reset_fusion(instance->host,
4712 MFI_IO_TIMEOUT_OCR);
4713 break;
4714 case KILL_ADAPTER:
4715 megaraid_sas_kill_hba(instance);
4716 break;
4717 case IGNORE_TIMEOUT:
4718 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4719 __func__, __LINE__);
4720 break;
4721 }
4722 case DCMD_FAILED:
4723 megaraid_sas_kill_hba(instance);
4724 break;
4725
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304726 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004727
4728 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4729 ci, ci_h);
4730
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304731 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304732
4733
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004734 return ret;
4735}
4736
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304737/*
4738 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4739 * to firmware
4740 *
4741 * @instance: Adapter soft state
4742 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4743 MR_CRASH_BUF_TURN_OFF = 0
4744 MR_CRASH_BUF_TURN_ON = 1
4745 * @return 0 on success non-zero on failure.
4746 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4747 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4748 * that driver supports crash dump feature. This DCMD will be sent only if
4749 * crash dump feature is supported by the FW.
4750 *
4751 */
4752int megasas_set_crash_dump_params(struct megasas_instance *instance,
4753 u8 crash_buf_state)
4754{
4755 int ret = 0;
4756 struct megasas_cmd *cmd;
4757 struct megasas_dcmd_frame *dcmd;
4758
4759 cmd = megasas_get_cmd(instance);
4760
4761 if (!cmd) {
4762 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4763 return -ENOMEM;
4764 }
4765
4766
4767 dcmd = &cmd->frame->dcmd;
4768
4769 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4770 dcmd->mbox.b[0] = crash_buf_state;
4771 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304772 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304773 dcmd->sge_count = 1;
4774 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4775 dcmd->timeout = 0;
4776 dcmd->pad_0 = 0;
4777 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4778 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4779 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4780 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4781
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304782 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304783 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304784 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304785 ret = megasas_issue_polled(instance, cmd);
4786
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304787 if (ret == DCMD_TIMEOUT) {
4788 switch (dcmd_timeout_ocr_possible(instance)) {
4789 case INITIATE_OCR:
4790 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4791 megasas_reset_fusion(instance->host,
4792 MFI_IO_TIMEOUT_OCR);
4793 break;
4794 case KILL_ADAPTER:
4795 megaraid_sas_kill_hba(instance);
4796 break;
4797 case IGNORE_TIMEOUT:
4798 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4799 __func__, __LINE__);
4800 break;
4801 }
4802 } else
4803 megasas_return_cmd(instance, cmd);
4804
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304805 return ret;
4806}
4807
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004808/**
bo yang31ea7082007-11-07 12:09:50 -05004809 * megasas_issue_init_mfi - Initializes the FW
4810 * @instance: Adapter soft state
4811 *
4812 * Issues the INIT MFI cmd
4813 */
4814static int
4815megasas_issue_init_mfi(struct megasas_instance *instance)
4816{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304817 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004818 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004819 struct megasas_init_frame *init_frame;
4820 struct megasas_init_queue_info *initq_info;
4821 dma_addr_t init_frame_h;
4822 dma_addr_t initq_info_h;
4823
4824 /*
4825 * Prepare a init frame. Note the init frame points to queue info
4826 * structure. Each frame has SGL allocated after first 64 bytes. For
4827 * this frame - since we don't need any SGL - we use SGL's space as
4828 * queue info structure
4829 *
4830 * We will not get a NULL command below. We just created the pool.
4831 */
4832 cmd = megasas_get_cmd(instance);
4833
4834 init_frame = (struct megasas_init_frame *)cmd->frame;
4835 initq_info = (struct megasas_init_queue_info *)
4836 ((unsigned long)init_frame + 64);
4837
4838 init_frame_h = cmd->frame_phys_addr;
4839 initq_info_h = init_frame_h + 64;
4840
4841 context = init_frame->context;
4842 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4843 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4844 init_frame->context = context;
4845
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304846 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4847 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004848
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304849 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4850 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004851
4852 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304853 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304854 init_frame->queue_info_new_phys_addr_lo =
4855 cpu_to_le32(lower_32_bits(initq_info_h));
4856 init_frame->queue_info_new_phys_addr_hi =
4857 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004858
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304859 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004860
4861 /*
4862 * disable the intr before firing the init frame to FW
4863 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304864 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004865
4866 /*
4867 * Issue the init frame in polled mode
4868 */
4869
4870 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004871 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004872 megasas_return_cmd(instance, cmd);
4873 goto fail_fw_init;
4874 }
4875
4876 megasas_return_cmd(instance, cmd);
4877
4878 return 0;
4879
4880fail_fw_init:
4881 return -EINVAL;
4882}
4883
adam radfordcd50ba82010-12-21 10:23:23 -08004884static u32
4885megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004886{
adam radfordcd50ba82010-12-21 10:23:23 -08004887 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004888 u32 context_sz;
4889 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004890
4891 reg_set = instance->reg_set;
4892
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004893 /*
4894 * Get various operational parameters from status register
4895 */
Sumant Patro1341c932006-01-25 12:02:40 -08004896 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004897 /*
4898 * Reduce the max supported cmds by 1. This is to ensure that the
4899 * reply_q_sz (1 more than the max cmd that driver may send)
4900 * does not exceed max cmds that the FW can support
4901 */
4902 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004903 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004904 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004905 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004906 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304907 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4908 * are reserved for IOCTL + driver's internal DCMDs.
4909 */
4910 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4911 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4912 instance->max_scsi_cmds = (instance->max_fw_cmds -
4913 MEGASAS_SKINNY_INT_CMDS);
4914 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4915 } else {
4916 instance->max_scsi_cmds = (instance->max_fw_cmds -
4917 MEGASAS_INT_CMDS);
4918 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4919 }
4920
Sumit Saxena308ec452016-01-28 21:04:30 +05304921 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304922 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004923 * Create a pool of commands
4924 */
4925 if (megasas_alloc_cmds(instance))
4926 goto fail_alloc_cmds;
4927
4928 /*
4929 * Allocate memory for reply queue. Length of reply queue should
4930 * be _one_ more than the maximum commands handled by the firmware.
4931 *
4932 * Note: When FW completes commands, it places corresponding contex
4933 * values in this circular reply queue. This circular queue is a fairly
4934 * typical producer-consumer queue. FW is the producer (of completed
4935 * commands) and the driver is the consumer.
4936 */
4937 context_sz = sizeof(u32);
4938 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4939
4940 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4941 reply_q_sz,
4942 &instance->reply_queue_h);
4943
4944 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004945 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004946 goto fail_reply_queue;
4947 }
4948
bo yang31ea7082007-11-07 12:09:50 -05004949 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004950 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004951
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304952 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304953 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4954 "Fail from %s %d\n", instance->unique_id,
4955 __func__, __LINE__);
4956 goto fail_fw_init;
4957 }
4958
bo yang39a98552010-09-22 22:36:29 -04004959 instance->fw_support_ieee = 0;
4960 instance->fw_support_ieee =
4961 (instance->instancet->read_fw_status_reg(reg_set) &
4962 0x04000000);
4963
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004964 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004965 instance->fw_support_ieee);
4966
4967 if (instance->fw_support_ieee)
4968 instance->flag_ieee = 1;
4969
adam radfordcd50ba82010-12-21 10:23:23 -08004970 return 0;
4971
4972fail_fw_init:
4973
4974 pci_free_consistent(instance->pdev, reply_q_sz,
4975 instance->reply_queue, instance->reply_queue_h);
4976fail_reply_queue:
4977 megasas_free_cmds(instance);
4978
4979fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004980 return 1;
4981}
4982
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304983/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01004984 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304985 * @instance: Adapter soft state
4986 *
4987 * Do not enable interrupt, only setup ISRs.
4988 *
4989 * Return 0 on success.
4990 */
4991static int
4992megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4993{
4994 struct pci_dev *pdev;
4995
4996 pdev = instance->pdev;
4997 instance->irq_context[0].instance = instance;
4998 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01004999 if (request_irq(pci_irq_vector(pdev, 0),
5000 instance->instancet->service_isr, IRQF_SHARED,
5001 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305002 dev_err(&instance->pdev->dev,
5003 "Failed to register IRQ from %s %d\n",
5004 __func__, __LINE__);
5005 return -1;
5006 }
5007 return 0;
5008}
5009
5010/**
5011 * megasas_setup_irqs_msix - register MSI-x interrupts.
5012 * @instance: Adapter soft state
5013 * @is_probe: Driver probe check
5014 *
5015 * Do not enable interrupt, only setup ISRs.
5016 *
5017 * Return 0 on success.
5018 */
5019static int
5020megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5021{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005022 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305023 struct pci_dev *pdev;
5024
5025 pdev = instance->pdev;
5026
5027 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305028 for (i = 0; i < instance->msix_vectors; i++) {
5029 instance->irq_context[i].instance = instance;
5030 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005031 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305032 instance->instancet->service_isr, 0, "megasas",
5033 &instance->irq_context[i])) {
5034 dev_err(&instance->pdev->dev,
5035 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005036 for (j = 0; j < i; j++)
5037 free_irq(pci_irq_vector(pdev, j),
5038 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305039 /* Retry irq register for IO_APIC*/
5040 instance->msix_vectors = 0;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005041 if (is_probe) {
5042 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305043 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005044 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305045 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005046 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305047 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305048 }
5049 return 0;
5050}
5051
5052/*
5053 * megasas_destroy_irqs- unregister interrupts.
5054 * @instance: Adapter soft state
5055 * return: void
5056 */
5057static void
5058megasas_destroy_irqs(struct megasas_instance *instance) {
5059
5060 int i;
5061
5062 if (instance->msix_vectors)
5063 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005064 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305065 &instance->irq_context[i]);
5066 }
5067 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005068 free_irq(pci_irq_vector(instance->pdev, 0),
5069 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305070}
5071
adam radfordcd50ba82010-12-21 10:23:23 -08005072/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305073 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5074 * @instance: Adapter soft state
5075 * @is_probe: Driver probe check
5076 *
5077 * Return 0 on success.
5078 */
5079void
5080megasas_setup_jbod_map(struct megasas_instance *instance)
5081{
5082 int i;
5083 struct fusion_context *fusion = instance->ctrl_context;
5084 u32 pd_seq_map_sz;
5085
5086 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5087 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5088
5089 if (reset_devices || !fusion ||
5090 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
5091 dev_info(&instance->pdev->dev,
5092 "Jbod map is not supported %s %d\n",
5093 __func__, __LINE__);
5094 instance->use_seqnum_jbod_fp = false;
5095 return;
5096 }
5097
5098 if (fusion->pd_seq_sync[0])
5099 goto skip_alloc;
5100
5101 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5102 fusion->pd_seq_sync[i] = dma_alloc_coherent
5103 (&instance->pdev->dev, pd_seq_map_sz,
5104 &fusion->pd_seq_phys[i], GFP_KERNEL);
5105 if (!fusion->pd_seq_sync[i]) {
5106 dev_err(&instance->pdev->dev,
5107 "Failed to allocate memory from %s %d\n",
5108 __func__, __LINE__);
5109 if (i == 1) {
5110 dma_free_coherent(&instance->pdev->dev,
5111 pd_seq_map_sz, fusion->pd_seq_sync[0],
5112 fusion->pd_seq_phys[0]);
5113 fusion->pd_seq_sync[0] = NULL;
5114 }
5115 instance->use_seqnum_jbod_fp = false;
5116 return;
5117 }
5118 }
5119
5120skip_alloc:
5121 if (!megasas_sync_pd_seq_num(instance, false) &&
5122 !megasas_sync_pd_seq_num(instance, true))
5123 instance->use_seqnum_jbod_fp = true;
5124 else
5125 instance->use_seqnum_jbod_fp = false;
5126}
5127
5128/**
adam radfordcd50ba82010-12-21 10:23:23 -08005129 * megasas_init_fw - Initializes the FW
5130 * @instance: Adapter soft state
5131 *
5132 * This is the main function for initializing firmware
5133 */
5134
5135static int megasas_init_fw(struct megasas_instance *instance)
5136{
5137 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005138 u32 max_sectors_2, tmp_sectors, msix_enable;
5139 u32 scratch_pad_2, scratch_pad_3, scratch_pad_4;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005140 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005141 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305142 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005143 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005144 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005145 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305146 struct fusion_context *fusion;
5147
5148 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005149
5150 /* Find first memory bar */
5151 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005152 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005153 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005154 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005155 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005156 return -EBUSY;
5157 }
5158
Ben Collins11f8a7b2013-09-13 12:46:44 -04005159 base_addr = pci_resource_start(instance->pdev, instance->bar);
5160 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005161
5162 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005163 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005164 goto fail_ioremap;
5165 }
5166
5167 reg_set = instance->reg_set;
5168
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005169 if (fusion)
adam radford9c915a82010-12-21 13:34:31 -08005170 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005171 else {
5172 switch (instance->pdev->device) {
5173 case PCI_DEVICE_ID_LSI_SAS1078R:
5174 case PCI_DEVICE_ID_LSI_SAS1078DE:
5175 instance->instancet = &megasas_instance_template_ppc;
5176 break;
5177 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5178 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5179 instance->instancet = &megasas_instance_template_gen2;
5180 break;
5181 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5182 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5183 instance->instancet = &megasas_instance_template_skinny;
5184 break;
5185 case PCI_DEVICE_ID_LSI_SAS1064R:
5186 case PCI_DEVICE_ID_DELL_PERC5:
5187 default:
5188 instance->instancet = &megasas_instance_template_xscale;
5189 instance->pd_list_not_supported = 1;
5190 break;
5191 }
adam radfordcd50ba82010-12-21 10:23:23 -08005192 }
5193
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305194 if (megasas_transition_to_ready(instance, 0)) {
5195 atomic_set(&instance->fw_reset_no_pci_access, 1);
5196 instance->instancet->adp_reset
5197 (instance, instance->reg_set);
5198 atomic_set(&instance->fw_reset_no_pci_access, 0);
5199 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005200 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305201 __func__);
5202
5203 /*waitting for about 30 second before retry*/
5204 ssleep(30);
5205
5206 if (megasas_transition_to_ready(instance, 0))
5207 goto fail_ready_state;
5208 }
adam radfordcd50ba82010-12-21 10:23:23 -08005209
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005210 if (instance->is_ventura) {
5211 scratch_pad_3 =
5212 readl(&instance->reg_set->outbound_scratch_pad_3);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005213 instance->max_raid_mapsize = ((scratch_pad_3 >>
5214 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5215 MR_MAX_RAID_MAP_SIZE_MASK);
5216 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305217
adam radford3f1abce2011-05-11 18:33:47 -07005218 /* Check if MSI-X is supported while in ready state */
5219 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5220 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005221 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005222 int irq_flags = PCI_IRQ_MSIX;
5223
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305224 scratch_pad_2 = readl
5225 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005226 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305227 if (fusion) {
5228 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5229 instance->msix_vectors = (scratch_pad_2
5230 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5231 fw_msix_count = instance->msix_vectors;
5232 } else { /* Invader series supports more than 8 MSI-x vectors*/
5233 instance->msix_vectors = ((scratch_pad_2
5234 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5235 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005236 if (instance->msix_vectors > 16)
5237 instance->msix_combined = true;
5238
Sumit Saxena179ac142016-01-28 21:04:28 +05305239 if (rdpq_enable)
5240 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5241 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305242 fw_msix_count = instance->msix_vectors;
5243 /* Save 1-15 reply post index address to local memory
5244 * Index 0 is already saved from reg offset
5245 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5246 */
5247 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5248 instance->reply_post_host_index_addr[loop] =
5249 (u32 __iomem *)
5250 ((u8 __iomem *)instance->reg_set +
5251 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5252 + (loop * 0x10));
5253 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305254 }
5255 if (msix_vectors)
5256 instance->msix_vectors = min(msix_vectors,
5257 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305258 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005259 instance->msix_vectors = 1;
5260 /* Don't bother allocating more MSI-X vectors than cpus */
5261 instance->msix_vectors = min(instance->msix_vectors,
5262 (unsigned int)num_online_cpus());
Hannes Reineckefad119b2016-12-02 12:52:23 +01005263 if (smp_affinity_enable)
5264 irq_flags |= PCI_IRQ_AFFINITY;
5265 i = pci_alloc_irq_vectors(instance->pdev, 1,
5266 instance->msix_vectors, irq_flags);
Jiang Liuc12de882014-11-03 20:44:20 +08005267 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005268 instance->msix_vectors = i;
5269 else
adam radfordc8e858f2011-10-08 18:15:13 -07005270 instance->msix_vectors = 0;
5271 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005272 /*
5273 * MSI-X host index 0 is common for all adapter.
5274 * It is used for all MPT based Adapters.
5275 */
5276 if (instance->msix_combined) {
5277 instance->reply_post_host_index_addr[0] =
5278 (u32 *)((u8 *)instance->reg_set +
5279 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5280 } else {
5281 instance->reply_post_host_index_addr[0] =
5282 (u32 *)((u8 *)instance->reg_set +
5283 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5284 }
5285
Shivasharan S64ff64b2017-03-10 03:22:12 -08005286 if (!instance->msix_vectors) {
5287 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5288 if (i < 0)
5289 goto fail_setup_irqs;
5290 }
adam radford3f1abce2011-05-11 18:33:47 -07005291
Tomas Henzl258c3af2015-06-02 16:09:46 +05305292 dev_info(&instance->pdev->dev,
5293 "firmware supports msix\t: (%d)", fw_msix_count);
5294 dev_info(&instance->pdev->dev,
5295 "current msix/online cpus\t: (%d/%d)\n",
5296 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305297 dev_info(&instance->pdev->dev,
5298 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305299
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305300 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5301 (unsigned long)instance);
5302
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305303 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5304 GFP_KERNEL);
5305 if (instance->ctrl_info == NULL)
5306 goto fail_init_adapter;
5307
5308 /*
5309 * Below are default value for legacy Firmware.
5310 * non-fusion based controllers
5311 */
5312 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5313 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005314 /* Get operational params, sge flags, send init cmd to controller */
5315 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005316 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005317
Shivasharan S15dd0382017-02-10 00:59:10 -08005318 if (instance->is_ventura) {
5319 scratch_pad_4 =
5320 readl(&instance->reg_set->outbound_scratch_pad_4);
5321 if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
5322 MR_DEFAULT_NVME_PAGE_SHIFT)
5323 instance->nvme_page_size =
5324 (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
5325
5326 dev_info(&instance->pdev->dev,
5327 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5328 }
5329
Tomas Henzl18103ef2016-11-01 17:32:02 +01005330 if (instance->msix_vectors ?
5331 megasas_setup_irqs_msix(instance, 1) :
5332 megasas_setup_irqs_ioapic(instance))
5333 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305334
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305335 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005336
Andy Lutomirski13f30772016-05-03 10:24:31 -07005337 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005338
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305339 megasas_setup_jbod_map(instance);
5340
bo yang39a98552010-09-22 22:36:29 -04005341 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005342 * the following function will get the PD LIST.
5343 */
5344 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005345 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005346 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005347 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Shivasharan S72bff2d2017-02-10 00:59:33 -08005348 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005349 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005350
Yang, Bobdc6fb82009-12-06 08:30:19 -07005351 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005352
5353 /* stream detection initialization */
Shivasharan S41064f12017-02-10 00:59:37 -08005354 if (instance->is_ventura && fusion) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005355 fusion->stream_detect_by_ld =
Shivasharan S41064f12017-02-10 00:59:37 -08005356 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5357 * MAX_LOGICAL_DRIVES_EXT,
5358 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005359 if (!fusion->stream_detect_by_ld) {
5360 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08005361 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005362 goto fail_get_ld_pd_list;
5363 }
5364 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5365 fusion->stream_detect_by_ld[i] =
5366 kmalloc(sizeof(struct LD_STREAM_DETECT),
5367 GFP_KERNEL);
5368 if (!fusion->stream_detect_by_ld[i]) {
5369 dev_err(&instance->pdev->dev,
5370 "unable to allocate stream detect by LD\n ");
5371 for (j = 0; j < i; ++j)
5372 kfree(fusion->stream_detect_by_ld[j]);
5373 kfree(fusion->stream_detect_by_ld);
5374 fusion->stream_detect_by_ld = NULL;
5375 goto fail_get_ld_pd_list;
5376 }
5377 fusion->stream_detect_by_ld[i]->mru_bit_map
5378 = MR_STREAM_BITMAP;
5379 }
5380 }
5381
adam radford21c9e162013-09-06 15:27:14 -07005382 if (megasas_ld_list_query(instance,
5383 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
Shivasharan S72bff2d2017-02-10 00:59:33 -08005384 goto fail_get_ld_pd_list;
Yang, Bobdc6fb82009-12-06 08:30:19 -07005385
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005386 /*
5387 * Compute the max allowed sectors per IO: The controller info has two
5388 * limits on max sectors. Driver should use the minimum of these two.
5389 *
5390 * 1 << stripe_sz_ops.min = max sectors per strip
5391 *
5392 * Note that older firmwares ( < FW ver 30) didn't report information
5393 * to calculate max_sectors_1. So the number ended up as zero always.
5394 */
bo yang14faea92007-11-09 04:14:00 -05005395 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305396 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005397
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305398 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5399 le16_to_cpu(ctrl_info->max_strips_per_io);
5400 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005401
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005402 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305403
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305404 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5405 instance->passive = ctrl_info->cluster.passive;
5406 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305407 instance->UnevenSpanSupport =
5408 ctrl_info->adapterOperations2.supportUnevenSpans;
5409 if (instance->UnevenSpanSupport) {
5410 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305411 if (MR_ValidateMapInfo(instance))
5412 fusion->fast_path_io = 1;
5413 else
5414 fusion->fast_path_io = 0;
5415
5416 }
5417 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305418 instance->requestorId = ctrl_info->iov.requestorId;
5419 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5420 if (!ctrl_info->adapterOperations2.activePassive)
5421 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305422
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305423 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5424 instance->PlasmaFW111 ? "1.11" : "new");
5425
5426 if (instance->PlasmaFW111) {
5427 iovPtr = (struct IOV_111 *)
5428 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5429 instance->requestorId = iovPtr->requestorId;
5430 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305431 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305432 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5433 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305434 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305435
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305436 instance->crash_dump_fw_support =
5437 ctrl_info->adapterOperations3.supportCrashDump;
5438 instance->crash_dump_drv_support =
5439 (instance->crash_dump_fw_support &&
5440 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305441 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305442 megasas_set_crash_dump_params(instance,
5443 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005444
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305445 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305446 if (instance->crash_dump_buf)
5447 pci_free_consistent(instance->pdev,
5448 CRASH_DMA_BUF_SIZE,
5449 instance->crash_dump_buf,
5450 instance->crash_dump_h);
5451 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005452 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305453
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305454
5455 dev_info(&instance->pdev->dev,
5456 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5457 le16_to_cpu(ctrl_info->pci.vendor_id),
5458 le16_to_cpu(ctrl_info->pci.device_id),
5459 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5460 le16_to_cpu(ctrl_info->pci.sub_device_id));
5461 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5462 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305463 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5464 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305465 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5466 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305467
5468
bo yang14faea92007-11-09 04:14:00 -05005469 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305470 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005471 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5472 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005473
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305474 /* Check for valid throttlequeuedepth module parameter */
5475 if (throttlequeuedepth &&
5476 throttlequeuedepth <= instance->max_scsi_cmds)
5477 instance->throttlequeuedepth = throttlequeuedepth;
5478 else
5479 instance->throttlequeuedepth =
5480 MEGASAS_THROTTLE_QUEUE_DEPTH;
5481
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305482 if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
5483 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5484
5485 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5486 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005487
adam radford229fe472014-03-10 02:51:56 -07005488 /* Launch SR-IOV heartbeat timer */
5489 if (instance->requestorId) {
5490 if (!megasas_sriov_start_heartbeat(instance, 1))
5491 megasas_start_timer(instance,
5492 &instance->sriov_heartbeat_timer,
5493 megasas_sriov_heartbeat_handler,
5494 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5495 else
5496 instance->skip_heartbeat_timer_del = 1;
5497 }
5498
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005499 return 0;
5500
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005501fail_get_ld_pd_list:
5502 instance->instancet->disable_intr(instance);
Tomas Henzl18103ef2016-11-01 17:32:02 +01005503fail_init_adapter:
Hannes Reineckefad119b2016-12-02 12:52:23 +01005504 megasas_destroy_irqs(instance);
5505fail_setup_irqs:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305506 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005507 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305508 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005509fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305510 kfree(instance->ctrl_info);
5511 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005512 iounmap(instance->reg_set);
5513
Shivasharan S72bff2d2017-02-10 00:59:33 -08005514fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005515 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005516
Shivasharan S72bff2d2017-02-10 00:59:33 -08005517 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5518 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005519 return -EINVAL;
5520}
5521
5522/**
5523 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005524 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005525 */
5526static void megasas_release_mfi(struct megasas_instance *instance)
5527{
adam radford9c915a82010-12-21 13:34:31 -08005528 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005529
adam radford9c915a82010-12-21 13:34:31 -08005530 if (instance->reply_queue)
5531 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005532 instance->reply_queue, instance->reply_queue_h);
5533
5534 megasas_free_cmds(instance);
5535
5536 iounmap(instance->reg_set);
5537
Yinghai Lue7f85162016-08-05 23:37:34 -07005538 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005539}
5540
5541/**
5542 * megasas_get_seq_num - Gets latest event sequence numbers
5543 * @instance: Adapter soft state
5544 * @eli: FW event log sequence numbers information
5545 *
5546 * FW maintains a log of all events in a non-volatile area. Upper layers would
5547 * usually find out the latest sequence number of the events, the seq number at
5548 * the boot etc. They would "read" all the events below the latest seq number
5549 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5550 * number), they would subsribe to AEN (asynchronous event notification) and
5551 * wait for the events to happen.
5552 */
5553static int
5554megasas_get_seq_num(struct megasas_instance *instance,
5555 struct megasas_evt_log_info *eli)
5556{
5557 struct megasas_cmd *cmd;
5558 struct megasas_dcmd_frame *dcmd;
5559 struct megasas_evt_log_info *el_info;
5560 dma_addr_t el_info_h = 0;
5561
5562 cmd = megasas_get_cmd(instance);
5563
5564 if (!cmd) {
5565 return -ENOMEM;
5566 }
5567
5568 dcmd = &cmd->frame->dcmd;
5569 el_info = pci_alloc_consistent(instance->pdev,
5570 sizeof(struct megasas_evt_log_info),
5571 &el_info_h);
5572
5573 if (!el_info) {
5574 megasas_return_cmd(instance, cmd);
5575 return -ENOMEM;
5576 }
5577
5578 memset(el_info, 0, sizeof(*el_info));
5579 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5580
5581 dcmd->cmd = MFI_CMD_DCMD;
5582 dcmd->cmd_status = 0x0;
5583 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305584 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005585 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005586 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305587 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5588 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5589 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5590 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005591
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305592 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5593 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305594 /*
5595 * Copy the data back into callers buffer
5596 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305597 eli->newest_seq_num = el_info->newest_seq_num;
5598 eli->oldest_seq_num = el_info->oldest_seq_num;
5599 eli->clear_seq_num = el_info->clear_seq_num;
5600 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5601 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305602 } else
5603 dev_err(&instance->pdev->dev, "DCMD failed "
5604 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005605
5606 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5607 el_info, el_info_h);
5608
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305609 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005610
5611 return 0;
5612}
5613
5614/**
5615 * megasas_register_aen - Registers for asynchronous event notification
5616 * @instance: Adapter soft state
5617 * @seq_num: The starting sequence number
5618 * @class_locale: Class of the event
5619 *
5620 * This function subscribes for AEN for events beyond the @seq_num. It requests
5621 * to be notified if and only if the event is of type @class_locale
5622 */
5623static int
5624megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5625 u32 class_locale_word)
5626{
5627 int ret_val;
5628 struct megasas_cmd *cmd;
5629 struct megasas_dcmd_frame *dcmd;
5630 union megasas_evt_class_locale curr_aen;
5631 union megasas_evt_class_locale prev_aen;
5632
5633 /*
5634 * If there an AEN pending already (aen_cmd), check if the
5635 * class_locale of that pending AEN is inclusive of the new
5636 * AEN request we currently have. If it is, then we don't have
5637 * to do anything. In other words, whichever events the current
5638 * AEN request is subscribing to, have already been subscribed
5639 * to.
5640 *
5641 * If the old_cmd is _not_ inclusive, then we have to abort
5642 * that command, form a class_locale that is superset of both
5643 * old and current and re-issue to the FW
5644 */
5645
5646 curr_aen.word = class_locale_word;
5647
5648 if (instance->aen_cmd) {
5649
Christoph Hellwiga9555532015-04-23 16:34:24 +05305650 prev_aen.word =
5651 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005652
5653 /*
5654 * A class whose enum value is smaller is inclusive of all
5655 * higher values. If a PROGRESS (= -1) was previously
5656 * registered, then a new registration requests for higher
5657 * classes need not be sent to FW. They are automatically
5658 * included.
5659 *
5660 * Locale numbers don't have such hierarchy. They are bitmap
5661 * values
5662 */
5663 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305664 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005665 curr_aen.members.locale)) {
5666 /*
5667 * Previously issued event registration includes
5668 * current request. Nothing to do.
5669 */
5670 return 0;
5671 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305672 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005673
5674 if (prev_aen.members.class < curr_aen.members.class)
5675 curr_aen.members.class = prev_aen.members.class;
5676
5677 instance->aen_cmd->abort_aen = 1;
5678 ret_val = megasas_issue_blocked_abort_cmd(instance,
5679 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305680 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005681
5682 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005683 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005684 "previous AEN command\n");
5685 return ret_val;
5686 }
5687 }
5688 }
5689
5690 cmd = megasas_get_cmd(instance);
5691
5692 if (!cmd)
5693 return -ENOMEM;
5694
5695 dcmd = &cmd->frame->dcmd;
5696
5697 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5698
5699 /*
5700 * Prepare DCMD for aen registration
5701 */
5702 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5703
5704 dcmd->cmd = MFI_CMD_DCMD;
5705 dcmd->cmd_status = 0x0;
5706 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305707 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005708 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005709 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305710 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5711 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5712 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005713 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305714 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5715 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5716 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005717
Yang, Bof4c9a132009-10-06 14:43:28 -06005718 if (instance->aen_cmd != NULL) {
5719 megasas_return_cmd(instance, cmd);
5720 return 0;
5721 }
5722
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005723 /*
5724 * Store reference to the cmd used to register for AEN. When an
5725 * application wants us to register for AEN, we have to abort this
5726 * cmd and re-register with a new EVENT LOCALE supplied by that app
5727 */
5728 instance->aen_cmd = cmd;
5729
5730 /*
5731 * Issue the aen registration frame
5732 */
adam radford9c915a82010-12-21 13:34:31 -08005733 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005734
5735 return 0;
5736}
5737
Shivasharan S96188a82017-02-10 00:59:11 -08005738/* megasas_get_target_prop - Send DCMD with below details to firmware.
5739 *
5740 * This DCMD will fetch few properties of LD/system PD defined
5741 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
5742 *
5743 * DCMD send by drivers whenever new target is added to the OS.
5744 *
5745 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
5746 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
5747 * 0 = system PD, 1 = LD.
5748 * dcmd.mbox.s[1] - TargetID for LD/system PD.
5749 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
5750 *
5751 * @instance: Adapter soft state
5752 * @sdev: OS provided scsi device
5753 *
5754 * Returns 0 on success non-zero on failure.
5755 */
5756static int
5757megasas_get_target_prop(struct megasas_instance *instance,
5758 struct scsi_device *sdev)
5759{
5760 int ret;
5761 struct megasas_cmd *cmd;
5762 struct megasas_dcmd_frame *dcmd;
5763 u16 targetId = (sdev->channel % 2) + sdev->id;
5764
5765 cmd = megasas_get_cmd(instance);
5766
5767 if (!cmd) {
5768 dev_err(&instance->pdev->dev,
5769 "Failed to get cmd %s\n", __func__);
5770 return -ENOMEM;
5771 }
5772
5773 dcmd = &cmd->frame->dcmd;
5774
5775 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
5776 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5777 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
5778
5779 dcmd->mbox.s[1] = cpu_to_le16(targetId);
5780 dcmd->cmd = MFI_CMD_DCMD;
5781 dcmd->cmd_status = 0xFF;
5782 dcmd->sge_count = 1;
5783 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5784 dcmd->timeout = 0;
5785 dcmd->pad_0 = 0;
5786 dcmd->data_xfer_len =
5787 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5788 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
5789 dcmd->sgl.sge32[0].phys_addr =
5790 cpu_to_le32(instance->tgt_prop_h);
5791 dcmd->sgl.sge32[0].length =
5792 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5793
5794 if (instance->ctrl_context && !instance->mask_interrupts)
5795 ret = megasas_issue_blocked_cmd(instance,
5796 cmd, MFI_IO_TIMEOUT_SECS);
5797 else
5798 ret = megasas_issue_polled(instance, cmd);
5799
5800 switch (ret) {
5801 case DCMD_TIMEOUT:
5802 switch (dcmd_timeout_ocr_possible(instance)) {
5803 case INITIATE_OCR:
5804 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5805 megasas_reset_fusion(instance->host,
5806 MFI_IO_TIMEOUT_OCR);
5807 break;
5808 case KILL_ADAPTER:
5809 megaraid_sas_kill_hba(instance);
5810 break;
5811 case IGNORE_TIMEOUT:
5812 dev_info(&instance->pdev->dev,
5813 "Ignore DCMD timeout: %s %d\n",
5814 __func__, __LINE__);
5815 break;
5816 }
5817 break;
5818
5819 default:
5820 megasas_return_cmd(instance, cmd);
5821 }
5822 if (ret != DCMD_SUCCESS)
5823 dev_err(&instance->pdev->dev,
5824 "return from %s %d return value %d\n",
5825 __func__, __LINE__, ret);
5826
5827 return ret;
5828}
5829
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005830/**
5831 * megasas_start_aen - Subscribes to AEN during driver load time
5832 * @instance: Adapter soft state
5833 */
5834static int megasas_start_aen(struct megasas_instance *instance)
5835{
5836 struct megasas_evt_log_info eli;
5837 union megasas_evt_class_locale class_locale;
5838
5839 /*
5840 * Get the latest sequence number from FW
5841 */
5842 memset(&eli, 0, sizeof(eli));
5843
5844 if (megasas_get_seq_num(instance, &eli))
5845 return -1;
5846
5847 /*
5848 * Register AEN with FW for latest sequence number plus 1
5849 */
5850 class_locale.members.reserved = 0;
5851 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5852 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5853
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305854 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305855 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305856 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005857}
5858
5859/**
5860 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5861 * @instance: Adapter soft state
5862 */
5863static int megasas_io_attach(struct megasas_instance *instance)
5864{
5865 struct Scsi_Host *host = instance->host;
5866
5867 /*
5868 * Export parameters required by SCSI mid-layer
5869 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005870 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305871 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005872 host->this_id = instance->init_id;
5873 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005874
5875 if (instance->fw_support_ieee)
5876 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5877
Yang, Bo1fd10682010-10-12 07:18:50 -06005878 /*
5879 * Check if the module parameter value for max_sectors can be used
5880 */
5881 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5882 instance->max_sectors_per_req = max_sectors;
5883 else {
5884 if (max_sectors) {
5885 if (((instance->pdev->device ==
5886 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5887 (instance->pdev->device ==
5888 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5889 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5890 instance->max_sectors_per_req = max_sectors;
5891 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005892 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005893 "and <= %d (or < 1MB for GEN2 controller)\n",
5894 instance->max_sectors_per_req);
5895 }
5896 }
5897 }
5898
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005899 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005900 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005901 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5902 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5903 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005904 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005905
5906 /*
5907 * Notify the mid-layer about the new controller
5908 */
5909 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305910 dev_err(&instance->pdev->dev,
5911 "Failed to add host from %s %d\n",
5912 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005913 return -ENODEV;
5914 }
5915
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005916 return 0;
5917}
5918
bo yang31ea7082007-11-07 12:09:50 -05005919static int
5920megasas_set_dma_mask(struct pci_dev *pdev)
5921{
5922 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005923 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005924 */
5925 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005926 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005927
Yang Hongyang284901a2009-04-06 19:01:15 -07005928 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005929 goto fail_set_dma_mask;
5930 }
5931 } else {
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 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305935 /*
5936 * Ensure that all data structures are allocated in 32-bit
5937 * memory.
5938 */
5939 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5940 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5941 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5942 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5943 dev_info(&pdev->dev, "set 32bit DMA mask"
5944 "and 32 bit consistent mask\n");
5945 else
5946 goto fail_set_dma_mask;
5947 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305948
bo yang31ea7082007-11-07 12:09:50 -05005949 return 0;
5950
5951fail_set_dma_mask:
5952 return 1;
5953}
5954
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005955/**
5956 * megasas_probe_one - PCI hotplug entry point
5957 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005958 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005959 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005960static int megasas_probe_one(struct pci_dev *pdev,
5961 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005962{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305963 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005964 struct Scsi_Host *host;
5965 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005966 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305967 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005968
5969 /* Reset MSI-X in the kdump kernel */
5970 if (reset_devices) {
5971 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5972 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005973 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005974 &control);
5975 if (control & PCI_MSIX_FLAGS_ENABLE) {
5976 dev_info(&pdev->dev, "resetting MSI-X\n");
5977 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005978 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005979 control &
5980 ~PCI_MSIX_FLAGS_ENABLE);
5981 }
5982 }
5983 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005984
5985 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005986 * PCI prepping: enable device set bus mastering and dma mask
5987 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005988 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005989
5990 if (rval) {
5991 return rval;
5992 }
5993
5994 pci_set_master(pdev);
5995
bo yang31ea7082007-11-07 12:09:50 -05005996 if (megasas_set_dma_mask(pdev))
5997 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005998
5999 host = scsi_host_alloc(&megasas_template,
6000 sizeof(struct megasas_instance));
6001
6002 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006003 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006004 goto fail_alloc_instance;
6005 }
6006
6007 instance = (struct megasas_instance *)host->hostdata;
6008 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006009 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08006010 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006011
adam radford9c915a82010-12-21 13:34:31 -08006012 switch (instance->pdev->device) {
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -05006013 case PCI_DEVICE_ID_LSI_VENTURA:
6014 case PCI_DEVICE_ID_LSI_HARPOON:
6015 case PCI_DEVICE_ID_LSI_TOMCAT:
6016 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6017 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6018 instance->is_ventura = true;
adam radford9c915a82010-12-21 13:34:31 -08006019 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07006020 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07006021 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05306022 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05306023 case PCI_DEVICE_ID_LSI_INTRUDER:
6024 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05306025 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6026 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08006027 {
Shivasharan S5fc499b2017-02-10 00:59:17 -08006028 if (megasas_alloc_fusion_context(instance)) {
6029 megasas_free_fusion_context(instance);
adam radford9c915a82010-12-21 13:34:31 -08006030 goto fail_alloc_dma_buf;
6031 }
6032 fusion = instance->ctrl_context;
Shivasharan S5fc499b2017-02-10 00:59:17 -08006033
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306034 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
6035 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
6036 fusion->adapter_type = THUNDERBOLT_SERIES;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05006037 else if (instance->is_ventura)
6038 fusion->adapter_type = VENTURA_SERIES;
6039 else
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306040 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08006041 }
6042 break;
6043 default: /* For all other supported controllers */
6044
6045 instance->producer =
6046 pci_alloc_consistent(pdev, sizeof(u32),
6047 &instance->producer_h);
6048 instance->consumer =
6049 pci_alloc_consistent(pdev, sizeof(u32),
6050 &instance->consumer_h);
6051
6052 if (!instance->producer || !instance->consumer) {
Colin Ian King4086eae52016-09-12 13:42:12 +01006053 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08006054 "memory for producer, consumer\n");
6055 goto fail_alloc_dma_buf;
6056 }
6057
6058 *instance->producer = 0;
6059 *instance->consumer = 0;
6060 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006061 }
6062
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306063 /* Crash dump feature related initialisation*/
6064 instance->drv_buf_index = 0;
6065 instance->drv_buf_alloc = 0;
6066 instance->crash_dump_fw_support = 0;
6067 instance->crash_dump_app_support = 0;
6068 instance->fw_crash_state = UNAVAILABLE;
6069 spin_lock_init(&instance->crashdump_lock);
6070 instance->crash_dump_buf = NULL;
6071
Yang, Boc3518832009-10-06 14:18:02 -06006072 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06006073 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006074 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04006075 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05306076 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306077 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006078
6079 instance->evt_detail = pci_alloc_consistent(pdev,
6080 sizeof(struct
6081 megasas_evt_detail),
6082 &instance->evt_detail_h);
6083
6084 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006085 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006086 "event detail structure\n");
6087 goto fail_alloc_dma_buf;
6088 }
6089
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006090 if (!reset_devices) {
6091 instance->system_info_buf = pci_zalloc_consistent(pdev,
6092 sizeof(struct MR_DRV_SYSTEM_INFO),
6093 &instance->system_info_h);
6094 if (!instance->system_info_buf)
6095 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
Sumit Saxena2216c302016-01-28 21:04:26 +05306096
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006097 instance->pd_info = pci_alloc_consistent(pdev,
6098 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
6099
6100 if (!instance->pd_info)
6101 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
6102
weiping3b8328e2017-08-08 13:15:55 +08006103 instance->tgt_prop = pci_alloc_consistent(pdev,
6104 sizeof(struct MR_TARGET_PROPERTIES), &instance->tgt_prop_h);
6105
Shivasharan S96188a82017-02-10 00:59:11 -08006106 if (!instance->tgt_prop)
6107 dev_err(&instance->pdev->dev, "Failed to alloc mem for tgt_prop\n");
6108
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006109 instance->crash_dump_buf = pci_alloc_consistent(pdev,
6110 CRASH_DMA_BUF_SIZE,
6111 &instance->crash_dump_h);
6112 if (!instance->crash_dump_buf)
6113 dev_err(&pdev->dev, "Can't allocate Firmware "
6114 "crash dump DMA buffer\n");
6115 }
Sumit Saxena2216c302016-01-28 21:04:26 +05306116
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006117 /*
6118 * Initialize locks and queues
6119 */
6120 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04006121 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006122
Sumant Patroe4a082c2006-05-30 12:03:37 -07006123 atomic_set(&instance->fw_outstanding,0);
6124
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006125 init_waitqueue_head(&instance->int_cmd_wait_q);
6126 init_waitqueue_head(&instance->abort_cmd_wait_q);
6127
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306128 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04006129 spin_lock_init(&instance->hba_lock);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006130 spin_lock_init(&instance->stream_lock);
bo yang7343eb62007-11-09 04:35:44 -05006131 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006132
adam radford9c915a82010-12-21 13:34:31 -08006133 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05306134 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006135
6136 /*
6137 * Initialize PCI related and misc parameters
6138 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006139 instance->host = host;
6140 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6141 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306142 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006143
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306144
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006145 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306146 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06006147 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006148
Sumant Patro658dced2006-10-03 13:09:14 -07006149 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006150 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06006151 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006152 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04006153 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05306154 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04006155
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306156 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006157 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306158 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
6159 } else
adam radford9c915a82010-12-21 13:34:31 -08006160 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07006161
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006162 /*
adam radford0a770662011-02-24 20:56:12 -08006163 * Initialize MFI Firmware
6164 */
6165 if (megasas_init_fw(instance))
6166 goto fail_init_mfi;
6167
adam radford229fe472014-03-10 02:51:56 -07006168 if (instance->requestorId) {
6169 if (instance->PlasmaFW111) {
6170 instance->vf_affiliation_111 =
6171 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
6172 &instance->vf_affiliation_111_h);
6173 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006174 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006175 "memory for VF affiliation buffer\n");
6176 } else {
6177 instance->vf_affiliation =
6178 pci_alloc_consistent(pdev,
6179 (MAX_LOGICAL_DRIVES + 1) *
6180 sizeof(struct MR_LD_VF_AFFILIATION),
6181 &instance->vf_affiliation_h);
6182 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006183 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006184 "memory for VF affiliation buffer\n");
6185 }
6186 }
6187
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006188 /*
6189 * Store instance in PCI softstate
6190 */
6191 pci_set_drvdata(pdev, instance);
6192
6193 /*
6194 * Add this controller to megasas_mgmt_info structure so that it
6195 * can be exported to management applications
6196 */
6197 megasas_mgmt_info.count++;
6198 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6199 megasas_mgmt_info.max_index++;
6200
6201 /*
adam radford541f90b2011-05-11 18:34:29 -07006202 * Register with SCSI mid-layer
6203 */
6204 if (megasas_io_attach(instance))
6205 goto fail_io_attach;
6206
6207 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306208 /*
6209 * Trigger SCSI to scan our drives
6210 */
6211 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07006212
6213 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006214 * Initiate AEN (Asynchronous Event Notification)
6215 */
6216 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006217 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006218 goto fail_start_aen;
6219 }
6220
Adam Radford9ea81f82014-07-09 15:17:57 -07006221 /* Get current SR-IOV LD/VF affiliation */
6222 if (instance->requestorId)
6223 megasas_get_ld_vf_affiliation(instance, 1);
6224
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006225 return 0;
6226
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006227fail_start_aen:
6228fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006229 megasas_mgmt_info.count--;
6230 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
6231 megasas_mgmt_info.max_index--;
6232
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306233 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306234 megasas_destroy_irqs(instance);
6235
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306236 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08006237 megasas_release_fusion(instance);
6238 else
6239 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006240 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006241 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306242fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006243fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006244 if (instance->evt_detail)
6245 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6246 instance->evt_detail,
6247 instance->evt_detail_h);
6248
Sumit Saxena2216c302016-01-28 21:04:26 +05306249 if (instance->pd_info)
6250 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6251 instance->pd_info,
6252 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006253 if (instance->tgt_prop)
6254 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6255 instance->tgt_prop,
6256 instance->tgt_prop_h);
adam radfordeb1b1232011-02-24 20:55:56 -08006257 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006258 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6259 instance->producer_h);
6260 if (instance->consumer)
6261 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6262 instance->consumer_h);
6263 scsi_host_put(host);
6264
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006265fail_alloc_instance:
6266fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006267 pci_disable_device(pdev);
6268
6269 return -ENODEV;
6270}
6271
6272/**
6273 * megasas_flush_cache - Requests FW to flush all its caches
6274 * @instance: Adapter soft state
6275 */
6276static void megasas_flush_cache(struct megasas_instance *instance)
6277{
6278 struct megasas_cmd *cmd;
6279 struct megasas_dcmd_frame *dcmd;
6280
Sumit Saxena8a01a412016-01-28 21:04:32 +05306281 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006282 return;
6283
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006284 cmd = megasas_get_cmd(instance);
6285
6286 if (!cmd)
6287 return;
6288
6289 dcmd = &cmd->frame->dcmd;
6290
6291 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6292
6293 dcmd->cmd = MFI_CMD_DCMD;
6294 dcmd->cmd_status = 0x0;
6295 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306296 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006297 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006298 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006299 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306300 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006301 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6302
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306303 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6304 != DCMD_SUCCESS) {
6305 dev_err(&instance->pdev->dev,
6306 "return from %s %d\n", __func__, __LINE__);
6307 return;
6308 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006309
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306310 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006311}
6312
6313/**
6314 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6315 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006316 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006317 */
bo yang31ea7082007-11-07 12:09:50 -05006318static void megasas_shutdown_controller(struct megasas_instance *instance,
6319 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006320{
6321 struct megasas_cmd *cmd;
6322 struct megasas_dcmd_frame *dcmd;
6323
Sumit Saxena8a01a412016-01-28 21:04:32 +05306324 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006325 return;
6326
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006327 cmd = megasas_get_cmd(instance);
6328
6329 if (!cmd)
6330 return;
6331
6332 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306333 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306334 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006335 if (instance->map_update_cmd)
6336 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306337 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306338 if (instance->jbod_seq_cmd)
6339 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306340 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306341
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006342 dcmd = &cmd->frame->dcmd;
6343
6344 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6345
6346 dcmd->cmd = MFI_CMD_DCMD;
6347 dcmd->cmd_status = 0x0;
6348 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306349 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006350 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006351 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006352 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306353 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006354
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306355 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6356 != DCMD_SUCCESS) {
6357 dev_err(&instance->pdev->dev,
6358 "return from %s %d\n", __func__, __LINE__);
6359 return;
6360 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006361
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306362 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006363}
6364
Jiri Slaby33139b22008-05-01 17:56:02 +02006365#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006366/**
bo yangad84db22007-11-09 04:40:16 -05006367 * megasas_suspend - driver suspend entry point
6368 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006369 * @state: PCI power state to suspend routine
6370 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006371static int
bo yang31ea7082007-11-07 12:09:50 -05006372megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6373{
6374 struct Scsi_Host *host;
6375 struct megasas_instance *instance;
6376
6377 instance = pci_get_drvdata(pdev);
6378 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006379 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006380
adam radford229fe472014-03-10 02:51:56 -07006381 /* Shutdown SR-IOV heartbeat timer */
6382 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6383 del_timer_sync(&instance->sriov_heartbeat_timer);
6384
bo yang31ea7082007-11-07 12:09:50 -05006385 megasas_flush_cache(instance);
6386 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006387
6388 /* cancel the delayed work if this work still in queue */
6389 if (instance->ev != NULL) {
6390 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006391 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006392 instance->ev = NULL;
6393 }
6394
bo yang31ea7082007-11-07 12:09:50 -05006395 tasklet_kill(&instance->isr_tasklet);
6396
6397 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306398 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006399
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306400 megasas_destroy_irqs(instance);
6401
adam radfordc8e858f2011-10-08 18:15:13 -07006402 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006403 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006404
6405 pci_save_state(pdev);
6406 pci_disable_device(pdev);
6407
6408 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6409
6410 return 0;
6411}
6412
6413/**
6414 * megasas_resume- driver resume entry point
6415 * @pdev: PCI device structure
6416 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006417static int
bo yang31ea7082007-11-07 12:09:50 -05006418megasas_resume(struct pci_dev *pdev)
6419{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306420 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006421 struct Scsi_Host *host;
6422 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01006423 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05006424
6425 instance = pci_get_drvdata(pdev);
6426 host = instance->host;
6427 pci_set_power_state(pdev, PCI_D0);
6428 pci_enable_wake(pdev, PCI_D0, 0);
6429 pci_restore_state(pdev);
6430
6431 /*
6432 * PCI prepping: enable device set bus mastering and dma mask
6433 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006434 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006435
6436 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006437 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006438 return rval;
6439 }
6440
6441 pci_set_master(pdev);
6442
6443 if (megasas_set_dma_mask(pdev))
6444 goto fail_set_dma_mask;
6445
6446 /*
6447 * Initialize MFI Firmware
6448 */
6449
bo yang31ea7082007-11-07 12:09:50 -05006450 atomic_set(&instance->fw_outstanding, 0);
6451
6452 /*
6453 * We expect the FW state to be READY
6454 */
adam radford058a8fa2011-10-08 18:14:27 -07006455 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006456 goto fail_ready_state;
6457
adam radford3f1abce2011-05-11 18:33:47 -07006458 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01006459 if (instance->msix_vectors) {
6460 irq_flags = PCI_IRQ_MSIX;
6461 if (smp_affinity_enable)
6462 irq_flags |= PCI_IRQ_AFFINITY;
6463 }
6464 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6465 instance->msix_vectors ?
6466 instance->msix_vectors : 1, irq_flags);
6467 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02006468 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006469
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306470 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006471 megasas_reset_reply_desc(instance);
6472 if (megasas_ioc_init_fusion(instance)) {
6473 megasas_free_cmds(instance);
6474 megasas_free_cmds_fusion(instance);
6475 goto fail_init_mfi;
6476 }
6477 if (!megasas_get_map_info(instance))
6478 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306479 } else {
adam radford9c915a82010-12-21 13:34:31 -08006480 *instance->producer = 0;
6481 *instance->consumer = 0;
6482 if (megasas_issue_init_mfi(instance))
6483 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006484 }
bo yang31ea7082007-11-07 12:09:50 -05006485
adam radford9c915a82010-12-21 13:34:31 -08006486 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6487 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006488
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306489 if (instance->msix_vectors ?
6490 megasas_setup_irqs_msix(instance, 0) :
6491 megasas_setup_irqs_ioapic(instance))
6492 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006493
adam radford229fe472014-03-10 02:51:56 -07006494 /* Re-launch SR-IOV heartbeat timer */
6495 if (instance->requestorId) {
6496 if (!megasas_sriov_start_heartbeat(instance, 0))
6497 megasas_start_timer(instance,
6498 &instance->sriov_heartbeat_timer,
6499 megasas_sriov_heartbeat_handler,
6500 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306501 else {
adam radford229fe472014-03-10 02:51:56 -07006502 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306503 goto fail_init_mfi;
6504 }
adam radford229fe472014-03-10 02:51:56 -07006505 }
6506
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306507 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306508 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006509 instance->unload = 0;
6510
adam radford541f90b2011-05-11 18:34:29 -07006511 /*
6512 * Initiate AEN (Asynchronous Event Notification)
6513 */
6514 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006515 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006516
bo yang31ea7082007-11-07 12:09:50 -05006517 return 0;
6518
bo yang31ea7082007-11-07 12:09:50 -05006519fail_init_mfi:
6520 if (instance->evt_detail)
6521 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6522 instance->evt_detail,
6523 instance->evt_detail_h);
6524
Sumit Saxena2216c302016-01-28 21:04:26 +05306525 if (instance->pd_info)
6526 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6527 instance->pd_info,
6528 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006529 if (instance->tgt_prop)
6530 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6531 instance->tgt_prop,
6532 instance->tgt_prop_h);
bo yang31ea7082007-11-07 12:09:50 -05006533 if (instance->producer)
6534 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6535 instance->producer_h);
6536 if (instance->consumer)
6537 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6538 instance->consumer_h);
6539 scsi_host_put(host);
6540
6541fail_set_dma_mask:
6542fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006543fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006544
6545 pci_disable_device(pdev);
6546
6547 return -ENODEV;
6548}
Jiri Slaby33139b22008-05-01 17:56:02 +02006549#else
6550#define megasas_suspend NULL
6551#define megasas_resume NULL
6552#endif
bo yang31ea7082007-11-07 12:09:50 -05006553
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006554static inline int
6555megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6556{
6557 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6558 int i;
6559
6560 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6561 return 1;
6562
6563 for (i = 0; i < wait_time; i++) {
6564 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6565 break;
6566
6567 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6568 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6569
6570 msleep(1000);
6571 }
6572
6573 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6574 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6575 __func__);
6576 return 1;
6577 }
6578
6579 return 0;
6580}
6581
bo yang31ea7082007-11-07 12:09:50 -05006582/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006583 * megasas_detach_one - PCI hot"un"plug entry point
6584 * @pdev: PCI device structure
6585 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006586static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006587{
6588 int i;
6589 struct Scsi_Host *host;
6590 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006591 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306592 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006593
6594 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006595 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006596 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006597 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006598
adam radford229fe472014-03-10 02:51:56 -07006599 /* Shutdown SR-IOV heartbeat timer */
6600 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6601 del_timer_sync(&instance->sriov_heartbeat_timer);
6602
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306603 if (instance->fw_crash_state != UNAVAILABLE)
6604 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006605 scsi_remove_host(instance->host);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006606
6607 if (megasas_wait_for_adapter_operational(instance))
6608 goto skip_firing_dcmds;
6609
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006610 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006611 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006612
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006613skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006614 /* cancel the delayed work if this work still in queue*/
6615 if (instance->ev != NULL) {
6616 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006617 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006618 instance->ev = NULL;
6619 }
6620
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306621 /* cancel all wait events */
6622 wake_up_all(&instance->int_cmd_wait_q);
6623
Sumant Patro5d018ad2006-10-03 13:13:18 -07006624 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006625
6626 /*
6627 * Take the instance off the instance array. Note that we will not
6628 * decrement the max_index. We let this array be sparse array
6629 */
6630 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6631 if (megasas_mgmt_info.instance[i] == instance) {
6632 megasas_mgmt_info.count--;
6633 megasas_mgmt_info.instance[i] = NULL;
6634
6635 break;
6636 }
6637 }
6638
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306639 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006640
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306641 megasas_destroy_irqs(instance);
6642
adam radfordc8e858f2011-10-08 18:15:13 -07006643 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006644 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006645
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006646 if (instance->is_ventura) {
6647 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6648 kfree(fusion->stream_detect_by_ld[i]);
6649 kfree(fusion->stream_detect_by_ld);
6650 fusion->stream_detect_by_ld = NULL;
6651 }
6652
6653
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306654 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006655 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306656 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6657 (sizeof(struct MR_PD_CFG_SEQ) *
6658 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306659 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006660 if (fusion->ld_map[i])
6661 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306662 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006663 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306664 fusion->ld_map_phys[i]);
6665 if (fusion->ld_drv_map[i])
6666 free_pages((ulong)fusion->ld_drv_map[i],
6667 fusion->drv_map_pages);
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006668 if (fusion->pd_seq_sync[i])
6669 dma_free_coherent(&instance->pdev->dev,
6670 pd_seq_map_sz,
6671 fusion->pd_seq_sync[i],
6672 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306673 }
Shivasharan S5fc499b2017-02-10 00:59:17 -08006674 megasas_free_fusion_context(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306675 } else {
adam radford9c915a82010-12-21 13:34:31 -08006676 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006677 pci_free_consistent(pdev, sizeof(u32),
6678 instance->producer,
6679 instance->producer_h);
6680 pci_free_consistent(pdev, sizeof(u32),
6681 instance->consumer,
6682 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08006683 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006684
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306685 kfree(instance->ctrl_info);
6686
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05306687 if (instance->evt_detail)
6688 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6689 instance->evt_detail, instance->evt_detail_h);
Sumit Saxena2216c302016-01-28 21:04:26 +05306690 if (instance->pd_info)
6691 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6692 instance->pd_info,
6693 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006694 if (instance->tgt_prop)
6695 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6696 instance->tgt_prop,
6697 instance->tgt_prop_h);
adam radford229fe472014-03-10 02:51:56 -07006698 if (instance->vf_affiliation)
6699 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6700 sizeof(struct MR_LD_VF_AFFILIATION),
6701 instance->vf_affiliation,
6702 instance->vf_affiliation_h);
6703
6704 if (instance->vf_affiliation_111)
6705 pci_free_consistent(pdev,
6706 sizeof(struct MR_LD_VF_AFFILIATION_111),
6707 instance->vf_affiliation_111,
6708 instance->vf_affiliation_111_h);
6709
6710 if (instance->hb_host_mem)
6711 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6712 instance->hb_host_mem,
6713 instance->hb_host_mem_h);
6714
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306715 if (instance->crash_dump_buf)
6716 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6717 instance->crash_dump_buf, instance->crash_dump_h);
6718
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306719 if (instance->system_info_buf)
6720 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6721 instance->system_info_buf, instance->system_info_h);
6722
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006723 scsi_host_put(host);
6724
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006725 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006726}
6727
6728/**
6729 * megasas_shutdown - Shutdown entry point
6730 * @device: Generic device structure
6731 */
6732static void megasas_shutdown(struct pci_dev *pdev)
6733{
6734 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006735
Yang, Bo0c79e682009-10-06 14:47:35 -06006736 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006737
6738 if (megasas_wait_for_adapter_operational(instance))
6739 goto skip_firing_dcmds;
6740
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006741 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006742 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006743
6744skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306745 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306746 megasas_destroy_irqs(instance);
6747
adam radfordc8e858f2011-10-08 18:15:13 -07006748 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006749 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006750}
6751
6752/**
6753 * megasas_mgmt_open - char node "open" entry point
6754 */
6755static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6756{
6757 /*
6758 * Allow only those users with admin rights
6759 */
6760 if (!capable(CAP_SYS_ADMIN))
6761 return -EACCES;
6762
6763 return 0;
6764}
6765
6766/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006767 * megasas_mgmt_fasync - Async notifier registration from applications
6768 *
6769 * This function adds the calling process to a driver global queue. When an
6770 * event occurs, SIGIO will be sent to all processes in this queue.
6771 */
6772static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6773{
6774 int rc;
6775
Arjan van de Ven0b950672006-01-11 13:16:10 +01006776 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006777
6778 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6779
Arjan van de Ven0b950672006-01-11 13:16:10 +01006780 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006781
6782 if (rc >= 0) {
6783 /* For sanity check when we get ioctl */
6784 filep->private_data = filep;
6785 return 0;
6786 }
6787
6788 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6789
6790 return rc;
6791}
6792
6793/**
Yang, Boc3518832009-10-06 14:18:02 -06006794 * megasas_mgmt_poll - char node "poll" entry point
6795 * */
6796static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6797{
6798 unsigned int mask;
6799 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006800
Yang, Boc3518832009-10-06 14:18:02 -06006801 poll_wait(file, &megasas_poll_wait, wait);
6802 spin_lock_irqsave(&poll_aen_lock, flags);
6803 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006804 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006805 else
6806 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306807 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006808 spin_unlock_irqrestore(&poll_aen_lock, flags);
6809 return mask;
6810}
6811
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306812/*
6813 * megasas_set_crash_dump_params_ioctl:
6814 * Send CRASH_DUMP_MODE DCMD to all controllers
6815 * @cmd: MFI command frame
6816 */
6817
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006818static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306819{
6820 struct megasas_instance *local_instance;
6821 int i, error = 0;
6822 int crash_support;
6823
6824 crash_support = cmd->frame->dcmd.mbox.w[0];
6825
6826 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6827 local_instance = megasas_mgmt_info.instance[i];
6828 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05306829 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306830 MEGASAS_HBA_OPERATIONAL) &&
6831 !megasas_set_crash_dump_params(local_instance,
6832 crash_support)) {
6833 local_instance->crash_dump_app_support =
6834 crash_support;
6835 dev_info(&local_instance->pdev->dev,
6836 "Application firmware crash "
6837 "dump mode set success\n");
6838 error = 0;
6839 } else {
6840 dev_info(&local_instance->pdev->dev,
6841 "Application firmware crash "
6842 "dump mode set failed\n");
6843 error = -1;
6844 }
6845 }
6846 }
6847 return error;
6848}
6849
Yang, Boc3518832009-10-06 14:18:02 -06006850/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006851 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6852 * @instance: Adapter soft state
6853 * @argp: User's ioctl packet
6854 */
6855static int
6856megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6857 struct megasas_iocpacket __user * user_ioc,
6858 struct megasas_iocpacket *ioc)
6859{
6860 struct megasas_sge32 *kern_sge32;
6861 struct megasas_cmd *cmd;
6862 void *kbuff_arr[MAX_IOCTL_SGE];
6863 dma_addr_t buf_handle = 0;
6864 int error = 0, i;
6865 void *sense = NULL;
6866 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006867 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006868
6869 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6870
6871 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006872 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006873 ioc->sge_count, MAX_IOCTL_SGE);
6874 return -EINVAL;
6875 }
6876
6877 cmd = megasas_get_cmd(instance);
6878 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006879 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006880 return -ENOMEM;
6881 }
6882
6883 /*
6884 * User's IOCTL packet has 2 frames (maximum). Copy those two
6885 * frames into our cmd's frames. cmd->frame's context will get
6886 * overwritten when we copy from user's frames. So set that value
6887 * alone separately
6888 */
6889 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306890 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006891 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306892 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6893 MFI_FRAME_SGL64 |
6894 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006895
Shivasharan S95c06082017-02-10 00:59:23 -08006896 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_SHUTDOWN) {
6897 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
6898 megasas_return_cmd(instance, cmd);
6899 return -1;
6900 }
6901 }
6902
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306903 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6904 error = megasas_set_crash_dump_params_ioctl(cmd);
6905 megasas_return_cmd(instance, cmd);
6906 return error;
6907 }
6908
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006909 /*
6910 * The management interface between applications and the fw uses
6911 * MFI frames. E.g, RAID configuration changes, LD property changes
6912 * etc are accomplishes through different kinds of MFI frames. The
6913 * driver needs to care only about substituting user buffers with
6914 * kernel buffers in SGLs. The location of SGL is embedded in the
6915 * struct iocpacket itself.
6916 */
6917 kern_sge32 = (struct megasas_sge32 *)
6918 ((unsigned long)cmd->frame + ioc->sgl_off);
6919
6920 /*
6921 * For each user buffer, create a mirror buffer and copy in
6922 */
6923 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006924 if (!ioc->sgl[i].iov_len)
6925 continue;
6926
Sumant Patro9f35fa82007-02-14 12:55:45 -08006927 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006928 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006929 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006930 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006931 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6932 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006933 error = -ENOMEM;
6934 goto out;
6935 }
6936
6937 /*
6938 * We don't change the dma_coherent_mask, so
6939 * pci_alloc_consistent only returns 32bit addresses
6940 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306941 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6942 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006943
6944 /*
6945 * We created a kernel buffer corresponding to the
6946 * user buffer. Now copy in from the user buffer
6947 */
6948 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6949 (u32) (ioc->sgl[i].iov_len))) {
6950 error = -EFAULT;
6951 goto out;
6952 }
6953 }
6954
6955 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006956 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6957 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006958 if (!sense) {
6959 error = -ENOMEM;
6960 goto out;
6961 }
6962
6963 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006964 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306965 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006966 }
6967
6968 /*
6969 * Set the sync_cmd flag so that the ISR knows not to complete this
6970 * cmd to the SCSI mid-layer
6971 */
6972 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306973 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6974 cmd->sync_cmd = 0;
6975 dev_err(&instance->pdev->dev,
6976 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6977 __func__, __LINE__, cmd->frame->dcmd.opcode,
6978 cmd->cmd_status_drv);
6979 return -EBUSY;
6980 }
6981
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006982 cmd->sync_cmd = 0;
6983
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306984 if (instance->unload == 1) {
6985 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6986 "don't submit data to application\n");
6987 goto out;
6988 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006989 /*
6990 * copy out the kernel buffers to user buffers
6991 */
6992 for (i = 0; i < ioc->sge_count; i++) {
6993 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6994 ioc->sgl[i].iov_len)) {
6995 error = -EFAULT;
6996 goto out;
6997 }
6998 }
6999
7000 /*
7001 * copy out the sense
7002 */
7003 if (ioc->sense_len) {
7004 /*
bo yangb70a41e2008-03-18 03:13:06 -04007005 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007006 * sense buffer address
7007 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007008 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7009 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007010
Shivasharan S318aaef2017-02-10 00:59:22 -08007011 if (copy_to_user((void __user *)((unsigned long)
7012 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04007013 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007014 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007015 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007016 error = -EFAULT;
7017 goto out;
7018 }
7019 }
7020
7021 /*
7022 * copy the status codes returned by the fw
7023 */
7024 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7025 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007026 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007027 error = -EFAULT;
7028 }
7029
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007030out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007031 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007032 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007033 sense, sense_handle);
7034 }
7035
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007036 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007037 if (kbuff_arr[i]) {
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007038 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307039 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007040 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307041 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307042 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007043 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007044 }
7045
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307046 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007047 return error;
7048}
7049
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007050static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7051{
7052 struct megasas_iocpacket __user *user_ioc =
7053 (struct megasas_iocpacket __user *)arg;
7054 struct megasas_iocpacket *ioc;
7055 struct megasas_instance *instance;
7056 int error;
bo yang39a98552010-09-22 22:36:29 -04007057 int i;
7058 unsigned long flags;
7059 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007060
Markus Elfring709ab232016-08-21 10:39:04 +02007061 ioc = memdup_user(user_ioc, sizeof(*ioc));
7062 if (IS_ERR(ioc))
7063 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007064
7065 instance = megasas_lookup_instance(ioc->host_no);
7066 if (!instance) {
7067 error = -ENODEV;
7068 goto out_kfree_ioc;
7069 }
7070
adam radford229fe472014-03-10 02:51:56 -07007071 /* Adjust ioctl wait time for VF mode */
7072 if (instance->requestorId)
7073 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7074
7075 /* Block ioctls in VF mode */
7076 if (instance->requestorId && !allow_vf_ioctls) {
7077 error = -ENODEV;
7078 goto out_kfree_ioc;
7079 }
7080
Sumit Saxena8a01a412016-01-28 21:04:32 +05307081 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007082 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007083 error = -ENODEV;
7084 goto out_kfree_ioc;
7085 }
7086
7087 if (instance->unload == 1) {
7088 error = -ENODEV;
7089 goto out_kfree_ioc;
7090 }
7091
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007092 if (down_interruptible(&instance->ioctl_sem)) {
7093 error = -ERESTARTSYS;
7094 goto out_kfree_ioc;
7095 }
bo yang39a98552010-09-22 22:36:29 -04007096
7097 for (i = 0; i < wait_time; i++) {
7098
7099 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307100 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007101 spin_unlock_irqrestore(&instance->hba_lock, flags);
7102 break;
7103 }
7104 spin_unlock_irqrestore(&instance->hba_lock, flags);
7105
7106 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007107 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04007108 "for controller reset to finish\n");
7109 }
7110
7111 msleep(1000);
7112 }
7113
7114 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307115 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007116 spin_unlock_irqrestore(&instance->hba_lock, flags);
7117
Colin Ian King644da3c2016-11-12 16:25:24 +00007118 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007119 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007120 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007121 }
7122 spin_unlock_irqrestore(&instance->hba_lock, flags);
7123
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007124 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007125out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007126 up(&instance->ioctl_sem);
7127
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007128out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007129 kfree(ioc);
7130 return error;
7131}
7132
7133static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7134{
7135 struct megasas_instance *instance;
7136 struct megasas_aen aen;
7137 int error;
bo yang39a98552010-09-22 22:36:29 -04007138 int i;
7139 unsigned long flags;
7140 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007141
7142 if (file->private_data != file) {
7143 printk(KERN_DEBUG "megasas: fasync_helper was not "
7144 "called first\n");
7145 return -EINVAL;
7146 }
7147
7148 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7149 return -EFAULT;
7150
7151 instance = megasas_lookup_instance(aen.host_no);
7152
7153 if (!instance)
7154 return -ENODEV;
7155
Sumit Saxena8a01a412016-01-28 21:04:32 +05307156 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007157 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007158 }
7159
7160 if (instance->unload == 1) {
7161 return -ENODEV;
7162 }
7163
bo yang39a98552010-09-22 22:36:29 -04007164 for (i = 0; i < wait_time; i++) {
7165
7166 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307167 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007168 spin_unlock_irqrestore(&instance->hba_lock,
7169 flags);
7170 break;
7171 }
7172
7173 spin_unlock_irqrestore(&instance->hba_lock, flags);
7174
7175 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007176 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04007177 "controller reset to finish\n");
7178 }
7179
7180 msleep(1000);
7181 }
7182
7183 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307184 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007185 spin_unlock_irqrestore(&instance->hba_lock, flags);
Colin Ian King644da3c2016-11-12 16:25:24 +00007186 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007187 return -ENODEV;
7188 }
7189 spin_unlock_irqrestore(&instance->hba_lock, flags);
7190
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307191 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007192 error = megasas_register_aen(instance, aen.seq_num,
7193 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307194 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007195 return error;
7196}
7197
7198/**
7199 * megasas_mgmt_ioctl - char node ioctl entry point
7200 */
7201static long
7202megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7203{
7204 switch (cmd) {
7205 case MEGASAS_IOC_FIRMWARE:
7206 return megasas_mgmt_ioctl_fw(file, arg);
7207
7208 case MEGASAS_IOC_GET_AEN:
7209 return megasas_mgmt_ioctl_aen(file, arg);
7210 }
7211
7212 return -ENOTTY;
7213}
7214
7215#ifdef CONFIG_COMPAT
7216static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7217{
7218 struct compat_megasas_iocpacket __user *cioc =
7219 (struct compat_megasas_iocpacket __user *)arg;
7220 struct megasas_iocpacket __user *ioc =
7221 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7222 int i;
7223 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007224 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307225 u32 local_sense_off;
7226 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307227 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007228
Jeff Garzik83aabc12006-10-04 06:34:03 -04007229 if (clear_user(ioc, sizeof(*ioc)))
7230 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007231
7232 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7233 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7234 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7235 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7236 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7237 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7238 return -EFAULT;
7239
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007240 /*
7241 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7242 * sense_len is not null, so prepare the 64bit value under
7243 * the same condition.
7244 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307245 if (get_user(local_sense_off, &ioc->sense_off) ||
7246 get_user(local_sense_len, &ioc->sense_len) ||
7247 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307248 return -EFAULT;
7249
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307250 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007251 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307252 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007253 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307254 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007255 if (get_user(ptr, sense_cioc_ptr) ||
7256 put_user(compat_ptr(ptr), sense_ioc_ptr))
7257 return -EFAULT;
7258 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007259
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007260 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007261 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7262 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7263 copy_in_user(&ioc->sgl[i].iov_len,
7264 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7265 return -EFAULT;
7266 }
7267
7268 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7269
7270 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7271 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7272 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7273 return -EFAULT;
7274 }
7275 return error;
7276}
7277
7278static long
7279megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7280 unsigned long arg)
7281{
7282 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007283 case MEGASAS_IOC_FIRMWARE32:
7284 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007285 case MEGASAS_IOC_GET_AEN:
7286 return megasas_mgmt_ioctl_aen(file, arg);
7287 }
7288
7289 return -ENOTTY;
7290}
7291#endif
7292
7293/*
7294 * File operations structure for management interface
7295 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007296static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007297 .owner = THIS_MODULE,
7298 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007299 .fasync = megasas_mgmt_fasync,
7300 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06007301 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007302#ifdef CONFIG_COMPAT
7303 .compat_ioctl = megasas_mgmt_compat_ioctl,
7304#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02007305 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007306};
7307
7308/*
7309 * PCI hotplug support registration structure
7310 */
7311static struct pci_driver megasas_pci_driver = {
7312
7313 .name = "megaraid_sas",
7314 .id_table = megasas_pci_table,
7315 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007316 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05007317 .suspend = megasas_suspend,
7318 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007319 .shutdown = megasas_shutdown,
7320};
7321
7322/*
7323 * Sysfs driver attributes
7324 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007325static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007326{
7327 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7328 MEGASAS_VERSION);
7329}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007330static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007331
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007332static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307333{
7334 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7335 MEGASAS_RELDATE);
7336}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007337static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307338
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007339static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06007340{
7341 return sprintf(buf, "%u\n", support_poll_for_event);
7342}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007343static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007344
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007345static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06007346{
7347 return sprintf(buf, "%u\n", support_device_change);
7348}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007349static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06007350
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007351static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07007352{
bo yangad84db22007-11-09 04:40:16 -05007353 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007354}
7355
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007356static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
7357 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07007358{
7359 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007360
7361 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007362 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7363 retval = -EINVAL;
7364 }
7365 return retval;
7366}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007367static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05007368
Shivasharan Sb4a42212017-02-10 00:59:16 -08007369static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
7370{
7371 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
7372 scsi_remove_device(sdev);
7373 scsi_device_put(sdev);
7374}
7375
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007376static void
7377megasas_aen_polling(struct work_struct *work)
7378{
7379 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007380 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007381 struct megasas_instance *instance = ev->instance;
7382 union megasas_evt_class_locale class_locale;
7383 struct Scsi_Host *host;
7384 struct scsi_device *sdev1;
7385 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007386 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007387 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007388 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007389 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307390 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007391
7392 if (!instance) {
7393 printk(KERN_ERR "invalid instance!\n");
7394 kfree(ev);
7395 return;
7396 }
adam radford229fe472014-03-10 02:51:56 -07007397
7398 /* Adjust event workqueue thread wait time for VF mode */
7399 if (instance->requestorId)
7400 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7401
7402 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307403 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007404
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007405 instance->ev = NULL;
7406 host = instance->host;
7407 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307408 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007409
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307410 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307411
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007412 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307413 case MR_EVT_PD_REMOVED:
7414 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307415 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307416 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007417 break;
7418
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307419 case MR_EVT_LD_OFFLINE:
7420 case MR_EVT_CFG_CLEARED:
7421 case MR_EVT_LD_DELETED:
7422 case MR_EVT_LD_CREATED:
7423 if (!instance->requestorId ||
7424 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7425 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007426
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307427 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307428 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007429
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307430 break;
7431
7432 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7433 case MR_EVT_FOREIGN_CFG_IMPORTED:
7434 case MR_EVT_LD_STATE_CHANGE:
7435 dcmd_ret = megasas_get_pd_list(instance);
7436
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307437 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307438 break;
7439
7440 if (!instance->requestorId ||
7441 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7442 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7443
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307444 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307445 break;
7446
7447 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7448 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7449 instance->host->host_no);
7450 break;
7451
7452 case MR_EVT_CTRL_PROP_CHANGED:
7453 dcmd_ret = megasas_get_ctrl_info(instance);
7454 break;
7455 default:
7456 doscan = 0;
7457 break;
7458 }
7459 } else {
7460 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7461 mutex_unlock(&instance->reset_mutex);
7462 kfree(ev);
7463 return;
7464 }
7465
7466 mutex_unlock(&instance->reset_mutex);
7467
7468 if (doscan & SCAN_PD_CHANNEL) {
7469 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7470 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7471 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007472 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307473 if (instance->pd_list[pd_index].driveState ==
7474 MR_PD_STATE_SYSTEM) {
7475 if (!sdev1)
7476 scsi_add_device(host, i, j, 0);
7477 else
Yang, Boc9786842009-12-06 08:39:25 -07007478 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007479 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007480 if (sdev1)
7481 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007482 }
Yang, Boc9786842009-12-06 08:39:25 -07007483 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007484 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007485 }
7486
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307487 if (doscan & SCAN_VD_CHANNEL) {
7488 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7489 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7490 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7491 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7492 if (instance->ld_ids[ld_index] != 0xff) {
7493 if (!sdev1)
7494 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7495 else
7496 scsi_device_put(sdev1);
7497 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007498 if (sdev1)
7499 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007500 }
7501 }
7502 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007503 }
7504
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307505 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307506 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7507 else
7508 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007509
7510 /* Register AEN with FW for latest sequence number plus 1 */
7511 class_locale.members.reserved = 0;
7512 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7513 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307514
7515 if (instance->aen_cmd != NULL) {
7516 kfree(ev);
7517 return;
7518 }
7519
7520 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007521 error = megasas_register_aen(instance, seq_num,
7522 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007523 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307524 dev_err(&instance->pdev->dev,
7525 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007526
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307527 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007528 kfree(ev);
7529}
7530
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007531/**
7532 * megasas_init - Driver load entry point
7533 */
7534static int __init megasas_init(void)
7535{
7536 int rval;
7537
7538 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007539 * Booted in kdump kernel, minimize memory footprints by
7540 * disabling few features
7541 */
7542 if (reset_devices) {
7543 msix_vectors = 1;
7544 rdpq_enable = 0;
7545 dual_qdepth_disable = 1;
7546 }
7547
7548 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007549 * Announce driver version and other information
7550 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307551 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007552
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007553 spin_lock_init(&poll_aen_lock);
7554
Yang, Bo72c4fd32009-10-06 14:20:59 -06007555 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007556 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007557
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007558 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7559
7560 /*
7561 * Register character device node
7562 */
7563 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7564
7565 if (rval < 0) {
7566 printk(KERN_DEBUG "megasas: failed to open device node\n");
7567 return rval;
7568 }
7569
7570 megasas_mgmt_majorno = rval;
7571
7572 /*
7573 * Register ourselves as PCI hotplug module
7574 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007575 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007576
7577 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007578 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007579 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007580 }
7581
Jeff Garzik83aabc12006-10-04 06:34:03 -04007582 rval = driver_create_file(&megasas_pci_driver.driver,
7583 &driver_attr_version);
7584 if (rval)
7585 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007586
7587 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307588 &driver_attr_release_date);
7589 if (rval)
7590 goto err_dcf_rel_date;
7591
7592 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007593 &driver_attr_support_poll_for_event);
7594 if (rval)
7595 goto err_dcf_support_poll_for_event;
7596
Jeff Garzik83aabc12006-10-04 06:34:03 -04007597 rval = driver_create_file(&megasas_pci_driver.driver,
7598 &driver_attr_dbg_lvl);
7599 if (rval)
7600 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007601 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007602 &driver_attr_support_device_change);
7603 if (rval)
7604 goto err_dcf_support_device_change;
7605
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007606 return rval;
bo yangad84db22007-11-09 04:40:16 -05007607
Yang, Bo837f5fe2010-10-11 06:59:20 -06007608err_dcf_support_device_change:
7609 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007610 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007611err_dcf_dbg_lvl:
7612 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007613 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007614err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307615 driver_remove_file(&megasas_pci_driver.driver,
7616 &driver_attr_release_date);
7617err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007618 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7619err_dcf_attr_ver:
7620 pci_unregister_driver(&megasas_pci_driver);
7621err_pcidrv:
7622 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007623 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007624}
7625
7626/**
7627 * megasas_exit - Driver unload entry point
7628 */
7629static void __exit megasas_exit(void)
7630{
Sumant Patro658dced2006-10-03 13:09:14 -07007631 driver_remove_file(&megasas_pci_driver.driver,
7632 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007633 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007634 &driver_attr_support_poll_for_event);
7635 driver_remove_file(&megasas_pci_driver.driver,
7636 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307637 driver_remove_file(&megasas_pci_driver.driver,
7638 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007639 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007640
7641 pci_unregister_driver(&megasas_pci_driver);
7642 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7643}
7644
7645module_init(megasas_init);
7646module_exit(megasas_exit);