blob: 170de6282c80e4121741bc77caf5e203b68baa07 [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>
Shivasharan Sdef3e8d2017-08-23 04:47:03 -070052#include <linux/vmalloc.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040053
54#include <scsi/scsi.h>
55#include <scsi/scsi_cmnd.h>
56#include <scsi/scsi_device.h>
57#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070058#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080059#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040060#include "megaraid_sas.h"
61
bo yangad84db22007-11-09 04:40:16 -050062/*
Yang, Bo1fd10682010-10-12 07:18:50 -060063 * Number of sectors per IO command
64 * Will be set in megasas_init_mfi if user does not provide
65 */
66static unsigned int max_sectors;
67module_param_named(max_sectors, max_sectors, int, 0);
68MODULE_PARM_DESC(max_sectors,
69 "Maximum number of sectors per IO command");
70
adam radford80d9da92010-12-21 10:17:40 -080071static int msix_disable;
72module_param(msix_disable, int, S_IRUGO);
73MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
74
adam radford079eadd2012-10-01 19:26:59 -070075static unsigned int msix_vectors;
76module_param(msix_vectors, int, S_IRUGO);
77MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
78
adam radford229fe472014-03-10 02:51:56 -070079static int allow_vf_ioctls;
80module_param(allow_vf_ioctls, int, S_IRUGO);
81MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
82
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053083static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070084module_param(throttlequeuedepth, int, S_IRUGO);
85MODULE_PARM_DESC(throttlequeuedepth,
86 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
87
Sumit Saxenae3d178c2016-01-28 21:04:34 +053088unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070089module_param(resetwaittime, int, S_IRUGO);
90MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
91 "before resetting adapter. Default: 180");
92
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053093int smp_affinity_enable = 1;
94module_param(smp_affinity_enable, int, S_IRUGO);
95MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
96
Sumit Saxena179ac142016-01-28 21:04:28 +053097int rdpq_enable = 1;
98module_param(rdpq_enable, int, S_IRUGO);
99MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
100
Sumit Saxena308ec452016-01-28 21:04:30 +0530101unsigned int dual_qdepth_disable;
102module_param(dual_qdepth_disable, int, S_IRUGO);
103MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
104
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530105unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
106module_param(scmd_timeout, int, S_IRUGO);
107MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
108
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400109MODULE_LICENSE("GPL");
110MODULE_VERSION(MEGASAS_VERSION);
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +0530111MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
112MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400113
adam radford058a8fa2011-10-08 18:14:27 -0700114int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400115static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700116static int megasas_ld_list_query(struct megasas_instance *instance,
117 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400118static int megasas_issue_init_mfi(struct megasas_instance *instance);
119static int megasas_register_aen(struct megasas_instance *instance,
120 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800121static void megasas_get_pd_info(struct megasas_instance *instance,
122 struct scsi_device *sdev);
Shivasharan S96188a82017-02-10 00:59:11 -0800123static int megasas_get_target_prop(struct megasas_instance *instance,
124 struct scsi_device *sdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400125/*
126 * PCI ID table for all supported controllers
127 */
128static struct pci_device_id megasas_pci_table[] = {
129
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
131 /* xscale IOP */
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
133 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
135 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
137 /* gen2*/
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
139 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
141 /* skinny*/
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
143 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
145 /* xscale IOP, vega */
146 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
147 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
149 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
151 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
153 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
155 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
157 /* Intruder */
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
159 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500162 /* VENTURA */
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
Shivasharan S754f1ba2017-10-19 02:48:49 -0700164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200169 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400170};
171
172MODULE_DEVICE_TABLE(pci, megasas_pci_table);
173
174static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700175struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400176static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100177static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400178
Yang, Boc3518832009-10-06 14:18:02 -0600179static int megasas_poll_wait_aen;
180static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600181static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800182u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600183static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700184
Yang, Boc3518832009-10-06 14:18:02 -0600185/* define lock for aen poll */
186spinlock_t poll_aen_lock;
187
adam radford9c915a82010-12-21 13:34:31 -0800188void
bo yang7343eb62007-11-09 04:35:44 -0500189megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
190 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800191static u32
192megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
193static int
194megasas_adp_reset_gen2(struct megasas_instance *instance,
195 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800196static irqreturn_t megasas_isr(int irq, void *devp);
197static u32
198megasas_init_adapter_mfi(struct megasas_instance *instance);
199u32
200megasas_build_and_issue_cmd(struct megasas_instance *instance,
201 struct scsi_cmnd *scmd);
202static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800203int
adam radford229fe472014-03-10 02:51:56 -0700204wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
205 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800206void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700207static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
208 int initial);
adam radfordcd50ba82010-12-21 10:23:23 -0800209
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800210void
adam radfordcd50ba82010-12-21 10:23:23 -0800211megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
212{
213 instance->instancet->fire_cmd(instance,
214 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800215 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800216}
bo yang7343eb62007-11-09 04:35:44 -0500217
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400218/**
219 * megasas_get_cmd - Get a command from the free pool
220 * @instance: Adapter soft state
221 *
222 * Returns a free command from the pool
223 */
adam radford9c915a82010-12-21 13:34:31 -0800224struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400225 *instance)
226{
227 unsigned long flags;
228 struct megasas_cmd *cmd = NULL;
229
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530230 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400231
232 if (!list_empty(&instance->cmd_pool)) {
233 cmd = list_entry((&instance->cmd_pool)->next,
234 struct megasas_cmd, list);
235 list_del_init(&cmd->list);
236 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500237 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400238 }
239
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530240 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400241 return cmd;
242}
243
244/**
245 * megasas_return_cmd - Return a cmd to free command pool
246 * @instance: Adapter soft state
247 * @cmd: Command packet to be returned to free command pool
248 */
Andi Kleen68b43742017-05-08 15:58:53 -0700249void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400250megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
251{
252 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530253 u32 blk_tags;
254 struct megasas_cmd_fusion *cmd_fusion;
255 struct fusion_context *fusion = instance->ctrl_context;
256
257 /* This flag is used only for fusion adapter.
258 * Wait for Interrupt for Polled mode DCMD
259 */
260 if (cmd->flags & DRV_DCMD_POLLED_MODE)
261 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400262
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530263 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400264
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530265 if (fusion) {
266 blk_tags = instance->max_scsi_cmds + cmd->index;
267 cmd_fusion = fusion->cmd_list[blk_tags];
268 megasas_return_cmd_fusion(instance, cmd_fusion);
269 }
270 cmd->scmd = NULL;
271 cmd->frame_count = 0;
272 cmd->flags = 0;
Shivasharan S21c34002017-02-10 00:59:28 -0800273 memset(cmd->frame, 0, instance->mfi_frame_size);
274 cmd->frame->io.context = cpu_to_le32(cmd->index);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530275 if (!fusion && reset_devices)
276 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
277 list_add(&cmd->list, (&instance->cmd_pool)->next);
278
279 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
280
281}
Sumant Patro1341c932006-01-25 12:02:40 -0800282
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530283static const char *
284format_timestamp(uint32_t timestamp)
285{
286 static char buffer[32];
287
288 if ((timestamp & 0xff000000) == 0xff000000)
289 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
290 0x00ffffff);
291 else
292 snprintf(buffer, sizeof(buffer), "%us", timestamp);
293 return buffer;
294}
295
296static const char *
297format_class(int8_t class)
298{
299 static char buffer[6];
300
301 switch (class) {
302 case MFI_EVT_CLASS_DEBUG:
303 return "debug";
304 case MFI_EVT_CLASS_PROGRESS:
305 return "progress";
306 case MFI_EVT_CLASS_INFO:
307 return "info";
308 case MFI_EVT_CLASS_WARNING:
309 return "WARN";
310 case MFI_EVT_CLASS_CRITICAL:
311 return "CRIT";
312 case MFI_EVT_CLASS_FATAL:
313 return "FATAL";
314 case MFI_EVT_CLASS_DEAD:
315 return "DEAD";
316 default:
317 snprintf(buffer, sizeof(buffer), "%d", class);
318 return buffer;
319 }
320}
321
322/**
323 * megasas_decode_evt: Decode FW AEN event and print critical event
324 * for information.
325 * @instance: Adapter soft state
326 */
327static void
328megasas_decode_evt(struct megasas_instance *instance)
329{
330 struct megasas_evt_detail *evt_detail = instance->evt_detail;
331 union megasas_evt_class_locale class_locale;
332 class_locale.word = le32_to_cpu(evt_detail->cl.word);
333
334 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
335 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
336 le32_to_cpu(evt_detail->seq_num),
337 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
338 (class_locale.members.locale),
339 format_class(class_locale.members.class),
340 evt_detail->description);
341}
342
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400343/**
adam radford0d490162010-12-14 19:17:17 -0800344* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800345* (deviceid : 1064R, PERC5) controllers
346*/
347
348/**
349 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400350 * @regs: MFI register set
351 */
352static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530353megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400354{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530355 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500356
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530357 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400358 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400359
360 /* Dummy readl to force pci flush */
361 readl(&regs->outbound_intr_mask);
362}
363
364/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700365 * megasas_disable_intr_xscale -Disables interrupt
366 * @regs: MFI register set
367 */
368static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530369megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700370{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530371 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700372 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500373
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530374 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700375 writel(mask, &regs->outbound_intr_mask);
376 /* Dummy readl to force pci flush */
377 readl(&regs->outbound_intr_mask);
378}
379
380/**
Sumant Patro1341c932006-01-25 12:02:40 -0800381 * megasas_read_fw_status_reg_xscale - returns the current FW status value
382 * @regs: MFI register set
383 */
384static u32
385megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
386{
387 return readl(&(regs)->outbound_msg_0);
388}
389/**
390 * megasas_clear_interrupt_xscale - Check & clear interrupt
391 * @regs: MFI register set
392 */
adam radford0d490162010-12-14 19:17:17 -0800393static int
Sumant Patro1341c932006-01-25 12:02:40 -0800394megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
395{
396 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400397 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500398
Sumant Patro1341c932006-01-25 12:02:40 -0800399 /*
400 * Check if it is our interrupt
401 */
402 status = readl(&regs->outbound_intr_status);
403
bo yang39a98552010-09-22 22:36:29 -0400404 if (status & MFI_OB_INTR_STATUS_MASK)
405 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
406 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
407 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800408
409 /*
410 * Clear the interrupt by writing back the same value
411 */
bo yang39a98552010-09-22 22:36:29 -0400412 if (mfiStatus)
413 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800414
Yang, Bo06f579d2008-08-10 12:42:37 -0700415 /* Dummy readl to force pci flush */
416 readl(&regs->outbound_intr_status);
417
bo yang39a98552010-09-22 22:36:29 -0400418 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800419}
420
421/**
422 * megasas_fire_cmd_xscale - Sends command to the FW
423 * @frame_phys_addr : Physical address of cmd
424 * @frame_count : Number of frames for the command
425 * @regs : MFI register set
426 */
adam radford0d490162010-12-14 19:17:17 -0800427static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600428megasas_fire_cmd_xscale(struct megasas_instance *instance,
429 dma_addr_t frame_phys_addr,
430 u32 frame_count,
431 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800432{
bo yang39a98552010-09-22 22:36:29 -0400433 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500434
bo yang39a98552010-09-22 22:36:29 -0400435 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800436 writel((frame_phys_addr >> 3)|(frame_count),
437 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400438 spin_unlock_irqrestore(&instance->hba_lock, flags);
439}
440
441/**
442 * megasas_adp_reset_xscale - For controller reset
443 * @regs: MFI register set
444 */
445static int
446megasas_adp_reset_xscale(struct megasas_instance *instance,
447 struct megasas_register_set __iomem *regs)
448{
449 u32 i;
450 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500451
bo yang39a98552010-09-22 22:36:29 -0400452 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
453
454 for (i = 0; i < 3; i++)
455 msleep(1000); /* sleep for 3 secs */
456 pcidata = 0;
457 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500458 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400459 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500460 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400461 pcidata &= ~0x2;
462 pci_write_config_dword(instance->pdev,
463 MFI_1068_PCSR_OFFSET, pcidata);
464
465 for (i = 0; i < 2; i++)
466 msleep(1000); /* need to wait 2 secs again */
467
468 pcidata = 0;
469 pci_read_config_dword(instance->pdev,
470 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500471 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400472 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500473 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400474 pcidata = 0;
475 pci_write_config_dword(instance->pdev,
476 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
477 }
478 }
479 return 0;
480}
481
482/**
483 * megasas_check_reset_xscale - For controller reset check
484 * @regs: MFI register set
485 */
486static int
487megasas_check_reset_xscale(struct megasas_instance *instance,
488 struct megasas_register_set __iomem *regs)
489{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530490 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530491 (le32_to_cpu(*instance->consumer) ==
492 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400493 return 1;
bo yang39a98552010-09-22 22:36:29 -0400494 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800495}
496
497static struct megasas_instance_template megasas_instance_template_xscale = {
498
499 .fire_cmd = megasas_fire_cmd_xscale,
500 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700501 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800502 .clear_intr = megasas_clear_intr_xscale,
503 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400504 .adp_reset = megasas_adp_reset_xscale,
505 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800506 .service_isr = megasas_isr,
507 .tasklet = megasas_complete_cmd_dpc,
508 .init_adapter = megasas_init_adapter_mfi,
509 .build_and_issue_cmd = megasas_build_and_issue_cmd,
510 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800511};
512
513/**
adam radford0d490162010-12-14 19:17:17 -0800514* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800515* to xscale (deviceid : 1064R, PERC5) controllers
516*/
517
518/**
adam radford0d490162010-12-14 19:17:17 -0800519* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500520* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800521*/
522
523/**
524 * megasas_enable_intr_ppc - Enables interrupts
525 * @regs: MFI register set
526 */
527static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530528megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800529{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530530 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500531
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530532 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800533 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800534
bo yang39a98552010-09-22 22:36:29 -0400535 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800536
537 /* Dummy readl to force pci flush */
538 readl(&regs->outbound_intr_mask);
539}
540
541/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700542 * megasas_disable_intr_ppc - Disable interrupt
543 * @regs: MFI register set
544 */
545static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530546megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700547{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530548 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700549 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500550
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530551 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700552 writel(mask, &regs->outbound_intr_mask);
553 /* Dummy readl to force pci flush */
554 readl(&regs->outbound_intr_mask);
555}
556
557/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800558 * megasas_read_fw_status_reg_ppc - returns the current FW status value
559 * @regs: MFI register set
560 */
561static u32
562megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
563{
564 return readl(&(regs)->outbound_scratch_pad);
565}
566
567/**
568 * megasas_clear_interrupt_ppc - Check & clear interrupt
569 * @regs: MFI register set
570 */
adam radford0d490162010-12-14 19:17:17 -0800571static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800572megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
573{
adam radford3cc6851f92011-05-11 18:34:52 -0700574 u32 status, mfiStatus = 0;
575
Sumant Patrof9876f02006-02-03 15:34:35 -0800576 /*
577 * Check if it is our interrupt
578 */
579 status = readl(&regs->outbound_intr_status);
580
adam radford3cc6851f92011-05-11 18:34:52 -0700581 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
582 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
583
584 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
585 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800586
587 /*
588 * Clear the interrupt by writing back the same value
589 */
590 writel(status, &regs->outbound_doorbell_clear);
591
Yang, Bo06f579d2008-08-10 12:42:37 -0700592 /* Dummy readl to force pci flush */
593 readl(&regs->outbound_doorbell_clear);
594
adam radford3cc6851f92011-05-11 18:34:52 -0700595 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800596}
adam radford3cc6851f92011-05-11 18:34:52 -0700597
Sumant Patrof9876f02006-02-03 15:34:35 -0800598/**
599 * megasas_fire_cmd_ppc - Sends command to the FW
600 * @frame_phys_addr : Physical address of cmd
601 * @frame_count : Number of frames for the command
602 * @regs : MFI register set
603 */
adam radford0d490162010-12-14 19:17:17 -0800604static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600605megasas_fire_cmd_ppc(struct megasas_instance *instance,
606 dma_addr_t frame_phys_addr,
607 u32 frame_count,
608 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800609{
bo yang39a98552010-09-22 22:36:29 -0400610 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500611
bo yang39a98552010-09-22 22:36:29 -0400612 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800613 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800614 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400615 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800616}
617
bo yang39a98552010-09-22 22:36:29 -0400618/**
bo yang39a98552010-09-22 22:36:29 -0400619 * megasas_check_reset_ppc - For controller reset check
620 * @regs: MFI register set
621 */
622static int
623megasas_check_reset_ppc(struct megasas_instance *instance,
624 struct megasas_register_set __iomem *regs)
625{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530626 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700627 return 1;
628
bo yang39a98552010-09-22 22:36:29 -0400629 return 0;
630}
adam radford3cc6851f92011-05-11 18:34:52 -0700631
Sumant Patrof9876f02006-02-03 15:34:35 -0800632static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800633
Sumant Patrof9876f02006-02-03 15:34:35 -0800634 .fire_cmd = megasas_fire_cmd_ppc,
635 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700636 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800637 .clear_intr = megasas_clear_intr_ppc,
638 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700639 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400640 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800641 .service_isr = megasas_isr,
642 .tasklet = megasas_complete_cmd_dpc,
643 .init_adapter = megasas_init_adapter_mfi,
644 .build_and_issue_cmd = megasas_build_and_issue_cmd,
645 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800646};
647
648/**
Yang, Bo87911122009-10-06 14:31:54 -0600649 * megasas_enable_intr_skinny - Enables interrupts
650 * @regs: MFI register set
651 */
652static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530653megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600654{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530655 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500656
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530657 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600658 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
659
660 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
661
662 /* Dummy readl to force pci flush */
663 readl(&regs->outbound_intr_mask);
664}
665
666/**
667 * megasas_disable_intr_skinny - Disables interrupt
668 * @regs: MFI register set
669 */
670static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530671megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600672{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530673 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600674 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500675
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530676 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600677 writel(mask, &regs->outbound_intr_mask);
678 /* Dummy readl to force pci flush */
679 readl(&regs->outbound_intr_mask);
680}
681
682/**
683 * megasas_read_fw_status_reg_skinny - returns the current FW status value
684 * @regs: MFI register set
685 */
686static u32
687megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
688{
689 return readl(&(regs)->outbound_scratch_pad);
690}
691
692/**
693 * megasas_clear_interrupt_skinny - Check & clear interrupt
694 * @regs: MFI register set
695 */
696static int
697megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
698{
699 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800700 u32 mfiStatus = 0;
701
Yang, Bo87911122009-10-06 14:31:54 -0600702 /*
703 * Check if it is our interrupt
704 */
705 status = readl(&regs->outbound_intr_status);
706
707 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400708 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600709 }
710
711 /*
adam radfordebf054b2011-02-24 20:57:15 -0800712 * Check if it is our interrupt
713 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600714 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800715 MFI_STATE_FAULT) {
716 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
717 } else
718 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
719
720 /*
Yang, Bo87911122009-10-06 14:31:54 -0600721 * Clear the interrupt by writing back the same value
722 */
723 writel(status, &regs->outbound_intr_status);
724
725 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500726 * dummy read to flush PCI
727 */
Yang, Bo87911122009-10-06 14:31:54 -0600728 readl(&regs->outbound_intr_status);
729
adam radfordebf054b2011-02-24 20:57:15 -0800730 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600731}
732
733/**
734 * megasas_fire_cmd_skinny - Sends command to the FW
735 * @frame_phys_addr : Physical address of cmd
736 * @frame_count : Number of frames for the command
737 * @regs : MFI register set
738 */
739static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600740megasas_fire_cmd_skinny(struct megasas_instance *instance,
741 dma_addr_t frame_phys_addr,
742 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600743 struct megasas_register_set __iomem *regs)
744{
Yang, Bo0c79e682009-10-06 14:47:35 -0600745 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500746
bo yang39a98552010-09-22 22:36:29 -0400747 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530748 writel(upper_32_bits(frame_phys_addr),
749 &(regs)->inbound_high_queue_port);
750 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
751 &(regs)->inbound_low_queue_port);
Tomas Henzlb99dbe52016-02-01 15:12:04 +0100752 mmiowb();
bo yang39a98552010-09-22 22:36:29 -0400753 spin_unlock_irqrestore(&instance->hba_lock, flags);
754}
755
756/**
bo yang39a98552010-09-22 22:36:29 -0400757 * megasas_check_reset_skinny - For controller reset check
758 * @regs: MFI register set
759 */
760static int
761megasas_check_reset_skinny(struct megasas_instance *instance,
762 struct megasas_register_set __iomem *regs)
763{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530764 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700765 return 1;
766
bo yang39a98552010-09-22 22:36:29 -0400767 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600768}
769
770static struct megasas_instance_template megasas_instance_template_skinny = {
771
772 .fire_cmd = megasas_fire_cmd_skinny,
773 .enable_intr = megasas_enable_intr_skinny,
774 .disable_intr = megasas_disable_intr_skinny,
775 .clear_intr = megasas_clear_intr_skinny,
776 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800777 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400778 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800779 .service_isr = megasas_isr,
780 .tasklet = megasas_complete_cmd_dpc,
781 .init_adapter = megasas_init_adapter_mfi,
782 .build_and_issue_cmd = megasas_build_and_issue_cmd,
783 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600784};
785
786
787/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700788* The following functions are defined for gen2 (deviceid : 0x78 0x79)
789* controllers
790*/
791
792/**
793 * megasas_enable_intr_gen2 - Enables interrupts
794 * @regs: MFI register set
795 */
796static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530797megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700798{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530799 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500800
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530801 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700802 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
803
804 /* write ~0x00000005 (4 & 1) to the intr mask*/
805 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
806
807 /* Dummy readl to force pci flush */
808 readl(&regs->outbound_intr_mask);
809}
810
811/**
812 * megasas_disable_intr_gen2 - Disables interrupt
813 * @regs: MFI register set
814 */
815static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530816megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700817{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530818 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700819 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500820
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530821 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700822 writel(mask, &regs->outbound_intr_mask);
823 /* Dummy readl to force pci flush */
824 readl(&regs->outbound_intr_mask);
825}
826
827/**
828 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
829 * @regs: MFI register set
830 */
831static u32
832megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
833{
834 return readl(&(regs)->outbound_scratch_pad);
835}
836
837/**
838 * megasas_clear_interrupt_gen2 - Check & clear interrupt
839 * @regs: MFI register set
840 */
841static int
842megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
843{
844 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400845 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500846
Yang, Bo6610a6b2008-08-10 12:42:38 -0700847 /*
848 * Check if it is our interrupt
849 */
850 status = readl(&regs->outbound_intr_status);
851
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530852 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400853 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
854 }
855 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
856 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
857 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700858
859 /*
860 * Clear the interrupt by writing back the same value
861 */
bo yang39a98552010-09-22 22:36:29 -0400862 if (mfiStatus)
863 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700864
865 /* Dummy readl to force pci flush */
866 readl(&regs->outbound_intr_status);
867
bo yang39a98552010-09-22 22:36:29 -0400868 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700869}
870/**
871 * megasas_fire_cmd_gen2 - Sends command to the FW
872 * @frame_phys_addr : Physical address of cmd
873 * @frame_count : Number of frames for the command
874 * @regs : MFI register set
875 */
876static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600877megasas_fire_cmd_gen2(struct megasas_instance *instance,
878 dma_addr_t frame_phys_addr,
879 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700880 struct megasas_register_set __iomem *regs)
881{
bo yang39a98552010-09-22 22:36:29 -0400882 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500883
bo yang39a98552010-09-22 22:36:29 -0400884 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700885 writel((frame_phys_addr | (frame_count<<1))|1,
886 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400887 spin_unlock_irqrestore(&instance->hba_lock, flags);
888}
889
890/**
891 * megasas_adp_reset_gen2 - For controller reset
892 * @regs: MFI register set
893 */
894static int
895megasas_adp_reset_gen2(struct megasas_instance *instance,
896 struct megasas_register_set __iomem *reg_set)
897{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500898 u32 retry = 0 ;
899 u32 HostDiag;
900 u32 __iomem *seq_offset = &reg_set->seq_offset;
901 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400902
adam radfordebf054b2011-02-24 20:57:15 -0800903 if (instance->instancet == &megasas_instance_template_skinny) {
904 seq_offset = &reg_set->fusion_seq_offset;
905 hostdiag_offset = &reg_set->fusion_host_diag;
906 }
907
908 writel(0, seq_offset);
909 writel(4, seq_offset);
910 writel(0xb, seq_offset);
911 writel(2, seq_offset);
912 writel(7, seq_offset);
913 writel(0xd, seq_offset);
914
bo yang39a98552010-09-22 22:36:29 -0400915 msleep(1000);
916
adam radfordebf054b2011-02-24 20:57:15 -0800917 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400918
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500919 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400920 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800921 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500922 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400923 retry, HostDiag);
924
925 if (retry++ >= 100)
926 return 1;
927
928 }
929
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500930 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400931
adam radfordebf054b2011-02-24 20:57:15 -0800932 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400933
934 ssleep(10);
935
adam radfordebf054b2011-02-24 20:57:15 -0800936 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500937 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400938 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800939 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500940 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400941 retry, HostDiag);
942
943 if (retry++ >= 1000)
944 return 1;
945
946 }
947 return 0;
948}
949
950/**
951 * megasas_check_reset_gen2 - For controller reset check
952 * @regs: MFI register set
953 */
954static int
955megasas_check_reset_gen2(struct megasas_instance *instance,
956 struct megasas_register_set __iomem *regs)
957{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530958 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600959 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600960
bo yang39a98552010-09-22 22:36:29 -0400961 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700962}
963
964static struct megasas_instance_template megasas_instance_template_gen2 = {
965
966 .fire_cmd = megasas_fire_cmd_gen2,
967 .enable_intr = megasas_enable_intr_gen2,
968 .disable_intr = megasas_disable_intr_gen2,
969 .clear_intr = megasas_clear_intr_gen2,
970 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400971 .adp_reset = megasas_adp_reset_gen2,
972 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800973 .service_isr = megasas_isr,
974 .tasklet = megasas_complete_cmd_dpc,
975 .init_adapter = megasas_init_adapter_mfi,
976 .build_and_issue_cmd = megasas_build_and_issue_cmd,
977 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700978};
979
980/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800981* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400982* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800983*/
984
adam radford9c915a82010-12-21 13:34:31 -0800985/*
986 * Template added for TB (Fusion)
987 */
988extern struct megasas_instance_template megasas_instance_template_fusion;
989
Sumant Patrof9876f02006-02-03 15:34:35 -0800990/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991 * megasas_issue_polled - Issues a polling command
992 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800993 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400994 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530995 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996 */
adam radford9c915a82010-12-21 13:34:31 -0800997int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
999{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001000 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1001
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301002 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301003 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001004
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001005 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301006 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1007 __func__, __LINE__);
1008 return DCMD_NOT_FIRED;
1009 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001010
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001011 instance->instancet->issue_dcmd(instance, cmd);
1012
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301013 return wait_and_poll(instance, cmd, instance->requestorId ?
1014 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001015}
1016
1017/**
1018 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1019 * @instance: Adapter soft state
1020 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301021 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001022 *
1023 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001024 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001025 * Used to issue ioctl commands.
1026 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301027int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301029 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001030{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301031 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301032 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001033
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001034 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301035 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1036 __func__, __LINE__);
1037 return DCMD_NOT_FIRED;
1038 }
1039
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001040 instance->instancet->issue_dcmd(instance, cmd);
1041
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301042 if (timeout) {
1043 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301044 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301045 if (!ret) {
1046 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1047 __func__, __LINE__);
1048 return DCMD_TIMEOUT;
1049 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301050 } else
1051 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301052 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001053
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301054 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301055 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001056}
1057
1058/**
1059 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1060 * @instance: Adapter soft state
1061 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301062 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001063 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301064 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001065 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001066 * cmd and waits for return status.
1067 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001068 */
1069static int
1070megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301071 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001072{
1073 struct megasas_cmd *cmd;
1074 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301075 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001076
1077 cmd = megasas_get_cmd(instance);
1078
1079 if (!cmd)
1080 return -1;
1081
1082 abort_fr = &cmd->frame->abort;
1083
1084 /*
1085 * Prepare and issue the abort frame
1086 */
1087 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301088 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301089 abort_fr->flags = cpu_to_le16(0);
1090 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1091 abort_fr->abort_mfi_phys_addr_lo =
1092 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1093 abort_fr->abort_mfi_phys_addr_hi =
1094 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001095
1096 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301097 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001098
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001099 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301100 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1101 __func__, __LINE__);
1102 return DCMD_NOT_FIRED;
1103 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001104
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001105 instance->instancet->issue_dcmd(instance, cmd);
1106
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301107 if (timeout) {
1108 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301109 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301110 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301111 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1112 __func__, __LINE__);
1113 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301114 }
1115 } else
1116 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301117 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301118
bo yang39a98552010-09-22 22:36:29 -04001119 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001120
1121 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301122 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1123 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001124}
1125
1126/**
1127 * megasas_make_sgl32 - Prepares 32-bit SGL
1128 * @instance: Adapter soft state
1129 * @scp: SCSI command from the mid-layer
1130 * @mfi_sgl: SGL to be filled in
1131 *
1132 * If successful, this function returns the number of SG elements. Otherwise,
1133 * it returnes -1.
1134 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001135static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001136megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1137 union megasas_sgl *mfi_sgl)
1138{
1139 int i;
1140 int sge_count;
1141 struct scatterlist *os_sgl;
1142
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001143 sge_count = scsi_dma_map(scp);
1144 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001145
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001146 if (sge_count) {
1147 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301148 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1149 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001150 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001151 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001152 return sge_count;
1153}
1154
1155/**
1156 * megasas_make_sgl64 - Prepares 64-bit SGL
1157 * @instance: Adapter soft state
1158 * @scp: SCSI command from the mid-layer
1159 * @mfi_sgl: SGL to be filled in
1160 *
1161 * If successful, this function returns the number of SG elements. Otherwise,
1162 * it returnes -1.
1163 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001164static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1166 union megasas_sgl *mfi_sgl)
1167{
1168 int i;
1169 int sge_count;
1170 struct scatterlist *os_sgl;
1171
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001172 sge_count = scsi_dma_map(scp);
1173 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001174
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001175 if (sge_count) {
1176 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301177 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1178 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001179 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001180 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001181 return sge_count;
1182}
1183
Yang, Bof4c9a132009-10-06 14:43:28 -06001184/**
1185 * megasas_make_sgl_skinny - Prepares IEEE SGL
1186 * @instance: Adapter soft state
1187 * @scp: SCSI command from the mid-layer
1188 * @mfi_sgl: SGL to be filled in
1189 *
1190 * If successful, this function returns the number of SG elements. Otherwise,
1191 * it returnes -1.
1192 */
1193static int
1194megasas_make_sgl_skinny(struct megasas_instance *instance,
1195 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1196{
1197 int i;
1198 int sge_count;
1199 struct scatterlist *os_sgl;
1200
1201 sge_count = scsi_dma_map(scp);
1202
1203 if (sge_count) {
1204 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301205 mfi_sgl->sge_skinny[i].length =
1206 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001207 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301208 cpu_to_le64(sg_dma_address(os_sgl));
1209 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001210 }
1211 }
1212 return sge_count;
1213}
1214
Sumant Patrob1df99d2006-10-03 12:40:47 -07001215 /**
1216 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001217 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001218 * @sge_count : number of sg elements
1219 *
1220 * Returns the number of frames required for numnber of sge's (sge_count)
1221 */
1222
Yang, Bof4c9a132009-10-06 14:43:28 -06001223static u32 megasas_get_frame_count(struct megasas_instance *instance,
1224 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001225{
1226 int num_cnt;
1227 int sge_bytes;
1228 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001229 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001230
1231 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1232 sizeof(struct megasas_sge32);
1233
Yang, Bof4c9a132009-10-06 14:43:28 -06001234 if (instance->flag_ieee) {
1235 sge_sz = sizeof(struct megasas_sge_skinny);
1236 }
1237
Sumant Patrob1df99d2006-10-03 12:40:47 -07001238 /*
bo yangd532dbe2008-03-17 03:36:43 -04001239 * Main frame can contain 2 SGEs for 64-bit SGLs and
1240 * 3 SGEs for 32-bit SGLs for ldio &
1241 * 1 SGEs for 64-bit SGLs and
1242 * 2 SGEs for 32-bit SGLs for pthru frame
1243 */
1244 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001245 if (instance->flag_ieee == 1) {
1246 num_cnt = sge_count - 1;
1247 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001248 num_cnt = sge_count - 1;
1249 else
1250 num_cnt = sge_count - 2;
1251 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001252 if (instance->flag_ieee == 1) {
1253 num_cnt = sge_count - 1;
1254 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001255 num_cnt = sge_count - 2;
1256 else
1257 num_cnt = sge_count - 3;
1258 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001259
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001260 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001261 sge_bytes = sge_sz * num_cnt;
1262
1263 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1264 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1265 }
1266 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001267 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001268
1269 if (frame_count > 7)
1270 frame_count = 8;
1271 return frame_count;
1272}
1273
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001274/**
1275 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1276 * @instance: Adapter soft state
1277 * @scp: SCSI command
1278 * @cmd: Command to be prepared in
1279 *
1280 * This function prepares CDB commands. These are typcially pass-through
1281 * commands to the devices.
1282 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001283static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001284megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1285 struct megasas_cmd *cmd)
1286{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001287 u32 is_logical;
1288 u32 device_id;
1289 u16 flags = 0;
1290 struct megasas_pthru_frame *pthru;
1291
Shivasharan S3cabd162017-02-10 00:59:05 -08001292 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301293 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001294 pthru = (struct megasas_pthru_frame *)cmd->frame;
1295
1296 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1297 flags = MFI_FRAME_DIR_WRITE;
1298 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1299 flags = MFI_FRAME_DIR_READ;
1300 else if (scp->sc_data_direction == PCI_DMA_NONE)
1301 flags = MFI_FRAME_DIR_NONE;
1302
Yang, Bof4c9a132009-10-06 14:43:28 -06001303 if (instance->flag_ieee == 1) {
1304 flags |= MFI_FRAME_IEEE;
1305 }
1306
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001307 /*
1308 * Prepare the DCDB frame
1309 */
1310 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1311 pthru->cmd_status = 0x0;
1312 pthru->scsi_status = 0x0;
1313 pthru->target_id = device_id;
1314 pthru->lun = scp->device->lun;
1315 pthru->cdb_len = scp->cmd_len;
1316 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001317 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301318 pthru->flags = cpu_to_le16(flags);
1319 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001320
1321 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1322
1323 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001324 * If the command is for the tape device, set the
1325 * pthru timeout to the os layer timeout value.
1326 */
Yang, Bo8d568252009-10-06 14:12:21 -06001327 if (scp->device->type == TYPE_TAPE) {
1328 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301329 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001330 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301331 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001332 }
1333
1334 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001335 * Construct SGL
1336 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001337 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301338 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001339 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1340 &pthru->sgl);
1341 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301342 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001343 pthru->sge_count = megasas_make_sgl64(instance, scp,
1344 &pthru->sgl);
1345 } else
1346 pthru->sge_count = megasas_make_sgl32(instance, scp,
1347 &pthru->sgl);
1348
Yang, Bobdc6fb82009-12-06 08:30:19 -07001349 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001350 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001351 pthru->sge_count);
1352 return 0;
1353 }
1354
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001355 /*
1356 * Sense info specific
1357 */
1358 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301359 pthru->sense_buf_phys_addr_hi =
1360 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1361 pthru->sense_buf_phys_addr_lo =
1362 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001363
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001364 /*
1365 * Compute the total number of frames this command consumes. FW uses
1366 * this number to pull sufficient number of frames from host memory.
1367 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001368 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001369 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001370
1371 return cmd->frame_count;
1372}
1373
1374/**
1375 * megasas_build_ldio - Prepares IOs to logical devices
1376 * @instance: Adapter soft state
1377 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001378 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001379 *
1380 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1381 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001382static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001383megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1384 struct megasas_cmd *cmd)
1385{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001386 u32 device_id;
1387 u8 sc = scp->cmnd[0];
1388 u16 flags = 0;
1389 struct megasas_io_frame *ldio;
1390
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301391 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001392 ldio = (struct megasas_io_frame *)cmd->frame;
1393
1394 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1395 flags = MFI_FRAME_DIR_WRITE;
1396 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1397 flags = MFI_FRAME_DIR_READ;
1398
Yang, Bof4c9a132009-10-06 14:43:28 -06001399 if (instance->flag_ieee == 1) {
1400 flags |= MFI_FRAME_IEEE;
1401 }
1402
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001403 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001404 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001405 */
1406 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1407 ldio->cmd_status = 0x0;
1408 ldio->scsi_status = 0x0;
1409 ldio->target_id = device_id;
1410 ldio->timeout = 0;
1411 ldio->reserved_0 = 0;
1412 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301413 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001414 ldio->start_lba_hi = 0;
1415 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1416
1417 /*
1418 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1419 */
1420 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301421 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1422 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1423 ((u32) scp->cmnd[2] << 8) |
1424 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001425
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301426 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001427 }
1428
1429 /*
1430 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1431 */
1432 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301433 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1434 ((u32) scp->cmnd[7] << 8));
1435 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1436 ((u32) scp->cmnd[3] << 16) |
1437 ((u32) scp->cmnd[4] << 8) |
1438 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001439 }
1440
1441 /*
1442 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1443 */
1444 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301445 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1446 ((u32) scp->cmnd[7] << 16) |
1447 ((u32) scp->cmnd[8] << 8) |
1448 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001449
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301450 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1451 ((u32) scp->cmnd[3] << 16) |
1452 ((u32) scp->cmnd[4] << 8) |
1453 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001454 }
1455
1456 /*
1457 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1458 */
1459 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301460 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1461 ((u32) scp->cmnd[11] << 16) |
1462 ((u32) scp->cmnd[12] << 8) |
1463 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301465 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1466 ((u32) scp->cmnd[7] << 16) |
1467 ((u32) scp->cmnd[8] << 8) |
1468 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001469
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301470 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1471 ((u32) scp->cmnd[3] << 16) |
1472 ((u32) scp->cmnd[4] << 8) |
1473 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001474
1475 }
1476
1477 /*
1478 * Construct SGL
1479 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001480 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301481 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001482 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1483 &ldio->sgl);
1484 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301485 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001486 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1487 } else
1488 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1489
Yang, Bobdc6fb82009-12-06 08:30:19 -07001490 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001491 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001492 ldio->sge_count);
1493 return 0;
1494 }
1495
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001496 /*
1497 * Sense info specific
1498 */
1499 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1500 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301501 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001502
Sumant Patrob1df99d2006-10-03 12:40:47 -07001503 /*
1504 * Compute the total number of frames this command consumes. FW uses
1505 * this number to pull sufficient number of frames from host memory.
1506 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001507 cmd->frame_count = megasas_get_frame_count(instance,
1508 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001509
1510 return cmd->frame_count;
1511}
1512
1513/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301514 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1515 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001516 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001517 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001518 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301519inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001520{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301521 int ret;
1522
Sumant Patrocb59aa62006-01-25 11:53:25 -08001523 switch (cmd->cmnd[0]) {
1524 case READ_10:
1525 case WRITE_10:
1526 case READ_12:
1527 case WRITE_12:
1528 case READ_6:
1529 case WRITE_6:
1530 case READ_16:
1531 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001532 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301533 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1534 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001535 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001536 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301537 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001538 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301539 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001540}
1541
Sumant Patro658dced2006-10-03 13:09:14 -07001542 /**
1543 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001544 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001545 * @instance: Adapter soft state
1546 */
1547static inline void
1548megasas_dump_pending_frames(struct megasas_instance *instance)
1549{
1550 struct megasas_cmd *cmd;
1551 int i,n;
1552 union megasas_sgl *mfi_sgl;
1553 struct megasas_io_frame *ldio;
1554 struct megasas_pthru_frame *pthru;
1555 u32 sgcount;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001556 u16 max_cmd = instance->max_fw_cmds;
Sumant Patro658dced2006-10-03 13:09:14 -07001557
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001558 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1559 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 -07001560 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001561 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 -07001562 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001563 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 -07001564
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001565 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001566 for (i = 0; i < max_cmd; i++) {
1567 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001568 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001569 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001570 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 +05301571 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001572 ldio = (struct megasas_io_frame *)cmd->frame;
1573 mfi_sgl = &ldio->sgl;
1574 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001575 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 +05301576 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1577 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1578 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1579 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001580 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001581 pthru = (struct megasas_pthru_frame *) cmd->frame;
1582 mfi_sgl = &pthru->sgl;
1583 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001584 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 +05301585 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1586 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1587 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1588 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001589 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001590 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1591 for (n = 0; n < sgcount; n++) {
1592 if (IS_DMA64)
1593 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1594 le32_to_cpu(mfi_sgl->sge64[n].length),
1595 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1596 else
1597 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1598 le32_to_cpu(mfi_sgl->sge32[n].length),
1599 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001600 }
1601 }
Sumant Patro658dced2006-10-03 13:09:14 -07001602 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001603 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001604 for (i = 0; i < max_cmd; i++) {
1605
1606 cmd = instance->cmd_list[i];
1607
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001608 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001609 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001610 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001611 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001612}
1613
adam radfordcd50ba82010-12-21 10:23:23 -08001614u32
1615megasas_build_and_issue_cmd(struct megasas_instance *instance,
1616 struct scsi_cmnd *scmd)
1617{
1618 struct megasas_cmd *cmd;
1619 u32 frame_count;
1620
1621 cmd = megasas_get_cmd(instance);
1622 if (!cmd)
1623 return SCSI_MLQUEUE_HOST_BUSY;
1624
1625 /*
1626 * Logical drive command
1627 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301628 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001629 frame_count = megasas_build_ldio(instance, scmd, cmd);
1630 else
1631 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1632
1633 if (!frame_count)
1634 goto out_return_cmd;
1635
1636 cmd->scmd = scmd;
1637 scmd->SCp.ptr = (char *)cmd;
1638
1639 /*
1640 * Issue the command to the FW
1641 */
1642 atomic_inc(&instance->fw_outstanding);
1643
1644 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1645 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001646
1647 return 0;
1648out_return_cmd:
1649 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301650 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001651}
1652
1653
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001654/**
1655 * megasas_queue_command - Queue entry point
1656 * @scmd: SCSI command to be queued
1657 * @done: Callback entry point
1658 */
1659static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301660megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001661{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001662 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301663 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001664
1665 instance = (struct megasas_instance *)
1666 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001667
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301668 if (instance->unload == 1) {
1669 scmd->result = DID_NO_CONNECT << 16;
1670 scmd->scsi_done(scmd);
1671 return 0;
1672 }
1673
bo yang39a98552010-09-22 22:36:29 -04001674 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001675 return SCSI_MLQUEUE_HOST_BUSY;
1676
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301677
adam radford229fe472014-03-10 02:51:56 -07001678 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301679 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001680 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001681 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001682 return SCSI_MLQUEUE_HOST_BUSY;
1683 } else {
adam radford229fe472014-03-10 02:51:56 -07001684 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301685 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001686 return 0;
1687 }
1688 }
1689
Sumit Saxena8a01a412016-01-28 21:04:32 +05301690 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001691 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301692 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301693 return 0;
1694 }
1695
Sumit Saxena18365b12016-01-28 21:04:25 +05301696 mr_device_priv_data = scmd->device->hostdata;
1697 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301698 scmd->result = DID_NO_CONNECT << 16;
1699 scmd->scsi_done(scmd);
1700 return 0;
1701 }
1702
Sumit Saxena8a01a412016-01-28 21:04:32 +05301703 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001704 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001705
Sumit Saxena8a01a412016-01-28 21:04:32 +05301706 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301707 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301708
bo yang39a98552010-09-22 22:36:29 -04001709
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001710 scmd->result = 0;
1711
Shivasharan S3cabd162017-02-10 00:59:05 -08001712 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301713 (scmd->device->id >= instance->fw_supported_vd_count ||
1714 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001715 scmd->result = DID_BAD_TARGET << 16;
1716 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001717 }
1718
Shivasharan S3cabd162017-02-10 00:59:05 -08001719 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1720 MEGASAS_IS_LOGICAL(scmd->device) &&
1721 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001722 scmd->result = DID_OK << 16;
1723 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001724 }
1725
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301726 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001727
Sumant Patrocb59aa62006-01-25 11:53:25 -08001728 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301729 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001730 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001731}
1732
Yang, Bo044833b2009-10-06 14:33:06 -06001733static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1734{
1735 int i;
1736
1737 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1738
1739 if ((megasas_mgmt_info.instance[i]) &&
1740 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1741 return megasas_mgmt_info.instance[i];
1742 }
1743
1744 return NULL;
1745}
1746
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301747/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001748* megasas_set_dynamic_target_properties -
1749* Device property set by driver may not be static and it is required to be
1750* updated after OCR
1751*
1752* set tm_capable.
1753* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301754*
1755* @sdev: OS provided scsi device
1756*
1757* Returns void
1758*/
Shivasharan S15dd0382017-02-10 00:59:10 -08001759void megasas_set_dynamic_target_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301760{
Shivasharan S15dd0382017-02-10 00:59:10 -08001761 u16 pd_index = 0, ld;
1762 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301763 struct megasas_instance *instance;
1764 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301765 struct MR_PRIV_DEVICE *mr_device_priv_data;
1766 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301767 struct MR_LD_RAID *raid;
1768 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1769
1770 instance = megasas_lookup_instance(sdev->host->host_no);
1771 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301772 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301773
Shivasharan Sed981b82017-02-10 00:59:06 -08001774 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301775 return;
1776
Shivasharan Sed981b82017-02-10 00:59:06 -08001777 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301778 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1779 + sdev->id;
1780 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1781 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001782 if (ld >= instance->fw_supported_vd_count)
1783 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301784 raid = MR_LdRaidGet(ld, local_map_ptr);
1785
1786 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001787 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001788
Sumit Saxena18365b12016-01-28 21:04:25 +05301789 mr_device_priv_data->is_tm_capable =
1790 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001791 } else if (instance->use_seqnum_jbod_fp) {
1792 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001793 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001794 pd_sync = (void *)fusion->pd_seq_sync
1795 [(instance->pd_seq_map_id - 1) & 1];
1796 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001797 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301798 }
1799}
1800
Shivasharan S15dd0382017-02-10 00:59:10 -08001801/*
1802 * megasas_set_nvme_device_properties -
1803 * set nomerges=2
1804 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1805 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1806 *
1807 * MR firmware provides value in KB. Caller of this function converts
1808 * kb into bytes.
1809 *
1810 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1811 * MR firmware provides value 128 as (32 * 4K) = 128K.
1812 *
1813 * @sdev: scsi device
1814 * @max_io_size: maximum io transfer size
1815 *
1816 */
1817static inline void
1818megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301819{
Sumit Saxena2216c302016-01-28 21:04:26 +05301820 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001821 u32 mr_nvme_pg_size;
1822
1823 instance = (struct megasas_instance *)sdev->host->hostdata;
1824 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1825 MR_DEFAULT_NVME_PAGE_SIZE);
1826
1827 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1828
1829 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1830 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1831}
1832
1833
1834/*
1835 * megasas_set_static_target_properties -
1836 * Device property set by driver are static and it is not required to be
1837 * updated after OCR.
1838 *
1839 * set io timeout
1840 * set device queue depth
1841 * set nvme device properties. see - megasas_set_nvme_device_properties
1842 *
1843 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001844 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001845 */
Shivasharan S96188a82017-02-10 00:59:11 -08001846static void megasas_set_static_target_properties(struct scsi_device *sdev,
1847 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001848{
1849 u16 target_index = 0;
1850 u8 interface_type;
1851 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1852 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001853 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001854 struct megasas_instance *instance;
1855 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301856
1857 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001858 mr_device_priv_data = sdev->hostdata;
1859 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301860
Shivasharan S15dd0382017-02-10 00:59:10 -08001861 /*
1862 * The RAID firmware may require extended timeouts.
1863 */
1864 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301865
Shivasharan S15dd0382017-02-10 00:59:10 -08001866 target_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05301867
Shivasharan S15dd0382017-02-10 00:59:10 -08001868 switch (interface_type) {
1869 case SAS_PD:
1870 device_qd = MEGASAS_SAS_QD;
1871 break;
1872 case SATA_PD:
1873 device_qd = MEGASAS_SATA_QD;
1874 break;
1875 case NVME_PD:
1876 device_qd = MEGASAS_NVME_QD;
1877 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301878 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001879
Shivasharan S96188a82017-02-10 00:59:11 -08001880 if (is_target_prop) {
1881 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1882 if (tgt_device_qd &&
1883 (tgt_device_qd <= instance->host->can_queue))
1884 device_qd = tgt_device_qd;
1885
1886 /* max_io_size_kb will be set to non zero for
1887 * nvme based vd and syspd.
1888 */
1889 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1890 }
1891
Shivasharan S15dd0382017-02-10 00:59:10 -08001892 if (instance->nvme_page_size && max_io_size_kb)
1893 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1894
1895 scsi_change_queue_depth(sdev, device_qd);
1896
Sumit Saxena2216c302016-01-28 21:04:26 +05301897}
1898
Sumit Saxena18365b12016-01-28 21:04:25 +05301899
Christoph Hellwig147aab62006-02-17 12:13:48 +01001900static int megasas_slave_configure(struct scsi_device *sdev)
1901{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301902 u16 pd_index = 0;
1903 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08001904 int ret_target_prop = DCMD_FAILED;
1905 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301906
1907 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001908 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001909 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301910 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1911 sdev->id;
1912 if (instance->pd_list[pd_index].driveState !=
1913 MR_PD_STATE_SYSTEM)
1914 return -ENXIO;
1915 }
1916 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301917
Shivasharan S15dd0382017-02-10 00:59:10 -08001918 mutex_lock(&instance->hba_mutex);
1919 /* Send DCMD to Firmware and cache the information */
1920 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
1921 megasas_get_pd_info(instance, sdev);
1922
Shivasharan S96188a82017-02-10 00:59:11 -08001923 /* Some ventura firmware may not have instance->nvme_page_size set.
1924 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
1925 */
1926 if ((instance->tgt_prop) && (instance->nvme_page_size))
1927 ret_target_prop = megasas_get_target_prop(instance, sdev);
1928
1929 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
1930 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08001931
1932 mutex_unlock(&instance->hba_mutex);
1933
1934 /* This sdev property may change post OCR */
1935 megasas_set_dynamic_target_properties(sdev);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301936
Yang, Bo044833b2009-10-06 14:33:06 -06001937 return 0;
1938}
1939
1940static int megasas_slave_alloc(struct scsi_device *sdev)
1941{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001942 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001943 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301944 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001945
Yang, Bo044833b2009-10-06 14:33:06 -06001946 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08001947 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06001948 /*
1949 * Open the OS scan to the SYSTEM PD
1950 */
1951 pd_index =
1952 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1953 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08001954 if ((instance->pd_list_not_supported ||
1955 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301956 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301957 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001958 }
1959 return -ENXIO;
1960 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301961
1962scan_target:
1963 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1964 GFP_KERNEL);
1965 if (!mr_device_priv_data)
1966 return -ENOMEM;
1967 sdev->hostdata = mr_device_priv_data;
Shivasharan S49524b32017-03-10 03:22:13 -08001968
1969 atomic_set(&mr_device_priv_data->r1_ldio_hint,
1970 instance->r1_ldio_hint_default);
Christoph Hellwig147aab62006-02-17 12:13:48 +01001971 return 0;
1972}
1973
Sumit Saxena18365b12016-01-28 21:04:25 +05301974static void megasas_slave_destroy(struct scsi_device *sdev)
1975{
1976 kfree(sdev->hostdata);
1977 sdev->hostdata = NULL;
1978}
1979
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301980/*
1981* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1982* kill adapter
1983* @instance: Adapter soft state
1984*
1985*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301986static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301987{
1988 int i;
1989 struct megasas_cmd *cmd_mfi;
1990 struct megasas_cmd_fusion *cmd_fusion;
1991 struct fusion_context *fusion = instance->ctrl_context;
1992
1993 /* Find all outstanding ioctls */
1994 if (fusion) {
1995 for (i = 0; i < instance->max_fw_cmds; i++) {
1996 cmd_fusion = fusion->cmd_list[i];
1997 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1998 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1999 if (cmd_mfi->sync_cmd &&
Shivasharan Seb3fe262017-08-23 04:47:04 -07002000 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2001 cmd_mfi->frame->hdr.cmd_status =
2002 MFI_STAT_WRONG_STATE;
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302003 megasas_complete_cmd(instance,
2004 cmd_mfi, DID_OK);
Shivasharan Seb3fe262017-08-23 04:47:04 -07002005 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302006 }
2007 }
2008 } else {
2009 for (i = 0; i < instance->max_fw_cmds; i++) {
2010 cmd_mfi = instance->cmd_list[i];
2011 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2012 MFI_CMD_ABORT)
2013 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2014 }
2015 }
2016}
2017
2018
adam radford9c915a82010-12-21 13:34:31 -08002019void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002020{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302021 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302022 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302023 /* Wait 1 second to ensure IO or ioctls in build have posted */
2024 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002025 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302026 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07002027 (instance->adapter_type != MFI_SERIES)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002028 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07002029 /* Flush */
2030 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302031 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002032 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002033 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302034 writel(MFI_STOP_ADP,
2035 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002036 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302037 /* Complete outstanding ioctls when adapter is killed */
2038 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002039}
2040
2041 /**
2042 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2043 * restored to max value
2044 * @instance: Adapter soft state
2045 *
2046 */
2047void
2048megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2049{
2050 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302051
adam radford9c915a82010-12-21 13:34:31 -08002052 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002053 && time_after(jiffies, instance->last_time + 5 * HZ)
2054 && atomic_read(&instance->fw_outstanding) <
2055 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002056
2057 spin_lock_irqsave(instance->host->host_lock, flags);
2058 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002059
Sumit Saxena308ec452016-01-28 21:04:30 +05302060 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002061 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002062 }
2063}
2064
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002065/**
bo yang7343eb62007-11-09 04:35:44 -05002066 * megasas_complete_cmd_dpc - Returns FW's controller structure
2067 * @instance_addr: Address of adapter soft state
2068 *
2069 * Tasklet to complete cmds
2070 */
2071static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2072{
2073 u32 producer;
2074 u32 consumer;
2075 u32 context;
2076 struct megasas_cmd *cmd;
2077 struct megasas_instance *instance =
2078 (struct megasas_instance *)instance_addr;
2079 unsigned long flags;
2080
2081 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302082 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002083 return;
2084
2085 spin_lock_irqsave(&instance->completion_lock, flags);
2086
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302087 producer = le32_to_cpu(*instance->producer);
2088 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002089
2090 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302091 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002092 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002093 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002094 context);
2095 BUG();
2096 }
bo yang7343eb62007-11-09 04:35:44 -05002097
2098 cmd = instance->cmd_list[context];
2099
2100 megasas_complete_cmd(instance, cmd, DID_OK);
2101
2102 consumer++;
2103 if (consumer == (instance->max_fw_cmds + 1)) {
2104 consumer = 0;
2105 }
2106 }
2107
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302108 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002109
2110 spin_unlock_irqrestore(&instance->completion_lock, flags);
2111
2112 /*
2113 * Check if we can restore can_queue
2114 */
adam radford9c915a82010-12-21 13:34:31 -08002115 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002116}
2117
adam radford229fe472014-03-10 02:51:56 -07002118/**
2119 * megasas_start_timer - Initializes a timer object
2120 * @instance: Adapter soft state
2121 * @timer: timer object to be initialized
2122 * @fn: timer function
2123 * @interval: time interval between timer function call
2124 *
2125 */
2126void megasas_start_timer(struct megasas_instance *instance,
2127 struct timer_list *timer,
2128 void *fn, unsigned long interval)
2129{
2130 init_timer(timer);
2131 timer->expires = jiffies + interval;
2132 timer->data = (unsigned long)instance;
2133 timer->function = fn;
2134 add_timer(timer);
2135}
2136
Yang, Bo707e09b2010-10-12 07:20:27 -06002137static void
2138megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2139
2140static void
2141process_fw_state_change_wq(struct work_struct *work);
2142
2143void megasas_do_ocr(struct megasas_instance *instance)
2144{
2145 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2146 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2147 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302148 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002149 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302150 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302151 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002152 instance->issuepend_done = 0;
2153
2154 atomic_set(&instance->fw_outstanding, 0);
2155 megasas_internal_reset_defer_cmds(instance);
2156 process_fw_state_change_wq(&instance->work_init);
2157}
2158
Adam Radford4cbfea82014-07-09 15:17:56 -07002159static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2160 int initial)
adam radford229fe472014-03-10 02:51:56 -07002161{
2162 struct megasas_cmd *cmd;
2163 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002164 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002165 dma_addr_t new_affiliation_111_h;
2166 int ld, retval = 0;
2167 u8 thisVf;
2168
2169 cmd = megasas_get_cmd(instance);
2170
2171 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002172 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2173 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002174 instance->host->host_no);
2175 return -ENOMEM;
2176 }
2177
2178 dcmd = &cmd->frame->dcmd;
2179
Adam Radford4cbfea82014-07-09 15:17:56 -07002180 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002181 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2182 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002183 megasas_return_cmd(instance, cmd);
2184 return -ENOMEM;
2185 }
2186
2187 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002188 memset(instance->vf_affiliation_111, 0,
2189 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002190 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002191 new_affiliation_111 =
2192 pci_alloc_consistent(instance->pdev,
2193 sizeof(struct MR_LD_VF_AFFILIATION_111),
2194 &new_affiliation_111_h);
2195 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002196 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2197 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002198 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002199 megasas_return_cmd(instance, cmd);
2200 return -ENOMEM;
2201 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002202 memset(new_affiliation_111, 0,
2203 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002204 }
2205
2206 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2207
2208 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302209 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002210 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302211 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002212 dcmd->timeout = 0;
2213 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302214 dcmd->data_xfer_len =
2215 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2216 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002217
Adam Radford4cbfea82014-07-09 15:17:56 -07002218 if (initial)
2219 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302220 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002221 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302222 dcmd->sgl.sge32[0].phys_addr =
2223 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002224
Christoph Hellwig2213a462015-04-23 16:33:54 +05302225 dcmd->sgl.sge32[0].length = cpu_to_le32(
2226 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002227
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002228 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002229 "scsi%d\n", instance->host->host_no);
2230
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302231 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002232 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2233 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002234 dcmd->cmd_status, instance->host->host_no);
2235 retval = 1; /* Do a scan if we couldn't get affiliation */
2236 goto out;
2237 }
2238
2239 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002240 thisVf = new_affiliation_111->thisVf;
2241 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2242 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2243 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002244 dev_warn(&instance->pdev->dev, "SR-IOV: "
2245 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002246 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002247 memcpy(instance->vf_affiliation_111,
2248 new_affiliation_111,
2249 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002250 retval = 1;
2251 goto out;
2252 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002253 }
2254out:
2255 if (new_affiliation_111) {
2256 pci_free_consistent(instance->pdev,
2257 sizeof(struct MR_LD_VF_AFFILIATION_111),
2258 new_affiliation_111,
2259 new_affiliation_111_h);
2260 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302261
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302262 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002263
2264 return retval;
2265}
2266
2267static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2268 int initial)
2269{
2270 struct megasas_cmd *cmd;
2271 struct megasas_dcmd_frame *dcmd;
2272 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2273 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2274 dma_addr_t new_affiliation_h;
2275 int i, j, retval = 0, found = 0, doscan = 0;
2276 u8 thisVf;
2277
2278 cmd = megasas_get_cmd(instance);
2279
2280 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002281 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2282 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002283 instance->host->host_no);
2284 return -ENOMEM;
2285 }
2286
2287 dcmd = &cmd->frame->dcmd;
2288
2289 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002290 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2291 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002292 megasas_return_cmd(instance, cmd);
2293 return -ENOMEM;
2294 }
2295
2296 if (initial)
2297 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2298 sizeof(struct MR_LD_VF_AFFILIATION));
2299 else {
2300 new_affiliation =
2301 pci_alloc_consistent(instance->pdev,
2302 (MAX_LOGICAL_DRIVES + 1) *
2303 sizeof(struct MR_LD_VF_AFFILIATION),
2304 &new_affiliation_h);
2305 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002306 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2307 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002308 instance->host->host_no);
2309 megasas_return_cmd(instance, cmd);
2310 return -ENOMEM;
2311 }
2312 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2313 sizeof(struct MR_LD_VF_AFFILIATION));
2314 }
2315
2316 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2317
2318 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302319 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002320 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302321 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002322 dcmd->timeout = 0;
2323 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302324 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2325 sizeof(struct MR_LD_VF_AFFILIATION));
2326 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002327
2328 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302329 dcmd->sgl.sge32[0].phys_addr =
2330 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002331 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302332 dcmd->sgl.sge32[0].phys_addr =
2333 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002334
Christoph Hellwig2213a462015-04-23 16:33:54 +05302335 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2336 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002337
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002338 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002339 "scsi%d\n", instance->host->host_no);
2340
Adam Radford4cbfea82014-07-09 15:17:56 -07002341
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302342 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002343 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2344 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002345 dcmd->cmd_status, instance->host->host_no);
2346 retval = 1; /* Do a scan if we couldn't get affiliation */
2347 goto out;
2348 }
2349
2350 if (!initial) {
2351 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002352 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2353 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002354 instance->host->host_no);
2355 retval = 1;
2356 goto out;
2357 }
2358 newmap = new_affiliation->map;
2359 savedmap = instance->vf_affiliation->map;
2360 thisVf = new_affiliation->thisVf;
2361 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2362 found = 0;
2363 for (j = 0; j < instance->vf_affiliation->ldCount;
2364 j++) {
2365 if (newmap->ref.targetId ==
2366 savedmap->ref.targetId) {
2367 found = 1;
2368 if (newmap->policy[thisVf] !=
2369 savedmap->policy[thisVf]) {
2370 doscan = 1;
2371 goto out;
2372 }
adam radford229fe472014-03-10 02:51:56 -07002373 }
2374 savedmap = (struct MR_LD_VF_MAP *)
2375 ((unsigned char *)savedmap +
2376 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002377 }
2378 if (!found && newmap->policy[thisVf] !=
2379 MR_LD_ACCESS_HIDDEN) {
2380 doscan = 1;
2381 goto out;
2382 }
2383 newmap = (struct MR_LD_VF_MAP *)
2384 ((unsigned char *)newmap + newmap->size);
2385 }
2386
2387 newmap = new_affiliation->map;
2388 savedmap = instance->vf_affiliation->map;
2389
2390 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2391 found = 0;
2392 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2393 if (savedmap->ref.targetId ==
2394 newmap->ref.targetId) {
2395 found = 1;
2396 if (savedmap->policy[thisVf] !=
2397 newmap->policy[thisVf]) {
2398 doscan = 1;
2399 goto out;
2400 }
2401 }
adam radford229fe472014-03-10 02:51:56 -07002402 newmap = (struct MR_LD_VF_MAP *)
2403 ((unsigned char *)newmap +
2404 newmap->size);
2405 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002406 if (!found && savedmap->policy[thisVf] !=
2407 MR_LD_ACCESS_HIDDEN) {
2408 doscan = 1;
2409 goto out;
2410 }
2411 savedmap = (struct MR_LD_VF_MAP *)
2412 ((unsigned char *)savedmap +
2413 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002414 }
2415 }
2416out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002417 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002418 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2419 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002420 memcpy(instance->vf_affiliation, new_affiliation,
2421 new_affiliation->size);
2422 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002423 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002424
2425 if (new_affiliation)
2426 pci_free_consistent(instance->pdev,
2427 (MAX_LOGICAL_DRIVES + 1) *
2428 sizeof(struct MR_LD_VF_AFFILIATION),
2429 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302430 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002431
2432 return retval;
2433}
2434
Adam Radford4cbfea82014-07-09 15:17:56 -07002435/* This function will get the current SR-IOV LD/VF affiliation */
2436static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2437 int initial)
2438{
2439 int retval;
2440
2441 if (instance->PlasmaFW111)
2442 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2443 else
2444 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2445 return retval;
2446}
2447
adam radford229fe472014-03-10 02:51:56 -07002448/* This function will tell FW to start the SR-IOV heartbeat */
2449int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2450 int initial)
2451{
2452 struct megasas_cmd *cmd;
2453 struct megasas_dcmd_frame *dcmd;
2454 int retval = 0;
2455
2456 cmd = megasas_get_cmd(instance);
2457
2458 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002459 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2460 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002461 instance->host->host_no);
2462 return -ENOMEM;
2463 }
2464
2465 dcmd = &cmd->frame->dcmd;
2466
2467 if (initial) {
2468 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002469 pci_zalloc_consistent(instance->pdev,
2470 sizeof(struct MR_CTRL_HB_HOST_MEM),
2471 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002472 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002473 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2474 " memory for heartbeat host memory for scsi%d\n",
2475 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002476 retval = -ENOMEM;
2477 goto out;
2478 }
adam radford229fe472014-03-10 02:51:56 -07002479 }
2480
2481 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2482
Christoph Hellwig2213a462015-04-23 16:33:54 +05302483 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002484 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302485 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002486 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302487 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002488 dcmd->timeout = 0;
2489 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302490 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2491 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2492 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2493 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002494
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002495 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002496 instance->host->host_no);
2497
Shivasharan Se7d36b82017-10-19 02:48:50 -07002498 if ((instance->adapter_type != MFI_SERIES) &&
2499 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302500 retval = megasas_issue_blocked_cmd(instance, cmd,
2501 MEGASAS_ROUTINE_WAIT_TIME_VF);
2502 else
2503 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002504
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302505 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302506 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2507 "_MEM_ALLOC DCMD %s for scsi%d\n",
2508 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2509 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002510 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002511 }
2512
2513out:
2514 megasas_return_cmd(instance, cmd);
2515
2516 return retval;
2517}
2518
2519/* Handler for SR-IOV heartbeat */
2520void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2521{
2522 struct megasas_instance *instance =
2523 (struct megasas_instance *)instance_addr;
2524
2525 if (instance->hb_host_mem->HB.fwCounter !=
2526 instance->hb_host_mem->HB.driverCounter) {
2527 instance->hb_host_mem->HB.driverCounter =
2528 instance->hb_host_mem->HB.fwCounter;
2529 mod_timer(&instance->sriov_heartbeat_timer,
2530 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2531 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002532 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002533 "completed for scsi%d\n", instance->host->host_no);
2534 schedule_work(&instance->work_init);
2535 }
2536}
2537
bo yang7343eb62007-11-09 04:35:44 -05002538/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002539 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2540 * @instance: Adapter soft state
2541 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002542 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002543 * complete all its outstanding commands. Returns error if one or more IOs
2544 * are pending after this time period. It also marks the controller dead.
2545 */
2546static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2547{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302548 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002549 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002550 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002551 unsigned long flags;
2552 struct list_head clist_local;
2553 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002554 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002555
Sumit Saxenaccc75072016-01-28 21:04:33 +05302556 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2557 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2558 __func__, __LINE__);
2559 return FAILED;
2560 }
bo yang39a98552010-09-22 22:36:29 -04002561
Sumit Saxena8a01a412016-01-28 21:04:32 +05302562 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002563
2564 INIT_LIST_HEAD(&clist_local);
2565 spin_lock_irqsave(&instance->hba_lock, flags);
2566 list_splice_init(&instance->internal_reset_pending_q,
2567 &clist_local);
2568 spin_unlock_irqrestore(&instance->hba_lock, flags);
2569
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002570 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002571 for (i = 0; i < wait_time; i++) {
2572 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302573 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002574 break;
2575 }
2576
Sumit Saxena8a01a412016-01-28 21:04:32 +05302577 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002578 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302579 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002580 return FAILED;
2581 }
2582
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002583 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002584 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002585 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002586 struct megasas_cmd, list);
2587 list_del_init(&reset_cmd->list);
2588 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002589 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002590 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002591 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002592 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002593
2594 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2595 megasas_return_cmd(instance, reset_cmd);
2596 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002597 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002598 "reset queue\n",
2599 reset_cmd);
2600
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302601 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002602 instance->instancet->fire_cmd(instance,
2603 reset_cmd->frame_phys_addr,
2604 0, instance->reg_set);
2605 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002606 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002607 "cmds lst\n",
2608 reset_cmd);
2609 }
2610 reset_index++;
2611 }
2612
2613 return SUCCESS;
2614 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002615
adam radfordc007b8b2012-07-17 18:20:24 -07002616 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302617 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002618
2619 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002620 break;
2621
2622 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002623 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002624 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002625 /*
2626 * Call cmd completion routine. Cmd to be
2627 * be completed directly without depending on isr.
2628 */
2629 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002630 }
2631
2632 msleep(1000);
2633 }
2634
Yang, Bo707e09b2010-10-12 07:20:27 -06002635 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302636 outstanding = atomic_read(&instance->fw_outstanding);
2637 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002638
Sumit Saxenaccc75072016-01-28 21:04:33 +05302639 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2640 goto no_outstanding;
2641
2642 if (instance->disableOnlineCtrlReset)
2643 goto kill_hba_and_failed;
2644 do {
2645 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2646 dev_info(&instance->pdev->dev,
2647 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2648 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2649 if (i == 3)
2650 goto kill_hba_and_failed;
2651 megasas_do_ocr(instance);
2652
2653 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2654 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2655 __func__, __LINE__);
2656 return FAILED;
2657 }
2658 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2659 __func__, __LINE__);
2660
2661 for (sl = 0; sl < 10; sl++)
2662 msleep(500);
2663
2664 outstanding = atomic_read(&instance->fw_outstanding);
2665
2666 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2667 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2668 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002669 }
2670 i++;
2671 } while (i <= 3);
2672
Sumit Saxenaccc75072016-01-28 21:04:33 +05302673no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002674
Sumit Saxenaccc75072016-01-28 21:04:33 +05302675 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2676 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002677 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302678
2679kill_hba_and_failed:
2680
2681 /* Reset not supported, kill adapter */
2682 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2683 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2684 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2685 atomic_read(&instance->fw_outstanding));
2686 megasas_dump_pending_frames(instance);
2687 megaraid_sas_kill_hba(instance);
2688
2689 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002690}
2691
2692/**
2693 * megasas_generic_reset - Generic reset routine
2694 * @scmd: Mid-layer SCSI command
2695 *
2696 * This routine implements a generic reset handler for device, bus and host
2697 * reset requests. Device, bus and host specific reset handlers can use this
2698 * function after they do their specific tasks.
2699 */
2700static int megasas_generic_reset(struct scsi_cmnd *scmd)
2701{
2702 int ret_val;
2703 struct megasas_instance *instance;
2704
2705 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2706
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002707 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2708 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002709
Sumit Saxena8a01a412016-01-28 21:04:32 +05302710 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002711 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002712 return FAILED;
2713 }
2714
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002715 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002716 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002717 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002718 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002719 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002720
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002721 return ret_val;
2722}
2723
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002724/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002725 * megasas_reset_timer - quiesce the adapter if required
2726 * @scmd: scsi cmnd
2727 *
2728 * Sets the FW busy flag and reduces the host->can_queue if the
2729 * cmd has not been completed within the timeout period.
2730 */
2731static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002732blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002733{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002734 struct megasas_instance *instance;
2735 unsigned long flags;
2736
2737 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302738 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002739 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002740 }
2741
adam radfordf575c5d2011-10-13 16:01:12 -07002742 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002743 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2744 /* FW is busy, throttle IO */
2745 spin_lock_irqsave(instance->host->host_lock, flags);
2746
adam radfordc5daa6a2012-07-17 18:20:03 -07002747 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002748 instance->last_time = jiffies;
2749 instance->flag |= MEGASAS_FW_BUSY;
2750
2751 spin_unlock_irqrestore(instance->host->host_lock, flags);
2752 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002753 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002754}
2755
2756/**
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002757 * megasas_dump_frame - This function will dump MPT/MFI frame
2758 */
2759static inline void
2760megasas_dump_frame(void *mpi_request, int sz)
2761{
2762 int i;
2763 __le32 *mfp = (__le32 *)mpi_request;
2764
2765 printk(KERN_INFO "IO request frame:\n\t");
Dan Carpenter40a4c2c2017-02-14 19:38:55 +03002766 for (i = 0; i < sz / sizeof(__le32); i++) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002767 if (i && ((i % 8) == 0))
2768 printk("\n\t");
2769 printk("%08x ", le32_to_cpu(mfp[i]));
2770 }
2771 printk("\n");
2772}
2773
2774/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002775 * megasas_reset_bus_host - Bus & host reset handler entry point
2776 */
2777static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2778{
2779 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002780 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002781
adam radford9c915a82010-12-21 13:34:31 -08002782 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002783
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002784 scmd_printk(KERN_INFO, scmd,
2785 "Controller reset is requested due to IO timeout\n"
2786 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2787 " SCSI host busy: %d\t FW outstanding: %d\n",
2788 scmd, scmd->device->host->shost_state,
2789 atomic_read((atomic_t *)&scmd->device->host->host_busy),
2790 atomic_read(&instance->fw_outstanding));
2791
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002792 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002793 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002794 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07002795 if (instance->adapter_type == MFI_SERIES) {
2796 ret = megasas_generic_reset(scmd);
2797 } else {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002798 struct megasas_cmd_fusion *cmd;
2799 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2800 if (cmd)
2801 megasas_dump_frame(cmd->io_request,
Shivasharan S5dd977e2017-08-23 04:47:07 -07002802 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002803 ret = megasas_reset_fusion(scmd->device->host,
2804 SCSIIO_TIMEOUT_OCR);
Shivasharan Se7d36b82017-10-19 02:48:50 -07002805 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002806
2807 return ret;
2808}
2809
2810/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002811 * megasas_task_abort - Issues task abort request to firmware
2812 * (supported only for fusion adapters)
2813 * @scmd: SCSI command pointer
2814 */
2815static int megasas_task_abort(struct scsi_cmnd *scmd)
2816{
2817 int ret;
2818 struct megasas_instance *instance;
2819
2820 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2821
Shivasharan Se7d36b82017-10-19 02:48:50 -07002822 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002823 ret = megasas_task_abort_fusion(scmd);
2824 else {
2825 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2826 ret = FAILED;
2827 }
2828
2829 return ret;
2830}
2831
2832/**
2833 * megasas_reset_target: Issues target reset request to firmware
2834 * (supported only for fusion adapters)
2835 * @scmd: SCSI command pointer
2836 */
2837static int megasas_reset_target(struct scsi_cmnd *scmd)
2838{
2839 int ret;
2840 struct megasas_instance *instance;
2841
2842 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2843
Shivasharan Se7d36b82017-10-19 02:48:50 -07002844 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002845 ret = megasas_reset_target_fusion(scmd);
2846 else {
2847 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2848 ret = FAILED;
2849 }
2850
2851 return ret;
2852}
2853
2854/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002855 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002856 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002857 * @bdev: block device
2858 * @capacity: drive capacity
2859 * @geom: geometry parameters
2860 */
2861static int
2862megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2863 sector_t capacity, int geom[])
2864{
2865 int heads;
2866 int sectors;
2867 sector_t cylinders;
2868 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002869
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002870 /* Default heads (64) & sectors (32) */
2871 heads = 64;
2872 sectors = 32;
2873
2874 tmp = heads * sectors;
2875 cylinders = capacity;
2876
2877 sector_div(cylinders, tmp);
2878
2879 /*
2880 * Handle extended translation size for logical drives > 1Gb
2881 */
2882
2883 if (capacity >= 0x200000) {
2884 heads = 255;
2885 sectors = 63;
2886 tmp = heads*sectors;
2887 cylinders = capacity;
2888 sector_div(cylinders, tmp);
2889 }
2890
2891 geom[0] = heads;
2892 geom[1] = sectors;
2893 geom[2] = cylinders;
2894
2895 return 0;
2896}
2897
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002898static void megasas_aen_polling(struct work_struct *work);
2899
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002900/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002901 * megasas_service_aen - Processes an event notification
2902 * @instance: Adapter soft state
2903 * @cmd: AEN command completed by the ISR
2904 *
2905 * For AEN, driver sends a command down to FW that is held by the FW till an
2906 * event occurs. When an event of interest occurs, FW completes the command
2907 * that it was previously holding.
2908 *
2909 * This routines sends SIGIO signal to processes that have registered with the
2910 * driver for AEN.
2911 */
2912static void
2913megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2914{
Yang, Boc3518832009-10-06 14:18:02 -06002915 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002916
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002917 /*
2918 * Don't signal app if it is just an aborted previously registered aen
2919 */
Yang, Boc3518832009-10-06 14:18:02 -06002920 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2921 spin_lock_irqsave(&poll_aen_lock, flags);
2922 megasas_poll_wait_aen = 1;
2923 spin_unlock_irqrestore(&poll_aen_lock, flags);
2924 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002925 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002926 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002927 else
2928 cmd->abort_aen = 0;
2929
2930 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302931
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302932 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002933
bo yang39a98552010-09-22 22:36:29 -04002934 if ((instance->unload == 0) &&
2935 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002936 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002937
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002938 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2939 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002940 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002941 } else {
2942 ev->instance = instance;
2943 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002944 INIT_DELAYED_WORK(&ev->hotplug_work,
2945 megasas_aen_polling);
2946 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002947 }
2948 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002949}
2950
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302951static ssize_t
2952megasas_fw_crash_buffer_store(struct device *cdev,
2953 struct device_attribute *attr, const char *buf, size_t count)
2954{
2955 struct Scsi_Host *shost = class_to_shost(cdev);
2956 struct megasas_instance *instance =
2957 (struct megasas_instance *) shost->hostdata;
2958 int val = 0;
2959 unsigned long flags;
2960
2961 if (kstrtoint(buf, 0, &val) != 0)
2962 return -EINVAL;
2963
2964 spin_lock_irqsave(&instance->crashdump_lock, flags);
2965 instance->fw_crash_buffer_offset = val;
2966 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2967 return strlen(buf);
2968}
2969
2970static ssize_t
2971megasas_fw_crash_buffer_show(struct device *cdev,
2972 struct device_attribute *attr, char *buf)
2973{
2974 struct Scsi_Host *shost = class_to_shost(cdev);
2975 struct megasas_instance *instance =
2976 (struct megasas_instance *) shost->hostdata;
2977 u32 size;
2978 unsigned long buff_addr;
2979 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2980 unsigned long src_addr;
2981 unsigned long flags;
2982 u32 buff_offset;
2983
2984 spin_lock_irqsave(&instance->crashdump_lock, flags);
2985 buff_offset = instance->fw_crash_buffer_offset;
2986 if (!instance->crash_dump_buf &&
2987 !((instance->fw_crash_state == AVAILABLE) ||
2988 (instance->fw_crash_state == COPYING))) {
2989 dev_err(&instance->pdev->dev,
2990 "Firmware crash dump is not available\n");
2991 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2992 return -EINVAL;
2993 }
2994
2995 buff_addr = (unsigned long) buf;
2996
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002997 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302998 dev_err(&instance->pdev->dev,
2999 "Firmware crash dump offset is out of range\n");
3000 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3001 return 0;
3002 }
3003
3004 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3005 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3006
3007 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3008 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003009 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303010 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3011
3012 return size;
3013}
3014
3015static ssize_t
3016megasas_fw_crash_buffer_size_show(struct device *cdev,
3017 struct device_attribute *attr, char *buf)
3018{
3019 struct Scsi_Host *shost = class_to_shost(cdev);
3020 struct megasas_instance *instance =
3021 (struct megasas_instance *) shost->hostdata;
3022
3023 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3024 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3025}
3026
3027static ssize_t
3028megasas_fw_crash_state_store(struct device *cdev,
3029 struct device_attribute *attr, const char *buf, size_t count)
3030{
3031 struct Scsi_Host *shost = class_to_shost(cdev);
3032 struct megasas_instance *instance =
3033 (struct megasas_instance *) shost->hostdata;
3034 int val = 0;
3035 unsigned long flags;
3036
3037 if (kstrtoint(buf, 0, &val) != 0)
3038 return -EINVAL;
3039
3040 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3041 dev_err(&instance->pdev->dev, "application updates invalid "
3042 "firmware crash state\n");
3043 return -EINVAL;
3044 }
3045
3046 instance->fw_crash_state = val;
3047
3048 if ((val == COPIED) || (val == COPY_ERROR)) {
3049 spin_lock_irqsave(&instance->crashdump_lock, flags);
3050 megasas_free_host_crash_buffer(instance);
3051 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3052 if (val == COPY_ERROR)
3053 dev_info(&instance->pdev->dev, "application failed to "
3054 "copy Firmware crash dump\n");
3055 else
3056 dev_info(&instance->pdev->dev, "Firmware crash dump "
3057 "copied successfully\n");
3058 }
3059 return strlen(buf);
3060}
3061
3062static ssize_t
3063megasas_fw_crash_state_show(struct device *cdev,
3064 struct device_attribute *attr, char *buf)
3065{
3066 struct Scsi_Host *shost = class_to_shost(cdev);
3067 struct megasas_instance *instance =
3068 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003069
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303070 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3071}
3072
3073static ssize_t
3074megasas_page_size_show(struct device *cdev,
3075 struct device_attribute *attr, char *buf)
3076{
3077 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3078}
3079
Sumit Saxena308ec452016-01-28 21:04:30 +05303080static ssize_t
3081megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3082 char *buf)
3083{
3084 struct Scsi_Host *shost = class_to_shost(cdev);
3085 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3086
3087 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3088}
3089
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303090static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3091 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3092static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3093 megasas_fw_crash_buffer_size_show, NULL);
3094static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3095 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3096static DEVICE_ATTR(page_size, S_IRUGO,
3097 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05303098static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3099 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303100
3101struct device_attribute *megaraid_host_attrs[] = {
3102 &dev_attr_fw_crash_buffer_size,
3103 &dev_attr_fw_crash_buffer,
3104 &dev_attr_fw_crash_state,
3105 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303106 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303107 NULL,
3108};
3109
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003110/*
3111 * Scsi host template for megaraid_sas driver
3112 */
3113static struct scsi_host_template megasas_template = {
3114
3115 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303116 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003117 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003118 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003119 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303120 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003121 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003122 .eh_target_reset_handler = megasas_reset_target,
3123 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003124 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003125 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303126 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003127 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003128 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003129 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003130 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003131};
3132
3133/**
3134 * megasas_complete_int_cmd - Completes an internal command
3135 * @instance: Adapter soft state
3136 * @cmd: Command to be completed
3137 *
3138 * The megasas_issue_blocked_cmd() function waits for a command to complete
3139 * after it issues a command. This function wakes up that waiting routine by
3140 * calling wake_up() on the wait queue.
3141 */
3142static void
3143megasas_complete_int_cmd(struct megasas_instance *instance,
3144 struct megasas_cmd *cmd)
3145{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303146 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003147 wake_up(&instance->int_cmd_wait_q);
3148}
3149
3150/**
3151 * megasas_complete_abort - Completes aborting a command
3152 * @instance: Adapter soft state
3153 * @cmd: Cmd that was issued to abort another cmd
3154 *
adam radford0d490162010-12-14 19:17:17 -08003155 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3156 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003157 * wakes up all functions waiting on the same wait queue.
3158 */
3159static void
3160megasas_complete_abort(struct megasas_instance *instance,
3161 struct megasas_cmd *cmd)
3162{
3163 if (cmd->sync_cmd) {
3164 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303165 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003166 wake_up(&instance->abort_cmd_wait_q);
3167 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003168}
3169
3170/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003171 * megasas_complete_cmd - Completes a command
3172 * @instance: Adapter soft state
3173 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003174 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003175 * SCSI mid-layer instead of the value returned
3176 * by the FW. This should be used if caller wants
3177 * an alternate status (as in the case of aborted
3178 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003179 */
adam radford9c915a82010-12-21 13:34:31 -08003180void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003181megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3182 u8 alt_status)
3183{
3184 int exception = 0;
3185 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003186 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003187 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303188 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003189
bo yang39a98552010-09-22 22:36:29 -04003190 /* flag for the retry reset */
3191 cmd->retry_for_fw_reset = 0;
3192
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003193 if (cmd->scmd)
3194 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003195
3196 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003197 case MFI_CMD_INVALID:
3198 /* Some older 1068 controller FW may keep a pended
3199 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3200 when booting the kdump kernel. Ignore this command to
3201 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003202 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3203 "completed\n");
3204 dev_warn(&instance->pdev->dev, "If you have a controller "
3205 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003206 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003207 case MFI_CMD_PD_SCSI_IO:
3208 case MFI_CMD_LD_SCSI_IO:
3209
3210 /*
3211 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3212 * issued either through an IO path or an IOCTL path. If it
3213 * was via IOCTL, we will send it to internal completion.
3214 */
3215 if (cmd->sync_cmd) {
3216 cmd->sync_cmd = 0;
3217 megasas_complete_int_cmd(instance, cmd);
3218 break;
3219 }
3220
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003221 case MFI_CMD_LD_READ:
3222 case MFI_CMD_LD_WRITE:
3223
3224 if (alt_status) {
3225 cmd->scmd->result = alt_status << 16;
3226 exception = 1;
3227 }
3228
3229 if (exception) {
3230
Sumant Patroe4a082c2006-05-30 12:03:37 -07003231 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003232
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003233 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003234 cmd->scmd->scsi_done(cmd->scmd);
3235 megasas_return_cmd(instance, cmd);
3236
3237 break;
3238 }
3239
3240 switch (hdr->cmd_status) {
3241
3242 case MFI_STAT_OK:
3243 cmd->scmd->result = DID_OK << 16;
3244 break;
3245
3246 case MFI_STAT_SCSI_IO_FAILED:
3247 case MFI_STAT_LD_INIT_IN_PROGRESS:
3248 cmd->scmd->result =
3249 (DID_ERROR << 16) | hdr->scsi_status;
3250 break;
3251
3252 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3253
3254 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3255
3256 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3257 memset(cmd->scmd->sense_buffer, 0,
3258 SCSI_SENSE_BUFFERSIZE);
3259 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3260 hdr->sense_len);
3261
3262 cmd->scmd->result |= DRIVER_SENSE << 24;
3263 }
3264
3265 break;
3266
3267 case MFI_STAT_LD_OFFLINE:
3268 case MFI_STAT_DEVICE_NOT_FOUND:
3269 cmd->scmd->result = DID_BAD_TARGET << 16;
3270 break;
3271
3272 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003273 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003274 hdr->cmd_status);
3275 cmd->scmd->result = DID_ERROR << 16;
3276 break;
3277 }
3278
Sumant Patroe4a082c2006-05-30 12:03:37 -07003279 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003280
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003281 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003282 cmd->scmd->scsi_done(cmd->scmd);
3283 megasas_return_cmd(instance, cmd);
3284
3285 break;
3286
3287 case MFI_CMD_SMP:
3288 case MFI_CMD_STP:
3289 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303290 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003291 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303292 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3293 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303294 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003295 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303296 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003297 if (cmd->frame->hdr.cmd_status != 0) {
3298 if (cmd->frame->hdr.cmd_status !=
3299 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003300 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003301 cmd->frame->hdr.cmd_status);
3302 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303303 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003304 spin_unlock_irqrestore(
3305 instance->host->host_lock,
3306 flags);
3307 break;
3308 }
3309 } else
3310 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303311 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303312
3313 /*
3314 * Set fast path IO to ZERO.
3315 * Validate Map will set proper value.
3316 * Meanwhile all IOs will go as LD IO.
3317 */
3318 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003319 fusion->fast_path_io = 1;
3320 else
3321 fusion->fast_path_io = 0;
3322 megasas_sync_map_info(instance);
3323 spin_unlock_irqrestore(instance->host->host_lock,
3324 flags);
3325 break;
3326 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303327 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3328 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003329 spin_lock_irqsave(&poll_aen_lock, flags);
3330 megasas_poll_wait_aen = 0;
3331 spin_unlock_irqrestore(&poll_aen_lock, flags);
3332 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003333
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303334 /* FW has an updated PD sequence */
3335 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3336 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3337
3338 spin_lock_irqsave(instance->host->host_lock, flags);
3339 status = cmd->frame->hdr.cmd_status;
3340 instance->jbod_seq_cmd = NULL;
3341 megasas_return_cmd(instance, cmd);
3342
3343 if (status == MFI_STAT_OK) {
3344 instance->pd_seq_map_id++;
3345 /* Re-register a pd sync seq num cmd */
3346 if (megasas_sync_pd_seq_num(instance, true))
3347 instance->use_seqnum_jbod_fp = false;
3348 } else
3349 instance->use_seqnum_jbod_fp = false;
3350
3351 spin_unlock_irqrestore(instance->host->host_lock, flags);
3352 break;
3353 }
3354
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003355 /*
3356 * See if got an event notification
3357 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303358 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003359 megasas_service_aen(instance, cmd);
3360 else
3361 megasas_complete_int_cmd(instance, cmd);
3362
3363 break;
3364
3365 case MFI_CMD_ABORT:
3366 /*
3367 * Cmd issued to abort another cmd returned
3368 */
3369 megasas_complete_abort(instance, cmd);
3370 break;
3371
3372 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003373 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003374 hdr->cmd);
3375 break;
3376 }
3377}
3378
3379/**
bo yang39a98552010-09-22 22:36:29 -04003380 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003381 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003382 * @instance: Adapter soft state
3383 */
3384static inline void
3385megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3386{
3387 struct megasas_cmd *cmd;
3388 struct list_head clist_local;
3389 union megasas_evt_class_locale class_locale;
3390 unsigned long flags;
3391 u32 seq_num;
3392
3393 INIT_LIST_HEAD(&clist_local);
3394 spin_lock_irqsave(&instance->hba_lock, flags);
3395 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3396 spin_unlock_irqrestore(&instance->hba_lock, flags);
3397
3398 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003399 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003400 struct megasas_cmd, list);
3401 list_del_init(&cmd->list);
3402
3403 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003404 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3405 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003406 cmd, cmd->scmd, cmd->sync_cmd);
3407
3408 cmd->retry_for_fw_reset++;
3409
3410 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003411 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003412 "was tried multiple times during reset."
3413 "Shutting down the HBA\n",
3414 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303415 instance->instancet->disable_intr(instance);
3416 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003417 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003418 return;
3419 }
3420 }
3421
3422 if (cmd->sync_cmd == 1) {
3423 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003424 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003425 "cmd attached to internal command!\n");
3426 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003427 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003428 "on the internal reset queue,"
3429 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303430 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003431 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003432 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003433 0, instance->reg_set);
3434 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003435 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003436 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003437 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003438
3439 atomic_inc(&instance->fw_outstanding);
3440 instance->instancet->fire_cmd(instance,
3441 cmd->frame_phys_addr,
3442 cmd->frame_count-1, instance->reg_set);
3443 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003444 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003445 "internal reset defer list while re-issue!!\n",
3446 cmd);
3447 }
3448 }
3449
3450 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003451 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003452 megasas_return_cmd(instance, instance->aen_cmd);
3453
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003454 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003455 }
3456
3457 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003458 * Initiate AEN (Asynchronous Event Notification)
3459 */
bo yang39a98552010-09-22 22:36:29 -04003460 seq_num = instance->last_seq_num;
3461 class_locale.members.reserved = 0;
3462 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3463 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3464
3465 megasas_register_aen(instance, seq_num, class_locale.word);
3466}
3467
3468/**
3469 * Move the internal reset pending commands to a deferred queue.
3470 *
3471 * We move the commands pending at internal reset time to a
3472 * pending queue. This queue would be flushed after successful
3473 * completion of the internal reset sequence. if the internal reset
3474 * did not complete in time, the kernel reset handler would flush
3475 * these commands.
3476 **/
3477static void
3478megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3479{
3480 struct megasas_cmd *cmd;
3481 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003482 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003483 u32 defer_index;
3484 unsigned long flags;
3485
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003486 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303487 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003488 for (i = 0; i < max_cmd; i++) {
3489 cmd = instance->cmd_list[i];
3490 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003491 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003492 "on the defer queue as internal\n",
3493 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3494
3495 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003496 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003497 " moving this cmd:%p, %d %p, it was"
3498 "discovered on some list?\n",
3499 cmd, cmd->sync_cmd, cmd->scmd);
3500
3501 list_del_init(&cmd->list);
3502 }
3503 defer_index++;
3504 list_add_tail(&cmd->list,
3505 &instance->internal_reset_pending_q);
3506 }
3507 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303508 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003509}
3510
3511
3512static void
3513process_fw_state_change_wq(struct work_struct *work)
3514{
3515 struct megasas_instance *instance =
3516 container_of(work, struct megasas_instance, work_init);
3517 u32 wait;
3518 unsigned long flags;
3519
Sumit Saxena8a01a412016-01-28 21:04:32 +05303520 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003521 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303522 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003523 return ;
3524 }
3525
Sumit Saxena8a01a412016-01-28 21:04:32 +05303526 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003527 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003528 "state, restarting it...\n");
3529
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303530 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003531 atomic_set(&instance->fw_outstanding, 0);
3532
3533 atomic_set(&instance->fw_reset_no_pci_access, 1);
3534 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003535 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003536
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003537 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003538 "initiating next stage...\n");
3539
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003540 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003541 "state 2 starting...\n");
3542
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003543 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003544 for (wait = 0; wait < 30; wait++) {
3545 msleep(1000);
3546 }
3547
adam radford058a8fa2011-10-08 18:14:27 -07003548 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003549 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003550
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303551 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003552 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003553 return ;
3554 }
3555
3556 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3557 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3558 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3559 ) {
3560 *instance->consumer = *instance->producer;
3561 } else {
3562 *instance->consumer = 0;
3563 *instance->producer = 0;
3564 }
3565
3566 megasas_issue_init_mfi(instance);
3567
3568 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303569 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003570 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303571 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003572
3573 megasas_issue_pending_cmds_again(instance);
3574 instance->issuepend_done = 1;
3575 }
bo yang39a98552010-09-22 22:36:29 -04003576}
3577
3578/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003579 * megasas_deplete_reply_queue - Processes all completed commands
3580 * @instance: Adapter soft state
3581 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003582 * SCSI mid-layer instead of the status
3583 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003584 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003585 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003586static int
bo yang39a98552010-09-22 22:36:29 -04003587megasas_deplete_reply_queue(struct megasas_instance *instance,
3588 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003589{
bo yang39a98552010-09-22 22:36:29 -04003590 u32 mfiStatus;
3591 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003592
bo yang39a98552010-09-22 22:36:29 -04003593 if ((mfiStatus = instance->instancet->check_reset(instance,
3594 instance->reg_set)) == 1) {
3595 return IRQ_HANDLED;
3596 }
3597
3598 if ((mfiStatus = instance->instancet->clear_intr(
3599 instance->reg_set)
3600 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003601 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003602 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003603 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003604 }
3605
3606 instance->mfiStatus = mfiStatus;
3607
3608 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3609 fw_state = instance->instancet->read_fw_status_reg(
3610 instance->reg_set) & MFI_STATE_MASK;
3611
3612 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003613 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003614 fw_state);
3615 }
3616
3617 if ((fw_state == MFI_STATE_FAULT) &&
3618 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003619 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003620
3621 if ((instance->pdev->device ==
3622 PCI_DEVICE_ID_LSI_SAS1064R) ||
3623 (instance->pdev->device ==
3624 PCI_DEVICE_ID_DELL_PERC5) ||
3625 (instance->pdev->device ==
3626 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3627
3628 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303629 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003630 }
3631
3632
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303633 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303634 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003635 instance->issuepend_done = 0;
3636
3637 atomic_set(&instance->fw_outstanding, 0);
3638 megasas_internal_reset_defer_cmds(instance);
3639
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003640 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303641 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003642
3643 schedule_work(&instance->work_init);
3644 return IRQ_HANDLED;
3645
3646 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003647 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003648 fw_state, instance->disableOnlineCtrlReset);
3649 }
3650 }
3651
Sumant Patro5d018ad2006-10-03 13:13:18 -07003652 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003653 return IRQ_HANDLED;
3654}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003655/**
3656 * megasas_isr - isr entry point
3657 */
David Howells7d12e782006-10-05 14:55:46 +01003658static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003659{
adam radfordc8e858f2011-10-08 18:15:13 -07003660 struct megasas_irq_context *irq_context = devp;
3661 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003662 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003663 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003664
adam radfordc8e858f2011-10-08 18:15:13 -07003665 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003666 return IRQ_HANDLED;
3667
bo yang39a98552010-09-22 22:36:29 -04003668 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003669 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003670 spin_unlock_irqrestore(&instance->hba_lock, flags);
3671
3672 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003673}
3674
3675/**
3676 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003677 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003678 *
3679 * During the initialization, FW passes can potentially be in any one of
3680 * several possible states. If the FW in operational, waiting-for-handshake
3681 * states, driver must take steps to bring it to ready state. Otherwise, it
3682 * has to wait for the ready state.
3683 */
adam radford9c915a82010-12-21 13:34:31 -08003684int
adam radford058a8fa2011-10-08 18:14:27 -07003685megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003686{
3687 int i;
3688 u8 max_wait;
3689 u32 fw_state;
3690 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003691 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003692
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003693 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3694 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003695
Sumant Patroe3bbff92006-10-03 12:28:49 -07003696 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003697 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003698 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003699
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003700 while (fw_state != MFI_STATE_READY) {
3701
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003702 switch (fw_state) {
3703
3704 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003705 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003706 if (ocr) {
3707 max_wait = MEGASAS_RESET_WAIT_TIME;
3708 cur_state = MFI_STATE_FAULT;
3709 break;
3710 } else
3711 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003712
3713 case MFI_STATE_WAIT_HANDSHAKE:
3714 /*
3715 * Set the CLR bit in inbound doorbell
3716 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003717 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003718 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3719 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003720 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003721 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003722 writel(
3723 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003724 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303725 else
Yang, Bo87911122009-10-06 14:31:54 -06003726 writel(
3727 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3728 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003729
Yang, Bo7218df62009-10-06 14:52:20 -06003730 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003731 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3732 break;
3733
Sumant Patroe3bbff92006-10-03 12:28:49 -07003734 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003735 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003736 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3737 (instance->pdev->device ==
3738 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003739 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003740 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003741 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303742 else
Yang, Bo87911122009-10-06 14:31:54 -06003743 writel(MFI_INIT_HOTPLUG,
3744 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003745
Yang, Bo7218df62009-10-06 14:52:20 -06003746 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003747 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3748 break;
3749
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003750 case MFI_STATE_OPERATIONAL:
3751 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003752 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003753 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303754 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003755 if ((instance->pdev->device ==
3756 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3757 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003758 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003759 (instance->adapter_type != MFI_SERIES)) {
Yang, Bo87911122009-10-06 14:31:54 -06003760 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003761 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303762
Shivasharan Se7d36b82017-10-19 02:48:50 -07003763 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08003764 for (i = 0; i < (10 * 1000); i += 20) {
3765 if (readl(
3766 &instance->
3767 reg_set->
3768 doorbell) & 1)
3769 msleep(20);
3770 else
3771 break;
3772 }
3773 }
Yang, Bo87911122009-10-06 14:31:54 -06003774 } else
3775 writel(MFI_RESET_FLAGS,
3776 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003777
Yang, Bo7218df62009-10-06 14:52:20 -06003778 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003779 cur_state = MFI_STATE_OPERATIONAL;
3780 break;
3781
3782 case MFI_STATE_UNDEFINED:
3783 /*
3784 * This state should not last for more than 2 seconds
3785 */
Yang, Bo7218df62009-10-06 14:52:20 -06003786 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003787 cur_state = MFI_STATE_UNDEFINED;
3788 break;
3789
3790 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003791 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003792 cur_state = MFI_STATE_BB_INIT;
3793 break;
3794
3795 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003796 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003797 cur_state = MFI_STATE_FW_INIT;
3798 break;
3799
3800 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003801 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003802 cur_state = MFI_STATE_FW_INIT_2;
3803 break;
3804
3805 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003806 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003807 cur_state = MFI_STATE_DEVICE_SCAN;
3808 break;
3809
3810 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003811 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003812 cur_state = MFI_STATE_FLUSH_CACHE;
3813 break;
3814
3815 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003816 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003817 fw_state);
3818 return -ENODEV;
3819 }
3820
3821 /*
3822 * The cur_state should not last for more than max_wait secs
3823 */
3824 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003825 curr_abs_state = instance->instancet->
3826 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003827
Yang, Bo7218df62009-10-06 14:52:20 -06003828 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003829 msleep(1);
3830 } else
3831 break;
3832 }
3833
3834 /*
3835 * Return error if fw_state hasn't changed after max_wait
3836 */
Yang, Bo7218df62009-10-06 14:52:20 -06003837 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003838 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003839 "in %d secs\n", fw_state, max_wait);
3840 return -ENODEV;
3841 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003842
3843 abs_state = curr_abs_state;
3844 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003845 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003846 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003847
3848 return 0;
3849}
3850
3851/**
3852 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3853 * @instance: Adapter soft state
3854 */
3855static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3856{
3857 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003858 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003859 struct megasas_cmd *cmd;
3860
3861 if (!instance->frame_dma_pool)
3862 return;
3863
3864 /*
3865 * Return all frames to pool
3866 */
3867 for (i = 0; i < max_cmd; i++) {
3868
3869 cmd = instance->cmd_list[i];
3870
3871 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02003872 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003873 cmd->frame_phys_addr);
3874
3875 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02003876 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003877 cmd->sense_phys_addr);
3878 }
3879
3880 /*
3881 * Now destroy the pool itself
3882 */
Romain Perierfc69d862017-07-06 10:13:06 +02003883 dma_pool_destroy(instance->frame_dma_pool);
3884 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003885
3886 instance->frame_dma_pool = NULL;
3887 instance->sense_dma_pool = NULL;
3888}
3889
3890/**
3891 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3892 * @instance: Adapter soft state
3893 *
3894 * Each command packet has an embedded DMA memory buffer that is used for
3895 * filling MFI frame and the SG list that immediately follows the frame. This
3896 * function creates those DMA memory buffers for each command packet by using
3897 * PCI pool facility.
3898 */
3899static int megasas_create_frame_pool(struct megasas_instance *instance)
3900{
3901 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003902 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003903 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003904 u32 frame_count;
3905 struct megasas_cmd *cmd;
3906
adam radford9c915a82010-12-21 13:34:31 -08003907 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003908
3909 /*
3910 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3911 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3912 */
3913 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3914 sizeof(struct megasas_sge32);
3915
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003916 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003917 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003918
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003919 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303920 * For MFI controllers.
3921 * max_num_sge = 60
3922 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3923 * Total 960 byte (15 MFI frame of 64 byte)
3924 *
3925 * Fusion adapter require only 3 extra frame.
3926 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3927 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3928 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003929 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07003930 frame_count = (instance->adapter_type == MFI_SERIES) ?
3931 (15 + 1) : (3 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08003932 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003933 /*
3934 * Use DMA pool facility provided by PCI layer
3935 */
Romain Perierfc69d862017-07-06 10:13:06 +02003936 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
3937 &instance->pdev->dev,
3938 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003939
3940 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003941 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003942 return -ENOMEM;
3943 }
3944
Romain Perierfc69d862017-07-06 10:13:06 +02003945 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
3946 &instance->pdev->dev, 128,
3947 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003948
3949 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003950 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003951
Romain Perierfc69d862017-07-06 10:13:06 +02003952 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003953 instance->frame_dma_pool = NULL;
3954
3955 return -ENOMEM;
3956 }
3957
3958 /*
3959 * Allocate and attach a frame to each of the commands in cmd_list.
3960 * By making cmd->index as the context instead of the &cmd, we can
3961 * always use 32bit context regardless of the architecture
3962 */
3963 for (i = 0; i < max_cmd; i++) {
3964
3965 cmd = instance->cmd_list[i];
3966
Romain Perierfc69d862017-07-06 10:13:06 +02003967 cmd->frame = dma_pool_alloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003968 GFP_KERNEL, &cmd->frame_phys_addr);
3969
Romain Perierfc69d862017-07-06 10:13:06 +02003970 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003971 GFP_KERNEL, &cmd->sense_phys_addr);
3972
3973 /*
3974 * megasas_teardown_frame_pool() takes care of freeing
3975 * whatever has been allocated
3976 */
3977 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02003978 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003979 megasas_teardown_frame_pool(instance);
3980 return -ENOMEM;
3981 }
3982
Shivasharan S21c34002017-02-10 00:59:28 -08003983 memset(cmd->frame, 0, instance->mfi_frame_size);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303984 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003985 cmd->frame->io.pad_0 = 0;
Shivasharan Se7d36b82017-10-19 02:48:50 -07003986 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003987 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003988 }
3989
3990 return 0;
3991}
3992
3993/**
3994 * megasas_free_cmds - Free all the cmds in the free cmd pool
3995 * @instance: Adapter soft state
3996 */
adam radford9c915a82010-12-21 13:34:31 -08003997void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003998{
3999 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004000
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004001 /* First free the MFI frame pool */
4002 megasas_teardown_frame_pool(instance);
4003
4004 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08004005 for (i = 0; i < instance->max_mfi_cmds; i++)
4006
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004007 kfree(instance->cmd_list[i]);
4008
4009 /* Free the cmd_list buffer itself */
4010 kfree(instance->cmd_list);
4011 instance->cmd_list = NULL;
4012
4013 INIT_LIST_HEAD(&instance->cmd_pool);
4014}
4015
4016/**
4017 * megasas_alloc_cmds - Allocates the command packets
4018 * @instance: Adapter soft state
4019 *
4020 * Each command that is issued to the FW, whether IO commands from the OS or
4021 * internal commands like IOCTLs, are wrapped in local data structure called
4022 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4023 * the FW.
4024 *
4025 * Each frame has a 32-bit field called context (tag). This context is used
4026 * to get back the megasas_cmd from the frame when a frame gets completed in
4027 * the ISR. Typically the address of the megasas_cmd itself would be used as
4028 * the context. But we wanted to keep the differences between 32 and 64 bit
4029 * systems to the mininum. We always use 32 bit integers for the context. In
4030 * this driver, the 32 bit values are the indices into an array cmd_list.
4031 * This array is used only to look up the megasas_cmd given the context. The
4032 * free commands themselves are maintained in a linked list called cmd_pool.
4033 */
adam radford9c915a82010-12-21 13:34:31 -08004034int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004035{
4036 int i;
4037 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004038 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004039 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304040 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004041
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304042 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08004043 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004044
4045 /*
4046 * instance->cmd_list is an array of struct megasas_cmd pointers.
4047 * Allocate the dynamic array first and then allocate individual
4048 * commands.
4049 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004050 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004051
4052 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004053 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004054 return -ENOMEM;
4055 }
4056
adam radford9c915a82010-12-21 13:34:31 -08004057 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004058
4059 for (i = 0; i < max_cmd; i++) {
4060 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4061 GFP_KERNEL);
4062
4063 if (!instance->cmd_list[i]) {
4064
4065 for (j = 0; j < i; j++)
4066 kfree(instance->cmd_list[j]);
4067
4068 kfree(instance->cmd_list);
4069 instance->cmd_list = NULL;
4070
4071 return -ENOMEM;
4072 }
4073 }
4074
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004075 for (i = 0; i < max_cmd; i++) {
4076 cmd = instance->cmd_list[i];
4077 memset(cmd, 0, sizeof(struct megasas_cmd));
4078 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004079 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004080 cmd->instance = instance;
4081
4082 list_add_tail(&cmd->list, &instance->cmd_pool);
4083 }
4084
4085 /*
4086 * Create a frame pool and assign one frame to each cmd
4087 */
4088 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004089 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004090 megasas_free_cmds(instance);
4091 }
4092
4093 return 0;
4094}
4095
Yang, Bo81e403c2009-10-06 14:27:54 -06004096/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304097 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4098 * @instance: Adapter soft state
4099 *
4100 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4101 * or FW is not under OCR.
4102 */
4103inline int
4104dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4105
Shivasharan Se7d36b82017-10-19 02:48:50 -07004106 if (instance->adapter_type == MFI_SERIES)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304107 return KILL_ADAPTER;
4108 else if (instance->unload ||
4109 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4110 return IGNORE_TIMEOUT;
4111 else
4112 return INITIATE_OCR;
4113}
4114
Shivasharan S15dd0382017-02-10 00:59:10 -08004115static void
4116megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304117{
4118 int ret;
4119 struct megasas_cmd *cmd;
4120 struct megasas_dcmd_frame *dcmd;
4121
Shivasharan S15dd0382017-02-10 00:59:10 -08004122 struct MR_PRIV_DEVICE *mr_device_priv_data;
4123 u16 device_id = 0;
4124
4125 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304126 cmd = megasas_get_cmd(instance);
4127
4128 if (!cmd) {
4129 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004130 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304131 }
4132
4133 dcmd = &cmd->frame->dcmd;
4134
4135 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4136 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4137
4138 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4139 dcmd->cmd = MFI_CMD_DCMD;
4140 dcmd->cmd_status = 0xFF;
4141 dcmd->sge_count = 1;
4142 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4143 dcmd->timeout = 0;
4144 dcmd->pad_0 = 0;
4145 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4146 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4147 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4148 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4149
Shivasharan Se7d36b82017-10-19 02:48:50 -07004150 if ((instance->adapter_type != MFI_SERIES) &&
4151 !instance->mask_interrupts)
Sumit Saxena2216c302016-01-28 21:04:26 +05304152 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4153 else
4154 ret = megasas_issue_polled(instance, cmd);
4155
4156 switch (ret) {
4157 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004158 mr_device_priv_data = sdev->hostdata;
4159 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4160 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304161 instance->pd_info->state.ddf.pdType.intf;
4162 break;
4163
4164 case DCMD_TIMEOUT:
4165
4166 switch (dcmd_timeout_ocr_possible(instance)) {
4167 case INITIATE_OCR:
4168 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4169 megasas_reset_fusion(instance->host,
4170 MFI_IO_TIMEOUT_OCR);
4171 break;
4172 case KILL_ADAPTER:
4173 megaraid_sas_kill_hba(instance);
4174 break;
4175 case IGNORE_TIMEOUT:
4176 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4177 __func__, __LINE__);
4178 break;
4179 }
4180
4181 break;
4182 }
4183
4184 if (ret != DCMD_TIMEOUT)
4185 megasas_return_cmd(instance, cmd);
4186
Shivasharan S15dd0382017-02-10 00:59:10 -08004187 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304188}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304189/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004190 * megasas_get_pd_list_info - Returns FW's pd_list structure
4191 * @instance: Adapter soft state
4192 * @pd_list: pd_list structure
4193 *
4194 * Issues an internal command (DCMD) to get the FW's controller PD
4195 * list structure. This information is mainly used to find out SYSTEM
4196 * supported by the FW.
4197 */
4198static int
4199megasas_get_pd_list(struct megasas_instance *instance)
4200{
4201 int ret = 0, pd_index = 0;
4202 struct megasas_cmd *cmd;
4203 struct megasas_dcmd_frame *dcmd;
4204 struct MR_PD_LIST *ci;
4205 struct MR_PD_ADDRESS *pd_addr;
4206 dma_addr_t ci_h = 0;
4207
Sumit Saxenad9083162016-07-08 03:30:16 -07004208 if (instance->pd_list_not_supported) {
4209 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4210 "not supported by firmware\n");
4211 return ret;
4212 }
4213
Yang, Bo81e403c2009-10-06 14:27:54 -06004214 cmd = megasas_get_cmd(instance);
4215
4216 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004217 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004218 return -ENOMEM;
4219 }
4220
4221 dcmd = &cmd->frame->dcmd;
4222
4223 ci = pci_alloc_consistent(instance->pdev,
4224 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4225
4226 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004227 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004228 megasas_return_cmd(instance, cmd);
4229 return -ENOMEM;
4230 }
4231
4232 memset(ci, 0, sizeof(*ci));
4233 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4234
4235 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4236 dcmd->mbox.b[1] = 0;
4237 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304238 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004239 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304240 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004241 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004242 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304243 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4244 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4245 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4246 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004247
Shivasharan Se7d36b82017-10-19 02:48:50 -07004248 if ((instance->adapter_type != MFI_SERIES) &&
4249 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304250 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304251 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304252 else
4253 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004254
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304255 switch (ret) {
4256 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004257 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4258 "failed/not supported by firmware\n");
4259
Shivasharan Se7d36b82017-10-19 02:48:50 -07004260 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004261 megaraid_sas_kill_hba(instance);
4262 else
4263 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304264 break;
4265 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004266
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304267 switch (dcmd_timeout_ocr_possible(instance)) {
4268 case INITIATE_OCR:
4269 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4270 /*
4271 * DCMD failed from AEN path.
4272 * AEN path already hold reset_mutex to avoid PCI access
4273 * while OCR is in progress.
4274 */
4275 mutex_unlock(&instance->reset_mutex);
4276 megasas_reset_fusion(instance->host,
4277 MFI_IO_TIMEOUT_OCR);
4278 mutex_lock(&instance->reset_mutex);
4279 break;
4280 case KILL_ADAPTER:
4281 megaraid_sas_kill_hba(instance);
4282 break;
4283 case IGNORE_TIMEOUT:
4284 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4285 __func__, __LINE__);
4286 break;
4287 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004288
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304289 break;
4290
4291 case DCMD_SUCCESS:
4292 pd_addr = ci->addr;
4293
4294 if ((le32_to_cpu(ci->count) >
4295 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4296 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004297
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304298 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304299 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004300
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304301 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304302 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304303 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304304 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304305 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304306 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304307 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004308 pd_addr++;
4309 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304310
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304311 memcpy(instance->pd_list, instance->local_pd_list,
4312 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304313 break;
4314
Yang, Bo81e403c2009-10-06 14:27:54 -06004315 }
4316
4317 pci_free_consistent(instance->pdev,
4318 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4319 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304320
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304321 if (ret != DCMD_TIMEOUT)
4322 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004323
4324 return ret;
4325}
4326
Yang, Bobdc6fb82009-12-06 08:30:19 -07004327/*
4328 * megasas_get_ld_list_info - Returns FW's ld_list structure
4329 * @instance: Adapter soft state
4330 * @ld_list: ld_list structure
4331 *
4332 * Issues an internal command (DCMD) to get the FW's controller PD
4333 * list structure. This information is mainly used to find out SYSTEM
4334 * supported by the FW.
4335 */
4336static int
4337megasas_get_ld_list(struct megasas_instance *instance)
4338{
4339 int ret = 0, ld_index = 0, ids = 0;
4340 struct megasas_cmd *cmd;
4341 struct megasas_dcmd_frame *dcmd;
4342 struct MR_LD_LIST *ci;
4343 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304344 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004345
4346 cmd = megasas_get_cmd(instance);
4347
4348 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004349 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004350 return -ENOMEM;
4351 }
4352
4353 dcmd = &cmd->frame->dcmd;
4354
4355 ci = pci_alloc_consistent(instance->pdev,
4356 sizeof(struct MR_LD_LIST),
4357 &ci_h);
4358
4359 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004360 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004361 megasas_return_cmd(instance, cmd);
4362 return -ENOMEM;
4363 }
4364
4365 memset(ci, 0, sizeof(*ci));
4366 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4367
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304368 if (instance->supportmax256vd)
4369 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004370 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304371 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004372 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304373 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004374 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304375 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4376 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4377 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4378 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004379 dcmd->pad_0 = 0;
4380
Shivasharan Se7d36b82017-10-19 02:48:50 -07004381 if ((instance->adapter_type != MFI_SERIES) &&
4382 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304383 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304384 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304385 else
4386 ret = megasas_issue_polled(instance, cmd);
4387
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304388 ld_count = le32_to_cpu(ci->ldCount);
4389
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304390 switch (ret) {
4391 case DCMD_FAILED:
4392 megaraid_sas_kill_hba(instance);
4393 break;
4394 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004395
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304396 switch (dcmd_timeout_ocr_possible(instance)) {
4397 case INITIATE_OCR:
4398 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4399 /*
4400 * DCMD failed from AEN path.
4401 * AEN path already hold reset_mutex to avoid PCI access
4402 * while OCR is in progress.
4403 */
4404 mutex_unlock(&instance->reset_mutex);
4405 megasas_reset_fusion(instance->host,
4406 MFI_IO_TIMEOUT_OCR);
4407 mutex_lock(&instance->reset_mutex);
4408 break;
4409 case KILL_ADAPTER:
4410 megaraid_sas_kill_hba(instance);
4411 break;
4412 case IGNORE_TIMEOUT:
4413 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4414 __func__, __LINE__);
4415 break;
4416 }
4417
4418 break;
4419
4420 case DCMD_SUCCESS:
4421 if (ld_count > instance->fw_supported_vd_count)
4422 break;
4423
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304424 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004425
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304426 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004427 if (ci->ldList[ld_index].state != 0) {
4428 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304429 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004430 }
4431 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304432
4433 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004434 }
4435
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304436 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004437
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304438 if (ret != DCMD_TIMEOUT)
4439 megasas_return_cmd(instance, cmd);
4440
Yang, Bobdc6fb82009-12-06 08:30:19 -07004441 return ret;
4442}
4443
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004444/**
adam radford21c9e162013-09-06 15:27:14 -07004445 * megasas_ld_list_query - Returns FW's ld_list structure
4446 * @instance: Adapter soft state
4447 * @ld_list: ld_list structure
4448 *
4449 * Issues an internal command (DCMD) to get the FW's controller PD
4450 * list structure. This information is mainly used to find out SYSTEM
4451 * supported by the FW.
4452 */
4453static int
4454megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4455{
4456 int ret = 0, ld_index = 0, ids = 0;
4457 struct megasas_cmd *cmd;
4458 struct megasas_dcmd_frame *dcmd;
4459 struct MR_LD_TARGETID_LIST *ci;
4460 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304461 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004462
4463 cmd = megasas_get_cmd(instance);
4464
4465 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004466 dev_warn(&instance->pdev->dev,
4467 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004468 return -ENOMEM;
4469 }
4470
4471 dcmd = &cmd->frame->dcmd;
4472
4473 ci = pci_alloc_consistent(instance->pdev,
4474 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4475
4476 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004477 dev_warn(&instance->pdev->dev,
4478 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004479 megasas_return_cmd(instance, cmd);
4480 return -ENOMEM;
4481 }
4482
4483 memset(ci, 0, sizeof(*ci));
4484 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4485
4486 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304487 if (instance->supportmax256vd)
4488 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004489
4490 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304491 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004492 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304493 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004494 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304495 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4496 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4497 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4498 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004499 dcmd->pad_0 = 0;
4500
Shivasharan Se7d36b82017-10-19 02:48:50 -07004501 if ((instance->adapter_type != MFI_SERIES) &&
4502 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304503 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304504 else
4505 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004506
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304507 switch (ret) {
4508 case DCMD_FAILED:
4509 dev_info(&instance->pdev->dev,
4510 "DCMD not supported by firmware - %s %d\n",
4511 __func__, __LINE__);
4512 ret = megasas_get_ld_list(instance);
4513 break;
4514 case DCMD_TIMEOUT:
4515 switch (dcmd_timeout_ocr_possible(instance)) {
4516 case INITIATE_OCR:
4517 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4518 /*
4519 * DCMD failed from AEN path.
4520 * AEN path already hold reset_mutex to avoid PCI access
4521 * while OCR is in progress.
4522 */
4523 mutex_unlock(&instance->reset_mutex);
4524 megasas_reset_fusion(instance->host,
4525 MFI_IO_TIMEOUT_OCR);
4526 mutex_lock(&instance->reset_mutex);
4527 break;
4528 case KILL_ADAPTER:
4529 megaraid_sas_kill_hba(instance);
4530 break;
4531 case IGNORE_TIMEOUT:
4532 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4533 __func__, __LINE__);
4534 break;
4535 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304536
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304537 break;
4538 case DCMD_SUCCESS:
4539 tgtid_count = le32_to_cpu(ci->count);
4540
4541 if ((tgtid_count > (instance->fw_supported_vd_count)))
4542 break;
4543
adam radford21c9e162013-09-06 15:27:14 -07004544 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304545 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004546 ids = ci->targetId[ld_index];
4547 instance->ld_ids[ids] = ci->targetId[ld_index];
4548 }
4549
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304550 break;
adam radford21c9e162013-09-06 15:27:14 -07004551 }
4552
4553 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304554 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004555
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304556 if (ret != DCMD_TIMEOUT)
4557 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004558
4559 return ret;
4560}
4561
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304562/*
4563 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4564 * instance : Controller's instance
4565*/
4566static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4567{
4568 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004569 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304570
4571 fusion = instance->ctrl_context;
4572 /* For MFI based controllers return dummy success */
4573 if (!fusion)
4574 return;
4575
4576 instance->supportmax256vd =
4577 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4578 /* Below is additional check to address future FW enhancement */
4579 if (instance->ctrl_info->max_lds > 64)
4580 instance->supportmax256vd = 1;
4581
4582 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4583 * MEGASAS_MAX_DEV_PER_CHANNEL;
4584 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4585 * MEGASAS_MAX_DEV_PER_CHANNEL;
4586 if (instance->supportmax256vd) {
4587 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4588 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4589 } else {
4590 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4591 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4592 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304593
4594 dev_info(&instance->pdev->dev,
4595 "firmware type\t: %s\n",
4596 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4597 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304598
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004599 if (instance->max_raid_mapsize) {
4600 ventura_map_sz = instance->max_raid_mapsize *
4601 MR_MIN_MAP_SIZE; /* 64k */
4602 fusion->current_map_sz = ventura_map_sz;
4603 fusion->max_map_sz = ventura_map_sz;
4604 } else {
4605 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4606 (sizeof(struct MR_LD_SPAN_MAP) *
4607 (instance->fw_supported_vd_count - 1));
4608 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304609
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004610 fusion->max_map_sz =
4611 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304612
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004613 if (instance->supportmax256vd)
4614 fusion->current_map_sz = fusion->new_map_sz;
4615 else
4616 fusion->current_map_sz = fusion->old_map_sz;
4617 }
4618 /* irrespective of FW raid maps, driver raid map is constant */
4619 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304620}
4621
adam radford21c9e162013-09-06 15:27:14 -07004622/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004623 * megasas_get_controller_info - Returns FW's controller structure
4624 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004625 *
4626 * Issues an internal command (DCMD) to get the FW's controller structure.
4627 * This information is mainly used to find out the maximum IO transfer per
4628 * command supported by the FW.
4629 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304630int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304631megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004632{
4633 int ret = 0;
4634 struct megasas_cmd *cmd;
4635 struct megasas_dcmd_frame *dcmd;
4636 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304637 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004638 dma_addr_t ci_h = 0;
4639
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304640 ctrl_info = instance->ctrl_info;
4641
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004642 cmd = megasas_get_cmd(instance);
4643
4644 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004645 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004646 return -ENOMEM;
4647 }
4648
4649 dcmd = &cmd->frame->dcmd;
4650
4651 ci = pci_alloc_consistent(instance->pdev,
4652 sizeof(struct megasas_ctrl_info), &ci_h);
4653
4654 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004655 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004656 megasas_return_cmd(instance, cmd);
4657 return -ENOMEM;
4658 }
4659
4660 memset(ci, 0, sizeof(*ci));
4661 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4662
4663 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304664 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004665 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304666 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004667 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004668 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304669 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4670 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4671 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4672 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304673 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004674
Shivasharan Se7d36b82017-10-19 02:48:50 -07004675 if ((instance->adapter_type != MFI_SERIES) &&
4676 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304677 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304678 else
4679 ret = megasas_issue_polled(instance, cmd);
4680
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304681 switch (ret) {
4682 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004683 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304684 /* Save required controller information in
4685 * CPU endianness format.
4686 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304687 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4688 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4689 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004690 le16_to_cpus((u16 *)&ctrl_info->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304691
4692 /* Update the latest Ext VD info.
4693 * From Init path, store current firmware details.
4694 * From OCR path, detect any firmware properties changes.
4695 * in case of Firmware upgrade without system reboot.
4696 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304697 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304698 instance->use_seqnum_jbod_fp =
4699 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004700 instance->support_morethan256jbod =
4701 ctrl_info->adapter_operations4.support_pd_map_target_id;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304702
4703 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304704 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4705 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304706 "controller type\t: %s(%dMB)\n",
4707 instance->is_imr ? "iMR" : "MR",
4708 le16_to_cpu(ctrl_info->memory_size));
4709
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304710 instance->disableOnlineCtrlReset =
4711 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304712 instance->secure_jbod_support =
4713 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304714 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4715 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304716 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4717 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304718 break;
4719
4720 case DCMD_TIMEOUT:
4721 switch (dcmd_timeout_ocr_possible(instance)) {
4722 case INITIATE_OCR:
4723 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4724 megasas_reset_fusion(instance->host,
4725 MFI_IO_TIMEOUT_OCR);
4726 break;
4727 case KILL_ADAPTER:
4728 megaraid_sas_kill_hba(instance);
4729 break;
4730 case IGNORE_TIMEOUT:
4731 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4732 __func__, __LINE__);
4733 break;
4734 }
4735 case DCMD_FAILED:
4736 megaraid_sas_kill_hba(instance);
4737 break;
4738
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304739 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004740
4741 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4742 ci, ci_h);
4743
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304744 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304745
4746
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004747 return ret;
4748}
4749
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304750/*
4751 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4752 * to firmware
4753 *
4754 * @instance: Adapter soft state
4755 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4756 MR_CRASH_BUF_TURN_OFF = 0
4757 MR_CRASH_BUF_TURN_ON = 1
4758 * @return 0 on success non-zero on failure.
4759 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4760 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4761 * that driver supports crash dump feature. This DCMD will be sent only if
4762 * crash dump feature is supported by the FW.
4763 *
4764 */
4765int megasas_set_crash_dump_params(struct megasas_instance *instance,
4766 u8 crash_buf_state)
4767{
4768 int ret = 0;
4769 struct megasas_cmd *cmd;
4770 struct megasas_dcmd_frame *dcmd;
4771
4772 cmd = megasas_get_cmd(instance);
4773
4774 if (!cmd) {
4775 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4776 return -ENOMEM;
4777 }
4778
4779
4780 dcmd = &cmd->frame->dcmd;
4781
4782 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4783 dcmd->mbox.b[0] = crash_buf_state;
4784 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304785 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304786 dcmd->sge_count = 1;
4787 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4788 dcmd->timeout = 0;
4789 dcmd->pad_0 = 0;
4790 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4791 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4792 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4793 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4794
Shivasharan Se7d36b82017-10-19 02:48:50 -07004795 if ((instance->adapter_type != MFI_SERIES) &&
4796 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304797 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304798 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304799 ret = megasas_issue_polled(instance, cmd);
4800
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304801 if (ret == DCMD_TIMEOUT) {
4802 switch (dcmd_timeout_ocr_possible(instance)) {
4803 case INITIATE_OCR:
4804 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4805 megasas_reset_fusion(instance->host,
4806 MFI_IO_TIMEOUT_OCR);
4807 break;
4808 case KILL_ADAPTER:
4809 megaraid_sas_kill_hba(instance);
4810 break;
4811 case IGNORE_TIMEOUT:
4812 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4813 __func__, __LINE__);
4814 break;
4815 }
4816 } else
4817 megasas_return_cmd(instance, cmd);
4818
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304819 return ret;
4820}
4821
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004822/**
bo yang31ea7082007-11-07 12:09:50 -05004823 * megasas_issue_init_mfi - Initializes the FW
4824 * @instance: Adapter soft state
4825 *
4826 * Issues the INIT MFI cmd
4827 */
4828static int
4829megasas_issue_init_mfi(struct megasas_instance *instance)
4830{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304831 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004832 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004833 struct megasas_init_frame *init_frame;
4834 struct megasas_init_queue_info *initq_info;
4835 dma_addr_t init_frame_h;
4836 dma_addr_t initq_info_h;
4837
4838 /*
4839 * Prepare a init frame. Note the init frame points to queue info
4840 * structure. Each frame has SGL allocated after first 64 bytes. For
4841 * this frame - since we don't need any SGL - we use SGL's space as
4842 * queue info structure
4843 *
4844 * We will not get a NULL command below. We just created the pool.
4845 */
4846 cmd = megasas_get_cmd(instance);
4847
4848 init_frame = (struct megasas_init_frame *)cmd->frame;
4849 initq_info = (struct megasas_init_queue_info *)
4850 ((unsigned long)init_frame + 64);
4851
4852 init_frame_h = cmd->frame_phys_addr;
4853 initq_info_h = init_frame_h + 64;
4854
4855 context = init_frame->context;
4856 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4857 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4858 init_frame->context = context;
4859
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304860 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4861 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004862
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304863 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4864 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004865
4866 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304867 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304868 init_frame->queue_info_new_phys_addr_lo =
4869 cpu_to_le32(lower_32_bits(initq_info_h));
4870 init_frame->queue_info_new_phys_addr_hi =
4871 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004872
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304873 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004874
4875 /*
4876 * disable the intr before firing the init frame to FW
4877 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304878 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004879
4880 /*
4881 * Issue the init frame in polled mode
4882 */
4883
4884 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004885 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004886 megasas_return_cmd(instance, cmd);
4887 goto fail_fw_init;
4888 }
4889
4890 megasas_return_cmd(instance, cmd);
4891
4892 return 0;
4893
4894fail_fw_init:
4895 return -EINVAL;
4896}
4897
adam radfordcd50ba82010-12-21 10:23:23 -08004898static u32
4899megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004900{
adam radfordcd50ba82010-12-21 10:23:23 -08004901 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004902 u32 context_sz;
4903 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004904
4905 reg_set = instance->reg_set;
4906
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004907 /*
4908 * Get various operational parameters from status register
4909 */
Sumant Patro1341c932006-01-25 12:02:40 -08004910 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004911 /*
4912 * Reduce the max supported cmds by 1. This is to ensure that the
4913 * reply_q_sz (1 more than the max cmd that driver may send)
4914 * does not exceed max cmds that the FW can support
4915 */
4916 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004917 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004918 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004919 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004920 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304921 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4922 * are reserved for IOCTL + driver's internal DCMDs.
4923 */
4924 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4925 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4926 instance->max_scsi_cmds = (instance->max_fw_cmds -
4927 MEGASAS_SKINNY_INT_CMDS);
4928 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4929 } else {
4930 instance->max_scsi_cmds = (instance->max_fw_cmds -
4931 MEGASAS_INT_CMDS);
4932 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4933 }
4934
Sumit Saxena308ec452016-01-28 21:04:30 +05304935 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304936 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004937 * Create a pool of commands
4938 */
4939 if (megasas_alloc_cmds(instance))
4940 goto fail_alloc_cmds;
4941
4942 /*
4943 * Allocate memory for reply queue. Length of reply queue should
4944 * be _one_ more than the maximum commands handled by the firmware.
4945 *
4946 * Note: When FW completes commands, it places corresponding contex
4947 * values in this circular reply queue. This circular queue is a fairly
4948 * typical producer-consumer queue. FW is the producer (of completed
4949 * commands) and the driver is the consumer.
4950 */
4951 context_sz = sizeof(u32);
4952 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4953
4954 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4955 reply_q_sz,
4956 &instance->reply_queue_h);
4957
4958 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004959 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004960 goto fail_reply_queue;
4961 }
4962
bo yang31ea7082007-11-07 12:09:50 -05004963 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004964 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004965
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304966 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304967 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4968 "Fail from %s %d\n", instance->unique_id,
4969 __func__, __LINE__);
4970 goto fail_fw_init;
4971 }
4972
bo yang39a98552010-09-22 22:36:29 -04004973 instance->fw_support_ieee = 0;
4974 instance->fw_support_ieee =
4975 (instance->instancet->read_fw_status_reg(reg_set) &
4976 0x04000000);
4977
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004978 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004979 instance->fw_support_ieee);
4980
4981 if (instance->fw_support_ieee)
4982 instance->flag_ieee = 1;
4983
adam radfordcd50ba82010-12-21 10:23:23 -08004984 return 0;
4985
4986fail_fw_init:
4987
4988 pci_free_consistent(instance->pdev, reply_q_sz,
4989 instance->reply_queue, instance->reply_queue_h);
4990fail_reply_queue:
4991 megasas_free_cmds(instance);
4992
4993fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004994 return 1;
4995}
4996
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304997/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01004998 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304999 * @instance: Adapter soft state
5000 *
5001 * Do not enable interrupt, only setup ISRs.
5002 *
5003 * Return 0 on success.
5004 */
5005static int
5006megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5007{
5008 struct pci_dev *pdev;
5009
5010 pdev = instance->pdev;
5011 instance->irq_context[0].instance = instance;
5012 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005013 if (request_irq(pci_irq_vector(pdev, 0),
5014 instance->instancet->service_isr, IRQF_SHARED,
5015 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305016 dev_err(&instance->pdev->dev,
5017 "Failed to register IRQ from %s %d\n",
5018 __func__, __LINE__);
5019 return -1;
5020 }
5021 return 0;
5022}
5023
5024/**
5025 * megasas_setup_irqs_msix - register MSI-x interrupts.
5026 * @instance: Adapter soft state
5027 * @is_probe: Driver probe check
5028 *
5029 * Do not enable interrupt, only setup ISRs.
5030 *
5031 * Return 0 on success.
5032 */
5033static int
5034megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5035{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005036 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305037 struct pci_dev *pdev;
5038
5039 pdev = instance->pdev;
5040
5041 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305042 for (i = 0; i < instance->msix_vectors; i++) {
5043 instance->irq_context[i].instance = instance;
5044 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005045 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305046 instance->instancet->service_isr, 0, "megasas",
5047 &instance->irq_context[i])) {
5048 dev_err(&instance->pdev->dev,
5049 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005050 for (j = 0; j < i; j++)
5051 free_irq(pci_irq_vector(pdev, j),
5052 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305053 /* Retry irq register for IO_APIC*/
5054 instance->msix_vectors = 0;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005055 if (is_probe) {
5056 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305057 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005058 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305059 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005060 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305061 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305062 }
5063 return 0;
5064}
5065
5066/*
5067 * megasas_destroy_irqs- unregister interrupts.
5068 * @instance: Adapter soft state
5069 * return: void
5070 */
5071static void
5072megasas_destroy_irqs(struct megasas_instance *instance) {
5073
5074 int i;
5075
5076 if (instance->msix_vectors)
5077 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005078 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305079 &instance->irq_context[i]);
5080 }
5081 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005082 free_irq(pci_irq_vector(instance->pdev, 0),
5083 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305084}
5085
adam radfordcd50ba82010-12-21 10:23:23 -08005086/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305087 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5088 * @instance: Adapter soft state
5089 * @is_probe: Driver probe check
5090 *
5091 * Return 0 on success.
5092 */
5093void
5094megasas_setup_jbod_map(struct megasas_instance *instance)
5095{
5096 int i;
5097 struct fusion_context *fusion = instance->ctrl_context;
5098 u32 pd_seq_map_sz;
5099
5100 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5101 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5102
5103 if (reset_devices || !fusion ||
5104 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
5105 dev_info(&instance->pdev->dev,
5106 "Jbod map is not supported %s %d\n",
5107 __func__, __LINE__);
5108 instance->use_seqnum_jbod_fp = false;
5109 return;
5110 }
5111
5112 if (fusion->pd_seq_sync[0])
5113 goto skip_alloc;
5114
5115 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5116 fusion->pd_seq_sync[i] = dma_alloc_coherent
5117 (&instance->pdev->dev, pd_seq_map_sz,
5118 &fusion->pd_seq_phys[i], GFP_KERNEL);
5119 if (!fusion->pd_seq_sync[i]) {
5120 dev_err(&instance->pdev->dev,
5121 "Failed to allocate memory from %s %d\n",
5122 __func__, __LINE__);
5123 if (i == 1) {
5124 dma_free_coherent(&instance->pdev->dev,
5125 pd_seq_map_sz, fusion->pd_seq_sync[0],
5126 fusion->pd_seq_phys[0]);
5127 fusion->pd_seq_sync[0] = NULL;
5128 }
5129 instance->use_seqnum_jbod_fp = false;
5130 return;
5131 }
5132 }
5133
5134skip_alloc:
5135 if (!megasas_sync_pd_seq_num(instance, false) &&
5136 !megasas_sync_pd_seq_num(instance, true))
5137 instance->use_seqnum_jbod_fp = true;
5138 else
5139 instance->use_seqnum_jbod_fp = false;
5140}
5141
5142/**
adam radfordcd50ba82010-12-21 10:23:23 -08005143 * megasas_init_fw - Initializes the FW
5144 * @instance: Adapter soft state
5145 *
5146 * This is the main function for initializing firmware
5147 */
5148
5149static int megasas_init_fw(struct megasas_instance *instance)
5150{
5151 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005152 u32 max_sectors_2, tmp_sectors, msix_enable;
5153 u32 scratch_pad_2, scratch_pad_3, scratch_pad_4;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005154 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005155 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305156 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005157 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005158 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005159 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305160 struct fusion_context *fusion;
5161
5162 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005163
5164 /* Find first memory bar */
5165 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005166 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005167 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005168 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005169 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005170 return -EBUSY;
5171 }
5172
Ben Collins11f8a7b2013-09-13 12:46:44 -04005173 base_addr = pci_resource_start(instance->pdev, instance->bar);
5174 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005175
5176 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005177 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005178 goto fail_ioremap;
5179 }
5180
5181 reg_set = instance->reg_set;
5182
Shivasharan Se7d36b82017-10-19 02:48:50 -07005183 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005184 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005185 else {
5186 switch (instance->pdev->device) {
5187 case PCI_DEVICE_ID_LSI_SAS1078R:
5188 case PCI_DEVICE_ID_LSI_SAS1078DE:
5189 instance->instancet = &megasas_instance_template_ppc;
5190 break;
5191 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5192 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5193 instance->instancet = &megasas_instance_template_gen2;
5194 break;
5195 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5196 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5197 instance->instancet = &megasas_instance_template_skinny;
5198 break;
5199 case PCI_DEVICE_ID_LSI_SAS1064R:
5200 case PCI_DEVICE_ID_DELL_PERC5:
5201 default:
5202 instance->instancet = &megasas_instance_template_xscale;
5203 instance->pd_list_not_supported = 1;
5204 break;
5205 }
adam radfordcd50ba82010-12-21 10:23:23 -08005206 }
5207
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305208 if (megasas_transition_to_ready(instance, 0)) {
5209 atomic_set(&instance->fw_reset_no_pci_access, 1);
5210 instance->instancet->adp_reset
5211 (instance, instance->reg_set);
5212 atomic_set(&instance->fw_reset_no_pci_access, 0);
5213 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005214 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305215 __func__);
5216
5217 /*waitting for about 30 second before retry*/
5218 ssleep(30);
5219
5220 if (megasas_transition_to_ready(instance, 0))
5221 goto fail_ready_state;
5222 }
adam radfordcd50ba82010-12-21 10:23:23 -08005223
Shivasharan Sf369a312017-10-19 02:48:52 -07005224 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005225 scratch_pad_3 =
5226 readl(&instance->reg_set->outbound_scratch_pad_3);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005227 instance->max_raid_mapsize = ((scratch_pad_3 >>
5228 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5229 MR_MAX_RAID_MAP_SIZE_MASK);
5230 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305231
adam radford3f1abce2011-05-11 18:33:47 -07005232 /* Check if MSI-X is supported while in ready state */
5233 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5234 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005235 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005236 int irq_flags = PCI_IRQ_MSIX;
5237
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305238 scratch_pad_2 = readl
5239 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005240 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305241 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005242 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5243 /* Thunderbolt Series*/
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305244 instance->msix_vectors = (scratch_pad_2
5245 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5246 fw_msix_count = instance->msix_vectors;
5247 } else { /* Invader series supports more than 8 MSI-x vectors*/
5248 instance->msix_vectors = ((scratch_pad_2
5249 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5250 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005251 if (instance->msix_vectors > 16)
5252 instance->msix_combined = true;
5253
Sumit Saxena179ac142016-01-28 21:04:28 +05305254 if (rdpq_enable)
5255 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5256 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305257 fw_msix_count = instance->msix_vectors;
5258 /* Save 1-15 reply post index address to local memory
5259 * Index 0 is already saved from reg offset
5260 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5261 */
5262 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5263 instance->reply_post_host_index_addr[loop] =
5264 (u32 __iomem *)
5265 ((u8 __iomem *)instance->reg_set +
5266 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5267 + (loop * 0x10));
5268 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305269 }
5270 if (msix_vectors)
5271 instance->msix_vectors = min(msix_vectors,
5272 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305273 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005274 instance->msix_vectors = 1;
5275 /* Don't bother allocating more MSI-X vectors than cpus */
5276 instance->msix_vectors = min(instance->msix_vectors,
5277 (unsigned int)num_online_cpus());
Hannes Reineckefad119b2016-12-02 12:52:23 +01005278 if (smp_affinity_enable)
5279 irq_flags |= PCI_IRQ_AFFINITY;
5280 i = pci_alloc_irq_vectors(instance->pdev, 1,
5281 instance->msix_vectors, irq_flags);
Jiang Liuc12de882014-11-03 20:44:20 +08005282 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005283 instance->msix_vectors = i;
5284 else
adam radfordc8e858f2011-10-08 18:15:13 -07005285 instance->msix_vectors = 0;
5286 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005287 /*
5288 * MSI-X host index 0 is common for all adapter.
5289 * It is used for all MPT based Adapters.
5290 */
5291 if (instance->msix_combined) {
5292 instance->reply_post_host_index_addr[0] =
5293 (u32 *)((u8 *)instance->reg_set +
5294 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5295 } else {
5296 instance->reply_post_host_index_addr[0] =
5297 (u32 *)((u8 *)instance->reg_set +
5298 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5299 }
5300
Shivasharan S64ff64b2017-03-10 03:22:12 -08005301 if (!instance->msix_vectors) {
5302 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5303 if (i < 0)
5304 goto fail_setup_irqs;
5305 }
adam radford3f1abce2011-05-11 18:33:47 -07005306
Tomas Henzl258c3af2015-06-02 16:09:46 +05305307 dev_info(&instance->pdev->dev,
5308 "firmware supports msix\t: (%d)", fw_msix_count);
5309 dev_info(&instance->pdev->dev,
5310 "current msix/online cpus\t: (%d/%d)\n",
5311 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305312 dev_info(&instance->pdev->dev,
5313 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305314
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305315 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5316 (unsigned long)instance);
5317
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305318 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5319 GFP_KERNEL);
5320 if (instance->ctrl_info == NULL)
5321 goto fail_init_adapter;
5322
5323 /*
5324 * Below are default value for legacy Firmware.
5325 * non-fusion based controllers
5326 */
5327 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5328 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005329 /* Get operational params, sge flags, send init cmd to controller */
5330 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005331 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005332
Shivasharan Sf369a312017-10-19 02:48:52 -07005333 if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan S15dd0382017-02-10 00:59:10 -08005334 scratch_pad_4 =
5335 readl(&instance->reg_set->outbound_scratch_pad_4);
5336 if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
5337 MR_DEFAULT_NVME_PAGE_SHIFT)
5338 instance->nvme_page_size =
5339 (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
5340
5341 dev_info(&instance->pdev->dev,
5342 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5343 }
5344
Tomas Henzl18103ef2016-11-01 17:32:02 +01005345 if (instance->msix_vectors ?
5346 megasas_setup_irqs_msix(instance, 1) :
5347 megasas_setup_irqs_ioapic(instance))
5348 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305349
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305350 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005351
Andy Lutomirski13f30772016-05-03 10:24:31 -07005352 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005353
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305354 megasas_setup_jbod_map(instance);
5355
bo yang39a98552010-09-22 22:36:29 -04005356 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005357 * the following function will get the PD LIST.
5358 */
5359 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005360 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005361 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005362 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Shivasharan S72bff2d2017-02-10 00:59:33 -08005363 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005364 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005365
Yang, Bobdc6fb82009-12-06 08:30:19 -07005366 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005367
5368 /* stream detection initialization */
Shivasharan Sf369a312017-10-19 02:48:52 -07005369 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005370 fusion->stream_detect_by_ld =
Shivasharan S41064f12017-02-10 00:59:37 -08005371 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5372 * MAX_LOGICAL_DRIVES_EXT,
5373 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005374 if (!fusion->stream_detect_by_ld) {
5375 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08005376 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005377 goto fail_get_ld_pd_list;
5378 }
5379 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5380 fusion->stream_detect_by_ld[i] =
5381 kmalloc(sizeof(struct LD_STREAM_DETECT),
5382 GFP_KERNEL);
5383 if (!fusion->stream_detect_by_ld[i]) {
5384 dev_err(&instance->pdev->dev,
5385 "unable to allocate stream detect by LD\n ");
5386 for (j = 0; j < i; ++j)
5387 kfree(fusion->stream_detect_by_ld[j]);
5388 kfree(fusion->stream_detect_by_ld);
5389 fusion->stream_detect_by_ld = NULL;
5390 goto fail_get_ld_pd_list;
5391 }
5392 fusion->stream_detect_by_ld[i]->mru_bit_map
5393 = MR_STREAM_BITMAP;
5394 }
5395 }
5396
adam radford21c9e162013-09-06 15:27:14 -07005397 if (megasas_ld_list_query(instance,
5398 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
Shivasharan S72bff2d2017-02-10 00:59:33 -08005399 goto fail_get_ld_pd_list;
Yang, Bobdc6fb82009-12-06 08:30:19 -07005400
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005401 /*
5402 * Compute the max allowed sectors per IO: The controller info has two
5403 * limits on max sectors. Driver should use the minimum of these two.
5404 *
5405 * 1 << stripe_sz_ops.min = max sectors per strip
5406 *
5407 * Note that older firmwares ( < FW ver 30) didn't report information
5408 * to calculate max_sectors_1. So the number ended up as zero always.
5409 */
bo yang14faea92007-11-09 04:14:00 -05005410 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305411 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005412
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305413 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5414 le16_to_cpu(ctrl_info->max_strips_per_io);
5415 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005416
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005417 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305418
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305419 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5420 instance->passive = ctrl_info->cluster.passive;
5421 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305422 instance->UnevenSpanSupport =
5423 ctrl_info->adapterOperations2.supportUnevenSpans;
5424 if (instance->UnevenSpanSupport) {
5425 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305426 if (MR_ValidateMapInfo(instance))
5427 fusion->fast_path_io = 1;
5428 else
5429 fusion->fast_path_io = 0;
5430
5431 }
5432 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305433 instance->requestorId = ctrl_info->iov.requestorId;
5434 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5435 if (!ctrl_info->adapterOperations2.activePassive)
5436 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305437
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305438 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5439 instance->PlasmaFW111 ? "1.11" : "new");
5440
5441 if (instance->PlasmaFW111) {
5442 iovPtr = (struct IOV_111 *)
5443 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5444 instance->requestorId = iovPtr->requestorId;
5445 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305446 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305447 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5448 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305449 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305450
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305451 instance->crash_dump_fw_support =
5452 ctrl_info->adapterOperations3.supportCrashDump;
5453 instance->crash_dump_drv_support =
5454 (instance->crash_dump_fw_support &&
5455 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305456 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305457 megasas_set_crash_dump_params(instance,
5458 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005459
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305460 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305461 if (instance->crash_dump_buf)
5462 pci_free_consistent(instance->pdev,
5463 CRASH_DMA_BUF_SIZE,
5464 instance->crash_dump_buf,
5465 instance->crash_dump_h);
5466 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005467 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305468
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305469
5470 dev_info(&instance->pdev->dev,
5471 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5472 le16_to_cpu(ctrl_info->pci.vendor_id),
5473 le16_to_cpu(ctrl_info->pci.device_id),
5474 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5475 le16_to_cpu(ctrl_info->pci.sub_device_id));
5476 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5477 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305478 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5479 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305480 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5481 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305482
5483
bo yang14faea92007-11-09 04:14:00 -05005484 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305485 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005486 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5487 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005488
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305489 /* Check for valid throttlequeuedepth module parameter */
5490 if (throttlequeuedepth &&
5491 throttlequeuedepth <= instance->max_scsi_cmds)
5492 instance->throttlequeuedepth = throttlequeuedepth;
5493 else
5494 instance->throttlequeuedepth =
5495 MEGASAS_THROTTLE_QUEUE_DEPTH;
5496
Shivasharan Se636a7a2017-08-23 04:46:56 -07005497 if ((resetwaittime < 1) ||
5498 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305499 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5500
5501 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5502 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005503
adam radford229fe472014-03-10 02:51:56 -07005504 /* Launch SR-IOV heartbeat timer */
5505 if (instance->requestorId) {
5506 if (!megasas_sriov_start_heartbeat(instance, 1))
5507 megasas_start_timer(instance,
5508 &instance->sriov_heartbeat_timer,
5509 megasas_sriov_heartbeat_handler,
5510 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5511 else
5512 instance->skip_heartbeat_timer_del = 1;
5513 }
5514
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005515 return 0;
5516
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005517fail_get_ld_pd_list:
5518 instance->instancet->disable_intr(instance);
Tomas Henzl18103ef2016-11-01 17:32:02 +01005519fail_init_adapter:
Hannes Reineckefad119b2016-12-02 12:52:23 +01005520 megasas_destroy_irqs(instance);
5521fail_setup_irqs:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305522 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005523 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305524 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005525fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305526 kfree(instance->ctrl_info);
5527 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005528 iounmap(instance->reg_set);
5529
Shivasharan S72bff2d2017-02-10 00:59:33 -08005530fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005531 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005532
Shivasharan S72bff2d2017-02-10 00:59:33 -08005533 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5534 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005535 return -EINVAL;
5536}
5537
5538/**
5539 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005540 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005541 */
5542static void megasas_release_mfi(struct megasas_instance *instance)
5543{
adam radford9c915a82010-12-21 13:34:31 -08005544 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005545
adam radford9c915a82010-12-21 13:34:31 -08005546 if (instance->reply_queue)
5547 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005548 instance->reply_queue, instance->reply_queue_h);
5549
5550 megasas_free_cmds(instance);
5551
5552 iounmap(instance->reg_set);
5553
Yinghai Lue7f85162016-08-05 23:37:34 -07005554 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005555}
5556
5557/**
5558 * megasas_get_seq_num - Gets latest event sequence numbers
5559 * @instance: Adapter soft state
5560 * @eli: FW event log sequence numbers information
5561 *
5562 * FW maintains a log of all events in a non-volatile area. Upper layers would
5563 * usually find out the latest sequence number of the events, the seq number at
5564 * the boot etc. They would "read" all the events below the latest seq number
5565 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5566 * number), they would subsribe to AEN (asynchronous event notification) and
5567 * wait for the events to happen.
5568 */
5569static int
5570megasas_get_seq_num(struct megasas_instance *instance,
5571 struct megasas_evt_log_info *eli)
5572{
5573 struct megasas_cmd *cmd;
5574 struct megasas_dcmd_frame *dcmd;
5575 struct megasas_evt_log_info *el_info;
5576 dma_addr_t el_info_h = 0;
5577
5578 cmd = megasas_get_cmd(instance);
5579
5580 if (!cmd) {
5581 return -ENOMEM;
5582 }
5583
5584 dcmd = &cmd->frame->dcmd;
5585 el_info = pci_alloc_consistent(instance->pdev,
5586 sizeof(struct megasas_evt_log_info),
5587 &el_info_h);
5588
5589 if (!el_info) {
5590 megasas_return_cmd(instance, cmd);
5591 return -ENOMEM;
5592 }
5593
5594 memset(el_info, 0, sizeof(*el_info));
5595 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5596
5597 dcmd->cmd = MFI_CMD_DCMD;
5598 dcmd->cmd_status = 0x0;
5599 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305600 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005601 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005602 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305603 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5604 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5605 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5606 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005607
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305608 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5609 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305610 /*
5611 * Copy the data back into callers buffer
5612 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305613 eli->newest_seq_num = el_info->newest_seq_num;
5614 eli->oldest_seq_num = el_info->oldest_seq_num;
5615 eli->clear_seq_num = el_info->clear_seq_num;
5616 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5617 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305618 } else
5619 dev_err(&instance->pdev->dev, "DCMD failed "
5620 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005621
5622 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5623 el_info, el_info_h);
5624
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305625 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005626
5627 return 0;
5628}
5629
5630/**
5631 * megasas_register_aen - Registers for asynchronous event notification
5632 * @instance: Adapter soft state
5633 * @seq_num: The starting sequence number
5634 * @class_locale: Class of the event
5635 *
5636 * This function subscribes for AEN for events beyond the @seq_num. It requests
5637 * to be notified if and only if the event is of type @class_locale
5638 */
5639static int
5640megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5641 u32 class_locale_word)
5642{
5643 int ret_val;
5644 struct megasas_cmd *cmd;
5645 struct megasas_dcmd_frame *dcmd;
5646 union megasas_evt_class_locale curr_aen;
5647 union megasas_evt_class_locale prev_aen;
5648
5649 /*
5650 * If there an AEN pending already (aen_cmd), check if the
5651 * class_locale of that pending AEN is inclusive of the new
5652 * AEN request we currently have. If it is, then we don't have
5653 * to do anything. In other words, whichever events the current
5654 * AEN request is subscribing to, have already been subscribed
5655 * to.
5656 *
5657 * If the old_cmd is _not_ inclusive, then we have to abort
5658 * that command, form a class_locale that is superset of both
5659 * old and current and re-issue to the FW
5660 */
5661
5662 curr_aen.word = class_locale_word;
5663
5664 if (instance->aen_cmd) {
5665
Christoph Hellwiga9555532015-04-23 16:34:24 +05305666 prev_aen.word =
5667 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005668
Shivasharan S91b3d9f2017-08-23 04:47:01 -07005669 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5670 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5671 dev_info(&instance->pdev->dev,
5672 "%s %d out of range class %d send by application\n",
5673 __func__, __LINE__, curr_aen.members.class);
5674 return 0;
5675 }
5676
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005677 /*
5678 * A class whose enum value is smaller is inclusive of all
5679 * higher values. If a PROGRESS (= -1) was previously
5680 * registered, then a new registration requests for higher
5681 * classes need not be sent to FW. They are automatically
5682 * included.
5683 *
5684 * Locale numbers don't have such hierarchy. They are bitmap
5685 * values
5686 */
5687 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305688 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005689 curr_aen.members.locale)) {
5690 /*
5691 * Previously issued event registration includes
5692 * current request. Nothing to do.
5693 */
5694 return 0;
5695 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305696 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005697
5698 if (prev_aen.members.class < curr_aen.members.class)
5699 curr_aen.members.class = prev_aen.members.class;
5700
5701 instance->aen_cmd->abort_aen = 1;
5702 ret_val = megasas_issue_blocked_abort_cmd(instance,
5703 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305704 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005705
5706 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005707 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005708 "previous AEN command\n");
5709 return ret_val;
5710 }
5711 }
5712 }
5713
5714 cmd = megasas_get_cmd(instance);
5715
5716 if (!cmd)
5717 return -ENOMEM;
5718
5719 dcmd = &cmd->frame->dcmd;
5720
5721 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5722
5723 /*
5724 * Prepare DCMD for aen registration
5725 */
5726 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5727
5728 dcmd->cmd = MFI_CMD_DCMD;
5729 dcmd->cmd_status = 0x0;
5730 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305731 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005732 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005733 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305734 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5735 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5736 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005737 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305738 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5739 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5740 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005741
Yang, Bof4c9a132009-10-06 14:43:28 -06005742 if (instance->aen_cmd != NULL) {
5743 megasas_return_cmd(instance, cmd);
5744 return 0;
5745 }
5746
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005747 /*
5748 * Store reference to the cmd used to register for AEN. When an
5749 * application wants us to register for AEN, we have to abort this
5750 * cmd and re-register with a new EVENT LOCALE supplied by that app
5751 */
5752 instance->aen_cmd = cmd;
5753
5754 /*
5755 * Issue the aen registration frame
5756 */
adam radford9c915a82010-12-21 13:34:31 -08005757 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005758
5759 return 0;
5760}
5761
Shivasharan S96188a82017-02-10 00:59:11 -08005762/* megasas_get_target_prop - Send DCMD with below details to firmware.
5763 *
5764 * This DCMD will fetch few properties of LD/system PD defined
5765 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
5766 *
5767 * DCMD send by drivers whenever new target is added to the OS.
5768 *
5769 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
5770 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
5771 * 0 = system PD, 1 = LD.
5772 * dcmd.mbox.s[1] - TargetID for LD/system PD.
5773 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
5774 *
5775 * @instance: Adapter soft state
5776 * @sdev: OS provided scsi device
5777 *
5778 * Returns 0 on success non-zero on failure.
5779 */
5780static int
5781megasas_get_target_prop(struct megasas_instance *instance,
5782 struct scsi_device *sdev)
5783{
5784 int ret;
5785 struct megasas_cmd *cmd;
5786 struct megasas_dcmd_frame *dcmd;
5787 u16 targetId = (sdev->channel % 2) + sdev->id;
5788
5789 cmd = megasas_get_cmd(instance);
5790
5791 if (!cmd) {
5792 dev_err(&instance->pdev->dev,
5793 "Failed to get cmd %s\n", __func__);
5794 return -ENOMEM;
5795 }
5796
5797 dcmd = &cmd->frame->dcmd;
5798
5799 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
5800 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5801 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
5802
5803 dcmd->mbox.s[1] = cpu_to_le16(targetId);
5804 dcmd->cmd = MFI_CMD_DCMD;
5805 dcmd->cmd_status = 0xFF;
5806 dcmd->sge_count = 1;
5807 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5808 dcmd->timeout = 0;
5809 dcmd->pad_0 = 0;
5810 dcmd->data_xfer_len =
5811 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5812 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
5813 dcmd->sgl.sge32[0].phys_addr =
5814 cpu_to_le32(instance->tgt_prop_h);
5815 dcmd->sgl.sge32[0].length =
5816 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5817
Shivasharan Se7d36b82017-10-19 02:48:50 -07005818 if ((instance->adapter_type != MFI_SERIES) &&
5819 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08005820 ret = megasas_issue_blocked_cmd(instance,
5821 cmd, MFI_IO_TIMEOUT_SECS);
5822 else
5823 ret = megasas_issue_polled(instance, cmd);
5824
5825 switch (ret) {
5826 case DCMD_TIMEOUT:
5827 switch (dcmd_timeout_ocr_possible(instance)) {
5828 case INITIATE_OCR:
5829 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5830 megasas_reset_fusion(instance->host,
5831 MFI_IO_TIMEOUT_OCR);
5832 break;
5833 case KILL_ADAPTER:
5834 megaraid_sas_kill_hba(instance);
5835 break;
5836 case IGNORE_TIMEOUT:
5837 dev_info(&instance->pdev->dev,
5838 "Ignore DCMD timeout: %s %d\n",
5839 __func__, __LINE__);
5840 break;
5841 }
5842 break;
5843
5844 default:
5845 megasas_return_cmd(instance, cmd);
5846 }
5847 if (ret != DCMD_SUCCESS)
5848 dev_err(&instance->pdev->dev,
5849 "return from %s %d return value %d\n",
5850 __func__, __LINE__, ret);
5851
5852 return ret;
5853}
5854
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005855/**
5856 * megasas_start_aen - Subscribes to AEN during driver load time
5857 * @instance: Adapter soft state
5858 */
5859static int megasas_start_aen(struct megasas_instance *instance)
5860{
5861 struct megasas_evt_log_info eli;
5862 union megasas_evt_class_locale class_locale;
5863
5864 /*
5865 * Get the latest sequence number from FW
5866 */
5867 memset(&eli, 0, sizeof(eli));
5868
5869 if (megasas_get_seq_num(instance, &eli))
5870 return -1;
5871
5872 /*
5873 * Register AEN with FW for latest sequence number plus 1
5874 */
5875 class_locale.members.reserved = 0;
5876 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5877 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5878
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305879 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305880 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305881 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005882}
5883
5884/**
5885 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5886 * @instance: Adapter soft state
5887 */
5888static int megasas_io_attach(struct megasas_instance *instance)
5889{
5890 struct Scsi_Host *host = instance->host;
5891
5892 /*
5893 * Export parameters required by SCSI mid-layer
5894 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005895 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305896 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005897 host->this_id = instance->init_id;
5898 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005899
5900 if (instance->fw_support_ieee)
5901 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5902
Yang, Bo1fd10682010-10-12 07:18:50 -06005903 /*
5904 * Check if the module parameter value for max_sectors can be used
5905 */
5906 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5907 instance->max_sectors_per_req = max_sectors;
5908 else {
5909 if (max_sectors) {
5910 if (((instance->pdev->device ==
5911 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5912 (instance->pdev->device ==
5913 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5914 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5915 instance->max_sectors_per_req = max_sectors;
5916 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005917 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005918 "and <= %d (or < 1MB for GEN2 controller)\n",
5919 instance->max_sectors_per_req);
5920 }
5921 }
5922 }
5923
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005924 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005925 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005926 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5927 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5928 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005929 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005930
5931 /*
5932 * Notify the mid-layer about the new controller
5933 */
5934 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305935 dev_err(&instance->pdev->dev,
5936 "Failed to add host from %s %d\n",
5937 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005938 return -ENODEV;
5939 }
5940
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005941 return 0;
5942}
5943
bo yang31ea7082007-11-07 12:09:50 -05005944static int
5945megasas_set_dma_mask(struct pci_dev *pdev)
5946{
5947 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005948 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005949 */
5950 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005951 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005952
Yang Hongyang284901a2009-04-06 19:01:15 -07005953 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005954 goto fail_set_dma_mask;
5955 }
5956 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005957 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005958 goto fail_set_dma_mask;
5959 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305960 /*
5961 * Ensure that all data structures are allocated in 32-bit
5962 * memory.
5963 */
5964 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5965 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5966 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5967 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5968 dev_info(&pdev->dev, "set 32bit DMA mask"
5969 "and 32 bit consistent mask\n");
5970 else
5971 goto fail_set_dma_mask;
5972 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305973
bo yang31ea7082007-11-07 12:09:50 -05005974 return 0;
5975
5976fail_set_dma_mask:
5977 return 1;
5978}
5979
Shivasharan Sc3651782017-10-19 02:48:48 -07005980/*
5981 * megasas_set_adapter_type - Set adapter type.
5982 * Supported controllers can be divided in
5983 * 4 categories- enum MR_ADAPTER_TYPE {
5984 * MFI_SERIES = 1,
5985 * THUNDERBOLT_SERIES = 2,
5986 * INVADER_SERIES = 3,
5987 * VENTURA_SERIES = 4,
5988 * };
5989 * @instance: Adapter soft state
5990 * return: void
5991 */
5992static inline void megasas_set_adapter_type(struct megasas_instance *instance)
5993{
Shivasharan S754f1ba2017-10-19 02:48:49 -07005994 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
5995 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005996 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07005997 } else {
5998 switch (instance->pdev->device) {
5999 case PCI_DEVICE_ID_LSI_VENTURA:
6000 case PCI_DEVICE_ID_LSI_CRUSADER:
6001 case PCI_DEVICE_ID_LSI_HARPOON:
6002 case PCI_DEVICE_ID_LSI_TOMCAT:
6003 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6004 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6005 instance->adapter_type = VENTURA_SERIES;
6006 break;
6007 case PCI_DEVICE_ID_LSI_FUSION:
6008 case PCI_DEVICE_ID_LSI_PLASMA:
6009 instance->adapter_type = THUNDERBOLT_SERIES;
6010 break;
6011 case PCI_DEVICE_ID_LSI_INVADER:
6012 case PCI_DEVICE_ID_LSI_INTRUDER:
6013 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6014 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6015 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6016 case PCI_DEVICE_ID_LSI_FURY:
6017 instance->adapter_type = INVADER_SERIES;
6018 break;
6019 default: /* For all other supported controllers */
6020 instance->adapter_type = MFI_SERIES;
6021 break;
6022 }
Shivasharan Sc3651782017-10-19 02:48:48 -07006023 }
6024}
6025
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006026/**
6027 * megasas_probe_one - PCI hotplug entry point
6028 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08006029 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006030 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006031static int megasas_probe_one(struct pci_dev *pdev,
6032 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006033{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306034 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006035 struct Scsi_Host *host;
6036 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08006037 u16 control = 0;
6038
6039 /* Reset MSI-X in the kdump kernel */
6040 if (reset_devices) {
6041 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6042 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06006043 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006044 &control);
6045 if (control & PCI_MSIX_FLAGS_ENABLE) {
6046 dev_info(&pdev->dev, "resetting MSI-X\n");
6047 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06006048 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006049 control &
6050 ~PCI_MSIX_FLAGS_ENABLE);
6051 }
6052 }
6053 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006054
6055 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006056 * PCI prepping: enable device set bus mastering and dma mask
6057 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006058 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006059
6060 if (rval) {
6061 return rval;
6062 }
6063
6064 pci_set_master(pdev);
6065
bo yang31ea7082007-11-07 12:09:50 -05006066 if (megasas_set_dma_mask(pdev))
6067 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006068
6069 host = scsi_host_alloc(&megasas_template,
6070 sizeof(struct megasas_instance));
6071
6072 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006073 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006074 goto fail_alloc_instance;
6075 }
6076
6077 instance = (struct megasas_instance *)host->hostdata;
6078 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006079 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08006080 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006081
Shivasharan Sc3651782017-10-19 02:48:48 -07006082 megasas_set_adapter_type(instance);
Shivasharan S5fc499b2017-02-10 00:59:17 -08006083
Shivasharan Sc3651782017-10-19 02:48:48 -07006084 switch (instance->adapter_type) {
6085 case MFI_SERIES:
adam radford9c915a82010-12-21 13:34:31 -08006086 instance->producer =
6087 pci_alloc_consistent(pdev, sizeof(u32),
6088 &instance->producer_h);
6089 instance->consumer =
6090 pci_alloc_consistent(pdev, sizeof(u32),
6091 &instance->consumer_h);
6092
6093 if (!instance->producer || !instance->consumer) {
Colin Ian King4086eae52016-09-12 13:42:12 +01006094 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08006095 "memory for producer, consumer\n");
6096 goto fail_alloc_dma_buf;
6097 }
6098
6099 *instance->producer = 0;
6100 *instance->consumer = 0;
Shivasharan Sc3651782017-10-19 02:48:48 -07006101
adam radford9c915a82010-12-21 13:34:31 -08006102 break;
Shivasharan Sc3651782017-10-19 02:48:48 -07006103 case VENTURA_SERIES:
Shivasharan Sc3651782017-10-19 02:48:48 -07006104 case THUNDERBOLT_SERIES:
6105 case INVADER_SERIES:
6106 if (megasas_alloc_fusion_context(instance)) {
6107 megasas_free_fusion_context(instance);
6108 goto fail_alloc_dma_buf;
6109 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006110 }
6111
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306112 /* Crash dump feature related initialisation*/
6113 instance->drv_buf_index = 0;
6114 instance->drv_buf_alloc = 0;
6115 instance->crash_dump_fw_support = 0;
6116 instance->crash_dump_app_support = 0;
6117 instance->fw_crash_state = UNAVAILABLE;
6118 spin_lock_init(&instance->crashdump_lock);
6119 instance->crash_dump_buf = NULL;
6120
Yang, Boc3518832009-10-06 14:18:02 -06006121 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06006122 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006123 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04006124 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05306125 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306126 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006127
6128 instance->evt_detail = pci_alloc_consistent(pdev,
6129 sizeof(struct
6130 megasas_evt_detail),
6131 &instance->evt_detail_h);
6132
6133 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006134 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006135 "event detail structure\n");
6136 goto fail_alloc_dma_buf;
6137 }
6138
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006139 if (!reset_devices) {
6140 instance->system_info_buf = pci_zalloc_consistent(pdev,
6141 sizeof(struct MR_DRV_SYSTEM_INFO),
6142 &instance->system_info_h);
6143 if (!instance->system_info_buf)
6144 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
Sumit Saxena2216c302016-01-28 21:04:26 +05306145
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006146 instance->pd_info = pci_alloc_consistent(pdev,
6147 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
6148
6149 if (!instance->pd_info)
6150 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
6151
weiping3b8328e2017-08-08 13:15:55 +08006152 instance->tgt_prop = pci_alloc_consistent(pdev,
6153 sizeof(struct MR_TARGET_PROPERTIES), &instance->tgt_prop_h);
6154
Shivasharan S96188a82017-02-10 00:59:11 -08006155 if (!instance->tgt_prop)
6156 dev_err(&instance->pdev->dev, "Failed to alloc mem for tgt_prop\n");
6157
Sumit Saxenac3e385a2016-04-15 00:23:30 -07006158 instance->crash_dump_buf = pci_alloc_consistent(pdev,
6159 CRASH_DMA_BUF_SIZE,
6160 &instance->crash_dump_h);
6161 if (!instance->crash_dump_buf)
6162 dev_err(&pdev->dev, "Can't allocate Firmware "
6163 "crash dump DMA buffer\n");
6164 }
Sumit Saxena2216c302016-01-28 21:04:26 +05306165
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006166 /*
6167 * Initialize locks and queues
6168 */
6169 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04006170 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006171
Sumant Patroe4a082c2006-05-30 12:03:37 -07006172 atomic_set(&instance->fw_outstanding,0);
6173
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006174 init_waitqueue_head(&instance->int_cmd_wait_q);
6175 init_waitqueue_head(&instance->abort_cmd_wait_q);
6176
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306177 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04006178 spin_lock_init(&instance->hba_lock);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006179 spin_lock_init(&instance->stream_lock);
bo yang7343eb62007-11-09 04:35:44 -05006180 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006181
adam radford9c915a82010-12-21 13:34:31 -08006182 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05306183 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006184
6185 /*
6186 * Initialize PCI related and misc parameters
6187 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006188 instance->host = host;
6189 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6190 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306191 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006192
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306193
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006194 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306195 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06006196 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006197
Sumant Patro658dced2006-10-03 13:09:14 -07006198 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006199 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06006200 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006201 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04006202 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05306203 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04006204
Shivasharan Se7d36b82017-10-19 02:48:50 -07006205 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006206 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306207 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
6208 } else
adam radford9c915a82010-12-21 13:34:31 -08006209 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07006210
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006211 /*
adam radford0a770662011-02-24 20:56:12 -08006212 * Initialize MFI Firmware
6213 */
6214 if (megasas_init_fw(instance))
6215 goto fail_init_mfi;
6216
adam radford229fe472014-03-10 02:51:56 -07006217 if (instance->requestorId) {
6218 if (instance->PlasmaFW111) {
6219 instance->vf_affiliation_111 =
6220 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
6221 &instance->vf_affiliation_111_h);
6222 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006223 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006224 "memory for VF affiliation buffer\n");
6225 } else {
6226 instance->vf_affiliation =
6227 pci_alloc_consistent(pdev,
6228 (MAX_LOGICAL_DRIVES + 1) *
6229 sizeof(struct MR_LD_VF_AFFILIATION),
6230 &instance->vf_affiliation_h);
6231 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006232 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006233 "memory for VF affiliation buffer\n");
6234 }
6235 }
6236
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006237 /*
6238 * Store instance in PCI softstate
6239 */
6240 pci_set_drvdata(pdev, instance);
6241
6242 /*
6243 * Add this controller to megasas_mgmt_info structure so that it
6244 * can be exported to management applications
6245 */
6246 megasas_mgmt_info.count++;
6247 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6248 megasas_mgmt_info.max_index++;
6249
6250 /*
adam radford541f90b2011-05-11 18:34:29 -07006251 * Register with SCSI mid-layer
6252 */
6253 if (megasas_io_attach(instance))
6254 goto fail_io_attach;
6255
6256 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306257 /*
6258 * Trigger SCSI to scan our drives
6259 */
6260 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07006261
6262 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006263 * Initiate AEN (Asynchronous Event Notification)
6264 */
6265 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006266 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006267 goto fail_start_aen;
6268 }
6269
Adam Radford9ea81f82014-07-09 15:17:57 -07006270 /* Get current SR-IOV LD/VF affiliation */
6271 if (instance->requestorId)
6272 megasas_get_ld_vf_affiliation(instance, 1);
6273
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006274 return 0;
6275
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006276fail_start_aen:
6277fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006278 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006279 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08006280 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006281
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306282 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306283 megasas_destroy_irqs(instance);
6284
Shivasharan Se7d36b82017-10-19 02:48:50 -07006285 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08006286 megasas_release_fusion(instance);
6287 else
6288 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006289 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006290 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306291fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006292fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006293 if (instance->evt_detail)
6294 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6295 instance->evt_detail,
6296 instance->evt_detail_h);
6297
Sumit Saxena2216c302016-01-28 21:04:26 +05306298 if (instance->pd_info)
6299 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6300 instance->pd_info,
6301 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006302 if (instance->tgt_prop)
6303 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6304 instance->tgt_prop,
6305 instance->tgt_prop_h);
adam radfordeb1b1232011-02-24 20:55:56 -08006306 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006307 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6308 instance->producer_h);
6309 if (instance->consumer)
6310 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6311 instance->consumer_h);
6312 scsi_host_put(host);
6313
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006314fail_alloc_instance:
6315fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006316 pci_disable_device(pdev);
6317
6318 return -ENODEV;
6319}
6320
6321/**
6322 * megasas_flush_cache - Requests FW to flush all its caches
6323 * @instance: Adapter soft state
6324 */
6325static void megasas_flush_cache(struct megasas_instance *instance)
6326{
6327 struct megasas_cmd *cmd;
6328 struct megasas_dcmd_frame *dcmd;
6329
Sumit Saxena8a01a412016-01-28 21:04:32 +05306330 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006331 return;
6332
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006333 cmd = megasas_get_cmd(instance);
6334
6335 if (!cmd)
6336 return;
6337
6338 dcmd = &cmd->frame->dcmd;
6339
6340 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6341
6342 dcmd->cmd = MFI_CMD_DCMD;
6343 dcmd->cmd_status = 0x0;
6344 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306345 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006346 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006347 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006348 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306349 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006350 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6351
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306352 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6353 != DCMD_SUCCESS) {
6354 dev_err(&instance->pdev->dev,
6355 "return from %s %d\n", __func__, __LINE__);
6356 return;
6357 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006358
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306359 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006360}
6361
6362/**
6363 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6364 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006365 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006366 */
bo yang31ea7082007-11-07 12:09:50 -05006367static void megasas_shutdown_controller(struct megasas_instance *instance,
6368 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006369{
6370 struct megasas_cmd *cmd;
6371 struct megasas_dcmd_frame *dcmd;
6372
Sumit Saxena8a01a412016-01-28 21:04:32 +05306373 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006374 return;
6375
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006376 cmd = megasas_get_cmd(instance);
6377
6378 if (!cmd)
6379 return;
6380
6381 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306382 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306383 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006384 if (instance->map_update_cmd)
6385 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306386 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306387 if (instance->jbod_seq_cmd)
6388 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306389 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306390
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006391 dcmd = &cmd->frame->dcmd;
6392
6393 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6394
6395 dcmd->cmd = MFI_CMD_DCMD;
6396 dcmd->cmd_status = 0x0;
6397 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306398 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006399 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006400 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006401 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306402 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006403
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306404 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6405 != DCMD_SUCCESS) {
6406 dev_err(&instance->pdev->dev,
6407 "return from %s %d\n", __func__, __LINE__);
6408 return;
6409 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006410
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306411 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006412}
6413
Jiri Slaby33139b22008-05-01 17:56:02 +02006414#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006415/**
bo yangad84db22007-11-09 04:40:16 -05006416 * megasas_suspend - driver suspend entry point
6417 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006418 * @state: PCI power state to suspend routine
6419 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006420static int
bo yang31ea7082007-11-07 12:09:50 -05006421megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6422{
6423 struct Scsi_Host *host;
6424 struct megasas_instance *instance;
6425
6426 instance = pci_get_drvdata(pdev);
6427 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006428 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006429
adam radford229fe472014-03-10 02:51:56 -07006430 /* Shutdown SR-IOV heartbeat timer */
6431 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6432 del_timer_sync(&instance->sriov_heartbeat_timer);
6433
bo yang31ea7082007-11-07 12:09:50 -05006434 megasas_flush_cache(instance);
6435 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006436
6437 /* cancel the delayed work if this work still in queue */
6438 if (instance->ev != NULL) {
6439 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006440 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006441 instance->ev = NULL;
6442 }
6443
bo yang31ea7082007-11-07 12:09:50 -05006444 tasklet_kill(&instance->isr_tasklet);
6445
6446 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306447 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006448
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306449 megasas_destroy_irqs(instance);
6450
adam radfordc8e858f2011-10-08 18:15:13 -07006451 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006452 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006453
6454 pci_save_state(pdev);
6455 pci_disable_device(pdev);
6456
6457 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6458
6459 return 0;
6460}
6461
6462/**
6463 * megasas_resume- driver resume entry point
6464 * @pdev: PCI device structure
6465 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006466static int
bo yang31ea7082007-11-07 12:09:50 -05006467megasas_resume(struct pci_dev *pdev)
6468{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306469 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006470 struct Scsi_Host *host;
6471 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01006472 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05006473
6474 instance = pci_get_drvdata(pdev);
6475 host = instance->host;
6476 pci_set_power_state(pdev, PCI_D0);
6477 pci_enable_wake(pdev, PCI_D0, 0);
6478 pci_restore_state(pdev);
6479
6480 /*
6481 * PCI prepping: enable device set bus mastering and dma mask
6482 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006483 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006484
6485 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006486 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006487 return rval;
6488 }
6489
6490 pci_set_master(pdev);
6491
6492 if (megasas_set_dma_mask(pdev))
6493 goto fail_set_dma_mask;
6494
6495 /*
6496 * Initialize MFI Firmware
6497 */
6498
bo yang31ea7082007-11-07 12:09:50 -05006499 atomic_set(&instance->fw_outstanding, 0);
6500
6501 /*
6502 * We expect the FW state to be READY
6503 */
adam radford058a8fa2011-10-08 18:14:27 -07006504 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006505 goto fail_ready_state;
6506
adam radford3f1abce2011-05-11 18:33:47 -07006507 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01006508 if (instance->msix_vectors) {
6509 irq_flags = PCI_IRQ_MSIX;
6510 if (smp_affinity_enable)
6511 irq_flags |= PCI_IRQ_AFFINITY;
6512 }
6513 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6514 instance->msix_vectors ?
6515 instance->msix_vectors : 1, irq_flags);
6516 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02006517 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006518
Shivasharan Se7d36b82017-10-19 02:48:50 -07006519 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006520 megasas_reset_reply_desc(instance);
6521 if (megasas_ioc_init_fusion(instance)) {
6522 megasas_free_cmds(instance);
6523 megasas_free_cmds_fusion(instance);
6524 goto fail_init_mfi;
6525 }
6526 if (!megasas_get_map_info(instance))
6527 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306528 } else {
adam radford9c915a82010-12-21 13:34:31 -08006529 *instance->producer = 0;
6530 *instance->consumer = 0;
6531 if (megasas_issue_init_mfi(instance))
6532 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006533 }
bo yang31ea7082007-11-07 12:09:50 -05006534
adam radford9c915a82010-12-21 13:34:31 -08006535 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6536 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006537
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306538 if (instance->msix_vectors ?
6539 megasas_setup_irqs_msix(instance, 0) :
6540 megasas_setup_irqs_ioapic(instance))
6541 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006542
adam radford229fe472014-03-10 02:51:56 -07006543 /* Re-launch SR-IOV heartbeat timer */
6544 if (instance->requestorId) {
6545 if (!megasas_sriov_start_heartbeat(instance, 0))
6546 megasas_start_timer(instance,
6547 &instance->sriov_heartbeat_timer,
6548 megasas_sriov_heartbeat_handler,
6549 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306550 else {
adam radford229fe472014-03-10 02:51:56 -07006551 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306552 goto fail_init_mfi;
6553 }
adam radford229fe472014-03-10 02:51:56 -07006554 }
6555
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306556 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306557 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006558 instance->unload = 0;
6559
adam radford541f90b2011-05-11 18:34:29 -07006560 /*
6561 * Initiate AEN (Asynchronous Event Notification)
6562 */
6563 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006564 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006565
bo yang31ea7082007-11-07 12:09:50 -05006566 return 0;
6567
bo yang31ea7082007-11-07 12:09:50 -05006568fail_init_mfi:
6569 if (instance->evt_detail)
6570 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6571 instance->evt_detail,
6572 instance->evt_detail_h);
6573
Sumit Saxena2216c302016-01-28 21:04:26 +05306574 if (instance->pd_info)
6575 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6576 instance->pd_info,
6577 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006578 if (instance->tgt_prop)
6579 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6580 instance->tgt_prop,
6581 instance->tgt_prop_h);
bo yang31ea7082007-11-07 12:09:50 -05006582 if (instance->producer)
6583 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6584 instance->producer_h);
6585 if (instance->consumer)
6586 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6587 instance->consumer_h);
6588 scsi_host_put(host);
6589
6590fail_set_dma_mask:
6591fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006592fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006593
6594 pci_disable_device(pdev);
6595
6596 return -ENODEV;
6597}
Jiri Slaby33139b22008-05-01 17:56:02 +02006598#else
6599#define megasas_suspend NULL
6600#define megasas_resume NULL
6601#endif
bo yang31ea7082007-11-07 12:09:50 -05006602
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006603static inline int
6604megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6605{
6606 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6607 int i;
6608
6609 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6610 return 1;
6611
6612 for (i = 0; i < wait_time; i++) {
6613 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6614 break;
6615
6616 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6617 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6618
6619 msleep(1000);
6620 }
6621
6622 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6623 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6624 __func__);
6625 return 1;
6626 }
6627
6628 return 0;
6629}
6630
bo yang31ea7082007-11-07 12:09:50 -05006631/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006632 * megasas_detach_one - PCI hot"un"plug entry point
6633 * @pdev: PCI device structure
6634 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006635static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006636{
6637 int i;
6638 struct Scsi_Host *host;
6639 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006640 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306641 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006642
6643 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006644 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006645 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006646 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006647
adam radford229fe472014-03-10 02:51:56 -07006648 /* Shutdown SR-IOV heartbeat timer */
6649 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6650 del_timer_sync(&instance->sriov_heartbeat_timer);
6651
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306652 if (instance->fw_crash_state != UNAVAILABLE)
6653 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006654 scsi_remove_host(instance->host);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006655
6656 if (megasas_wait_for_adapter_operational(instance))
6657 goto skip_firing_dcmds;
6658
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006659 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006660 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006661
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006662skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006663 /* cancel the delayed work if this work still in queue*/
6664 if (instance->ev != NULL) {
6665 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006666 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006667 instance->ev = NULL;
6668 }
6669
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306670 /* cancel all wait events */
6671 wake_up_all(&instance->int_cmd_wait_q);
6672
Sumant Patro5d018ad2006-10-03 13:13:18 -07006673 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006674
6675 /*
6676 * Take the instance off the instance array. Note that we will not
6677 * decrement the max_index. We let this array be sparse array
6678 */
6679 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6680 if (megasas_mgmt_info.instance[i] == instance) {
6681 megasas_mgmt_info.count--;
6682 megasas_mgmt_info.instance[i] = NULL;
6683
6684 break;
6685 }
6686 }
6687
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306688 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006689
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306690 megasas_destroy_irqs(instance);
6691
adam radfordc8e858f2011-10-08 18:15:13 -07006692 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006693 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006694
Shivasharan Sf369a312017-10-19 02:48:52 -07006695 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006696 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6697 kfree(fusion->stream_detect_by_ld[i]);
6698 kfree(fusion->stream_detect_by_ld);
6699 fusion->stream_detect_by_ld = NULL;
6700 }
6701
6702
Shivasharan Se7d36b82017-10-19 02:48:50 -07006703 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006704 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306705 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6706 (sizeof(struct MR_PD_CFG_SEQ) *
6707 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306708 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006709 if (fusion->ld_map[i])
6710 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306711 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006712 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306713 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07006714 if (fusion->ld_drv_map[i]) {
6715 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
6716 vfree(fusion->ld_drv_map[i]);
6717 else
6718 free_pages((ulong)fusion->ld_drv_map[i],
6719 fusion->drv_map_pages);
6720 }
6721
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006722 if (fusion->pd_seq_sync[i])
6723 dma_free_coherent(&instance->pdev->dev,
6724 pd_seq_map_sz,
6725 fusion->pd_seq_sync[i],
6726 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306727 }
Shivasharan S5fc499b2017-02-10 00:59:17 -08006728 megasas_free_fusion_context(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306729 } else {
adam radford9c915a82010-12-21 13:34:31 -08006730 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006731 pci_free_consistent(pdev, sizeof(u32),
6732 instance->producer,
6733 instance->producer_h);
6734 pci_free_consistent(pdev, sizeof(u32),
6735 instance->consumer,
6736 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08006737 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006738
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306739 kfree(instance->ctrl_info);
6740
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05306741 if (instance->evt_detail)
6742 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6743 instance->evt_detail, instance->evt_detail_h);
Sumit Saxena2216c302016-01-28 21:04:26 +05306744 if (instance->pd_info)
6745 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6746 instance->pd_info,
6747 instance->pd_info_h);
Shivasharan S96188a82017-02-10 00:59:11 -08006748 if (instance->tgt_prop)
6749 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6750 instance->tgt_prop,
6751 instance->tgt_prop_h);
adam radford229fe472014-03-10 02:51:56 -07006752 if (instance->vf_affiliation)
6753 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6754 sizeof(struct MR_LD_VF_AFFILIATION),
6755 instance->vf_affiliation,
6756 instance->vf_affiliation_h);
6757
6758 if (instance->vf_affiliation_111)
6759 pci_free_consistent(pdev,
6760 sizeof(struct MR_LD_VF_AFFILIATION_111),
6761 instance->vf_affiliation_111,
6762 instance->vf_affiliation_111_h);
6763
6764 if (instance->hb_host_mem)
6765 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6766 instance->hb_host_mem,
6767 instance->hb_host_mem_h);
6768
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306769 if (instance->crash_dump_buf)
6770 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6771 instance->crash_dump_buf, instance->crash_dump_h);
6772
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306773 if (instance->system_info_buf)
6774 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6775 instance->system_info_buf, instance->system_info_h);
6776
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006777 scsi_host_put(host);
6778
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006779 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006780}
6781
6782/**
6783 * megasas_shutdown - Shutdown entry point
6784 * @device: Generic device structure
6785 */
6786static void megasas_shutdown(struct pci_dev *pdev)
6787{
6788 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006789
Yang, Bo0c79e682009-10-06 14:47:35 -06006790 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006791
6792 if (megasas_wait_for_adapter_operational(instance))
6793 goto skip_firing_dcmds;
6794
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006795 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006796 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006797
6798skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306799 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306800 megasas_destroy_irqs(instance);
6801
adam radfordc8e858f2011-10-08 18:15:13 -07006802 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006803 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006804}
6805
6806/**
6807 * megasas_mgmt_open - char node "open" entry point
6808 */
6809static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6810{
6811 /*
6812 * Allow only those users with admin rights
6813 */
6814 if (!capable(CAP_SYS_ADMIN))
6815 return -EACCES;
6816
6817 return 0;
6818}
6819
6820/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006821 * megasas_mgmt_fasync - Async notifier registration from applications
6822 *
6823 * This function adds the calling process to a driver global queue. When an
6824 * event occurs, SIGIO will be sent to all processes in this queue.
6825 */
6826static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6827{
6828 int rc;
6829
Arjan van de Ven0b950672006-01-11 13:16:10 +01006830 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006831
6832 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6833
Arjan van de Ven0b950672006-01-11 13:16:10 +01006834 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006835
6836 if (rc >= 0) {
6837 /* For sanity check when we get ioctl */
6838 filep->private_data = filep;
6839 return 0;
6840 }
6841
6842 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6843
6844 return rc;
6845}
6846
6847/**
Yang, Boc3518832009-10-06 14:18:02 -06006848 * megasas_mgmt_poll - char node "poll" entry point
6849 * */
6850static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6851{
6852 unsigned int mask;
6853 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006854
Yang, Boc3518832009-10-06 14:18:02 -06006855 poll_wait(file, &megasas_poll_wait, wait);
6856 spin_lock_irqsave(&poll_aen_lock, flags);
6857 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006858 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006859 else
6860 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306861 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006862 spin_unlock_irqrestore(&poll_aen_lock, flags);
6863 return mask;
6864}
6865
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306866/*
6867 * megasas_set_crash_dump_params_ioctl:
6868 * Send CRASH_DUMP_MODE DCMD to all controllers
6869 * @cmd: MFI command frame
6870 */
6871
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006872static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306873{
6874 struct megasas_instance *local_instance;
6875 int i, error = 0;
6876 int crash_support;
6877
6878 crash_support = cmd->frame->dcmd.mbox.w[0];
6879
6880 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6881 local_instance = megasas_mgmt_info.instance[i];
6882 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05306883 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306884 MEGASAS_HBA_OPERATIONAL) &&
6885 !megasas_set_crash_dump_params(local_instance,
6886 crash_support)) {
6887 local_instance->crash_dump_app_support =
6888 crash_support;
6889 dev_info(&local_instance->pdev->dev,
6890 "Application firmware crash "
6891 "dump mode set success\n");
6892 error = 0;
6893 } else {
6894 dev_info(&local_instance->pdev->dev,
6895 "Application firmware crash "
6896 "dump mode set failed\n");
6897 error = -1;
6898 }
6899 }
6900 }
6901 return error;
6902}
6903
Yang, Boc3518832009-10-06 14:18:02 -06006904/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006905 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6906 * @instance: Adapter soft state
6907 * @argp: User's ioctl packet
6908 */
6909static int
6910megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6911 struct megasas_iocpacket __user * user_ioc,
6912 struct megasas_iocpacket *ioc)
6913{
6914 struct megasas_sge32 *kern_sge32;
6915 struct megasas_cmd *cmd;
6916 void *kbuff_arr[MAX_IOCTL_SGE];
6917 dma_addr_t buf_handle = 0;
6918 int error = 0, i;
6919 void *sense = NULL;
6920 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006921 unsigned long *sense_ptr;
Shivasharan S8823abe2017-08-23 04:47:00 -07006922 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006923
6924 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6925
6926 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006927 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006928 ioc->sge_count, MAX_IOCTL_SGE);
6929 return -EINVAL;
6930 }
6931
6932 cmd = megasas_get_cmd(instance);
6933 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006934 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006935 return -ENOMEM;
6936 }
6937
6938 /*
6939 * User's IOCTL packet has 2 frames (maximum). Copy those two
6940 * frames into our cmd's frames. cmd->frame's context will get
6941 * overwritten when we copy from user's frames. So set that value
6942 * alone separately
6943 */
6944 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306945 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006946 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306947 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6948 MFI_FRAME_SGL64 |
6949 MFI_FRAME_SENSE64));
Shivasharan S8823abe2017-08-23 04:47:00 -07006950 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006951
Shivasharan S8823abe2017-08-23 04:47:00 -07006952 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Shivasharan S95c06082017-02-10 00:59:23 -08006953 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
6954 megasas_return_cmd(instance, cmd);
6955 return -1;
6956 }
6957 }
6958
Shivasharan S8823abe2017-08-23 04:47:00 -07006959 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306960 error = megasas_set_crash_dump_params_ioctl(cmd);
6961 megasas_return_cmd(instance, cmd);
6962 return error;
6963 }
6964
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006965 /*
6966 * The management interface between applications and the fw uses
6967 * MFI frames. E.g, RAID configuration changes, LD property changes
6968 * etc are accomplishes through different kinds of MFI frames. The
6969 * driver needs to care only about substituting user buffers with
6970 * kernel buffers in SGLs. The location of SGL is embedded in the
6971 * struct iocpacket itself.
6972 */
6973 kern_sge32 = (struct megasas_sge32 *)
6974 ((unsigned long)cmd->frame + ioc->sgl_off);
6975
6976 /*
6977 * For each user buffer, create a mirror buffer and copy in
6978 */
6979 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006980 if (!ioc->sgl[i].iov_len)
6981 continue;
6982
Sumant Patro9f35fa82007-02-14 12:55:45 -08006983 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006984 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006985 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006986 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006987 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6988 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006989 error = -ENOMEM;
6990 goto out;
6991 }
6992
6993 /*
6994 * We don't change the dma_coherent_mask, so
6995 * pci_alloc_consistent only returns 32bit addresses
6996 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306997 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6998 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006999
7000 /*
7001 * We created a kernel buffer corresponding to the
7002 * user buffer. Now copy in from the user buffer
7003 */
7004 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7005 (u32) (ioc->sgl[i].iov_len))) {
7006 error = -EFAULT;
7007 goto out;
7008 }
7009 }
7010
7011 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007012 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7013 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007014 if (!sense) {
7015 error = -ENOMEM;
7016 goto out;
7017 }
7018
7019 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06007020 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307021 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007022 }
7023
7024 /*
7025 * Set the sync_cmd flag so that the ISR knows not to complete this
7026 * cmd to the SCSI mid-layer
7027 */
7028 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307029 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7030 cmd->sync_cmd = 0;
7031 dev_err(&instance->pdev->dev,
7032 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
Shivasharan S8823abe2017-08-23 04:47:00 -07007033 __func__, __LINE__, opcode, cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307034 return -EBUSY;
7035 }
7036
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007037 cmd->sync_cmd = 0;
7038
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307039 if (instance->unload == 1) {
7040 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7041 "don't submit data to application\n");
7042 goto out;
7043 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007044 /*
7045 * copy out the kernel buffers to user buffers
7046 */
7047 for (i = 0; i < ioc->sge_count; i++) {
7048 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7049 ioc->sgl[i].iov_len)) {
7050 error = -EFAULT;
7051 goto out;
7052 }
7053 }
7054
7055 /*
7056 * copy out the sense
7057 */
7058 if (ioc->sense_len) {
7059 /*
bo yangb70a41e2008-03-18 03:13:06 -04007060 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007061 * sense buffer address
7062 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007063 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7064 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007065
Shivasharan S318aaef2017-02-10 00:59:22 -08007066 if (copy_to_user((void __user *)((unsigned long)
7067 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04007068 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007069 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007070 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007071 error = -EFAULT;
7072 goto out;
7073 }
7074 }
7075
7076 /*
7077 * copy the status codes returned by the fw
7078 */
7079 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7080 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007081 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007082 error = -EFAULT;
7083 }
7084
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007085out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007086 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007087 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007088 sense, sense_handle);
7089 }
7090
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007091 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007092 if (kbuff_arr[i]) {
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007093 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307094 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007095 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307096 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307097 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007098 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007099 }
7100
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307101 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007102 return error;
7103}
7104
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007105static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7106{
7107 struct megasas_iocpacket __user *user_ioc =
7108 (struct megasas_iocpacket __user *)arg;
7109 struct megasas_iocpacket *ioc;
7110 struct megasas_instance *instance;
7111 int error;
bo yang39a98552010-09-22 22:36:29 -04007112 int i;
7113 unsigned long flags;
7114 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007115
Markus Elfring709ab232016-08-21 10:39:04 +02007116 ioc = memdup_user(user_ioc, sizeof(*ioc));
7117 if (IS_ERR(ioc))
7118 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007119
7120 instance = megasas_lookup_instance(ioc->host_no);
7121 if (!instance) {
7122 error = -ENODEV;
7123 goto out_kfree_ioc;
7124 }
7125
adam radford229fe472014-03-10 02:51:56 -07007126 /* Adjust ioctl wait time for VF mode */
7127 if (instance->requestorId)
7128 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7129
7130 /* Block ioctls in VF mode */
7131 if (instance->requestorId && !allow_vf_ioctls) {
7132 error = -ENODEV;
7133 goto out_kfree_ioc;
7134 }
7135
Sumit Saxena8a01a412016-01-28 21:04:32 +05307136 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007137 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007138 error = -ENODEV;
7139 goto out_kfree_ioc;
7140 }
7141
7142 if (instance->unload == 1) {
7143 error = -ENODEV;
7144 goto out_kfree_ioc;
7145 }
7146
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007147 if (down_interruptible(&instance->ioctl_sem)) {
7148 error = -ERESTARTSYS;
7149 goto out_kfree_ioc;
7150 }
bo yang39a98552010-09-22 22:36:29 -04007151
7152 for (i = 0; i < wait_time; i++) {
7153
7154 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307155 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007156 spin_unlock_irqrestore(&instance->hba_lock, flags);
7157 break;
7158 }
7159 spin_unlock_irqrestore(&instance->hba_lock, flags);
7160
7161 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007162 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04007163 "for controller reset to finish\n");
7164 }
7165
7166 msleep(1000);
7167 }
7168
7169 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307170 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007171 spin_unlock_irqrestore(&instance->hba_lock, flags);
7172
Colin Ian King644da3c2016-11-12 16:25:24 +00007173 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007174 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007175 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007176 }
7177 spin_unlock_irqrestore(&instance->hba_lock, flags);
7178
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007179 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007180out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007181 up(&instance->ioctl_sem);
7182
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007183out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007184 kfree(ioc);
7185 return error;
7186}
7187
7188static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7189{
7190 struct megasas_instance *instance;
7191 struct megasas_aen aen;
7192 int error;
bo yang39a98552010-09-22 22:36:29 -04007193 int i;
7194 unsigned long flags;
7195 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007196
7197 if (file->private_data != file) {
7198 printk(KERN_DEBUG "megasas: fasync_helper was not "
7199 "called first\n");
7200 return -EINVAL;
7201 }
7202
7203 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7204 return -EFAULT;
7205
7206 instance = megasas_lookup_instance(aen.host_no);
7207
7208 if (!instance)
7209 return -ENODEV;
7210
Sumit Saxena8a01a412016-01-28 21:04:32 +05307211 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007212 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007213 }
7214
7215 if (instance->unload == 1) {
7216 return -ENODEV;
7217 }
7218
bo yang39a98552010-09-22 22:36:29 -04007219 for (i = 0; i < wait_time; i++) {
7220
7221 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307222 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007223 spin_unlock_irqrestore(&instance->hba_lock,
7224 flags);
7225 break;
7226 }
7227
7228 spin_unlock_irqrestore(&instance->hba_lock, flags);
7229
7230 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007231 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04007232 "controller reset to finish\n");
7233 }
7234
7235 msleep(1000);
7236 }
7237
7238 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307239 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007240 spin_unlock_irqrestore(&instance->hba_lock, flags);
Colin Ian King644da3c2016-11-12 16:25:24 +00007241 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007242 return -ENODEV;
7243 }
7244 spin_unlock_irqrestore(&instance->hba_lock, flags);
7245
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307246 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007247 error = megasas_register_aen(instance, aen.seq_num,
7248 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307249 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007250 return error;
7251}
7252
7253/**
7254 * megasas_mgmt_ioctl - char node ioctl entry point
7255 */
7256static long
7257megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7258{
7259 switch (cmd) {
7260 case MEGASAS_IOC_FIRMWARE:
7261 return megasas_mgmt_ioctl_fw(file, arg);
7262
7263 case MEGASAS_IOC_GET_AEN:
7264 return megasas_mgmt_ioctl_aen(file, arg);
7265 }
7266
7267 return -ENOTTY;
7268}
7269
7270#ifdef CONFIG_COMPAT
7271static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7272{
7273 struct compat_megasas_iocpacket __user *cioc =
7274 (struct compat_megasas_iocpacket __user *)arg;
7275 struct megasas_iocpacket __user *ioc =
7276 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7277 int i;
7278 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007279 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307280 u32 local_sense_off;
7281 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307282 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007283
Jeff Garzik83aabc12006-10-04 06:34:03 -04007284 if (clear_user(ioc, sizeof(*ioc)))
7285 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007286
7287 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7288 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7289 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7290 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7291 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7292 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7293 return -EFAULT;
7294
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007295 /*
7296 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7297 * sense_len is not null, so prepare the 64bit value under
7298 * the same condition.
7299 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307300 if (get_user(local_sense_off, &ioc->sense_off) ||
7301 get_user(local_sense_len, &ioc->sense_len) ||
7302 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307303 return -EFAULT;
7304
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307305 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007306 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307307 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007308 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307309 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007310 if (get_user(ptr, sense_cioc_ptr) ||
7311 put_user(compat_ptr(ptr), sense_ioc_ptr))
7312 return -EFAULT;
7313 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007314
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007315 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007316 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7317 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7318 copy_in_user(&ioc->sgl[i].iov_len,
7319 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7320 return -EFAULT;
7321 }
7322
7323 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7324
7325 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7326 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7327 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7328 return -EFAULT;
7329 }
7330 return error;
7331}
7332
7333static long
7334megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7335 unsigned long arg)
7336{
7337 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007338 case MEGASAS_IOC_FIRMWARE32:
7339 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007340 case MEGASAS_IOC_GET_AEN:
7341 return megasas_mgmt_ioctl_aen(file, arg);
7342 }
7343
7344 return -ENOTTY;
7345}
7346#endif
7347
7348/*
7349 * File operations structure for management interface
7350 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007351static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007352 .owner = THIS_MODULE,
7353 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007354 .fasync = megasas_mgmt_fasync,
7355 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06007356 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007357#ifdef CONFIG_COMPAT
7358 .compat_ioctl = megasas_mgmt_compat_ioctl,
7359#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02007360 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007361};
7362
7363/*
7364 * PCI hotplug support registration structure
7365 */
7366static struct pci_driver megasas_pci_driver = {
7367
7368 .name = "megaraid_sas",
7369 .id_table = megasas_pci_table,
7370 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007371 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05007372 .suspend = megasas_suspend,
7373 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007374 .shutdown = megasas_shutdown,
7375};
7376
7377/*
7378 * Sysfs driver attributes
7379 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007380static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007381{
7382 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7383 MEGASAS_VERSION);
7384}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007385static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007386
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007387static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307388{
7389 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7390 MEGASAS_RELDATE);
7391}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007392static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307393
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007394static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06007395{
7396 return sprintf(buf, "%u\n", support_poll_for_event);
7397}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007398static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007399
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007400static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06007401{
7402 return sprintf(buf, "%u\n", support_device_change);
7403}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007404static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06007405
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007406static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07007407{
bo yangad84db22007-11-09 04:40:16 -05007408 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007409}
7410
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007411static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
7412 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07007413{
7414 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007415
7416 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007417 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7418 retval = -EINVAL;
7419 }
7420 return retval;
7421}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007422static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05007423
Shivasharan Sb4a42212017-02-10 00:59:16 -08007424static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
7425{
7426 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
7427 scsi_remove_device(sdev);
7428 scsi_device_put(sdev);
7429}
7430
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007431static void
7432megasas_aen_polling(struct work_struct *work)
7433{
7434 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007435 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007436 struct megasas_instance *instance = ev->instance;
7437 union megasas_evt_class_locale class_locale;
7438 struct Scsi_Host *host;
7439 struct scsi_device *sdev1;
7440 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007441 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007442 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007443 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007444 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307445 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007446
7447 if (!instance) {
7448 printk(KERN_ERR "invalid instance!\n");
7449 kfree(ev);
7450 return;
7451 }
adam radford229fe472014-03-10 02:51:56 -07007452
7453 /* Adjust event workqueue thread wait time for VF mode */
7454 if (instance->requestorId)
7455 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7456
7457 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307458 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007459
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007460 instance->ev = NULL;
7461 host = instance->host;
7462 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307463 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007464
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307465 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307466
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007467 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307468 case MR_EVT_PD_REMOVED:
7469 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307470 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307471 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007472 break;
7473
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307474 case MR_EVT_LD_OFFLINE:
7475 case MR_EVT_CFG_CLEARED:
7476 case MR_EVT_LD_DELETED:
7477 case MR_EVT_LD_CREATED:
7478 if (!instance->requestorId ||
7479 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7480 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007481
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307482 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307483 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007484
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307485 break;
7486
7487 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7488 case MR_EVT_FOREIGN_CFG_IMPORTED:
7489 case MR_EVT_LD_STATE_CHANGE:
7490 dcmd_ret = megasas_get_pd_list(instance);
7491
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307492 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307493 break;
7494
7495 if (!instance->requestorId ||
7496 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7497 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7498
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307499 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307500 break;
7501
7502 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7503 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7504 instance->host->host_no);
7505 break;
7506
7507 case MR_EVT_CTRL_PROP_CHANGED:
7508 dcmd_ret = megasas_get_ctrl_info(instance);
7509 break;
7510 default:
7511 doscan = 0;
7512 break;
7513 }
7514 } else {
7515 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7516 mutex_unlock(&instance->reset_mutex);
7517 kfree(ev);
7518 return;
7519 }
7520
7521 mutex_unlock(&instance->reset_mutex);
7522
7523 if (doscan & SCAN_PD_CHANNEL) {
7524 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7525 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7526 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007527 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307528 if (instance->pd_list[pd_index].driveState ==
7529 MR_PD_STATE_SYSTEM) {
7530 if (!sdev1)
7531 scsi_add_device(host, i, j, 0);
7532 else
Yang, Boc9786842009-12-06 08:39:25 -07007533 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007534 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007535 if (sdev1)
7536 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007537 }
Yang, Boc9786842009-12-06 08:39:25 -07007538 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007539 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007540 }
7541
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307542 if (doscan & SCAN_VD_CHANNEL) {
7543 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7544 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7545 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7546 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7547 if (instance->ld_ids[ld_index] != 0xff) {
7548 if (!sdev1)
7549 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7550 else
7551 scsi_device_put(sdev1);
7552 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007553 if (sdev1)
7554 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007555 }
7556 }
7557 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007558 }
7559
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307560 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307561 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7562 else
7563 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007564
7565 /* Register AEN with FW for latest sequence number plus 1 */
7566 class_locale.members.reserved = 0;
7567 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7568 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307569
7570 if (instance->aen_cmd != NULL) {
7571 kfree(ev);
7572 return;
7573 }
7574
7575 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007576 error = megasas_register_aen(instance, seq_num,
7577 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007578 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307579 dev_err(&instance->pdev->dev,
7580 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007581
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307582 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007583 kfree(ev);
7584}
7585
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007586/**
7587 * megasas_init - Driver load entry point
7588 */
7589static int __init megasas_init(void)
7590{
7591 int rval;
7592
7593 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007594 * Booted in kdump kernel, minimize memory footprints by
7595 * disabling few features
7596 */
7597 if (reset_devices) {
7598 msix_vectors = 1;
7599 rdpq_enable = 0;
7600 dual_qdepth_disable = 1;
7601 }
7602
7603 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007604 * Announce driver version and other information
7605 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307606 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007607
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007608 spin_lock_init(&poll_aen_lock);
7609
Yang, Bo72c4fd32009-10-06 14:20:59 -06007610 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007611 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007612
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007613 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7614
7615 /*
7616 * Register character device node
7617 */
7618 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7619
7620 if (rval < 0) {
7621 printk(KERN_DEBUG "megasas: failed to open device node\n");
7622 return rval;
7623 }
7624
7625 megasas_mgmt_majorno = rval;
7626
7627 /*
7628 * Register ourselves as PCI hotplug module
7629 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007630 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007631
7632 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007633 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007634 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007635 }
7636
Jeff Garzik83aabc12006-10-04 06:34:03 -04007637 rval = driver_create_file(&megasas_pci_driver.driver,
7638 &driver_attr_version);
7639 if (rval)
7640 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007641
7642 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307643 &driver_attr_release_date);
7644 if (rval)
7645 goto err_dcf_rel_date;
7646
7647 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007648 &driver_attr_support_poll_for_event);
7649 if (rval)
7650 goto err_dcf_support_poll_for_event;
7651
Jeff Garzik83aabc12006-10-04 06:34:03 -04007652 rval = driver_create_file(&megasas_pci_driver.driver,
7653 &driver_attr_dbg_lvl);
7654 if (rval)
7655 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007656 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007657 &driver_attr_support_device_change);
7658 if (rval)
7659 goto err_dcf_support_device_change;
7660
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007661 return rval;
bo yangad84db22007-11-09 04:40:16 -05007662
Yang, Bo837f5fe2010-10-11 06:59:20 -06007663err_dcf_support_device_change:
7664 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007665 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007666err_dcf_dbg_lvl:
7667 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007668 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007669err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307670 driver_remove_file(&megasas_pci_driver.driver,
7671 &driver_attr_release_date);
7672err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007673 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7674err_dcf_attr_ver:
7675 pci_unregister_driver(&megasas_pci_driver);
7676err_pcidrv:
7677 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007678 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007679}
7680
7681/**
7682 * megasas_exit - Driver unload entry point
7683 */
7684static void __exit megasas_exit(void)
7685{
Sumant Patro658dced2006-10-03 13:09:14 -07007686 driver_remove_file(&megasas_pci_driver.driver,
7687 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007688 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007689 &driver_attr_support_poll_for_event);
7690 driver_remove_file(&megasas_pci_driver.driver,
7691 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307692 driver_remove_file(&megasas_pci_driver.driver,
7693 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007694 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007695
7696 pci_unregister_driver(&megasas_pci_driver);
7697 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7698}
7699
7700module_init(megasas_init);
7701module_exit(megasas_exit);