blob: 19649ca11a1ff09158a6473869760580ee14a500 [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
Shivasharan S9b3d0282017-10-19 02:48:56 -07004214 ci = instance->pd_list_buf;
4215 ci_h = instance->pd_list_buf_h;
4216
Yang, Bo81e403c2009-10-06 14:27:54 -06004217 cmd = megasas_get_cmd(instance);
4218
4219 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004220 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004221 return -ENOMEM;
4222 }
4223
4224 dcmd = &cmd->frame->dcmd;
4225
Yang, Bo81e403c2009-10-06 14:27:54 -06004226 memset(ci, 0, sizeof(*ci));
4227 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4228
4229 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4230 dcmd->mbox.b[1] = 0;
4231 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304232 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004233 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304234 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004235 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004236 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304237 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4238 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4239 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4240 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004241
Shivasharan Se7d36b82017-10-19 02:48:50 -07004242 if ((instance->adapter_type != MFI_SERIES) &&
4243 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304244 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304245 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304246 else
4247 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004248
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304249 switch (ret) {
4250 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004251 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4252 "failed/not supported by firmware\n");
4253
Shivasharan Se7d36b82017-10-19 02:48:50 -07004254 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004255 megaraid_sas_kill_hba(instance);
4256 else
4257 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304258 break;
4259 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004260
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304261 switch (dcmd_timeout_ocr_possible(instance)) {
4262 case INITIATE_OCR:
4263 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4264 /*
4265 * DCMD failed from AEN path.
4266 * AEN path already hold reset_mutex to avoid PCI access
4267 * while OCR is in progress.
4268 */
4269 mutex_unlock(&instance->reset_mutex);
4270 megasas_reset_fusion(instance->host,
4271 MFI_IO_TIMEOUT_OCR);
4272 mutex_lock(&instance->reset_mutex);
4273 break;
4274 case KILL_ADAPTER:
4275 megaraid_sas_kill_hba(instance);
4276 break;
4277 case IGNORE_TIMEOUT:
4278 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4279 __func__, __LINE__);
4280 break;
4281 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004282
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304283 break;
4284
4285 case DCMD_SUCCESS:
4286 pd_addr = ci->addr;
4287
4288 if ((le32_to_cpu(ci->count) >
4289 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4290 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004291
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304292 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304293 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004294
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304295 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304296 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304297 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304298 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304299 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304300 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304301 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004302 pd_addr++;
4303 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304304
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304305 memcpy(instance->pd_list, instance->local_pd_list,
4306 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304307 break;
4308
Yang, Bo81e403c2009-10-06 14:27:54 -06004309 }
4310
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304311 if (ret != DCMD_TIMEOUT)
4312 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004313
4314 return ret;
4315}
4316
Yang, Bobdc6fb82009-12-06 08:30:19 -07004317/*
4318 * megasas_get_ld_list_info - Returns FW's ld_list structure
4319 * @instance: Adapter soft state
4320 * @ld_list: ld_list structure
4321 *
4322 * Issues an internal command (DCMD) to get the FW's controller PD
4323 * list structure. This information is mainly used to find out SYSTEM
4324 * supported by the FW.
4325 */
4326static int
4327megasas_get_ld_list(struct megasas_instance *instance)
4328{
4329 int ret = 0, ld_index = 0, ids = 0;
4330 struct megasas_cmd *cmd;
4331 struct megasas_dcmd_frame *dcmd;
4332 struct MR_LD_LIST *ci;
4333 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304334 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004335
Shivasharan S9b3d0282017-10-19 02:48:56 -07004336 ci = instance->ld_list_buf;
4337 ci_h = instance->ld_list_buf_h;
4338
Yang, Bobdc6fb82009-12-06 08:30:19 -07004339 cmd = megasas_get_cmd(instance);
4340
4341 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004342 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004343 return -ENOMEM;
4344 }
4345
4346 dcmd = &cmd->frame->dcmd;
4347
Yang, Bobdc6fb82009-12-06 08:30:19 -07004348 memset(ci, 0, sizeof(*ci));
4349 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4350
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304351 if (instance->supportmax256vd)
4352 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004353 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304354 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004355 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304356 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004357 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304358 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4359 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4360 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4361 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004362 dcmd->pad_0 = 0;
4363
Shivasharan Se7d36b82017-10-19 02:48:50 -07004364 if ((instance->adapter_type != MFI_SERIES) &&
4365 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304366 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304367 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304368 else
4369 ret = megasas_issue_polled(instance, cmd);
4370
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304371 ld_count = le32_to_cpu(ci->ldCount);
4372
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304373 switch (ret) {
4374 case DCMD_FAILED:
4375 megaraid_sas_kill_hba(instance);
4376 break;
4377 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004378
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304379 switch (dcmd_timeout_ocr_possible(instance)) {
4380 case INITIATE_OCR:
4381 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4382 /*
4383 * DCMD failed from AEN path.
4384 * AEN path already hold reset_mutex to avoid PCI access
4385 * while OCR is in progress.
4386 */
4387 mutex_unlock(&instance->reset_mutex);
4388 megasas_reset_fusion(instance->host,
4389 MFI_IO_TIMEOUT_OCR);
4390 mutex_lock(&instance->reset_mutex);
4391 break;
4392 case KILL_ADAPTER:
4393 megaraid_sas_kill_hba(instance);
4394 break;
4395 case IGNORE_TIMEOUT:
4396 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4397 __func__, __LINE__);
4398 break;
4399 }
4400
4401 break;
4402
4403 case DCMD_SUCCESS:
4404 if (ld_count > instance->fw_supported_vd_count)
4405 break;
4406
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304407 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004408
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304409 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004410 if (ci->ldList[ld_index].state != 0) {
4411 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304412 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004413 }
4414 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304415
4416 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004417 }
4418
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304419 if (ret != DCMD_TIMEOUT)
4420 megasas_return_cmd(instance, cmd);
4421
Yang, Bobdc6fb82009-12-06 08:30:19 -07004422 return ret;
4423}
4424
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004425/**
adam radford21c9e162013-09-06 15:27:14 -07004426 * megasas_ld_list_query - Returns FW's ld_list structure
4427 * @instance: Adapter soft state
4428 * @ld_list: ld_list structure
4429 *
4430 * Issues an internal command (DCMD) to get the FW's controller PD
4431 * list structure. This information is mainly used to find out SYSTEM
4432 * supported by the FW.
4433 */
4434static int
4435megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4436{
4437 int ret = 0, ld_index = 0, ids = 0;
4438 struct megasas_cmd *cmd;
4439 struct megasas_dcmd_frame *dcmd;
4440 struct MR_LD_TARGETID_LIST *ci;
4441 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304442 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004443
Shivasharan S9b3d0282017-10-19 02:48:56 -07004444 ci = instance->ld_targetid_list_buf;
4445 ci_h = instance->ld_targetid_list_buf_h;
4446
adam radford21c9e162013-09-06 15:27:14 -07004447 cmd = megasas_get_cmd(instance);
4448
4449 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004450 dev_warn(&instance->pdev->dev,
4451 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004452 return -ENOMEM;
4453 }
4454
4455 dcmd = &cmd->frame->dcmd;
4456
adam radford21c9e162013-09-06 15:27:14 -07004457 memset(ci, 0, sizeof(*ci));
4458 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4459
4460 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304461 if (instance->supportmax256vd)
4462 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004463
4464 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304465 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004466 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304467 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004468 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304469 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4470 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4471 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4472 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004473 dcmd->pad_0 = 0;
4474
Shivasharan Se7d36b82017-10-19 02:48:50 -07004475 if ((instance->adapter_type != MFI_SERIES) &&
4476 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304477 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304478 else
4479 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004480
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304481 switch (ret) {
4482 case DCMD_FAILED:
4483 dev_info(&instance->pdev->dev,
4484 "DCMD not supported by firmware - %s %d\n",
4485 __func__, __LINE__);
4486 ret = megasas_get_ld_list(instance);
4487 break;
4488 case DCMD_TIMEOUT:
4489 switch (dcmd_timeout_ocr_possible(instance)) {
4490 case INITIATE_OCR:
4491 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4492 /*
4493 * DCMD failed from AEN path.
4494 * AEN path already hold reset_mutex to avoid PCI access
4495 * while OCR is in progress.
4496 */
4497 mutex_unlock(&instance->reset_mutex);
4498 megasas_reset_fusion(instance->host,
4499 MFI_IO_TIMEOUT_OCR);
4500 mutex_lock(&instance->reset_mutex);
4501 break;
4502 case KILL_ADAPTER:
4503 megaraid_sas_kill_hba(instance);
4504 break;
4505 case IGNORE_TIMEOUT:
4506 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4507 __func__, __LINE__);
4508 break;
4509 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304510
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304511 break;
4512 case DCMD_SUCCESS:
4513 tgtid_count = le32_to_cpu(ci->count);
4514
4515 if ((tgtid_count > (instance->fw_supported_vd_count)))
4516 break;
4517
adam radford21c9e162013-09-06 15:27:14 -07004518 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304519 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004520 ids = ci->targetId[ld_index];
4521 instance->ld_ids[ids] = ci->targetId[ld_index];
4522 }
4523
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304524 break;
adam radford21c9e162013-09-06 15:27:14 -07004525 }
4526
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304527 if (ret != DCMD_TIMEOUT)
4528 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004529
4530 return ret;
4531}
4532
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304533/*
4534 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4535 * instance : Controller's instance
4536*/
4537static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4538{
4539 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004540 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304541
4542 fusion = instance->ctrl_context;
4543 /* For MFI based controllers return dummy success */
4544 if (!fusion)
4545 return;
4546
4547 instance->supportmax256vd =
4548 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4549 /* Below is additional check to address future FW enhancement */
4550 if (instance->ctrl_info->max_lds > 64)
4551 instance->supportmax256vd = 1;
4552
4553 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4554 * MEGASAS_MAX_DEV_PER_CHANNEL;
4555 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4556 * MEGASAS_MAX_DEV_PER_CHANNEL;
4557 if (instance->supportmax256vd) {
4558 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4559 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4560 } else {
4561 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4562 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4563 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304564
4565 dev_info(&instance->pdev->dev,
4566 "firmware type\t: %s\n",
4567 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4568 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304569
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004570 if (instance->max_raid_mapsize) {
4571 ventura_map_sz = instance->max_raid_mapsize *
4572 MR_MIN_MAP_SIZE; /* 64k */
4573 fusion->current_map_sz = ventura_map_sz;
4574 fusion->max_map_sz = ventura_map_sz;
4575 } else {
4576 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4577 (sizeof(struct MR_LD_SPAN_MAP) *
4578 (instance->fw_supported_vd_count - 1));
4579 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304580
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004581 fusion->max_map_sz =
4582 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304583
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004584 if (instance->supportmax256vd)
4585 fusion->current_map_sz = fusion->new_map_sz;
4586 else
4587 fusion->current_map_sz = fusion->old_map_sz;
4588 }
4589 /* irrespective of FW raid maps, driver raid map is constant */
4590 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304591}
4592
adam radford21c9e162013-09-06 15:27:14 -07004593/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004594 * megasas_get_controller_info - Returns FW's controller structure
4595 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004596 *
4597 * Issues an internal command (DCMD) to get the FW's controller structure.
4598 * This information is mainly used to find out the maximum IO transfer per
4599 * command supported by the FW.
4600 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304601int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304602megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004603{
4604 int ret = 0;
4605 struct megasas_cmd *cmd;
4606 struct megasas_dcmd_frame *dcmd;
4607 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304608 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004609 dma_addr_t ci_h = 0;
4610
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304611 ctrl_info = instance->ctrl_info;
4612
Shivasharan S9b3d0282017-10-19 02:48:56 -07004613 ci = instance->ctrl_info_buf;
4614 ci_h = instance->ctrl_info_buf_h;
4615
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004616 cmd = megasas_get_cmd(instance);
4617
4618 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004619 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004620 return -ENOMEM;
4621 }
4622
4623 dcmd = &cmd->frame->dcmd;
4624
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004625 memset(ci, 0, sizeof(*ci));
4626 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4627
4628 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304629 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004630 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304631 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004632 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004633 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304634 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4635 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4636 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4637 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304638 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004639
Shivasharan Se7d36b82017-10-19 02:48:50 -07004640 if ((instance->adapter_type != MFI_SERIES) &&
4641 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304642 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304643 else
4644 ret = megasas_issue_polled(instance, cmd);
4645
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304646 switch (ret) {
4647 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004648 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304649 /* Save required controller information in
4650 * CPU endianness format.
4651 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304652 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4653 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4654 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004655 le16_to_cpus((u16 *)&ctrl_info->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304656
4657 /* Update the latest Ext VD info.
4658 * From Init path, store current firmware details.
4659 * From OCR path, detect any firmware properties changes.
4660 * in case of Firmware upgrade without system reboot.
4661 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304662 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304663 instance->use_seqnum_jbod_fp =
4664 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004665 instance->support_morethan256jbod =
4666 ctrl_info->adapter_operations4.support_pd_map_target_id;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304667
4668 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304669 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4670 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304671 "controller type\t: %s(%dMB)\n",
4672 instance->is_imr ? "iMR" : "MR",
4673 le16_to_cpu(ctrl_info->memory_size));
4674
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304675 instance->disableOnlineCtrlReset =
4676 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304677 instance->secure_jbod_support =
4678 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304679 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4680 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304681 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4682 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304683 break;
4684
4685 case DCMD_TIMEOUT:
4686 switch (dcmd_timeout_ocr_possible(instance)) {
4687 case INITIATE_OCR:
4688 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4689 megasas_reset_fusion(instance->host,
4690 MFI_IO_TIMEOUT_OCR);
4691 break;
4692 case KILL_ADAPTER:
4693 megaraid_sas_kill_hba(instance);
4694 break;
4695 case IGNORE_TIMEOUT:
4696 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4697 __func__, __LINE__);
4698 break;
4699 }
4700 case DCMD_FAILED:
4701 megaraid_sas_kill_hba(instance);
4702 break;
4703
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304704 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004705
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304706 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304707
4708
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004709 return ret;
4710}
4711
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304712/*
4713 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4714 * to firmware
4715 *
4716 * @instance: Adapter soft state
4717 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4718 MR_CRASH_BUF_TURN_OFF = 0
4719 MR_CRASH_BUF_TURN_ON = 1
4720 * @return 0 on success non-zero on failure.
4721 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4722 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4723 * that driver supports crash dump feature. This DCMD will be sent only if
4724 * crash dump feature is supported by the FW.
4725 *
4726 */
4727int megasas_set_crash_dump_params(struct megasas_instance *instance,
4728 u8 crash_buf_state)
4729{
4730 int ret = 0;
4731 struct megasas_cmd *cmd;
4732 struct megasas_dcmd_frame *dcmd;
4733
4734 cmd = megasas_get_cmd(instance);
4735
4736 if (!cmd) {
4737 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4738 return -ENOMEM;
4739 }
4740
4741
4742 dcmd = &cmd->frame->dcmd;
4743
4744 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4745 dcmd->mbox.b[0] = crash_buf_state;
4746 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304747 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304748 dcmd->sge_count = 1;
4749 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4750 dcmd->timeout = 0;
4751 dcmd->pad_0 = 0;
4752 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4753 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4754 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4755 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4756
Shivasharan Se7d36b82017-10-19 02:48:50 -07004757 if ((instance->adapter_type != MFI_SERIES) &&
4758 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304759 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304760 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304761 ret = megasas_issue_polled(instance, cmd);
4762
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304763 if (ret == DCMD_TIMEOUT) {
4764 switch (dcmd_timeout_ocr_possible(instance)) {
4765 case INITIATE_OCR:
4766 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4767 megasas_reset_fusion(instance->host,
4768 MFI_IO_TIMEOUT_OCR);
4769 break;
4770 case KILL_ADAPTER:
4771 megaraid_sas_kill_hba(instance);
4772 break;
4773 case IGNORE_TIMEOUT:
4774 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4775 __func__, __LINE__);
4776 break;
4777 }
4778 } else
4779 megasas_return_cmd(instance, cmd);
4780
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304781 return ret;
4782}
4783
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004784/**
bo yang31ea7082007-11-07 12:09:50 -05004785 * megasas_issue_init_mfi - Initializes the FW
4786 * @instance: Adapter soft state
4787 *
4788 * Issues the INIT MFI cmd
4789 */
4790static int
4791megasas_issue_init_mfi(struct megasas_instance *instance)
4792{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304793 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004794 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004795 struct megasas_init_frame *init_frame;
4796 struct megasas_init_queue_info *initq_info;
4797 dma_addr_t init_frame_h;
4798 dma_addr_t initq_info_h;
4799
4800 /*
4801 * Prepare a init frame. Note the init frame points to queue info
4802 * structure. Each frame has SGL allocated after first 64 bytes. For
4803 * this frame - since we don't need any SGL - we use SGL's space as
4804 * queue info structure
4805 *
4806 * We will not get a NULL command below. We just created the pool.
4807 */
4808 cmd = megasas_get_cmd(instance);
4809
4810 init_frame = (struct megasas_init_frame *)cmd->frame;
4811 initq_info = (struct megasas_init_queue_info *)
4812 ((unsigned long)init_frame + 64);
4813
4814 init_frame_h = cmd->frame_phys_addr;
4815 initq_info_h = init_frame_h + 64;
4816
4817 context = init_frame->context;
4818 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4819 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4820 init_frame->context = context;
4821
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304822 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4823 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004824
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304825 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4826 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004827
4828 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304829 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304830 init_frame->queue_info_new_phys_addr_lo =
4831 cpu_to_le32(lower_32_bits(initq_info_h));
4832 init_frame->queue_info_new_phys_addr_hi =
4833 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004834
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304835 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004836
4837 /*
4838 * disable the intr before firing the init frame to FW
4839 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304840 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004841
4842 /*
4843 * Issue the init frame in polled mode
4844 */
4845
4846 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004847 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004848 megasas_return_cmd(instance, cmd);
4849 goto fail_fw_init;
4850 }
4851
4852 megasas_return_cmd(instance, cmd);
4853
4854 return 0;
4855
4856fail_fw_init:
4857 return -EINVAL;
4858}
4859
adam radfordcd50ba82010-12-21 10:23:23 -08004860static u32
4861megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004862{
adam radfordcd50ba82010-12-21 10:23:23 -08004863 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004864 u32 context_sz;
4865 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004866
4867 reg_set = instance->reg_set;
4868
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004869 /*
4870 * Get various operational parameters from status register
4871 */
Sumant Patro1341c932006-01-25 12:02:40 -08004872 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004873 /*
4874 * Reduce the max supported cmds by 1. This is to ensure that the
4875 * reply_q_sz (1 more than the max cmd that driver may send)
4876 * does not exceed max cmds that the FW can support
4877 */
4878 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004879 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004880 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004881 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004882 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304883 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4884 * are reserved for IOCTL + driver's internal DCMDs.
4885 */
4886 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4887 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4888 instance->max_scsi_cmds = (instance->max_fw_cmds -
4889 MEGASAS_SKINNY_INT_CMDS);
4890 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4891 } else {
4892 instance->max_scsi_cmds = (instance->max_fw_cmds -
4893 MEGASAS_INT_CMDS);
4894 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4895 }
4896
Sumit Saxena308ec452016-01-28 21:04:30 +05304897 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304898 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004899 * Create a pool of commands
4900 */
4901 if (megasas_alloc_cmds(instance))
4902 goto fail_alloc_cmds;
4903
4904 /*
4905 * Allocate memory for reply queue. Length of reply queue should
4906 * be _one_ more than the maximum commands handled by the firmware.
4907 *
4908 * Note: When FW completes commands, it places corresponding contex
4909 * values in this circular reply queue. This circular queue is a fairly
4910 * typical producer-consumer queue. FW is the producer (of completed
4911 * commands) and the driver is the consumer.
4912 */
4913 context_sz = sizeof(u32);
4914 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4915
4916 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4917 reply_q_sz,
4918 &instance->reply_queue_h);
4919
4920 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004921 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004922 goto fail_reply_queue;
4923 }
4924
bo yang31ea7082007-11-07 12:09:50 -05004925 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004926 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004927
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304928 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304929 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4930 "Fail from %s %d\n", instance->unique_id,
4931 __func__, __LINE__);
4932 goto fail_fw_init;
4933 }
4934
bo yang39a98552010-09-22 22:36:29 -04004935 instance->fw_support_ieee = 0;
4936 instance->fw_support_ieee =
4937 (instance->instancet->read_fw_status_reg(reg_set) &
4938 0x04000000);
4939
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004940 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004941 instance->fw_support_ieee);
4942
4943 if (instance->fw_support_ieee)
4944 instance->flag_ieee = 1;
4945
adam radfordcd50ba82010-12-21 10:23:23 -08004946 return 0;
4947
4948fail_fw_init:
4949
4950 pci_free_consistent(instance->pdev, reply_q_sz,
4951 instance->reply_queue, instance->reply_queue_h);
4952fail_reply_queue:
4953 megasas_free_cmds(instance);
4954
4955fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004956 return 1;
4957}
4958
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304959/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01004960 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304961 * @instance: Adapter soft state
4962 *
4963 * Do not enable interrupt, only setup ISRs.
4964 *
4965 * Return 0 on success.
4966 */
4967static int
4968megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4969{
4970 struct pci_dev *pdev;
4971
4972 pdev = instance->pdev;
4973 instance->irq_context[0].instance = instance;
4974 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01004975 if (request_irq(pci_irq_vector(pdev, 0),
4976 instance->instancet->service_isr, IRQF_SHARED,
4977 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304978 dev_err(&instance->pdev->dev,
4979 "Failed to register IRQ from %s %d\n",
4980 __func__, __LINE__);
4981 return -1;
4982 }
4983 return 0;
4984}
4985
4986/**
4987 * megasas_setup_irqs_msix - register MSI-x interrupts.
4988 * @instance: Adapter soft state
4989 * @is_probe: Driver probe check
4990 *
4991 * Do not enable interrupt, only setup ISRs.
4992 *
4993 * Return 0 on success.
4994 */
4995static int
4996megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4997{
Hannes Reineckefad119b2016-12-02 12:52:23 +01004998 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304999 struct pci_dev *pdev;
5000
5001 pdev = instance->pdev;
5002
5003 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305004 for (i = 0; i < instance->msix_vectors; i++) {
5005 instance->irq_context[i].instance = instance;
5006 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005007 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305008 instance->instancet->service_isr, 0, "megasas",
5009 &instance->irq_context[i])) {
5010 dev_err(&instance->pdev->dev,
5011 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005012 for (j = 0; j < i; j++)
5013 free_irq(pci_irq_vector(pdev, j),
5014 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305015 /* Retry irq register for IO_APIC*/
5016 instance->msix_vectors = 0;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005017 if (is_probe) {
5018 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305019 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005020 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305021 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005022 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305023 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305024 }
5025 return 0;
5026}
5027
5028/*
5029 * megasas_destroy_irqs- unregister interrupts.
5030 * @instance: Adapter soft state
5031 * return: void
5032 */
5033static void
5034megasas_destroy_irqs(struct megasas_instance *instance) {
5035
5036 int i;
5037
5038 if (instance->msix_vectors)
5039 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005040 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305041 &instance->irq_context[i]);
5042 }
5043 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005044 free_irq(pci_irq_vector(instance->pdev, 0),
5045 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305046}
5047
adam radfordcd50ba82010-12-21 10:23:23 -08005048/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305049 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5050 * @instance: Adapter soft state
5051 * @is_probe: Driver probe check
5052 *
5053 * Return 0 on success.
5054 */
5055void
5056megasas_setup_jbod_map(struct megasas_instance *instance)
5057{
5058 int i;
5059 struct fusion_context *fusion = instance->ctrl_context;
5060 u32 pd_seq_map_sz;
5061
5062 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5063 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5064
5065 if (reset_devices || !fusion ||
5066 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
5067 dev_info(&instance->pdev->dev,
5068 "Jbod map is not supported %s %d\n",
5069 __func__, __LINE__);
5070 instance->use_seqnum_jbod_fp = false;
5071 return;
5072 }
5073
5074 if (fusion->pd_seq_sync[0])
5075 goto skip_alloc;
5076
5077 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5078 fusion->pd_seq_sync[i] = dma_alloc_coherent
5079 (&instance->pdev->dev, pd_seq_map_sz,
5080 &fusion->pd_seq_phys[i], GFP_KERNEL);
5081 if (!fusion->pd_seq_sync[i]) {
5082 dev_err(&instance->pdev->dev,
5083 "Failed to allocate memory from %s %d\n",
5084 __func__, __LINE__);
5085 if (i == 1) {
5086 dma_free_coherent(&instance->pdev->dev,
5087 pd_seq_map_sz, fusion->pd_seq_sync[0],
5088 fusion->pd_seq_phys[0]);
5089 fusion->pd_seq_sync[0] = NULL;
5090 }
5091 instance->use_seqnum_jbod_fp = false;
5092 return;
5093 }
5094 }
5095
5096skip_alloc:
5097 if (!megasas_sync_pd_seq_num(instance, false) &&
5098 !megasas_sync_pd_seq_num(instance, true))
5099 instance->use_seqnum_jbod_fp = true;
5100 else
5101 instance->use_seqnum_jbod_fp = false;
5102}
5103
5104/**
adam radfordcd50ba82010-12-21 10:23:23 -08005105 * megasas_init_fw - Initializes the FW
5106 * @instance: Adapter soft state
5107 *
5108 * This is the main function for initializing firmware
5109 */
5110
5111static int megasas_init_fw(struct megasas_instance *instance)
5112{
5113 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005114 u32 max_sectors_2, tmp_sectors, msix_enable;
5115 u32 scratch_pad_2, scratch_pad_3, scratch_pad_4;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005116 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005117 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305118 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005119 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005120 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005121 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305122 struct fusion_context *fusion;
5123
5124 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005125
5126 /* Find first memory bar */
5127 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005128 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005129 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005130 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005131 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005132 return -EBUSY;
5133 }
5134
Ben Collins11f8a7b2013-09-13 12:46:44 -04005135 base_addr = pci_resource_start(instance->pdev, instance->bar);
5136 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005137
5138 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005139 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005140 goto fail_ioremap;
5141 }
5142
5143 reg_set = instance->reg_set;
5144
Shivasharan Se7d36b82017-10-19 02:48:50 -07005145 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005146 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005147 else {
5148 switch (instance->pdev->device) {
5149 case PCI_DEVICE_ID_LSI_SAS1078R:
5150 case PCI_DEVICE_ID_LSI_SAS1078DE:
5151 instance->instancet = &megasas_instance_template_ppc;
5152 break;
5153 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5154 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5155 instance->instancet = &megasas_instance_template_gen2;
5156 break;
5157 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5158 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5159 instance->instancet = &megasas_instance_template_skinny;
5160 break;
5161 case PCI_DEVICE_ID_LSI_SAS1064R:
5162 case PCI_DEVICE_ID_DELL_PERC5:
5163 default:
5164 instance->instancet = &megasas_instance_template_xscale;
5165 instance->pd_list_not_supported = 1;
5166 break;
5167 }
adam radfordcd50ba82010-12-21 10:23:23 -08005168 }
5169
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305170 if (megasas_transition_to_ready(instance, 0)) {
5171 atomic_set(&instance->fw_reset_no_pci_access, 1);
5172 instance->instancet->adp_reset
5173 (instance, instance->reg_set);
5174 atomic_set(&instance->fw_reset_no_pci_access, 0);
5175 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005176 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305177 __func__);
5178
5179 /*waitting for about 30 second before retry*/
5180 ssleep(30);
5181
5182 if (megasas_transition_to_ready(instance, 0))
5183 goto fail_ready_state;
5184 }
adam radfordcd50ba82010-12-21 10:23:23 -08005185
Shivasharan Sf369a312017-10-19 02:48:52 -07005186 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005187 scratch_pad_3 =
5188 readl(&instance->reg_set->outbound_scratch_pad_3);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005189 instance->max_raid_mapsize = ((scratch_pad_3 >>
5190 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5191 MR_MAX_RAID_MAP_SIZE_MASK);
5192 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305193
adam radford3f1abce2011-05-11 18:33:47 -07005194 /* Check if MSI-X is supported while in ready state */
5195 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5196 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005197 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005198 int irq_flags = PCI_IRQ_MSIX;
5199
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305200 scratch_pad_2 = readl
5201 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005202 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305203 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005204 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5205 /* Thunderbolt Series*/
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305206 instance->msix_vectors = (scratch_pad_2
5207 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5208 fw_msix_count = instance->msix_vectors;
5209 } else { /* Invader series supports more than 8 MSI-x vectors*/
5210 instance->msix_vectors = ((scratch_pad_2
5211 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5212 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005213 if (instance->msix_vectors > 16)
5214 instance->msix_combined = true;
5215
Sumit Saxena179ac142016-01-28 21:04:28 +05305216 if (rdpq_enable)
5217 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5218 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305219 fw_msix_count = instance->msix_vectors;
5220 /* Save 1-15 reply post index address to local memory
5221 * Index 0 is already saved from reg offset
5222 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5223 */
5224 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5225 instance->reply_post_host_index_addr[loop] =
5226 (u32 __iomem *)
5227 ((u8 __iomem *)instance->reg_set +
5228 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5229 + (loop * 0x10));
5230 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305231 }
5232 if (msix_vectors)
5233 instance->msix_vectors = min(msix_vectors,
5234 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305235 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005236 instance->msix_vectors = 1;
5237 /* Don't bother allocating more MSI-X vectors than cpus */
5238 instance->msix_vectors = min(instance->msix_vectors,
5239 (unsigned int)num_online_cpus());
Hannes Reineckefad119b2016-12-02 12:52:23 +01005240 if (smp_affinity_enable)
5241 irq_flags |= PCI_IRQ_AFFINITY;
5242 i = pci_alloc_irq_vectors(instance->pdev, 1,
5243 instance->msix_vectors, irq_flags);
Jiang Liuc12de882014-11-03 20:44:20 +08005244 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005245 instance->msix_vectors = i;
5246 else
adam radfordc8e858f2011-10-08 18:15:13 -07005247 instance->msix_vectors = 0;
5248 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005249 /*
5250 * MSI-X host index 0 is common for all adapter.
5251 * It is used for all MPT based Adapters.
5252 */
5253 if (instance->msix_combined) {
5254 instance->reply_post_host_index_addr[0] =
5255 (u32 *)((u8 *)instance->reg_set +
5256 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5257 } else {
5258 instance->reply_post_host_index_addr[0] =
5259 (u32 *)((u8 *)instance->reg_set +
5260 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5261 }
5262
Shivasharan S64ff64b2017-03-10 03:22:12 -08005263 if (!instance->msix_vectors) {
5264 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5265 if (i < 0)
5266 goto fail_setup_irqs;
5267 }
adam radford3f1abce2011-05-11 18:33:47 -07005268
Tomas Henzl258c3af2015-06-02 16:09:46 +05305269 dev_info(&instance->pdev->dev,
5270 "firmware supports msix\t: (%d)", fw_msix_count);
5271 dev_info(&instance->pdev->dev,
5272 "current msix/online cpus\t: (%d/%d)\n",
5273 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305274 dev_info(&instance->pdev->dev,
5275 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305276
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305277 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5278 (unsigned long)instance);
5279
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305280 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5281 GFP_KERNEL);
5282 if (instance->ctrl_info == NULL)
5283 goto fail_init_adapter;
5284
5285 /*
5286 * Below are default value for legacy Firmware.
5287 * non-fusion based controllers
5288 */
5289 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5290 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005291 /* Get operational params, sge flags, send init cmd to controller */
5292 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005293 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005294
Shivasharan Sf369a312017-10-19 02:48:52 -07005295 if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan S15dd0382017-02-10 00:59:10 -08005296 scratch_pad_4 =
5297 readl(&instance->reg_set->outbound_scratch_pad_4);
5298 if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
5299 MR_DEFAULT_NVME_PAGE_SHIFT)
5300 instance->nvme_page_size =
5301 (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
5302
5303 dev_info(&instance->pdev->dev,
5304 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5305 }
5306
Tomas Henzl18103ef2016-11-01 17:32:02 +01005307 if (instance->msix_vectors ?
5308 megasas_setup_irqs_msix(instance, 1) :
5309 megasas_setup_irqs_ioapic(instance))
5310 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305311
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305312 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005313
Andy Lutomirski13f30772016-05-03 10:24:31 -07005314 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005315
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305316 megasas_setup_jbod_map(instance);
5317
bo yang39a98552010-09-22 22:36:29 -04005318 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005319 * the following function will get the PD LIST.
5320 */
5321 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005322 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005323 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005324 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Shivasharan S72bff2d2017-02-10 00:59:33 -08005325 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005326 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005327
Yang, Bobdc6fb82009-12-06 08:30:19 -07005328 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005329
5330 /* stream detection initialization */
Shivasharan Sf369a312017-10-19 02:48:52 -07005331 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005332 fusion->stream_detect_by_ld =
Shivasharan S41064f12017-02-10 00:59:37 -08005333 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5334 * MAX_LOGICAL_DRIVES_EXT,
5335 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005336 if (!fusion->stream_detect_by_ld) {
5337 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08005338 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005339 goto fail_get_ld_pd_list;
5340 }
5341 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5342 fusion->stream_detect_by_ld[i] =
5343 kmalloc(sizeof(struct LD_STREAM_DETECT),
5344 GFP_KERNEL);
5345 if (!fusion->stream_detect_by_ld[i]) {
5346 dev_err(&instance->pdev->dev,
5347 "unable to allocate stream detect by LD\n ");
5348 for (j = 0; j < i; ++j)
5349 kfree(fusion->stream_detect_by_ld[j]);
5350 kfree(fusion->stream_detect_by_ld);
5351 fusion->stream_detect_by_ld = NULL;
5352 goto fail_get_ld_pd_list;
5353 }
5354 fusion->stream_detect_by_ld[i]->mru_bit_map
5355 = MR_STREAM_BITMAP;
5356 }
5357 }
5358
adam radford21c9e162013-09-06 15:27:14 -07005359 if (megasas_ld_list_query(instance,
5360 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
Shivasharan S72bff2d2017-02-10 00:59:33 -08005361 goto fail_get_ld_pd_list;
Yang, Bobdc6fb82009-12-06 08:30:19 -07005362
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005363 /*
5364 * Compute the max allowed sectors per IO: The controller info has two
5365 * limits on max sectors. Driver should use the minimum of these two.
5366 *
5367 * 1 << stripe_sz_ops.min = max sectors per strip
5368 *
5369 * Note that older firmwares ( < FW ver 30) didn't report information
5370 * to calculate max_sectors_1. So the number ended up as zero always.
5371 */
bo yang14faea92007-11-09 04:14:00 -05005372 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305373 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005374
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305375 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5376 le16_to_cpu(ctrl_info->max_strips_per_io);
5377 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005378
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005379 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305380
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305381 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5382 instance->passive = ctrl_info->cluster.passive;
5383 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305384 instance->UnevenSpanSupport =
5385 ctrl_info->adapterOperations2.supportUnevenSpans;
5386 if (instance->UnevenSpanSupport) {
5387 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305388 if (MR_ValidateMapInfo(instance))
5389 fusion->fast_path_io = 1;
5390 else
5391 fusion->fast_path_io = 0;
5392
5393 }
5394 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305395 instance->requestorId = ctrl_info->iov.requestorId;
5396 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5397 if (!ctrl_info->adapterOperations2.activePassive)
5398 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305399
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305400 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5401 instance->PlasmaFW111 ? "1.11" : "new");
5402
5403 if (instance->PlasmaFW111) {
5404 iovPtr = (struct IOV_111 *)
5405 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5406 instance->requestorId = iovPtr->requestorId;
5407 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305408 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305409 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5410 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305411 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305412
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305413 instance->crash_dump_fw_support =
5414 ctrl_info->adapterOperations3.supportCrashDump;
5415 instance->crash_dump_drv_support =
5416 (instance->crash_dump_fw_support &&
5417 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305418 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305419 megasas_set_crash_dump_params(instance,
5420 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005421
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305422 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305423 if (instance->crash_dump_buf)
5424 pci_free_consistent(instance->pdev,
5425 CRASH_DMA_BUF_SIZE,
5426 instance->crash_dump_buf,
5427 instance->crash_dump_h);
5428 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005429 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305430
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305431
5432 dev_info(&instance->pdev->dev,
5433 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5434 le16_to_cpu(ctrl_info->pci.vendor_id),
5435 le16_to_cpu(ctrl_info->pci.device_id),
5436 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5437 le16_to_cpu(ctrl_info->pci.sub_device_id));
5438 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5439 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305440 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5441 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305442 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5443 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305444
5445
bo yang14faea92007-11-09 04:14:00 -05005446 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305447 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005448 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5449 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005450
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305451 /* Check for valid throttlequeuedepth module parameter */
5452 if (throttlequeuedepth &&
5453 throttlequeuedepth <= instance->max_scsi_cmds)
5454 instance->throttlequeuedepth = throttlequeuedepth;
5455 else
5456 instance->throttlequeuedepth =
5457 MEGASAS_THROTTLE_QUEUE_DEPTH;
5458
Shivasharan Se636a7a2017-08-23 04:46:56 -07005459 if ((resetwaittime < 1) ||
5460 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305461 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5462
5463 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5464 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005465
adam radford229fe472014-03-10 02:51:56 -07005466 /* Launch SR-IOV heartbeat timer */
5467 if (instance->requestorId) {
5468 if (!megasas_sriov_start_heartbeat(instance, 1))
5469 megasas_start_timer(instance,
5470 &instance->sriov_heartbeat_timer,
5471 megasas_sriov_heartbeat_handler,
5472 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5473 else
5474 instance->skip_heartbeat_timer_del = 1;
5475 }
5476
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005477 return 0;
5478
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005479fail_get_ld_pd_list:
5480 instance->instancet->disable_intr(instance);
Tomas Henzl18103ef2016-11-01 17:32:02 +01005481fail_init_adapter:
Hannes Reineckefad119b2016-12-02 12:52:23 +01005482 megasas_destroy_irqs(instance);
5483fail_setup_irqs:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305484 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005485 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305486 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005487fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305488 kfree(instance->ctrl_info);
5489 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005490 iounmap(instance->reg_set);
5491
Shivasharan S72bff2d2017-02-10 00:59:33 -08005492fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005493 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005494
Shivasharan S72bff2d2017-02-10 00:59:33 -08005495 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5496 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005497 return -EINVAL;
5498}
5499
5500/**
5501 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005502 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005503 */
5504static void megasas_release_mfi(struct megasas_instance *instance)
5505{
adam radford9c915a82010-12-21 13:34:31 -08005506 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005507
adam radford9c915a82010-12-21 13:34:31 -08005508 if (instance->reply_queue)
5509 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005510 instance->reply_queue, instance->reply_queue_h);
5511
5512 megasas_free_cmds(instance);
5513
5514 iounmap(instance->reg_set);
5515
Yinghai Lue7f85162016-08-05 23:37:34 -07005516 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005517}
5518
5519/**
5520 * megasas_get_seq_num - Gets latest event sequence numbers
5521 * @instance: Adapter soft state
5522 * @eli: FW event log sequence numbers information
5523 *
5524 * FW maintains a log of all events in a non-volatile area. Upper layers would
5525 * usually find out the latest sequence number of the events, the seq number at
5526 * the boot etc. They would "read" all the events below the latest seq number
5527 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5528 * number), they would subsribe to AEN (asynchronous event notification) and
5529 * wait for the events to happen.
5530 */
5531static int
5532megasas_get_seq_num(struct megasas_instance *instance,
5533 struct megasas_evt_log_info *eli)
5534{
5535 struct megasas_cmd *cmd;
5536 struct megasas_dcmd_frame *dcmd;
5537 struct megasas_evt_log_info *el_info;
5538 dma_addr_t el_info_h = 0;
5539
5540 cmd = megasas_get_cmd(instance);
5541
5542 if (!cmd) {
5543 return -ENOMEM;
5544 }
5545
5546 dcmd = &cmd->frame->dcmd;
5547 el_info = pci_alloc_consistent(instance->pdev,
5548 sizeof(struct megasas_evt_log_info),
5549 &el_info_h);
5550
5551 if (!el_info) {
5552 megasas_return_cmd(instance, cmd);
5553 return -ENOMEM;
5554 }
5555
5556 memset(el_info, 0, sizeof(*el_info));
5557 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5558
5559 dcmd->cmd = MFI_CMD_DCMD;
5560 dcmd->cmd_status = 0x0;
5561 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305562 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005563 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005564 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305565 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5566 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5567 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5568 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005569
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305570 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5571 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305572 /*
5573 * Copy the data back into callers buffer
5574 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305575 eli->newest_seq_num = el_info->newest_seq_num;
5576 eli->oldest_seq_num = el_info->oldest_seq_num;
5577 eli->clear_seq_num = el_info->clear_seq_num;
5578 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5579 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305580 } else
5581 dev_err(&instance->pdev->dev, "DCMD failed "
5582 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005583
5584 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5585 el_info, el_info_h);
5586
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305587 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005588
5589 return 0;
5590}
5591
5592/**
5593 * megasas_register_aen - Registers for asynchronous event notification
5594 * @instance: Adapter soft state
5595 * @seq_num: The starting sequence number
5596 * @class_locale: Class of the event
5597 *
5598 * This function subscribes for AEN for events beyond the @seq_num. It requests
5599 * to be notified if and only if the event is of type @class_locale
5600 */
5601static int
5602megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5603 u32 class_locale_word)
5604{
5605 int ret_val;
5606 struct megasas_cmd *cmd;
5607 struct megasas_dcmd_frame *dcmd;
5608 union megasas_evt_class_locale curr_aen;
5609 union megasas_evt_class_locale prev_aen;
5610
5611 /*
5612 * If there an AEN pending already (aen_cmd), check if the
5613 * class_locale of that pending AEN is inclusive of the new
5614 * AEN request we currently have. If it is, then we don't have
5615 * to do anything. In other words, whichever events the current
5616 * AEN request is subscribing to, have already been subscribed
5617 * to.
5618 *
5619 * If the old_cmd is _not_ inclusive, then we have to abort
5620 * that command, form a class_locale that is superset of both
5621 * old and current and re-issue to the FW
5622 */
5623
5624 curr_aen.word = class_locale_word;
5625
5626 if (instance->aen_cmd) {
5627
Christoph Hellwiga9555532015-04-23 16:34:24 +05305628 prev_aen.word =
5629 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005630
Shivasharan S91b3d9f2017-08-23 04:47:01 -07005631 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5632 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5633 dev_info(&instance->pdev->dev,
5634 "%s %d out of range class %d send by application\n",
5635 __func__, __LINE__, curr_aen.members.class);
5636 return 0;
5637 }
5638
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005639 /*
5640 * A class whose enum value is smaller is inclusive of all
5641 * higher values. If a PROGRESS (= -1) was previously
5642 * registered, then a new registration requests for higher
5643 * classes need not be sent to FW. They are automatically
5644 * included.
5645 *
5646 * Locale numbers don't have such hierarchy. They are bitmap
5647 * values
5648 */
5649 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305650 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005651 curr_aen.members.locale)) {
5652 /*
5653 * Previously issued event registration includes
5654 * current request. Nothing to do.
5655 */
5656 return 0;
5657 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305658 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005659
5660 if (prev_aen.members.class < curr_aen.members.class)
5661 curr_aen.members.class = prev_aen.members.class;
5662
5663 instance->aen_cmd->abort_aen = 1;
5664 ret_val = megasas_issue_blocked_abort_cmd(instance,
5665 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305666 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005667
5668 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005669 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005670 "previous AEN command\n");
5671 return ret_val;
5672 }
5673 }
5674 }
5675
5676 cmd = megasas_get_cmd(instance);
5677
5678 if (!cmd)
5679 return -ENOMEM;
5680
5681 dcmd = &cmd->frame->dcmd;
5682
5683 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5684
5685 /*
5686 * Prepare DCMD for aen registration
5687 */
5688 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5689
5690 dcmd->cmd = MFI_CMD_DCMD;
5691 dcmd->cmd_status = 0x0;
5692 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305693 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005694 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005695 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305696 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5697 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5698 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005699 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305700 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5701 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5702 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005703
Yang, Bof4c9a132009-10-06 14:43:28 -06005704 if (instance->aen_cmd != NULL) {
5705 megasas_return_cmd(instance, cmd);
5706 return 0;
5707 }
5708
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005709 /*
5710 * Store reference to the cmd used to register for AEN. When an
5711 * application wants us to register for AEN, we have to abort this
5712 * cmd and re-register with a new EVENT LOCALE supplied by that app
5713 */
5714 instance->aen_cmd = cmd;
5715
5716 /*
5717 * Issue the aen registration frame
5718 */
adam radford9c915a82010-12-21 13:34:31 -08005719 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005720
5721 return 0;
5722}
5723
Shivasharan S96188a82017-02-10 00:59:11 -08005724/* megasas_get_target_prop - Send DCMD with below details to firmware.
5725 *
5726 * This DCMD will fetch few properties of LD/system PD defined
5727 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
5728 *
5729 * DCMD send by drivers whenever new target is added to the OS.
5730 *
5731 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
5732 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
5733 * 0 = system PD, 1 = LD.
5734 * dcmd.mbox.s[1] - TargetID for LD/system PD.
5735 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
5736 *
5737 * @instance: Adapter soft state
5738 * @sdev: OS provided scsi device
5739 *
5740 * Returns 0 on success non-zero on failure.
5741 */
5742static int
5743megasas_get_target_prop(struct megasas_instance *instance,
5744 struct scsi_device *sdev)
5745{
5746 int ret;
5747 struct megasas_cmd *cmd;
5748 struct megasas_dcmd_frame *dcmd;
5749 u16 targetId = (sdev->channel % 2) + sdev->id;
5750
5751 cmd = megasas_get_cmd(instance);
5752
5753 if (!cmd) {
5754 dev_err(&instance->pdev->dev,
5755 "Failed to get cmd %s\n", __func__);
5756 return -ENOMEM;
5757 }
5758
5759 dcmd = &cmd->frame->dcmd;
5760
5761 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
5762 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5763 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
5764
5765 dcmd->mbox.s[1] = cpu_to_le16(targetId);
5766 dcmd->cmd = MFI_CMD_DCMD;
5767 dcmd->cmd_status = 0xFF;
5768 dcmd->sge_count = 1;
5769 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5770 dcmd->timeout = 0;
5771 dcmd->pad_0 = 0;
5772 dcmd->data_xfer_len =
5773 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5774 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
5775 dcmd->sgl.sge32[0].phys_addr =
5776 cpu_to_le32(instance->tgt_prop_h);
5777 dcmd->sgl.sge32[0].length =
5778 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5779
Shivasharan Se7d36b82017-10-19 02:48:50 -07005780 if ((instance->adapter_type != MFI_SERIES) &&
5781 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08005782 ret = megasas_issue_blocked_cmd(instance,
5783 cmd, MFI_IO_TIMEOUT_SECS);
5784 else
5785 ret = megasas_issue_polled(instance, cmd);
5786
5787 switch (ret) {
5788 case DCMD_TIMEOUT:
5789 switch (dcmd_timeout_ocr_possible(instance)) {
5790 case INITIATE_OCR:
5791 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5792 megasas_reset_fusion(instance->host,
5793 MFI_IO_TIMEOUT_OCR);
5794 break;
5795 case KILL_ADAPTER:
5796 megaraid_sas_kill_hba(instance);
5797 break;
5798 case IGNORE_TIMEOUT:
5799 dev_info(&instance->pdev->dev,
5800 "Ignore DCMD timeout: %s %d\n",
5801 __func__, __LINE__);
5802 break;
5803 }
5804 break;
5805
5806 default:
5807 megasas_return_cmd(instance, cmd);
5808 }
5809 if (ret != DCMD_SUCCESS)
5810 dev_err(&instance->pdev->dev,
5811 "return from %s %d return value %d\n",
5812 __func__, __LINE__, ret);
5813
5814 return ret;
5815}
5816
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005817/**
5818 * megasas_start_aen - Subscribes to AEN during driver load time
5819 * @instance: Adapter soft state
5820 */
5821static int megasas_start_aen(struct megasas_instance *instance)
5822{
5823 struct megasas_evt_log_info eli;
5824 union megasas_evt_class_locale class_locale;
5825
5826 /*
5827 * Get the latest sequence number from FW
5828 */
5829 memset(&eli, 0, sizeof(eli));
5830
5831 if (megasas_get_seq_num(instance, &eli))
5832 return -1;
5833
5834 /*
5835 * Register AEN with FW for latest sequence number plus 1
5836 */
5837 class_locale.members.reserved = 0;
5838 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5839 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5840
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305841 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305842 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305843 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005844}
5845
5846/**
5847 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5848 * @instance: Adapter soft state
5849 */
5850static int megasas_io_attach(struct megasas_instance *instance)
5851{
5852 struct Scsi_Host *host = instance->host;
5853
5854 /*
5855 * Export parameters required by SCSI mid-layer
5856 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005857 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305858 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005859 host->this_id = instance->init_id;
5860 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005861
5862 if (instance->fw_support_ieee)
5863 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5864
Yang, Bo1fd10682010-10-12 07:18:50 -06005865 /*
5866 * Check if the module parameter value for max_sectors can be used
5867 */
5868 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5869 instance->max_sectors_per_req = max_sectors;
5870 else {
5871 if (max_sectors) {
5872 if (((instance->pdev->device ==
5873 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5874 (instance->pdev->device ==
5875 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5876 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5877 instance->max_sectors_per_req = max_sectors;
5878 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005879 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005880 "and <= %d (or < 1MB for GEN2 controller)\n",
5881 instance->max_sectors_per_req);
5882 }
5883 }
5884 }
5885
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005886 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005887 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005888 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5889 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5890 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005891 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005892
5893 /*
5894 * Notify the mid-layer about the new controller
5895 */
5896 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305897 dev_err(&instance->pdev->dev,
5898 "Failed to add host from %s %d\n",
5899 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005900 return -ENODEV;
5901 }
5902
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005903 return 0;
5904}
5905
bo yang31ea7082007-11-07 12:09:50 -05005906static int
5907megasas_set_dma_mask(struct pci_dev *pdev)
5908{
5909 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005910 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005911 */
5912 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005913 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005914
Yang Hongyang284901a2009-04-06 19:01:15 -07005915 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005916 goto fail_set_dma_mask;
5917 }
5918 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005919 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005920 goto fail_set_dma_mask;
5921 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305922 /*
5923 * Ensure that all data structures are allocated in 32-bit
5924 * memory.
5925 */
5926 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5927 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5928 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5929 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5930 dev_info(&pdev->dev, "set 32bit DMA mask"
5931 "and 32 bit consistent mask\n");
5932 else
5933 goto fail_set_dma_mask;
5934 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305935
bo yang31ea7082007-11-07 12:09:50 -05005936 return 0;
5937
5938fail_set_dma_mask:
5939 return 1;
5940}
5941
Shivasharan Sc3651782017-10-19 02:48:48 -07005942/*
5943 * megasas_set_adapter_type - Set adapter type.
5944 * Supported controllers can be divided in
5945 * 4 categories- enum MR_ADAPTER_TYPE {
5946 * MFI_SERIES = 1,
5947 * THUNDERBOLT_SERIES = 2,
5948 * INVADER_SERIES = 3,
5949 * VENTURA_SERIES = 4,
5950 * };
5951 * @instance: Adapter soft state
5952 * return: void
5953 */
5954static inline void megasas_set_adapter_type(struct megasas_instance *instance)
5955{
Shivasharan S754f1ba2017-10-19 02:48:49 -07005956 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
5957 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005958 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07005959 } else {
5960 switch (instance->pdev->device) {
5961 case PCI_DEVICE_ID_LSI_VENTURA:
5962 case PCI_DEVICE_ID_LSI_CRUSADER:
5963 case PCI_DEVICE_ID_LSI_HARPOON:
5964 case PCI_DEVICE_ID_LSI_TOMCAT:
5965 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
5966 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
5967 instance->adapter_type = VENTURA_SERIES;
5968 break;
5969 case PCI_DEVICE_ID_LSI_FUSION:
5970 case PCI_DEVICE_ID_LSI_PLASMA:
5971 instance->adapter_type = THUNDERBOLT_SERIES;
5972 break;
5973 case PCI_DEVICE_ID_LSI_INVADER:
5974 case PCI_DEVICE_ID_LSI_INTRUDER:
5975 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5976 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5977 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5978 case PCI_DEVICE_ID_LSI_FURY:
5979 instance->adapter_type = INVADER_SERIES;
5980 break;
5981 default: /* For all other supported controllers */
5982 instance->adapter_type = MFI_SERIES;
5983 break;
5984 }
Shivasharan Sc3651782017-10-19 02:48:48 -07005985 }
5986}
5987
Shivasharan S49a7a4a2017-10-19 02:48:54 -07005988static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
5989{
5990 instance->producer = pci_alloc_consistent(instance->pdev, sizeof(u32),
5991 &instance->producer_h);
5992 instance->consumer = pci_alloc_consistent(instance->pdev, sizeof(u32),
5993 &instance->consumer_h);
5994
5995 if (!instance->producer || !instance->consumer) {
5996 dev_err(&instance->pdev->dev,
5997 "Failed to allocate memory for producer, consumer\n");
5998 return -1;
5999 }
6000
6001 *instance->producer = 0;
6002 *instance->consumer = 0;
6003 return 0;
6004}
6005
6006/**
6007 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6008 * structures which are not common across MFI
6009 * adapters and fusion adapters.
6010 * For MFI based adapters, allocate producer and
6011 * consumer buffers. For fusion adapters, allocate
6012 * memory for fusion context.
6013 * @instance: Adapter soft state
6014 * return: 0 for SUCCESS
6015 */
6016static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6017{
6018 switch (instance->adapter_type) {
6019 case MFI_SERIES:
6020 if (megasas_alloc_mfi_ctrl_mem(instance))
6021 return -ENOMEM;
6022 break;
6023 case VENTURA_SERIES:
6024 case THUNDERBOLT_SERIES:
6025 case INVADER_SERIES:
6026 if (megasas_alloc_fusion_context(instance))
6027 return -ENOMEM;
6028 break;
6029 }
6030
6031 return 0;
6032}
6033
6034/*
6035 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6036 * producer, consumer buffers for MFI adapters
6037 *
6038 * @instance - Adapter soft instance
6039 *
6040 */
6041static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6042{
6043 if (instance->adapter_type == MFI_SERIES) {
6044 if (instance->producer)
6045 pci_free_consistent(instance->pdev, sizeof(u32),
6046 instance->producer,
6047 instance->producer_h);
6048 if (instance->consumer)
6049 pci_free_consistent(instance->pdev, sizeof(u32),
6050 instance->consumer,
6051 instance->consumer_h);
6052 } else {
6053 megasas_free_fusion_context(instance);
6054 }
6055}
6056
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006057/**
Shivasharan S1b4bed22017-10-19 02:48:55 -07006058 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6059 * driver load time
6060 *
6061 * @instance- Adapter soft instance
6062 * @return- O for SUCCESS
6063 */
6064static inline
6065int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6066{
6067 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006068 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006069
6070 instance->evt_detail =
6071 pci_alloc_consistent(pdev,
6072 sizeof(struct megasas_evt_detail),
6073 &instance->evt_detail_h);
6074
6075 if (!instance->evt_detail) {
6076 dev_err(&instance->pdev->dev,
6077 "Failed to allocate event detail buffer\n");
6078 return -ENOMEM;
6079 }
6080
Shivasharan S9b3d0282017-10-19 02:48:56 -07006081 if (fusion) {
6082 fusion->ioc_init_request =
6083 dma_alloc_coherent(&pdev->dev,
6084 sizeof(struct MPI2_IOC_INIT_REQUEST),
6085 &fusion->ioc_init_request_phys,
6086 GFP_KERNEL);
6087
6088 if (!fusion->ioc_init_request) {
6089 dev_err(&pdev->dev,
6090 "Failed to allocate PD list buffer\n");
6091 return -ENOMEM;
6092 }
6093 }
6094
6095 instance->pd_list_buf =
6096 pci_alloc_consistent(pdev,
6097 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6098 &instance->pd_list_buf_h);
6099
6100 if (!instance->pd_list_buf) {
6101 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6102 return -ENOMEM;
6103 }
6104
6105 instance->ctrl_info_buf =
6106 pci_alloc_consistent(pdev,
6107 sizeof(struct megasas_ctrl_info),
6108 &instance->ctrl_info_buf_h);
6109
6110 if (!instance->ctrl_info_buf) {
6111 dev_err(&pdev->dev,
6112 "Failed to allocate controller info buffer\n");
6113 return -ENOMEM;
6114 }
6115
6116 instance->ld_list_buf =
6117 pci_alloc_consistent(pdev,
6118 sizeof(struct MR_LD_LIST),
6119 &instance->ld_list_buf_h);
6120
6121 if (!instance->ld_list_buf) {
6122 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6123 return -ENOMEM;
6124 }
6125
6126 instance->ld_targetid_list_buf =
6127 pci_alloc_consistent(pdev,
6128 sizeof(struct MR_LD_TARGETID_LIST),
6129 &instance->ld_targetid_list_buf_h);
6130
6131 if (!instance->ld_targetid_list_buf) {
6132 dev_err(&pdev->dev,
6133 "Failed to allocate LD targetid list buffer\n");
6134 return -ENOMEM;
6135 }
6136
Shivasharan S1b4bed22017-10-19 02:48:55 -07006137 if (!reset_devices) {
6138 instance->system_info_buf =
6139 pci_alloc_consistent(pdev,
6140 sizeof(struct MR_DRV_SYSTEM_INFO),
6141 &instance->system_info_h);
6142 instance->pd_info =
6143 pci_alloc_consistent(pdev,
6144 sizeof(struct MR_PD_INFO),
6145 &instance->pd_info_h);
6146 instance->tgt_prop =
6147 pci_alloc_consistent(pdev,
6148 sizeof(struct MR_TARGET_PROPERTIES),
6149 &instance->tgt_prop_h);
6150 instance->crash_dump_buf =
6151 pci_alloc_consistent(pdev,
6152 CRASH_DMA_BUF_SIZE,
6153 &instance->crash_dump_h);
6154
6155 if (!instance->system_info_buf)
6156 dev_err(&instance->pdev->dev,
6157 "Failed to allocate system info buffer\n");
6158
6159 if (!instance->pd_info)
6160 dev_err(&instance->pdev->dev,
6161 "Failed to allocate pd_info buffer\n");
6162
6163 if (!instance->tgt_prop)
6164 dev_err(&instance->pdev->dev,
6165 "Failed to allocate tgt_prop buffer\n");
6166
6167 if (!instance->crash_dump_buf)
6168 dev_err(&instance->pdev->dev,
6169 "Failed to allocate crash dump buffer\n");
6170 }
6171
6172 return 0;
6173}
6174
6175/*
6176 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
6177 * during driver load time
6178 *
6179 * @instance- Adapter soft instance
6180 *
6181 */
6182static inline
6183void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6184{
6185 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006186 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006187
6188 if (instance->evt_detail)
6189 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6190 instance->evt_detail,
6191 instance->evt_detail_h);
6192
Shivasharan S9b3d0282017-10-19 02:48:56 -07006193 if (fusion && fusion->ioc_init_request)
6194 dma_free_coherent(&pdev->dev,
6195 sizeof(struct MPI2_IOC_INIT_REQUEST),
6196 fusion->ioc_init_request,
6197 fusion->ioc_init_request_phys);
6198
6199 if (instance->pd_list_buf)
6200 pci_free_consistent(pdev,
6201 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6202 instance->pd_list_buf,
6203 instance->pd_list_buf_h);
6204
6205 if (instance->ld_list_buf)
6206 pci_free_consistent(pdev, sizeof(struct MR_LD_LIST),
6207 instance->ld_list_buf,
6208 instance->ld_list_buf_h);
6209
6210 if (instance->ld_targetid_list_buf)
6211 pci_free_consistent(pdev, sizeof(struct MR_LD_TARGETID_LIST),
6212 instance->ld_targetid_list_buf,
6213 instance->ld_targetid_list_buf_h);
6214
6215 if (instance->ctrl_info_buf)
6216 pci_free_consistent(pdev, sizeof(struct megasas_ctrl_info),
6217 instance->ctrl_info_buf,
6218 instance->ctrl_info_buf_h);
6219
Shivasharan S1b4bed22017-10-19 02:48:55 -07006220 if (instance->system_info_buf)
6221 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6222 instance->system_info_buf,
6223 instance->system_info_h);
6224
6225 if (instance->pd_info)
6226 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6227 instance->pd_info, instance->pd_info_h);
6228
6229 if (instance->tgt_prop)
6230 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6231 instance->tgt_prop, instance->tgt_prop_h);
6232
6233 if (instance->crash_dump_buf)
6234 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6235 instance->crash_dump_buf,
6236 instance->crash_dump_h);
6237}
6238
6239/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006240 * megasas_probe_one - PCI hotplug entry point
6241 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08006242 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006243 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006244static int megasas_probe_one(struct pci_dev *pdev,
6245 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006246{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306247 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006248 struct Scsi_Host *host;
6249 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08006250 u16 control = 0;
6251
6252 /* Reset MSI-X in the kdump kernel */
6253 if (reset_devices) {
6254 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6255 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06006256 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006257 &control);
6258 if (control & PCI_MSIX_FLAGS_ENABLE) {
6259 dev_info(&pdev->dev, "resetting MSI-X\n");
6260 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06006261 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006262 control &
6263 ~PCI_MSIX_FLAGS_ENABLE);
6264 }
6265 }
6266 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006267
6268 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006269 * PCI prepping: enable device set bus mastering and dma mask
6270 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006271 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006272
6273 if (rval) {
6274 return rval;
6275 }
6276
6277 pci_set_master(pdev);
6278
bo yang31ea7082007-11-07 12:09:50 -05006279 if (megasas_set_dma_mask(pdev))
6280 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006281
6282 host = scsi_host_alloc(&megasas_template,
6283 sizeof(struct megasas_instance));
6284
6285 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006286 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006287 goto fail_alloc_instance;
6288 }
6289
6290 instance = (struct megasas_instance *)host->hostdata;
6291 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006292 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08006293 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006294
Shivasharan Sc3651782017-10-19 02:48:48 -07006295 megasas_set_adapter_type(instance);
Shivasharan S5fc499b2017-02-10 00:59:17 -08006296
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006297 if (megasas_alloc_ctrl_mem(instance))
6298 goto fail_alloc_dma_buf;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006299
Shivasharan S1b4bed22017-10-19 02:48:55 -07006300 if (megasas_alloc_ctrl_dma_buffers(instance))
6301 goto fail_alloc_dma_buf;
6302
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306303 /* Crash dump feature related initialisation*/
6304 instance->drv_buf_index = 0;
6305 instance->drv_buf_alloc = 0;
6306 instance->crash_dump_fw_support = 0;
6307 instance->crash_dump_app_support = 0;
6308 instance->fw_crash_state = UNAVAILABLE;
6309 spin_lock_init(&instance->crashdump_lock);
6310 instance->crash_dump_buf = NULL;
6311
Yang, Boc3518832009-10-06 14:18:02 -06006312 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06006313 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006314 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04006315 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05306316 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306317 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006318
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006319 /*
6320 * Initialize locks and queues
6321 */
6322 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04006323 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006324
Sumant Patroe4a082c2006-05-30 12:03:37 -07006325 atomic_set(&instance->fw_outstanding,0);
6326
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006327 init_waitqueue_head(&instance->int_cmd_wait_q);
6328 init_waitqueue_head(&instance->abort_cmd_wait_q);
6329
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306330 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04006331 spin_lock_init(&instance->hba_lock);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006332 spin_lock_init(&instance->stream_lock);
bo yang7343eb62007-11-09 04:35:44 -05006333 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006334
adam radford9c915a82010-12-21 13:34:31 -08006335 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05306336 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006337
6338 /*
6339 * Initialize PCI related and misc parameters
6340 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006341 instance->host = host;
6342 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6343 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306344 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006345
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306346
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006347 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306348 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06006349 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06006350
Sumant Patro658dced2006-10-03 13:09:14 -07006351 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006352 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06006353 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07006354 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04006355 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05306356 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04006357
Shivasharan Se7d36b82017-10-19 02:48:50 -07006358 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006359 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306360 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
6361 } else
adam radford9c915a82010-12-21 13:34:31 -08006362 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07006363
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006364 /*
adam radford0a770662011-02-24 20:56:12 -08006365 * Initialize MFI Firmware
6366 */
6367 if (megasas_init_fw(instance))
6368 goto fail_init_mfi;
6369
adam radford229fe472014-03-10 02:51:56 -07006370 if (instance->requestorId) {
6371 if (instance->PlasmaFW111) {
6372 instance->vf_affiliation_111 =
6373 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
6374 &instance->vf_affiliation_111_h);
6375 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006376 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006377 "memory for VF affiliation buffer\n");
6378 } else {
6379 instance->vf_affiliation =
6380 pci_alloc_consistent(pdev,
6381 (MAX_LOGICAL_DRIVES + 1) *
6382 sizeof(struct MR_LD_VF_AFFILIATION),
6383 &instance->vf_affiliation_h);
6384 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006385 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006386 "memory for VF affiliation buffer\n");
6387 }
6388 }
6389
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006390 /*
6391 * Store instance in PCI softstate
6392 */
6393 pci_set_drvdata(pdev, instance);
6394
6395 /*
6396 * Add this controller to megasas_mgmt_info structure so that it
6397 * can be exported to management applications
6398 */
6399 megasas_mgmt_info.count++;
6400 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6401 megasas_mgmt_info.max_index++;
6402
6403 /*
adam radford541f90b2011-05-11 18:34:29 -07006404 * Register with SCSI mid-layer
6405 */
6406 if (megasas_io_attach(instance))
6407 goto fail_io_attach;
6408
6409 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306410 /*
6411 * Trigger SCSI to scan our drives
6412 */
6413 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07006414
6415 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006416 * Initiate AEN (Asynchronous Event Notification)
6417 */
6418 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006419 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006420 goto fail_start_aen;
6421 }
6422
Adam Radford9ea81f82014-07-09 15:17:57 -07006423 /* Get current SR-IOV LD/VF affiliation */
6424 if (instance->requestorId)
6425 megasas_get_ld_vf_affiliation(instance, 1);
6426
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006427 return 0;
6428
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006429fail_start_aen:
6430fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006431 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006432 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08006433 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006434
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306435 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306436 megasas_destroy_irqs(instance);
6437
Shivasharan Se7d36b82017-10-19 02:48:50 -07006438 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08006439 megasas_release_fusion(instance);
6440 else
6441 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006442 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006443 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306444fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006445fail_alloc_dma_buf:
Shivasharan S1b4bed22017-10-19 02:48:55 -07006446 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006447 megasas_free_ctrl_mem(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006448 scsi_host_put(host);
6449
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006450fail_alloc_instance:
6451fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006452 pci_disable_device(pdev);
6453
6454 return -ENODEV;
6455}
6456
6457/**
6458 * megasas_flush_cache - Requests FW to flush all its caches
6459 * @instance: Adapter soft state
6460 */
6461static void megasas_flush_cache(struct megasas_instance *instance)
6462{
6463 struct megasas_cmd *cmd;
6464 struct megasas_dcmd_frame *dcmd;
6465
Sumit Saxena8a01a412016-01-28 21:04:32 +05306466 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006467 return;
6468
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006469 cmd = megasas_get_cmd(instance);
6470
6471 if (!cmd)
6472 return;
6473
6474 dcmd = &cmd->frame->dcmd;
6475
6476 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6477
6478 dcmd->cmd = MFI_CMD_DCMD;
6479 dcmd->cmd_status = 0x0;
6480 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306481 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006482 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006483 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006484 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306485 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006486 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6487
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306488 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6489 != DCMD_SUCCESS) {
6490 dev_err(&instance->pdev->dev,
6491 "return from %s %d\n", __func__, __LINE__);
6492 return;
6493 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006494
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306495 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006496}
6497
6498/**
6499 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6500 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006501 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006502 */
bo yang31ea7082007-11-07 12:09:50 -05006503static void megasas_shutdown_controller(struct megasas_instance *instance,
6504 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006505{
6506 struct megasas_cmd *cmd;
6507 struct megasas_dcmd_frame *dcmd;
6508
Sumit Saxena8a01a412016-01-28 21:04:32 +05306509 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006510 return;
6511
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006512 cmd = megasas_get_cmd(instance);
6513
6514 if (!cmd)
6515 return;
6516
6517 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306518 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306519 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006520 if (instance->map_update_cmd)
6521 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306522 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306523 if (instance->jbod_seq_cmd)
6524 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306525 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306526
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006527 dcmd = &cmd->frame->dcmd;
6528
6529 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6530
6531 dcmd->cmd = MFI_CMD_DCMD;
6532 dcmd->cmd_status = 0x0;
6533 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306534 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006535 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006536 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006537 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306538 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006539
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306540 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6541 != DCMD_SUCCESS) {
6542 dev_err(&instance->pdev->dev,
6543 "return from %s %d\n", __func__, __LINE__);
6544 return;
6545 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006546
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306547 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006548}
6549
Jiri Slaby33139b22008-05-01 17:56:02 +02006550#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006551/**
bo yangad84db22007-11-09 04:40:16 -05006552 * megasas_suspend - driver suspend entry point
6553 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006554 * @state: PCI power state to suspend routine
6555 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006556static int
bo yang31ea7082007-11-07 12:09:50 -05006557megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6558{
6559 struct Scsi_Host *host;
6560 struct megasas_instance *instance;
6561
6562 instance = pci_get_drvdata(pdev);
6563 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006564 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006565
adam radford229fe472014-03-10 02:51:56 -07006566 /* Shutdown SR-IOV heartbeat timer */
6567 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6568 del_timer_sync(&instance->sriov_heartbeat_timer);
6569
bo yang31ea7082007-11-07 12:09:50 -05006570 megasas_flush_cache(instance);
6571 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006572
6573 /* cancel the delayed work if this work still in queue */
6574 if (instance->ev != NULL) {
6575 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006576 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006577 instance->ev = NULL;
6578 }
6579
bo yang31ea7082007-11-07 12:09:50 -05006580 tasklet_kill(&instance->isr_tasklet);
6581
6582 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306583 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006584
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306585 megasas_destroy_irqs(instance);
6586
adam radfordc8e858f2011-10-08 18:15:13 -07006587 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006588 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006589
6590 pci_save_state(pdev);
6591 pci_disable_device(pdev);
6592
6593 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6594
6595 return 0;
6596}
6597
6598/**
6599 * megasas_resume- driver resume entry point
6600 * @pdev: PCI device structure
6601 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006602static int
bo yang31ea7082007-11-07 12:09:50 -05006603megasas_resume(struct pci_dev *pdev)
6604{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306605 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006606 struct Scsi_Host *host;
6607 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01006608 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05006609
6610 instance = pci_get_drvdata(pdev);
6611 host = instance->host;
6612 pci_set_power_state(pdev, PCI_D0);
6613 pci_enable_wake(pdev, PCI_D0, 0);
6614 pci_restore_state(pdev);
6615
6616 /*
6617 * PCI prepping: enable device set bus mastering and dma mask
6618 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006619 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006620
6621 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006622 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006623 return rval;
6624 }
6625
6626 pci_set_master(pdev);
6627
6628 if (megasas_set_dma_mask(pdev))
6629 goto fail_set_dma_mask;
6630
6631 /*
6632 * Initialize MFI Firmware
6633 */
6634
bo yang31ea7082007-11-07 12:09:50 -05006635 atomic_set(&instance->fw_outstanding, 0);
6636
6637 /*
6638 * We expect the FW state to be READY
6639 */
adam radford058a8fa2011-10-08 18:14:27 -07006640 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006641 goto fail_ready_state;
6642
adam radford3f1abce2011-05-11 18:33:47 -07006643 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01006644 if (instance->msix_vectors) {
6645 irq_flags = PCI_IRQ_MSIX;
6646 if (smp_affinity_enable)
6647 irq_flags |= PCI_IRQ_AFFINITY;
6648 }
6649 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6650 instance->msix_vectors ?
6651 instance->msix_vectors : 1, irq_flags);
6652 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02006653 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006654
Shivasharan Se7d36b82017-10-19 02:48:50 -07006655 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006656 megasas_reset_reply_desc(instance);
6657 if (megasas_ioc_init_fusion(instance)) {
6658 megasas_free_cmds(instance);
6659 megasas_free_cmds_fusion(instance);
6660 goto fail_init_mfi;
6661 }
6662 if (!megasas_get_map_info(instance))
6663 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306664 } else {
adam radford9c915a82010-12-21 13:34:31 -08006665 *instance->producer = 0;
6666 *instance->consumer = 0;
6667 if (megasas_issue_init_mfi(instance))
6668 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006669 }
bo yang31ea7082007-11-07 12:09:50 -05006670
adam radford9c915a82010-12-21 13:34:31 -08006671 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6672 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006673
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306674 if (instance->msix_vectors ?
6675 megasas_setup_irqs_msix(instance, 0) :
6676 megasas_setup_irqs_ioapic(instance))
6677 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006678
adam radford229fe472014-03-10 02:51:56 -07006679 /* Re-launch SR-IOV heartbeat timer */
6680 if (instance->requestorId) {
6681 if (!megasas_sriov_start_heartbeat(instance, 0))
6682 megasas_start_timer(instance,
6683 &instance->sriov_heartbeat_timer,
6684 megasas_sriov_heartbeat_handler,
6685 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306686 else {
adam radford229fe472014-03-10 02:51:56 -07006687 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306688 goto fail_init_mfi;
6689 }
adam radford229fe472014-03-10 02:51:56 -07006690 }
6691
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306692 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306693 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006694 instance->unload = 0;
6695
adam radford541f90b2011-05-11 18:34:29 -07006696 /*
6697 * Initiate AEN (Asynchronous Event Notification)
6698 */
6699 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006700 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006701
bo yang31ea7082007-11-07 12:09:50 -05006702 return 0;
6703
bo yang31ea7082007-11-07 12:09:50 -05006704fail_init_mfi:
Shivasharan S1b4bed22017-10-19 02:48:55 -07006705 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006706 megasas_free_ctrl_mem(instance);
bo yang31ea7082007-11-07 12:09:50 -05006707 scsi_host_put(host);
6708
6709fail_set_dma_mask:
6710fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006711fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006712
6713 pci_disable_device(pdev);
6714
6715 return -ENODEV;
6716}
Jiri Slaby33139b22008-05-01 17:56:02 +02006717#else
6718#define megasas_suspend NULL
6719#define megasas_resume NULL
6720#endif
bo yang31ea7082007-11-07 12:09:50 -05006721
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006722static inline int
6723megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6724{
6725 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6726 int i;
6727
6728 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6729 return 1;
6730
6731 for (i = 0; i < wait_time; i++) {
6732 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6733 break;
6734
6735 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6736 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6737
6738 msleep(1000);
6739 }
6740
6741 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6742 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6743 __func__);
6744 return 1;
6745 }
6746
6747 return 0;
6748}
6749
bo yang31ea7082007-11-07 12:09:50 -05006750/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006751 * megasas_detach_one - PCI hot"un"plug entry point
6752 * @pdev: PCI device structure
6753 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006754static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006755{
6756 int i;
6757 struct Scsi_Host *host;
6758 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006759 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306760 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006761
6762 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006763 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006764 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006765 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006766
adam radford229fe472014-03-10 02:51:56 -07006767 /* Shutdown SR-IOV heartbeat timer */
6768 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6769 del_timer_sync(&instance->sriov_heartbeat_timer);
6770
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306771 if (instance->fw_crash_state != UNAVAILABLE)
6772 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006773 scsi_remove_host(instance->host);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006774
6775 if (megasas_wait_for_adapter_operational(instance))
6776 goto skip_firing_dcmds;
6777
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006778 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006779 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006780
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006781skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006782 /* cancel the delayed work if this work still in queue*/
6783 if (instance->ev != NULL) {
6784 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006785 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006786 instance->ev = NULL;
6787 }
6788
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306789 /* cancel all wait events */
6790 wake_up_all(&instance->int_cmd_wait_q);
6791
Sumant Patro5d018ad2006-10-03 13:13:18 -07006792 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006793
6794 /*
6795 * Take the instance off the instance array. Note that we will not
6796 * decrement the max_index. We let this array be sparse array
6797 */
6798 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6799 if (megasas_mgmt_info.instance[i] == instance) {
6800 megasas_mgmt_info.count--;
6801 megasas_mgmt_info.instance[i] = NULL;
6802
6803 break;
6804 }
6805 }
6806
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306807 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006808
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306809 megasas_destroy_irqs(instance);
6810
adam radfordc8e858f2011-10-08 18:15:13 -07006811 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006812 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006813
Shivasharan Sf369a312017-10-19 02:48:52 -07006814 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006815 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6816 kfree(fusion->stream_detect_by_ld[i]);
6817 kfree(fusion->stream_detect_by_ld);
6818 fusion->stream_detect_by_ld = NULL;
6819 }
6820
6821
Shivasharan Se7d36b82017-10-19 02:48:50 -07006822 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006823 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306824 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6825 (sizeof(struct MR_PD_CFG_SEQ) *
6826 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306827 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006828 if (fusion->ld_map[i])
6829 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306830 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006831 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306832 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07006833 if (fusion->ld_drv_map[i]) {
6834 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
6835 vfree(fusion->ld_drv_map[i]);
6836 else
6837 free_pages((ulong)fusion->ld_drv_map[i],
6838 fusion->drv_map_pages);
6839 }
6840
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006841 if (fusion->pd_seq_sync[i])
6842 dma_free_coherent(&instance->pdev->dev,
6843 pd_seq_map_sz,
6844 fusion->pd_seq_sync[i],
6845 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306846 }
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306847 } else {
adam radford9c915a82010-12-21 13:34:31 -08006848 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006849 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006850
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306851 kfree(instance->ctrl_info);
6852
adam radford229fe472014-03-10 02:51:56 -07006853 if (instance->vf_affiliation)
6854 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6855 sizeof(struct MR_LD_VF_AFFILIATION),
6856 instance->vf_affiliation,
6857 instance->vf_affiliation_h);
6858
6859 if (instance->vf_affiliation_111)
6860 pci_free_consistent(pdev,
6861 sizeof(struct MR_LD_VF_AFFILIATION_111),
6862 instance->vf_affiliation_111,
6863 instance->vf_affiliation_111_h);
6864
6865 if (instance->hb_host_mem)
6866 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6867 instance->hb_host_mem,
6868 instance->hb_host_mem_h);
6869
Shivasharan S1b4bed22017-10-19 02:48:55 -07006870 megasas_free_ctrl_dma_buffers(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306871
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006872 megasas_free_ctrl_mem(instance);
6873
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006874 scsi_host_put(host);
6875
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006876 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006877}
6878
6879/**
6880 * megasas_shutdown - Shutdown entry point
6881 * @device: Generic device structure
6882 */
6883static void megasas_shutdown(struct pci_dev *pdev)
6884{
6885 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006886
Yang, Bo0c79e682009-10-06 14:47:35 -06006887 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006888
6889 if (megasas_wait_for_adapter_operational(instance))
6890 goto skip_firing_dcmds;
6891
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006892 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006893 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006894
6895skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306896 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306897 megasas_destroy_irqs(instance);
6898
adam radfordc8e858f2011-10-08 18:15:13 -07006899 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006900 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006901}
6902
6903/**
6904 * megasas_mgmt_open - char node "open" entry point
6905 */
6906static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6907{
6908 /*
6909 * Allow only those users with admin rights
6910 */
6911 if (!capable(CAP_SYS_ADMIN))
6912 return -EACCES;
6913
6914 return 0;
6915}
6916
6917/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006918 * megasas_mgmt_fasync - Async notifier registration from applications
6919 *
6920 * This function adds the calling process to a driver global queue. When an
6921 * event occurs, SIGIO will be sent to all processes in this queue.
6922 */
6923static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6924{
6925 int rc;
6926
Arjan van de Ven0b950672006-01-11 13:16:10 +01006927 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006928
6929 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6930
Arjan van de Ven0b950672006-01-11 13:16:10 +01006931 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006932
6933 if (rc >= 0) {
6934 /* For sanity check when we get ioctl */
6935 filep->private_data = filep;
6936 return 0;
6937 }
6938
6939 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6940
6941 return rc;
6942}
6943
6944/**
Yang, Boc3518832009-10-06 14:18:02 -06006945 * megasas_mgmt_poll - char node "poll" entry point
6946 * */
6947static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6948{
6949 unsigned int mask;
6950 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006951
Yang, Boc3518832009-10-06 14:18:02 -06006952 poll_wait(file, &megasas_poll_wait, wait);
6953 spin_lock_irqsave(&poll_aen_lock, flags);
6954 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006955 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006956 else
6957 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306958 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006959 spin_unlock_irqrestore(&poll_aen_lock, flags);
6960 return mask;
6961}
6962
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306963/*
6964 * megasas_set_crash_dump_params_ioctl:
6965 * Send CRASH_DUMP_MODE DCMD to all controllers
6966 * @cmd: MFI command frame
6967 */
6968
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006969static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306970{
6971 struct megasas_instance *local_instance;
6972 int i, error = 0;
6973 int crash_support;
6974
6975 crash_support = cmd->frame->dcmd.mbox.w[0];
6976
6977 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6978 local_instance = megasas_mgmt_info.instance[i];
6979 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05306980 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306981 MEGASAS_HBA_OPERATIONAL) &&
6982 !megasas_set_crash_dump_params(local_instance,
6983 crash_support)) {
6984 local_instance->crash_dump_app_support =
6985 crash_support;
6986 dev_info(&local_instance->pdev->dev,
6987 "Application firmware crash "
6988 "dump mode set success\n");
6989 error = 0;
6990 } else {
6991 dev_info(&local_instance->pdev->dev,
6992 "Application firmware crash "
6993 "dump mode set failed\n");
6994 error = -1;
6995 }
6996 }
6997 }
6998 return error;
6999}
7000
Yang, Boc3518832009-10-06 14:18:02 -06007001/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007002 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7003 * @instance: Adapter soft state
7004 * @argp: User's ioctl packet
7005 */
7006static int
7007megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7008 struct megasas_iocpacket __user * user_ioc,
7009 struct megasas_iocpacket *ioc)
7010{
7011 struct megasas_sge32 *kern_sge32;
7012 struct megasas_cmd *cmd;
7013 void *kbuff_arr[MAX_IOCTL_SGE];
7014 dma_addr_t buf_handle = 0;
7015 int error = 0, i;
7016 void *sense = NULL;
7017 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06007018 unsigned long *sense_ptr;
Shivasharan S8823abe2017-08-23 04:47:00 -07007019 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007020
7021 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7022
7023 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007024 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007025 ioc->sge_count, MAX_IOCTL_SGE);
7026 return -EINVAL;
7027 }
7028
7029 cmd = megasas_get_cmd(instance);
7030 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007031 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007032 return -ENOMEM;
7033 }
7034
7035 /*
7036 * User's IOCTL packet has 2 frames (maximum). Copy those two
7037 * frames into our cmd's frames. cmd->frame's context will get
7038 * overwritten when we copy from user's frames. So set that value
7039 * alone separately
7040 */
7041 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307042 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06007043 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307044 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
7045 MFI_FRAME_SGL64 |
7046 MFI_FRAME_SENSE64));
Shivasharan S8823abe2017-08-23 04:47:00 -07007047 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007048
Shivasharan S8823abe2017-08-23 04:47:00 -07007049 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Shivasharan S95c06082017-02-10 00:59:23 -08007050 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7051 megasas_return_cmd(instance, cmd);
7052 return -1;
7053 }
7054 }
7055
Shivasharan S8823abe2017-08-23 04:47:00 -07007056 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307057 error = megasas_set_crash_dump_params_ioctl(cmd);
7058 megasas_return_cmd(instance, cmd);
7059 return error;
7060 }
7061
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007062 /*
7063 * The management interface between applications and the fw uses
7064 * MFI frames. E.g, RAID configuration changes, LD property changes
7065 * etc are accomplishes through different kinds of MFI frames. The
7066 * driver needs to care only about substituting user buffers with
7067 * kernel buffers in SGLs. The location of SGL is embedded in the
7068 * struct iocpacket itself.
7069 */
7070 kern_sge32 = (struct megasas_sge32 *)
7071 ((unsigned long)cmd->frame + ioc->sgl_off);
7072
7073 /*
7074 * For each user buffer, create a mirror buffer and copy in
7075 */
7076 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01007077 if (!ioc->sgl[i].iov_len)
7078 continue;
7079
Sumant Patro9f35fa82007-02-14 12:55:45 -08007080 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007081 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08007082 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007083 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007084 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7085 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007086 error = -ENOMEM;
7087 goto out;
7088 }
7089
7090 /*
7091 * We don't change the dma_coherent_mask, so
7092 * pci_alloc_consistent only returns 32bit addresses
7093 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307094 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7095 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007096
7097 /*
7098 * We created a kernel buffer corresponding to the
7099 * user buffer. Now copy in from the user buffer
7100 */
7101 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7102 (u32) (ioc->sgl[i].iov_len))) {
7103 error = -EFAULT;
7104 goto out;
7105 }
7106 }
7107
7108 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007109 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7110 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007111 if (!sense) {
7112 error = -ENOMEM;
7113 goto out;
7114 }
7115
7116 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06007117 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307118 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007119 }
7120
7121 /*
7122 * Set the sync_cmd flag so that the ISR knows not to complete this
7123 * cmd to the SCSI mid-layer
7124 */
7125 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307126 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7127 cmd->sync_cmd = 0;
7128 dev_err(&instance->pdev->dev,
7129 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
Shivasharan S8823abe2017-08-23 04:47:00 -07007130 __func__, __LINE__, opcode, cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307131 return -EBUSY;
7132 }
7133
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007134 cmd->sync_cmd = 0;
7135
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307136 if (instance->unload == 1) {
7137 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7138 "don't submit data to application\n");
7139 goto out;
7140 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007141 /*
7142 * copy out the kernel buffers to user buffers
7143 */
7144 for (i = 0; i < ioc->sge_count; i++) {
7145 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7146 ioc->sgl[i].iov_len)) {
7147 error = -EFAULT;
7148 goto out;
7149 }
7150 }
7151
7152 /*
7153 * copy out the sense
7154 */
7155 if (ioc->sense_len) {
7156 /*
bo yangb70a41e2008-03-18 03:13:06 -04007157 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007158 * sense buffer address
7159 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007160 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7161 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007162
Shivasharan S318aaef2017-02-10 00:59:22 -08007163 if (copy_to_user((void __user *)((unsigned long)
7164 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04007165 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007166 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007167 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007168 error = -EFAULT;
7169 goto out;
7170 }
7171 }
7172
7173 /*
7174 * copy the status codes returned by the fw
7175 */
7176 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7177 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007178 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007179 error = -EFAULT;
7180 }
7181
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007182out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007183 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007184 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007185 sense, sense_handle);
7186 }
7187
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007188 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007189 if (kbuff_arr[i]) {
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007190 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307191 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007192 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307193 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307194 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007195 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007196 }
7197
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307198 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007199 return error;
7200}
7201
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007202static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7203{
7204 struct megasas_iocpacket __user *user_ioc =
7205 (struct megasas_iocpacket __user *)arg;
7206 struct megasas_iocpacket *ioc;
7207 struct megasas_instance *instance;
7208 int error;
bo yang39a98552010-09-22 22:36:29 -04007209 int i;
7210 unsigned long flags;
7211 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007212
Markus Elfring709ab232016-08-21 10:39:04 +02007213 ioc = memdup_user(user_ioc, sizeof(*ioc));
7214 if (IS_ERR(ioc))
7215 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007216
7217 instance = megasas_lookup_instance(ioc->host_no);
7218 if (!instance) {
7219 error = -ENODEV;
7220 goto out_kfree_ioc;
7221 }
7222
adam radford229fe472014-03-10 02:51:56 -07007223 /* Adjust ioctl wait time for VF mode */
7224 if (instance->requestorId)
7225 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7226
7227 /* Block ioctls in VF mode */
7228 if (instance->requestorId && !allow_vf_ioctls) {
7229 error = -ENODEV;
7230 goto out_kfree_ioc;
7231 }
7232
Sumit Saxena8a01a412016-01-28 21:04:32 +05307233 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007234 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007235 error = -ENODEV;
7236 goto out_kfree_ioc;
7237 }
7238
7239 if (instance->unload == 1) {
7240 error = -ENODEV;
7241 goto out_kfree_ioc;
7242 }
7243
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007244 if (down_interruptible(&instance->ioctl_sem)) {
7245 error = -ERESTARTSYS;
7246 goto out_kfree_ioc;
7247 }
bo yang39a98552010-09-22 22:36:29 -04007248
7249 for (i = 0; i < wait_time; i++) {
7250
7251 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307252 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007253 spin_unlock_irqrestore(&instance->hba_lock, flags);
7254 break;
7255 }
7256 spin_unlock_irqrestore(&instance->hba_lock, flags);
7257
7258 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007259 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04007260 "for controller reset to finish\n");
7261 }
7262
7263 msleep(1000);
7264 }
7265
7266 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307267 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007268 spin_unlock_irqrestore(&instance->hba_lock, flags);
7269
Colin Ian King644da3c2016-11-12 16:25:24 +00007270 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007271 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007272 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007273 }
7274 spin_unlock_irqrestore(&instance->hba_lock, flags);
7275
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007276 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007277out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007278 up(&instance->ioctl_sem);
7279
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007280out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007281 kfree(ioc);
7282 return error;
7283}
7284
7285static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7286{
7287 struct megasas_instance *instance;
7288 struct megasas_aen aen;
7289 int error;
bo yang39a98552010-09-22 22:36:29 -04007290 int i;
7291 unsigned long flags;
7292 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007293
7294 if (file->private_data != file) {
7295 printk(KERN_DEBUG "megasas: fasync_helper was not "
7296 "called first\n");
7297 return -EINVAL;
7298 }
7299
7300 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7301 return -EFAULT;
7302
7303 instance = megasas_lookup_instance(aen.host_no);
7304
7305 if (!instance)
7306 return -ENODEV;
7307
Sumit Saxena8a01a412016-01-28 21:04:32 +05307308 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007309 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007310 }
7311
7312 if (instance->unload == 1) {
7313 return -ENODEV;
7314 }
7315
bo yang39a98552010-09-22 22:36:29 -04007316 for (i = 0; i < wait_time; i++) {
7317
7318 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307319 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007320 spin_unlock_irqrestore(&instance->hba_lock,
7321 flags);
7322 break;
7323 }
7324
7325 spin_unlock_irqrestore(&instance->hba_lock, flags);
7326
7327 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007328 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04007329 "controller reset to finish\n");
7330 }
7331
7332 msleep(1000);
7333 }
7334
7335 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307336 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007337 spin_unlock_irqrestore(&instance->hba_lock, flags);
Colin Ian King644da3c2016-11-12 16:25:24 +00007338 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007339 return -ENODEV;
7340 }
7341 spin_unlock_irqrestore(&instance->hba_lock, flags);
7342
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307343 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007344 error = megasas_register_aen(instance, aen.seq_num,
7345 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307346 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007347 return error;
7348}
7349
7350/**
7351 * megasas_mgmt_ioctl - char node ioctl entry point
7352 */
7353static long
7354megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7355{
7356 switch (cmd) {
7357 case MEGASAS_IOC_FIRMWARE:
7358 return megasas_mgmt_ioctl_fw(file, arg);
7359
7360 case MEGASAS_IOC_GET_AEN:
7361 return megasas_mgmt_ioctl_aen(file, arg);
7362 }
7363
7364 return -ENOTTY;
7365}
7366
7367#ifdef CONFIG_COMPAT
7368static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7369{
7370 struct compat_megasas_iocpacket __user *cioc =
7371 (struct compat_megasas_iocpacket __user *)arg;
7372 struct megasas_iocpacket __user *ioc =
7373 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7374 int i;
7375 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007376 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307377 u32 local_sense_off;
7378 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307379 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007380
Jeff Garzik83aabc12006-10-04 06:34:03 -04007381 if (clear_user(ioc, sizeof(*ioc)))
7382 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007383
7384 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7385 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7386 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7387 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7388 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7389 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7390 return -EFAULT;
7391
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007392 /*
7393 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7394 * sense_len is not null, so prepare the 64bit value under
7395 * the same condition.
7396 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307397 if (get_user(local_sense_off, &ioc->sense_off) ||
7398 get_user(local_sense_len, &ioc->sense_len) ||
7399 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307400 return -EFAULT;
7401
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307402 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007403 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307404 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007405 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307406 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007407 if (get_user(ptr, sense_cioc_ptr) ||
7408 put_user(compat_ptr(ptr), sense_ioc_ptr))
7409 return -EFAULT;
7410 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007411
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007412 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007413 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7414 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7415 copy_in_user(&ioc->sgl[i].iov_len,
7416 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7417 return -EFAULT;
7418 }
7419
7420 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7421
7422 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7423 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7424 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7425 return -EFAULT;
7426 }
7427 return error;
7428}
7429
7430static long
7431megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7432 unsigned long arg)
7433{
7434 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007435 case MEGASAS_IOC_FIRMWARE32:
7436 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007437 case MEGASAS_IOC_GET_AEN:
7438 return megasas_mgmt_ioctl_aen(file, arg);
7439 }
7440
7441 return -ENOTTY;
7442}
7443#endif
7444
7445/*
7446 * File operations structure for management interface
7447 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007448static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007449 .owner = THIS_MODULE,
7450 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007451 .fasync = megasas_mgmt_fasync,
7452 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06007453 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007454#ifdef CONFIG_COMPAT
7455 .compat_ioctl = megasas_mgmt_compat_ioctl,
7456#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02007457 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007458};
7459
7460/*
7461 * PCI hotplug support registration structure
7462 */
7463static struct pci_driver megasas_pci_driver = {
7464
7465 .name = "megaraid_sas",
7466 .id_table = megasas_pci_table,
7467 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007468 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05007469 .suspend = megasas_suspend,
7470 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007471 .shutdown = megasas_shutdown,
7472};
7473
7474/*
7475 * Sysfs driver attributes
7476 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007477static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007478{
7479 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7480 MEGASAS_VERSION);
7481}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007482static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007483
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007484static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307485{
7486 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7487 MEGASAS_RELDATE);
7488}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007489static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307490
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007491static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06007492{
7493 return sprintf(buf, "%u\n", support_poll_for_event);
7494}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007495static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007496
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007497static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06007498{
7499 return sprintf(buf, "%u\n", support_device_change);
7500}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007501static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06007502
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007503static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07007504{
bo yangad84db22007-11-09 04:40:16 -05007505 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007506}
7507
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007508static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
7509 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07007510{
7511 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007512
7513 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007514 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7515 retval = -EINVAL;
7516 }
7517 return retval;
7518}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007519static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05007520
Shivasharan Sb4a42212017-02-10 00:59:16 -08007521static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
7522{
7523 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
7524 scsi_remove_device(sdev);
7525 scsi_device_put(sdev);
7526}
7527
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007528static void
7529megasas_aen_polling(struct work_struct *work)
7530{
7531 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007532 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007533 struct megasas_instance *instance = ev->instance;
7534 union megasas_evt_class_locale class_locale;
7535 struct Scsi_Host *host;
7536 struct scsi_device *sdev1;
7537 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007538 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007539 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007540 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007541 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307542 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007543
7544 if (!instance) {
7545 printk(KERN_ERR "invalid instance!\n");
7546 kfree(ev);
7547 return;
7548 }
adam radford229fe472014-03-10 02:51:56 -07007549
7550 /* Adjust event workqueue thread wait time for VF mode */
7551 if (instance->requestorId)
7552 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7553
7554 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307555 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007556
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007557 instance->ev = NULL;
7558 host = instance->host;
7559 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307560 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007561
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307562 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307563
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007564 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307565 case MR_EVT_PD_REMOVED:
7566 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307567 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307568 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007569 break;
7570
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307571 case MR_EVT_LD_OFFLINE:
7572 case MR_EVT_CFG_CLEARED:
7573 case MR_EVT_LD_DELETED:
7574 case MR_EVT_LD_CREATED:
7575 if (!instance->requestorId ||
7576 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7577 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007578
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307579 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307580 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007581
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307582 break;
7583
7584 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7585 case MR_EVT_FOREIGN_CFG_IMPORTED:
7586 case MR_EVT_LD_STATE_CHANGE:
7587 dcmd_ret = megasas_get_pd_list(instance);
7588
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307589 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307590 break;
7591
7592 if (!instance->requestorId ||
7593 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7594 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7595
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307596 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307597 break;
7598
7599 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7600 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7601 instance->host->host_no);
7602 break;
7603
7604 case MR_EVT_CTRL_PROP_CHANGED:
7605 dcmd_ret = megasas_get_ctrl_info(instance);
7606 break;
7607 default:
7608 doscan = 0;
7609 break;
7610 }
7611 } else {
7612 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7613 mutex_unlock(&instance->reset_mutex);
7614 kfree(ev);
7615 return;
7616 }
7617
7618 mutex_unlock(&instance->reset_mutex);
7619
7620 if (doscan & SCAN_PD_CHANNEL) {
7621 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7622 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7623 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007624 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307625 if (instance->pd_list[pd_index].driveState ==
7626 MR_PD_STATE_SYSTEM) {
7627 if (!sdev1)
7628 scsi_add_device(host, i, j, 0);
7629 else
Yang, Boc9786842009-12-06 08:39:25 -07007630 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007631 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007632 if (sdev1)
7633 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007634 }
Yang, Boc9786842009-12-06 08:39:25 -07007635 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007636 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007637 }
7638
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307639 if (doscan & SCAN_VD_CHANNEL) {
7640 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7641 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7642 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7643 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7644 if (instance->ld_ids[ld_index] != 0xff) {
7645 if (!sdev1)
7646 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7647 else
7648 scsi_device_put(sdev1);
7649 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007650 if (sdev1)
7651 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007652 }
7653 }
7654 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007655 }
7656
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307657 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307658 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7659 else
7660 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007661
7662 /* Register AEN with FW for latest sequence number plus 1 */
7663 class_locale.members.reserved = 0;
7664 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7665 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307666
7667 if (instance->aen_cmd != NULL) {
7668 kfree(ev);
7669 return;
7670 }
7671
7672 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007673 error = megasas_register_aen(instance, seq_num,
7674 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007675 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307676 dev_err(&instance->pdev->dev,
7677 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007678
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307679 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007680 kfree(ev);
7681}
7682
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007683/**
7684 * megasas_init - Driver load entry point
7685 */
7686static int __init megasas_init(void)
7687{
7688 int rval;
7689
7690 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007691 * Booted in kdump kernel, minimize memory footprints by
7692 * disabling few features
7693 */
7694 if (reset_devices) {
7695 msix_vectors = 1;
7696 rdpq_enable = 0;
7697 dual_qdepth_disable = 1;
7698 }
7699
7700 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007701 * Announce driver version and other information
7702 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307703 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007704
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007705 spin_lock_init(&poll_aen_lock);
7706
Yang, Bo72c4fd32009-10-06 14:20:59 -06007707 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007708 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007709
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007710 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7711
7712 /*
7713 * Register character device node
7714 */
7715 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7716
7717 if (rval < 0) {
7718 printk(KERN_DEBUG "megasas: failed to open device node\n");
7719 return rval;
7720 }
7721
7722 megasas_mgmt_majorno = rval;
7723
7724 /*
7725 * Register ourselves as PCI hotplug module
7726 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007727 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007728
7729 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007730 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007731 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007732 }
7733
Jeff Garzik83aabc12006-10-04 06:34:03 -04007734 rval = driver_create_file(&megasas_pci_driver.driver,
7735 &driver_attr_version);
7736 if (rval)
7737 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007738
7739 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307740 &driver_attr_release_date);
7741 if (rval)
7742 goto err_dcf_rel_date;
7743
7744 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007745 &driver_attr_support_poll_for_event);
7746 if (rval)
7747 goto err_dcf_support_poll_for_event;
7748
Jeff Garzik83aabc12006-10-04 06:34:03 -04007749 rval = driver_create_file(&megasas_pci_driver.driver,
7750 &driver_attr_dbg_lvl);
7751 if (rval)
7752 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007753 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007754 &driver_attr_support_device_change);
7755 if (rval)
7756 goto err_dcf_support_device_change;
7757
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007758 return rval;
bo yangad84db22007-11-09 04:40:16 -05007759
Yang, Bo837f5fe2010-10-11 06:59:20 -06007760err_dcf_support_device_change:
7761 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007762 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007763err_dcf_dbg_lvl:
7764 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007765 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007766err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307767 driver_remove_file(&megasas_pci_driver.driver,
7768 &driver_attr_release_date);
7769err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007770 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7771err_dcf_attr_ver:
7772 pci_unregister_driver(&megasas_pci_driver);
7773err_pcidrv:
7774 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007775 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007776}
7777
7778/**
7779 * megasas_exit - Driver unload entry point
7780 */
7781static void __exit megasas_exit(void)
7782{
Sumant Patro658dced2006-10-03 13:09:14 -07007783 driver_remove_file(&megasas_pci_driver.driver,
7784 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007785 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007786 &driver_attr_support_poll_for_event);
7787 driver_remove_file(&megasas_pci_driver.driver,
7788 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307789 driver_remove_file(&megasas_pci_driver.driver,
7790 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007791 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007792
7793 pci_unregister_driver(&megasas_pci_driver);
7794 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7795}
7796
7797module_init(megasas_init);
7798module_exit(megasas_exit);