blob: e0b4ca1c57e19ab393d48c690365caf18165a53b [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
adam radford3f1530c2010-12-14 18:51:48 -08002 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006 *
adam radford3f1530c2010-12-14 18:51:48 -08007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040011 *
adam radford3f1530c2010-12-14 18:51:48 -080012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040016 *
adam radford3f1530c2010-12-14 18:51:48 -080017 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040019 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053020 * Authors: Avago Technologies
adam radford3f1530c2010-12-14 18:51:48 -080021 * Sreenivas Bagalkote
22 * Sumant Patro
23 * Bo Yang
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford3f1530c2010-12-14 18:51:48 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040038#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
43#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040045#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010046#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040047#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080048#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010049#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060050#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040051
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070056#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080057#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040058#include "megaraid_sas.h"
59
bo yangad84db22007-11-09 04:40:16 -050060/*
Yang, Bo1fd10682010-10-12 07:18:50 -060061 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
63 */
64static unsigned int max_sectors;
65module_param_named(max_sectors, max_sectors, int, 0);
66MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
68
adam radford80d9da92010-12-21 10:17:40 -080069static int msix_disable;
70module_param(msix_disable, int, S_IRUGO);
71MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
72
adam radford079eadd2012-10-01 19:26:59 -070073static unsigned int msix_vectors;
74module_param(msix_vectors, int, S_IRUGO);
75MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
76
adam radford229fe472014-03-10 02:51:56 -070077static int allow_vf_ioctls;
78module_param(allow_vf_ioctls, int, S_IRUGO);
79MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
80
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053081static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070082module_param(throttlequeuedepth, int, S_IRUGO);
83MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
85
Sumit Saxenae3d178c2016-01-28 21:04:34 +053086unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070087module_param(resetwaittime, int, S_IRUGO);
88MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
90
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053091int smp_affinity_enable = 1;
92module_param(smp_affinity_enable, int, S_IRUGO);
93MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
94
Sumit Saxena179ac142016-01-28 21:04:28 +053095int rdpq_enable = 1;
96module_param(rdpq_enable, int, S_IRUGO);
97MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
98
Sumit Saxena308ec452016-01-28 21:04:30 +053099unsigned int dual_qdepth_disable;
100module_param(dual_qdepth_disable, int, S_IRUGO);
101MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
102
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530103unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
104module_param(scmd_timeout, int, S_IRUGO);
105MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
106
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400107MODULE_LICENSE("GPL");
108MODULE_VERSION(MEGASAS_VERSION);
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +0530109MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
110MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400111
adam radford058a8fa2011-10-08 18:14:27 -0700112int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400113static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700114static int megasas_ld_list_query(struct megasas_instance *instance,
115 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400116static int megasas_issue_init_mfi(struct megasas_instance *instance);
117static int megasas_register_aen(struct megasas_instance *instance,
118 u32 seq_num, u32 class_locale_word);
Sumit Saxena2216c302016-01-28 21:04:26 +0530119static int
120megasas_get_pd_info(struct megasas_instance *instance, u16 device_id);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400121/*
122 * PCI ID table for all supported controllers
123 */
124static struct pci_device_id megasas_pci_table[] = {
125
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
127 /* xscale IOP */
128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
129 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
131 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
133 /* gen2*/
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
135 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
137 /* skinny*/
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
139 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
141 /* xscale IOP, vega */
142 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
143 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
145 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
147 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
149 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
151 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
153 /* Intruder */
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
155 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
157 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200158 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400159};
160
161MODULE_DEVICE_TABLE(pci, megasas_pci_table);
162
163static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700164struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400165static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100166static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400167
Yang, Boc3518832009-10-06 14:18:02 -0600168static int megasas_poll_wait_aen;
169static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600170static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800171u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600172static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700173
Yang, Boc3518832009-10-06 14:18:02 -0600174/* define lock for aen poll */
175spinlock_t poll_aen_lock;
176
adam radford9c915a82010-12-21 13:34:31 -0800177void
bo yang7343eb62007-11-09 04:35:44 -0500178megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
179 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800180static u32
181megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
182static int
183megasas_adp_reset_gen2(struct megasas_instance *instance,
184 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800185static irqreturn_t megasas_isr(int irq, void *devp);
186static u32
187megasas_init_adapter_mfi(struct megasas_instance *instance);
188u32
189megasas_build_and_issue_cmd(struct megasas_instance *instance,
190 struct scsi_cmnd *scmd);
191static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800192void
193megasas_release_fusion(struct megasas_instance *instance);
194int
195megasas_ioc_init_fusion(struct megasas_instance *instance);
196void
197megasas_free_cmds_fusion(struct megasas_instance *instance);
198u8
199megasas_get_map_info(struct megasas_instance *instance);
200int
201megasas_sync_map_info(struct megasas_instance *instance);
202int
adam radford229fe472014-03-10 02:51:56 -0700203wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
204 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800205void megasas_reset_reply_desc(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -0800206void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700207static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
208 int initial);
209int megasas_check_mpio_paths(struct megasas_instance *instance,
210 struct scsi_cmnd *scmd);
adam radfordcd50ba82010-12-21 10:23:23 -0800211
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530212int
adam radfordcd50ba82010-12-21 10:23:23 -0800213megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
214{
215 instance->instancet->fire_cmd(instance,
216 cmd->frame_phys_addr, 0, instance->reg_set);
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530217 return 0;
adam radfordcd50ba82010-12-21 10:23:23 -0800218}
bo yang7343eb62007-11-09 04:35:44 -0500219
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400220/**
221 * megasas_get_cmd - Get a command from the free pool
222 * @instance: Adapter soft state
223 *
224 * Returns a free command from the pool
225 */
adam radford9c915a82010-12-21 13:34:31 -0800226struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400227 *instance)
228{
229 unsigned long flags;
230 struct megasas_cmd *cmd = NULL;
231
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530232 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400233
234 if (!list_empty(&instance->cmd_pool)) {
235 cmd = list_entry((&instance->cmd_pool)->next,
236 struct megasas_cmd, list);
237 list_del_init(&cmd->list);
238 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500239 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400240 }
241
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530242 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400243 return cmd;
244}
245
246/**
247 * megasas_return_cmd - Return a cmd to free command pool
248 * @instance: Adapter soft state
249 * @cmd: Command packet to be returned to free command pool
250 */
adam radford9c915a82010-12-21 13:34:31 -0800251inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400252megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
253{
254 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530255 u32 blk_tags;
256 struct megasas_cmd_fusion *cmd_fusion;
257 struct fusion_context *fusion = instance->ctrl_context;
258
259 /* This flag is used only for fusion adapter.
260 * Wait for Interrupt for Polled mode DCMD
261 */
262 if (cmd->flags & DRV_DCMD_POLLED_MODE)
263 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400264
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530265 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400266
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530267 if (fusion) {
268 blk_tags = instance->max_scsi_cmds + cmd->index;
269 cmd_fusion = fusion->cmd_list[blk_tags];
270 megasas_return_cmd_fusion(instance, cmd_fusion);
271 }
272 cmd->scmd = NULL;
273 cmd->frame_count = 0;
274 cmd->flags = 0;
275 if (!fusion && reset_devices)
276 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
277 list_add(&cmd->list, (&instance->cmd_pool)->next);
278
279 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
280
281}
Sumant Patro1341c932006-01-25 12:02:40 -0800282
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530283static const char *
284format_timestamp(uint32_t timestamp)
285{
286 static char buffer[32];
287
288 if ((timestamp & 0xff000000) == 0xff000000)
289 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
290 0x00ffffff);
291 else
292 snprintf(buffer, sizeof(buffer), "%us", timestamp);
293 return buffer;
294}
295
296static const char *
297format_class(int8_t class)
298{
299 static char buffer[6];
300
301 switch (class) {
302 case MFI_EVT_CLASS_DEBUG:
303 return "debug";
304 case MFI_EVT_CLASS_PROGRESS:
305 return "progress";
306 case MFI_EVT_CLASS_INFO:
307 return "info";
308 case MFI_EVT_CLASS_WARNING:
309 return "WARN";
310 case MFI_EVT_CLASS_CRITICAL:
311 return "CRIT";
312 case MFI_EVT_CLASS_FATAL:
313 return "FATAL";
314 case MFI_EVT_CLASS_DEAD:
315 return "DEAD";
316 default:
317 snprintf(buffer, sizeof(buffer), "%d", class);
318 return buffer;
319 }
320}
321
322/**
323 * megasas_decode_evt: Decode FW AEN event and print critical event
324 * for information.
325 * @instance: Adapter soft state
326 */
327static void
328megasas_decode_evt(struct megasas_instance *instance)
329{
330 struct megasas_evt_detail *evt_detail = instance->evt_detail;
331 union megasas_evt_class_locale class_locale;
332 class_locale.word = le32_to_cpu(evt_detail->cl.word);
333
334 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
335 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
336 le32_to_cpu(evt_detail->seq_num),
337 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
338 (class_locale.members.locale),
339 format_class(class_locale.members.class),
340 evt_detail->description);
341}
342
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400343/**
adam radford0d490162010-12-14 19:17:17 -0800344* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800345* (deviceid : 1064R, PERC5) controllers
346*/
347
348/**
349 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400350 * @regs: MFI register set
351 */
352static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530353megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400354{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530355 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500356
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530357 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400358 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400359
360 /* Dummy readl to force pci flush */
361 readl(&regs->outbound_intr_mask);
362}
363
364/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700365 * megasas_disable_intr_xscale -Disables interrupt
366 * @regs: MFI register set
367 */
368static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530369megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700370{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530371 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700372 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500373
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530374 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700375 writel(mask, &regs->outbound_intr_mask);
376 /* Dummy readl to force pci flush */
377 readl(&regs->outbound_intr_mask);
378}
379
380/**
Sumant Patro1341c932006-01-25 12:02:40 -0800381 * megasas_read_fw_status_reg_xscale - returns the current FW status value
382 * @regs: MFI register set
383 */
384static u32
385megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
386{
387 return readl(&(regs)->outbound_msg_0);
388}
389/**
390 * megasas_clear_interrupt_xscale - Check & clear interrupt
391 * @regs: MFI register set
392 */
adam radford0d490162010-12-14 19:17:17 -0800393static int
Sumant Patro1341c932006-01-25 12:02:40 -0800394megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
395{
396 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400397 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500398
Sumant Patro1341c932006-01-25 12:02:40 -0800399 /*
400 * Check if it is our interrupt
401 */
402 status = readl(&regs->outbound_intr_status);
403
bo yang39a98552010-09-22 22:36:29 -0400404 if (status & MFI_OB_INTR_STATUS_MASK)
405 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
406 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
407 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800408
409 /*
410 * Clear the interrupt by writing back the same value
411 */
bo yang39a98552010-09-22 22:36:29 -0400412 if (mfiStatus)
413 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800414
Yang, Bo06f579d2008-08-10 12:42:37 -0700415 /* Dummy readl to force pci flush */
416 readl(&regs->outbound_intr_status);
417
bo yang39a98552010-09-22 22:36:29 -0400418 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800419}
420
421/**
422 * megasas_fire_cmd_xscale - Sends command to the FW
423 * @frame_phys_addr : Physical address of cmd
424 * @frame_count : Number of frames for the command
425 * @regs : MFI register set
426 */
adam radford0d490162010-12-14 19:17:17 -0800427static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600428megasas_fire_cmd_xscale(struct megasas_instance *instance,
429 dma_addr_t frame_phys_addr,
430 u32 frame_count,
431 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800432{
bo yang39a98552010-09-22 22:36:29 -0400433 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500434
bo yang39a98552010-09-22 22:36:29 -0400435 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800436 writel((frame_phys_addr >> 3)|(frame_count),
437 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400438 spin_unlock_irqrestore(&instance->hba_lock, flags);
439}
440
441/**
442 * megasas_adp_reset_xscale - For controller reset
443 * @regs: MFI register set
444 */
445static int
446megasas_adp_reset_xscale(struct megasas_instance *instance,
447 struct megasas_register_set __iomem *regs)
448{
449 u32 i;
450 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500451
bo yang39a98552010-09-22 22:36:29 -0400452 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
453
454 for (i = 0; i < 3; i++)
455 msleep(1000); /* sleep for 3 secs */
456 pcidata = 0;
457 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500458 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400459 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500460 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400461 pcidata &= ~0x2;
462 pci_write_config_dword(instance->pdev,
463 MFI_1068_PCSR_OFFSET, pcidata);
464
465 for (i = 0; i < 2; i++)
466 msleep(1000); /* need to wait 2 secs again */
467
468 pcidata = 0;
469 pci_read_config_dword(instance->pdev,
470 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500471 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400472 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500473 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400474 pcidata = 0;
475 pci_write_config_dword(instance->pdev,
476 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
477 }
478 }
479 return 0;
480}
481
482/**
483 * megasas_check_reset_xscale - For controller reset check
484 * @regs: MFI register set
485 */
486static int
487megasas_check_reset_xscale(struct megasas_instance *instance,
488 struct megasas_register_set __iomem *regs)
489{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530490 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530491 (le32_to_cpu(*instance->consumer) ==
492 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400493 return 1;
bo yang39a98552010-09-22 22:36:29 -0400494 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800495}
496
497static struct megasas_instance_template megasas_instance_template_xscale = {
498
499 .fire_cmd = megasas_fire_cmd_xscale,
500 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700501 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800502 .clear_intr = megasas_clear_intr_xscale,
503 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400504 .adp_reset = megasas_adp_reset_xscale,
505 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800506 .service_isr = megasas_isr,
507 .tasklet = megasas_complete_cmd_dpc,
508 .init_adapter = megasas_init_adapter_mfi,
509 .build_and_issue_cmd = megasas_build_and_issue_cmd,
510 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800511};
512
513/**
adam radford0d490162010-12-14 19:17:17 -0800514* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800515* to xscale (deviceid : 1064R, PERC5) controllers
516*/
517
518/**
adam radford0d490162010-12-14 19:17:17 -0800519* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500520* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800521*/
522
523/**
524 * megasas_enable_intr_ppc - Enables interrupts
525 * @regs: MFI register set
526 */
527static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530528megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800529{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530530 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500531
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530532 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800533 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800534
bo yang39a98552010-09-22 22:36:29 -0400535 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800536
537 /* Dummy readl to force pci flush */
538 readl(&regs->outbound_intr_mask);
539}
540
541/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700542 * megasas_disable_intr_ppc - Disable interrupt
543 * @regs: MFI register set
544 */
545static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530546megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700547{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530548 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700549 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500550
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530551 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700552 writel(mask, &regs->outbound_intr_mask);
553 /* Dummy readl to force pci flush */
554 readl(&regs->outbound_intr_mask);
555}
556
557/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800558 * megasas_read_fw_status_reg_ppc - returns the current FW status value
559 * @regs: MFI register set
560 */
561static u32
562megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
563{
564 return readl(&(regs)->outbound_scratch_pad);
565}
566
567/**
568 * megasas_clear_interrupt_ppc - Check & clear interrupt
569 * @regs: MFI register set
570 */
adam radford0d490162010-12-14 19:17:17 -0800571static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800572megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
573{
adam radford3cc6851f92011-05-11 18:34:52 -0700574 u32 status, mfiStatus = 0;
575
Sumant Patrof9876f02006-02-03 15:34:35 -0800576 /*
577 * Check if it is our interrupt
578 */
579 status = readl(&regs->outbound_intr_status);
580
adam radford3cc6851f92011-05-11 18:34:52 -0700581 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
582 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
583
584 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
585 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800586
587 /*
588 * Clear the interrupt by writing back the same value
589 */
590 writel(status, &regs->outbound_doorbell_clear);
591
Yang, Bo06f579d2008-08-10 12:42:37 -0700592 /* Dummy readl to force pci flush */
593 readl(&regs->outbound_doorbell_clear);
594
adam radford3cc6851f92011-05-11 18:34:52 -0700595 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800596}
adam radford3cc6851f92011-05-11 18:34:52 -0700597
Sumant Patrof9876f02006-02-03 15:34:35 -0800598/**
599 * megasas_fire_cmd_ppc - Sends command to the FW
600 * @frame_phys_addr : Physical address of cmd
601 * @frame_count : Number of frames for the command
602 * @regs : MFI register set
603 */
adam radford0d490162010-12-14 19:17:17 -0800604static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600605megasas_fire_cmd_ppc(struct megasas_instance *instance,
606 dma_addr_t frame_phys_addr,
607 u32 frame_count,
608 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800609{
bo yang39a98552010-09-22 22:36:29 -0400610 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500611
bo yang39a98552010-09-22 22:36:29 -0400612 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800613 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800614 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400615 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800616}
617
bo yang39a98552010-09-22 22:36:29 -0400618/**
bo yang39a98552010-09-22 22:36:29 -0400619 * megasas_check_reset_ppc - For controller reset check
620 * @regs: MFI register set
621 */
622static int
623megasas_check_reset_ppc(struct megasas_instance *instance,
624 struct megasas_register_set __iomem *regs)
625{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530626 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700627 return 1;
628
bo yang39a98552010-09-22 22:36:29 -0400629 return 0;
630}
adam radford3cc6851f92011-05-11 18:34:52 -0700631
Sumant Patrof9876f02006-02-03 15:34:35 -0800632static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800633
Sumant Patrof9876f02006-02-03 15:34:35 -0800634 .fire_cmd = megasas_fire_cmd_ppc,
635 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700636 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800637 .clear_intr = megasas_clear_intr_ppc,
638 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700639 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400640 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800641 .service_isr = megasas_isr,
642 .tasklet = megasas_complete_cmd_dpc,
643 .init_adapter = megasas_init_adapter_mfi,
644 .build_and_issue_cmd = megasas_build_and_issue_cmd,
645 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800646};
647
648/**
Yang, Bo87911122009-10-06 14:31:54 -0600649 * megasas_enable_intr_skinny - Enables interrupts
650 * @regs: MFI register set
651 */
652static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530653megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600654{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530655 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500656
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530657 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600658 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
659
660 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
661
662 /* Dummy readl to force pci flush */
663 readl(&regs->outbound_intr_mask);
664}
665
666/**
667 * megasas_disable_intr_skinny - Disables interrupt
668 * @regs: MFI register set
669 */
670static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530671megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600672{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530673 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600674 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500675
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530676 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600677 writel(mask, &regs->outbound_intr_mask);
678 /* Dummy readl to force pci flush */
679 readl(&regs->outbound_intr_mask);
680}
681
682/**
683 * megasas_read_fw_status_reg_skinny - returns the current FW status value
684 * @regs: MFI register set
685 */
686static u32
687megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
688{
689 return readl(&(regs)->outbound_scratch_pad);
690}
691
692/**
693 * megasas_clear_interrupt_skinny - Check & clear interrupt
694 * @regs: MFI register set
695 */
696static int
697megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
698{
699 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800700 u32 mfiStatus = 0;
701
Yang, Bo87911122009-10-06 14:31:54 -0600702 /*
703 * Check if it is our interrupt
704 */
705 status = readl(&regs->outbound_intr_status);
706
707 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400708 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600709 }
710
711 /*
adam radfordebf054b2011-02-24 20:57:15 -0800712 * Check if it is our interrupt
713 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600714 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800715 MFI_STATE_FAULT) {
716 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
717 } else
718 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
719
720 /*
Yang, Bo87911122009-10-06 14:31:54 -0600721 * Clear the interrupt by writing back the same value
722 */
723 writel(status, &regs->outbound_intr_status);
724
725 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500726 * dummy read to flush PCI
727 */
Yang, Bo87911122009-10-06 14:31:54 -0600728 readl(&regs->outbound_intr_status);
729
adam radfordebf054b2011-02-24 20:57:15 -0800730 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600731}
732
733/**
734 * megasas_fire_cmd_skinny - Sends command to the FW
735 * @frame_phys_addr : Physical address of cmd
736 * @frame_count : Number of frames for the command
737 * @regs : MFI register set
738 */
739static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600740megasas_fire_cmd_skinny(struct megasas_instance *instance,
741 dma_addr_t frame_phys_addr,
742 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600743 struct megasas_register_set __iomem *regs)
744{
Yang, Bo0c79e682009-10-06 14:47:35 -0600745 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500746
bo yang39a98552010-09-22 22:36:29 -0400747 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530748 writel(upper_32_bits(frame_phys_addr),
749 &(regs)->inbound_high_queue_port);
750 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
751 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400752 spin_unlock_irqrestore(&instance->hba_lock, flags);
753}
754
755/**
bo yang39a98552010-09-22 22:36:29 -0400756 * megasas_check_reset_skinny - For controller reset check
757 * @regs: MFI register set
758 */
759static int
760megasas_check_reset_skinny(struct megasas_instance *instance,
761 struct megasas_register_set __iomem *regs)
762{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530763 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700764 return 1;
765
bo yang39a98552010-09-22 22:36:29 -0400766 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600767}
768
769static struct megasas_instance_template megasas_instance_template_skinny = {
770
771 .fire_cmd = megasas_fire_cmd_skinny,
772 .enable_intr = megasas_enable_intr_skinny,
773 .disable_intr = megasas_disable_intr_skinny,
774 .clear_intr = megasas_clear_intr_skinny,
775 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800776 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400777 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800778 .service_isr = megasas_isr,
779 .tasklet = megasas_complete_cmd_dpc,
780 .init_adapter = megasas_init_adapter_mfi,
781 .build_and_issue_cmd = megasas_build_and_issue_cmd,
782 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600783};
784
785
786/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700787* The following functions are defined for gen2 (deviceid : 0x78 0x79)
788* controllers
789*/
790
791/**
792 * megasas_enable_intr_gen2 - Enables interrupts
793 * @regs: MFI register set
794 */
795static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530796megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700797{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530798 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500799
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530800 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700801 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
802
803 /* write ~0x00000005 (4 & 1) to the intr mask*/
804 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
805
806 /* Dummy readl to force pci flush */
807 readl(&regs->outbound_intr_mask);
808}
809
810/**
811 * megasas_disable_intr_gen2 - Disables interrupt
812 * @regs: MFI register set
813 */
814static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530815megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700816{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530817 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700818 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500819
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530820 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700821 writel(mask, &regs->outbound_intr_mask);
822 /* Dummy readl to force pci flush */
823 readl(&regs->outbound_intr_mask);
824}
825
826/**
827 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
828 * @regs: MFI register set
829 */
830static u32
831megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
832{
833 return readl(&(regs)->outbound_scratch_pad);
834}
835
836/**
837 * megasas_clear_interrupt_gen2 - Check & clear interrupt
838 * @regs: MFI register set
839 */
840static int
841megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
842{
843 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400844 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500845
Yang, Bo6610a6b2008-08-10 12:42:38 -0700846 /*
847 * Check if it is our interrupt
848 */
849 status = readl(&regs->outbound_intr_status);
850
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530851 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400852 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
853 }
854 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
855 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
856 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700857
858 /*
859 * Clear the interrupt by writing back the same value
860 */
bo yang39a98552010-09-22 22:36:29 -0400861 if (mfiStatus)
862 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700863
864 /* Dummy readl to force pci flush */
865 readl(&regs->outbound_intr_status);
866
bo yang39a98552010-09-22 22:36:29 -0400867 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700868}
869/**
870 * megasas_fire_cmd_gen2 - Sends command to the FW
871 * @frame_phys_addr : Physical address of cmd
872 * @frame_count : Number of frames for the command
873 * @regs : MFI register set
874 */
875static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600876megasas_fire_cmd_gen2(struct megasas_instance *instance,
877 dma_addr_t frame_phys_addr,
878 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700879 struct megasas_register_set __iomem *regs)
880{
bo yang39a98552010-09-22 22:36:29 -0400881 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500882
bo yang39a98552010-09-22 22:36:29 -0400883 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700884 writel((frame_phys_addr | (frame_count<<1))|1,
885 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400886 spin_unlock_irqrestore(&instance->hba_lock, flags);
887}
888
889/**
890 * megasas_adp_reset_gen2 - For controller reset
891 * @regs: MFI register set
892 */
893static int
894megasas_adp_reset_gen2(struct megasas_instance *instance,
895 struct megasas_register_set __iomem *reg_set)
896{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500897 u32 retry = 0 ;
898 u32 HostDiag;
899 u32 __iomem *seq_offset = &reg_set->seq_offset;
900 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400901
adam radfordebf054b2011-02-24 20:57:15 -0800902 if (instance->instancet == &megasas_instance_template_skinny) {
903 seq_offset = &reg_set->fusion_seq_offset;
904 hostdiag_offset = &reg_set->fusion_host_diag;
905 }
906
907 writel(0, seq_offset);
908 writel(4, seq_offset);
909 writel(0xb, seq_offset);
910 writel(2, seq_offset);
911 writel(7, seq_offset);
912 writel(0xd, seq_offset);
913
bo yang39a98552010-09-22 22:36:29 -0400914 msleep(1000);
915
adam radfordebf054b2011-02-24 20:57:15 -0800916 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400917
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500918 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400919 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800920 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500921 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400922 retry, HostDiag);
923
924 if (retry++ >= 100)
925 return 1;
926
927 }
928
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500929 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400930
adam radfordebf054b2011-02-24 20:57:15 -0800931 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400932
933 ssleep(10);
934
adam radfordebf054b2011-02-24 20:57:15 -0800935 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500936 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400937 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800938 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500939 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400940 retry, HostDiag);
941
942 if (retry++ >= 1000)
943 return 1;
944
945 }
946 return 0;
947}
948
949/**
950 * megasas_check_reset_gen2 - For controller reset check
951 * @regs: MFI register set
952 */
953static int
954megasas_check_reset_gen2(struct megasas_instance *instance,
955 struct megasas_register_set __iomem *regs)
956{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530957 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600958 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600959
bo yang39a98552010-09-22 22:36:29 -0400960 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700961}
962
963static struct megasas_instance_template megasas_instance_template_gen2 = {
964
965 .fire_cmd = megasas_fire_cmd_gen2,
966 .enable_intr = megasas_enable_intr_gen2,
967 .disable_intr = megasas_disable_intr_gen2,
968 .clear_intr = megasas_clear_intr_gen2,
969 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400970 .adp_reset = megasas_adp_reset_gen2,
971 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800972 .service_isr = megasas_isr,
973 .tasklet = megasas_complete_cmd_dpc,
974 .init_adapter = megasas_init_adapter_mfi,
975 .build_and_issue_cmd = megasas_build_and_issue_cmd,
976 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700977};
978
979/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800980* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400981* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800982*/
983
adam radford9c915a82010-12-21 13:34:31 -0800984/*
985 * Template added for TB (Fusion)
986 */
987extern struct megasas_instance_template megasas_instance_template_fusion;
988
Sumant Patrof9876f02006-02-03 15:34:35 -0800989/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400990 * megasas_issue_polled - Issues a polling command
991 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800992 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400993 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530994 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400995 */
adam radford9c915a82010-12-21 13:34:31 -0800996int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400997megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
998{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400999 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1000
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301001 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301002 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001003
Sumit Saxena8a01a412016-01-28 21:04:32 +05301004 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301005 (instance->instancet->issue_dcmd(instance, cmd))) {
1006 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
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301011 return wait_and_poll(instance, cmd, instance->requestorId ?
1012 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001013}
1014
1015/**
1016 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1017 * @instance: Adapter soft state
1018 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301019 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001020 *
1021 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001022 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001023 * Used to issue ioctl commands.
1024 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301025int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001026megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301027 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301029 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301030 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001031
Sumit Saxena8a01a412016-01-28 21:04:32 +05301032 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301033 (instance->instancet->issue_dcmd(instance, cmd))) {
1034 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1035 __func__, __LINE__);
1036 return DCMD_NOT_FIRED;
1037 }
1038
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301039 if (timeout) {
1040 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301041 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301042 if (!ret) {
1043 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1044 __func__, __LINE__);
1045 return DCMD_TIMEOUT;
1046 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301047 } else
1048 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301049 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001050
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301051 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301052 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001053}
1054
1055/**
1056 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1057 * @instance: Adapter soft state
1058 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301059 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001060 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301061 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001062 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001063 * cmd and waits for return status.
1064 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001065 */
1066static int
1067megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301068 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001069{
1070 struct megasas_cmd *cmd;
1071 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301072 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001073
1074 cmd = megasas_get_cmd(instance);
1075
1076 if (!cmd)
1077 return -1;
1078
1079 abort_fr = &cmd->frame->abort;
1080
1081 /*
1082 * Prepare and issue the abort frame
1083 */
1084 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301085 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301086 abort_fr->flags = cpu_to_le16(0);
1087 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1088 abort_fr->abort_mfi_phys_addr_lo =
1089 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1090 abort_fr->abort_mfi_phys_addr_hi =
1091 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001092
1093 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301094 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001095
Sumit Saxena8a01a412016-01-28 21:04:32 +05301096 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301097 (instance->instancet->issue_dcmd(instance, cmd))) {
1098 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1099 __func__, __LINE__);
1100 return DCMD_NOT_FIRED;
1101 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001102
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301103 if (timeout) {
1104 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301105 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301106 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301107 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1108 __func__, __LINE__);
1109 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301110 }
1111 } else
1112 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301113 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301114
bo yang39a98552010-09-22 22:36:29 -04001115 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001116
1117 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301118 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1119 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001120}
1121
1122/**
1123 * megasas_make_sgl32 - Prepares 32-bit SGL
1124 * @instance: Adapter soft state
1125 * @scp: SCSI command from the mid-layer
1126 * @mfi_sgl: SGL to be filled in
1127 *
1128 * If successful, this function returns the number of SG elements. Otherwise,
1129 * it returnes -1.
1130 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001131static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001132megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1133 union megasas_sgl *mfi_sgl)
1134{
1135 int i;
1136 int sge_count;
1137 struct scatterlist *os_sgl;
1138
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001139 sge_count = scsi_dma_map(scp);
1140 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001141
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001142 if (sge_count) {
1143 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301144 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1145 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001146 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001147 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001148 return sge_count;
1149}
1150
1151/**
1152 * megasas_make_sgl64 - Prepares 64-bit SGL
1153 * @instance: Adapter soft state
1154 * @scp: SCSI command from the mid-layer
1155 * @mfi_sgl: SGL to be filled in
1156 *
1157 * If successful, this function returns the number of SG elements. Otherwise,
1158 * it returnes -1.
1159 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001160static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001161megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1162 union megasas_sgl *mfi_sgl)
1163{
1164 int i;
1165 int sge_count;
1166 struct scatterlist *os_sgl;
1167
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001168 sge_count = scsi_dma_map(scp);
1169 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001170
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001171 if (sge_count) {
1172 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301173 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1174 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001175 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001176 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001177 return sge_count;
1178}
1179
Yang, Bof4c9a132009-10-06 14:43:28 -06001180/**
1181 * megasas_make_sgl_skinny - Prepares IEEE SGL
1182 * @instance: Adapter soft state
1183 * @scp: SCSI command from the mid-layer
1184 * @mfi_sgl: SGL to be filled in
1185 *
1186 * If successful, this function returns the number of SG elements. Otherwise,
1187 * it returnes -1.
1188 */
1189static int
1190megasas_make_sgl_skinny(struct megasas_instance *instance,
1191 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1192{
1193 int i;
1194 int sge_count;
1195 struct scatterlist *os_sgl;
1196
1197 sge_count = scsi_dma_map(scp);
1198
1199 if (sge_count) {
1200 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301201 mfi_sgl->sge_skinny[i].length =
1202 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001203 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301204 cpu_to_le64(sg_dma_address(os_sgl));
1205 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001206 }
1207 }
1208 return sge_count;
1209}
1210
Sumant Patrob1df99d2006-10-03 12:40:47 -07001211 /**
1212 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001213 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001214 * @sge_count : number of sg elements
1215 *
1216 * Returns the number of frames required for numnber of sge's (sge_count)
1217 */
1218
Yang, Bof4c9a132009-10-06 14:43:28 -06001219static u32 megasas_get_frame_count(struct megasas_instance *instance,
1220 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001221{
1222 int num_cnt;
1223 int sge_bytes;
1224 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001225 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001226
1227 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1228 sizeof(struct megasas_sge32);
1229
Yang, Bof4c9a132009-10-06 14:43:28 -06001230 if (instance->flag_ieee) {
1231 sge_sz = sizeof(struct megasas_sge_skinny);
1232 }
1233
Sumant Patrob1df99d2006-10-03 12:40:47 -07001234 /*
bo yangd532dbe2008-03-17 03:36:43 -04001235 * Main frame can contain 2 SGEs for 64-bit SGLs and
1236 * 3 SGEs for 32-bit SGLs for ldio &
1237 * 1 SGEs for 64-bit SGLs and
1238 * 2 SGEs for 32-bit SGLs for pthru frame
1239 */
1240 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001241 if (instance->flag_ieee == 1) {
1242 num_cnt = sge_count - 1;
1243 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001244 num_cnt = sge_count - 1;
1245 else
1246 num_cnt = sge_count - 2;
1247 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001248 if (instance->flag_ieee == 1) {
1249 num_cnt = sge_count - 1;
1250 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001251 num_cnt = sge_count - 2;
1252 else
1253 num_cnt = sge_count - 3;
1254 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001255
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001256 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001257 sge_bytes = sge_sz * num_cnt;
1258
1259 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1260 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1261 }
1262 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001263 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001264
1265 if (frame_count > 7)
1266 frame_count = 8;
1267 return frame_count;
1268}
1269
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001270/**
1271 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1272 * @instance: Adapter soft state
1273 * @scp: SCSI command
1274 * @cmd: Command to be prepared in
1275 *
1276 * This function prepares CDB commands. These are typcially pass-through
1277 * commands to the devices.
1278 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001279static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001280megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1281 struct megasas_cmd *cmd)
1282{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001283 u32 is_logical;
1284 u32 device_id;
1285 u16 flags = 0;
1286 struct megasas_pthru_frame *pthru;
1287
1288 is_logical = MEGASAS_IS_LOGICAL(scp);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301289 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001290 pthru = (struct megasas_pthru_frame *)cmd->frame;
1291
1292 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1293 flags = MFI_FRAME_DIR_WRITE;
1294 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1295 flags = MFI_FRAME_DIR_READ;
1296 else if (scp->sc_data_direction == PCI_DMA_NONE)
1297 flags = MFI_FRAME_DIR_NONE;
1298
Yang, Bof4c9a132009-10-06 14:43:28 -06001299 if (instance->flag_ieee == 1) {
1300 flags |= MFI_FRAME_IEEE;
1301 }
1302
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001303 /*
1304 * Prepare the DCDB frame
1305 */
1306 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1307 pthru->cmd_status = 0x0;
1308 pthru->scsi_status = 0x0;
1309 pthru->target_id = device_id;
1310 pthru->lun = scp->device->lun;
1311 pthru->cdb_len = scp->cmd_len;
1312 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001313 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301314 pthru->flags = cpu_to_le16(flags);
1315 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001316
1317 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1318
1319 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001320 * If the command is for the tape device, set the
1321 * pthru timeout to the os layer timeout value.
1322 */
Yang, Bo8d568252009-10-06 14:12:21 -06001323 if (scp->device->type == TYPE_TAPE) {
1324 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301325 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001326 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301327 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001328 }
1329
1330 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001331 * Construct SGL
1332 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001333 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301334 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001335 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1336 &pthru->sgl);
1337 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301338 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001339 pthru->sge_count = megasas_make_sgl64(instance, scp,
1340 &pthru->sgl);
1341 } else
1342 pthru->sge_count = megasas_make_sgl32(instance, scp,
1343 &pthru->sgl);
1344
Yang, Bobdc6fb82009-12-06 08:30:19 -07001345 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001346 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001347 pthru->sge_count);
1348 return 0;
1349 }
1350
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001351 /*
1352 * Sense info specific
1353 */
1354 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301355 pthru->sense_buf_phys_addr_hi =
1356 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1357 pthru->sense_buf_phys_addr_lo =
1358 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001359
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001360 /*
1361 * Compute the total number of frames this command consumes. FW uses
1362 * this number to pull sufficient number of frames from host memory.
1363 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001364 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001365 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001366
1367 return cmd->frame_count;
1368}
1369
1370/**
1371 * megasas_build_ldio - Prepares IOs to logical devices
1372 * @instance: Adapter soft state
1373 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001374 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001375 *
1376 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1377 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001378static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001379megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1380 struct megasas_cmd *cmd)
1381{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001382 u32 device_id;
1383 u8 sc = scp->cmnd[0];
1384 u16 flags = 0;
1385 struct megasas_io_frame *ldio;
1386
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301387 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001388 ldio = (struct megasas_io_frame *)cmd->frame;
1389
1390 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1391 flags = MFI_FRAME_DIR_WRITE;
1392 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1393 flags = MFI_FRAME_DIR_READ;
1394
Yang, Bof4c9a132009-10-06 14:43:28 -06001395 if (instance->flag_ieee == 1) {
1396 flags |= MFI_FRAME_IEEE;
1397 }
1398
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001399 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001400 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001401 */
1402 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1403 ldio->cmd_status = 0x0;
1404 ldio->scsi_status = 0x0;
1405 ldio->target_id = device_id;
1406 ldio->timeout = 0;
1407 ldio->reserved_0 = 0;
1408 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301409 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001410 ldio->start_lba_hi = 0;
1411 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1412
1413 /*
1414 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1415 */
1416 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301417 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1418 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1419 ((u32) scp->cmnd[2] << 8) |
1420 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001421
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301422 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423 }
1424
1425 /*
1426 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1427 */
1428 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301429 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1430 ((u32) scp->cmnd[7] << 8));
1431 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1432 ((u32) scp->cmnd[3] << 16) |
1433 ((u32) scp->cmnd[4] << 8) |
1434 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001435 }
1436
1437 /*
1438 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1439 */
1440 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301441 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1442 ((u32) scp->cmnd[7] << 16) |
1443 ((u32) scp->cmnd[8] << 8) |
1444 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001445
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301446 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1447 ((u32) scp->cmnd[3] << 16) |
1448 ((u32) scp->cmnd[4] << 8) |
1449 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001450 }
1451
1452 /*
1453 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1454 */
1455 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301456 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1457 ((u32) scp->cmnd[11] << 16) |
1458 ((u32) scp->cmnd[12] << 8) |
1459 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001460
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301461 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1462 ((u32) scp->cmnd[7] << 16) |
1463 ((u32) scp->cmnd[8] << 8) |
1464 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001465
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301466 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1467 ((u32) scp->cmnd[3] << 16) |
1468 ((u32) scp->cmnd[4] << 8) |
1469 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001470
1471 }
1472
1473 /*
1474 * Construct SGL
1475 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001476 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301477 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001478 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1479 &ldio->sgl);
1480 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301481 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001482 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1483 } else
1484 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1485
Yang, Bobdc6fb82009-12-06 08:30:19 -07001486 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001487 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001488 ldio->sge_count);
1489 return 0;
1490 }
1491
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001492 /*
1493 * Sense info specific
1494 */
1495 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1496 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301497 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001498
Sumant Patrob1df99d2006-10-03 12:40:47 -07001499 /*
1500 * Compute the total number of frames this command consumes. FW uses
1501 * this number to pull sufficient number of frames from host memory.
1502 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001503 cmd->frame_count = megasas_get_frame_count(instance,
1504 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001505
1506 return cmd->frame_count;
1507}
1508
1509/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301510 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1511 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001512 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001513 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001514 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301515inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001516{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301517 int ret;
1518
Sumant Patrocb59aa62006-01-25 11:53:25 -08001519 switch (cmd->cmnd[0]) {
1520 case READ_10:
1521 case WRITE_10:
1522 case READ_12:
1523 case WRITE_12:
1524 case READ_6:
1525 case WRITE_6:
1526 case READ_16:
1527 case WRITE_16:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301528 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1529 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1530 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001531 default:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301532 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1533 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001534 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301535 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001536}
1537
Sumant Patro658dced2006-10-03 13:09:14 -07001538 /**
1539 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001540 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001541 * @instance: Adapter soft state
1542 */
1543static inline void
1544megasas_dump_pending_frames(struct megasas_instance *instance)
1545{
1546 struct megasas_cmd *cmd;
1547 int i,n;
1548 union megasas_sgl *mfi_sgl;
1549 struct megasas_io_frame *ldio;
1550 struct megasas_pthru_frame *pthru;
1551 u32 sgcount;
1552 u32 max_cmd = instance->max_fw_cmds;
1553
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001554 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1555 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 -07001556 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001557 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 -07001558 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001559 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 -07001560
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001561 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001562 for (i = 0; i < max_cmd; i++) {
1563 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001564 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001565 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001566 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 +05301567 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001568 ldio = (struct megasas_io_frame *)cmd->frame;
1569 mfi_sgl = &ldio->sgl;
1570 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001571 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 +05301572 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1573 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1574 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1575 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001576 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001577 pthru = (struct megasas_pthru_frame *) cmd->frame;
1578 mfi_sgl = &pthru->sgl;
1579 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001580 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 +05301581 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1582 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1583 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1584 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001585 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001586 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1587 for (n = 0; n < sgcount; n++) {
1588 if (IS_DMA64)
1589 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1590 le32_to_cpu(mfi_sgl->sge64[n].length),
1591 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1592 else
1593 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1594 le32_to_cpu(mfi_sgl->sge32[n].length),
1595 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001596 }
1597 }
Sumant Patro658dced2006-10-03 13:09:14 -07001598 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001599 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001600 for (i = 0; i < max_cmd; i++) {
1601
1602 cmd = instance->cmd_list[i];
1603
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001604 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001605 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001606 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001607 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001608}
1609
adam radfordcd50ba82010-12-21 10:23:23 -08001610u32
1611megasas_build_and_issue_cmd(struct megasas_instance *instance,
1612 struct scsi_cmnd *scmd)
1613{
1614 struct megasas_cmd *cmd;
1615 u32 frame_count;
1616
1617 cmd = megasas_get_cmd(instance);
1618 if (!cmd)
1619 return SCSI_MLQUEUE_HOST_BUSY;
1620
1621 /*
1622 * Logical drive command
1623 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301624 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001625 frame_count = megasas_build_ldio(instance, scmd, cmd);
1626 else
1627 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1628
1629 if (!frame_count)
1630 goto out_return_cmd;
1631
1632 cmd->scmd = scmd;
1633 scmd->SCp.ptr = (char *)cmd;
1634
1635 /*
1636 * Issue the command to the FW
1637 */
1638 atomic_inc(&instance->fw_outstanding);
1639
1640 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1641 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001642
1643 return 0;
1644out_return_cmd:
1645 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301646 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001647}
1648
1649
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001650/**
1651 * megasas_queue_command - Queue entry point
1652 * @scmd: SCSI command to be queued
1653 * @done: Callback entry point
1654 */
1655static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301656megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001657{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001658 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301659 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001660
1661 instance = (struct megasas_instance *)
1662 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001663
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301664 if (instance->unload == 1) {
1665 scmd->result = DID_NO_CONNECT << 16;
1666 scmd->scsi_done(scmd);
1667 return 0;
1668 }
1669
bo yang39a98552010-09-22 22:36:29 -04001670 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001671 return SCSI_MLQUEUE_HOST_BUSY;
1672
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301673
adam radford229fe472014-03-10 02:51:56 -07001674 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301675 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001676 if (megasas_check_mpio_paths(instance, scmd) ==
1677 (DID_RESET << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001678 return SCSI_MLQUEUE_HOST_BUSY;
1679 } else {
adam radford229fe472014-03-10 02:51:56 -07001680 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301681 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001682 return 0;
1683 }
1684 }
1685
Sumit Saxena8a01a412016-01-28 21:04:32 +05301686 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001687 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301688 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301689 return 0;
1690 }
1691
Sumit Saxena18365b12016-01-28 21:04:25 +05301692 mr_device_priv_data = scmd->device->hostdata;
1693 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301694 scmd->result = DID_NO_CONNECT << 16;
1695 scmd->scsi_done(scmd);
1696 return 0;
1697 }
1698
Sumit Saxena8a01a412016-01-28 21:04:32 +05301699 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001700 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001701
Sumit Saxena8a01a412016-01-28 21:04:32 +05301702 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301703 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301704
bo yang39a98552010-09-22 22:36:29 -04001705
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001706 scmd->result = 0;
1707
Sumant Patrocb59aa62006-01-25 11:53:25 -08001708 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301709 (scmd->device->id >= instance->fw_supported_vd_count ||
1710 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001711 scmd->result = DID_BAD_TARGET << 16;
1712 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001713 }
1714
Sumant Patro02b01e02007-02-14 13:00:55 -08001715 switch (scmd->cmnd[0]) {
1716 case SYNCHRONIZE_CACHE:
1717 /*
1718 * FW takes care of flush cache on its own
1719 * No need to send it down
1720 */
1721 scmd->result = DID_OK << 16;
1722 goto out_done;
1723 default:
1724 break;
1725 }
1726
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301727 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001728
Sumant Patrocb59aa62006-01-25 11:53:25 -08001729 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301730 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001731 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001732}
1733
Yang, Bo044833b2009-10-06 14:33:06 -06001734static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1735{
1736 int i;
1737
1738 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1739
1740 if ((megasas_mgmt_info.instance[i]) &&
1741 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1742 return megasas_mgmt_info.instance[i];
1743 }
1744
1745 return NULL;
1746}
1747
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301748/*
Sumit Saxena18365b12016-01-28 21:04:25 +05301749* megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301750*
1751* @sdev: OS provided scsi device
1752*
1753* Returns void
1754*/
Sumit Saxena18365b12016-01-28 21:04:25 +05301755void megasas_update_sdev_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301756{
Sumit Saxena18365b12016-01-28 21:04:25 +05301757 u16 pd_index = 0;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301758 u32 device_id, ld;
1759 struct megasas_instance *instance;
1760 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301761 struct MR_PRIV_DEVICE *mr_device_priv_data;
1762 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301763 struct MR_LD_RAID *raid;
1764 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1765
1766 instance = megasas_lookup_instance(sdev->host->host_no);
1767 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301768 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301769
1770 if (!fusion)
1771 return;
1772
Sumit Saxena18365b12016-01-28 21:04:25 +05301773 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1774 instance->use_seqnum_jbod_fp) {
1775 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1776 sdev->id;
1777 pd_sync = (void *)fusion->pd_seq_sync
1778 [(instance->pd_seq_map_id - 1) & 1];
1779 mr_device_priv_data->is_tm_capable =
1780 pd_sync->seq[pd_index].capability.tmCapable;
1781 } else {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301782 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1783 + sdev->id;
1784 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1785 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1786 raid = MR_LdRaidGet(ld, local_map_ptr);
1787
1788 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Sumit Saxena18365b12016-01-28 21:04:25 +05301789 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1790 mr_device_priv_data->is_tm_capable =
1791 raid->capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301792 }
1793}
1794
Sumit Saxena2216c302016-01-28 21:04:26 +05301795static void megasas_set_device_queue_depth(struct scsi_device *sdev)
1796{
1797 u16 pd_index = 0;
1798 int ret = DCMD_FAILED;
1799 struct megasas_instance *instance;
1800
1801 instance = megasas_lookup_instance(sdev->host->host_no);
1802
1803 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1804 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
1805
1806 if (instance->pd_info) {
1807 mutex_lock(&instance->hba_mutex);
1808 ret = megasas_get_pd_info(instance, pd_index);
1809 mutex_unlock(&instance->hba_mutex);
1810 }
1811
1812 if (ret != DCMD_SUCCESS)
1813 return;
1814
1815 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1816
1817 switch (instance->pd_list[pd_index].interface) {
1818 case SAS_PD:
1819 scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
1820 break;
1821
1822 case SATA_PD:
1823 scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
1824 break;
1825
1826 default:
1827 scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
1828 }
1829 }
1830 }
1831}
1832
Sumit Saxena18365b12016-01-28 21:04:25 +05301833
Christoph Hellwig147aab62006-02-17 12:13:48 +01001834static int megasas_slave_configure(struct scsi_device *sdev)
1835{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301836 u16 pd_index = 0;
1837 struct megasas_instance *instance;
1838
1839 instance = megasas_lookup_instance(sdev->host->host_no);
1840 if (instance->allow_fw_scan) {
1841 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1842 sdev->type == TYPE_DISK) {
1843 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1844 sdev->id;
1845 if (instance->pd_list[pd_index].driveState !=
1846 MR_PD_STATE_SYSTEM)
1847 return -ENXIO;
1848 }
1849 }
Sumit Saxena2216c302016-01-28 21:04:26 +05301850 megasas_set_device_queue_depth(sdev);
Sumit Saxena18365b12016-01-28 21:04:25 +05301851 megasas_update_sdev_properties(sdev);
1852
Yang, Bo044833b2009-10-06 14:33:06 -06001853 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001854 * The RAID firmware may require extended timeouts.
1855 */
Yang, Bo044833b2009-10-06 14:33:06 -06001856 blk_queue_rq_timeout(sdev->request_queue,
Sumit Saxenae3d178c2016-01-28 21:04:34 +05301857 scmd_timeout * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301858
Yang, Bo044833b2009-10-06 14:33:06 -06001859 return 0;
1860}
1861
1862static int megasas_slave_alloc(struct scsi_device *sdev)
1863{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001864 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001865 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301866 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001867
Yang, Bo044833b2009-10-06 14:33:06 -06001868 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301869 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001870 /*
1871 * Open the OS scan to the SYSTEM PD
1872 */
1873 pd_index =
1874 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1875 sdev->id;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301876 if ((instance->allow_fw_scan || instance->pd_list[pd_index].driveState ==
1877 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301878 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001879 }
1880 return -ENXIO;
1881 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301882
1883scan_target:
1884 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1885 GFP_KERNEL);
1886 if (!mr_device_priv_data)
1887 return -ENOMEM;
1888 sdev->hostdata = mr_device_priv_data;
Christoph Hellwig147aab62006-02-17 12:13:48 +01001889 return 0;
1890}
1891
Sumit Saxena18365b12016-01-28 21:04:25 +05301892static void megasas_slave_destroy(struct scsi_device *sdev)
1893{
1894 kfree(sdev->hostdata);
1895 sdev->hostdata = NULL;
1896}
1897
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301898/*
1899* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1900* kill adapter
1901* @instance: Adapter soft state
1902*
1903*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301904static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301905{
1906 int i;
1907 struct megasas_cmd *cmd_mfi;
1908 struct megasas_cmd_fusion *cmd_fusion;
1909 struct fusion_context *fusion = instance->ctrl_context;
1910
1911 /* Find all outstanding ioctls */
1912 if (fusion) {
1913 for (i = 0; i < instance->max_fw_cmds; i++) {
1914 cmd_fusion = fusion->cmd_list[i];
1915 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1916 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1917 if (cmd_mfi->sync_cmd &&
1918 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1919 megasas_complete_cmd(instance,
1920 cmd_mfi, DID_OK);
1921 }
1922 }
1923 } else {
1924 for (i = 0; i < instance->max_fw_cmds; i++) {
1925 cmd_mfi = instance->cmd_list[i];
1926 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1927 MFI_CMD_ABORT)
1928 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1929 }
1930 }
1931}
1932
1933
adam radford9c915a82010-12-21 13:34:31 -08001934void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001935{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301936 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301937 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301938 /* Wait 1 second to ensure IO or ioctls in build have posted */
1939 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001940 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301941 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301942 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001943 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001944 /* Flush */
1945 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05301946 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07001947 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001948 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301949 writel(MFI_STOP_ADP,
1950 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001951 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301952 /* Complete outstanding ioctls when adapter is killed */
1953 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001954}
1955
1956 /**
1957 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1958 * restored to max value
1959 * @instance: Adapter soft state
1960 *
1961 */
1962void
1963megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1964{
1965 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301966
adam radford9c915a82010-12-21 13:34:31 -08001967 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001968 && time_after(jiffies, instance->last_time + 5 * HZ)
1969 && atomic_read(&instance->fw_outstanding) <
1970 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001971
1972 spin_lock_irqsave(instance->host->host_lock, flags);
1973 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001974
Sumit Saxena308ec452016-01-28 21:04:30 +05301975 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08001976 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001977 }
1978}
1979
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001980/**
bo yang7343eb62007-11-09 04:35:44 -05001981 * megasas_complete_cmd_dpc - Returns FW's controller structure
1982 * @instance_addr: Address of adapter soft state
1983 *
1984 * Tasklet to complete cmds
1985 */
1986static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1987{
1988 u32 producer;
1989 u32 consumer;
1990 u32 context;
1991 struct megasas_cmd *cmd;
1992 struct megasas_instance *instance =
1993 (struct megasas_instance *)instance_addr;
1994 unsigned long flags;
1995
1996 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301997 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05001998 return;
1999
2000 spin_lock_irqsave(&instance->completion_lock, flags);
2001
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302002 producer = le32_to_cpu(*instance->producer);
2003 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002004
2005 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302006 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002007 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002008 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002009 context);
2010 BUG();
2011 }
bo yang7343eb62007-11-09 04:35:44 -05002012
2013 cmd = instance->cmd_list[context];
2014
2015 megasas_complete_cmd(instance, cmd, DID_OK);
2016
2017 consumer++;
2018 if (consumer == (instance->max_fw_cmds + 1)) {
2019 consumer = 0;
2020 }
2021 }
2022
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302023 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002024
2025 spin_unlock_irqrestore(&instance->completion_lock, flags);
2026
2027 /*
2028 * Check if we can restore can_queue
2029 */
adam radford9c915a82010-12-21 13:34:31 -08002030 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002031}
2032
adam radford229fe472014-03-10 02:51:56 -07002033/**
2034 * megasas_start_timer - Initializes a timer object
2035 * @instance: Adapter soft state
2036 * @timer: timer object to be initialized
2037 * @fn: timer function
2038 * @interval: time interval between timer function call
2039 *
2040 */
2041void megasas_start_timer(struct megasas_instance *instance,
2042 struct timer_list *timer,
2043 void *fn, unsigned long interval)
2044{
2045 init_timer(timer);
2046 timer->expires = jiffies + interval;
2047 timer->data = (unsigned long)instance;
2048 timer->function = fn;
2049 add_timer(timer);
2050}
2051
Yang, Bo707e09b2010-10-12 07:20:27 -06002052static void
2053megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2054
2055static void
2056process_fw_state_change_wq(struct work_struct *work);
2057
2058void megasas_do_ocr(struct megasas_instance *instance)
2059{
2060 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2061 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2062 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302063 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002064 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302065 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302066 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002067 instance->issuepend_done = 0;
2068
2069 atomic_set(&instance->fw_outstanding, 0);
2070 megasas_internal_reset_defer_cmds(instance);
2071 process_fw_state_change_wq(&instance->work_init);
2072}
2073
Adam Radford4cbfea82014-07-09 15:17:56 -07002074static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2075 int initial)
adam radford229fe472014-03-10 02:51:56 -07002076{
2077 struct megasas_cmd *cmd;
2078 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002079 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002080 dma_addr_t new_affiliation_111_h;
2081 int ld, retval = 0;
2082 u8 thisVf;
2083
2084 cmd = megasas_get_cmd(instance);
2085
2086 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002087 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2088 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002089 instance->host->host_no);
2090 return -ENOMEM;
2091 }
2092
2093 dcmd = &cmd->frame->dcmd;
2094
Adam Radford4cbfea82014-07-09 15:17:56 -07002095 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002096 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2097 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002098 megasas_return_cmd(instance, cmd);
2099 return -ENOMEM;
2100 }
2101
2102 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002103 memset(instance->vf_affiliation_111, 0,
2104 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002105 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002106 new_affiliation_111 =
2107 pci_alloc_consistent(instance->pdev,
2108 sizeof(struct MR_LD_VF_AFFILIATION_111),
2109 &new_affiliation_111_h);
2110 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002111 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2112 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002113 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002114 megasas_return_cmd(instance, cmd);
2115 return -ENOMEM;
2116 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002117 memset(new_affiliation_111, 0,
2118 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002119 }
2120
2121 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2122
2123 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302124 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002125 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302126 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002127 dcmd->timeout = 0;
2128 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302129 dcmd->data_xfer_len =
2130 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2131 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002132
Adam Radford4cbfea82014-07-09 15:17:56 -07002133 if (initial)
2134 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302135 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002136 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302137 dcmd->sgl.sge32[0].phys_addr =
2138 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002139
Christoph Hellwig2213a462015-04-23 16:33:54 +05302140 dcmd->sgl.sge32[0].length = cpu_to_le32(
2141 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002142
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002143 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002144 "scsi%d\n", instance->host->host_no);
2145
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302146 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002147 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2148 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002149 dcmd->cmd_status, instance->host->host_no);
2150 retval = 1; /* Do a scan if we couldn't get affiliation */
2151 goto out;
2152 }
2153
2154 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002155 thisVf = new_affiliation_111->thisVf;
2156 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2157 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2158 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002159 dev_warn(&instance->pdev->dev, "SR-IOV: "
2160 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002161 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002162 memcpy(instance->vf_affiliation_111,
2163 new_affiliation_111,
2164 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002165 retval = 1;
2166 goto out;
2167 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002168 }
2169out:
2170 if (new_affiliation_111) {
2171 pci_free_consistent(instance->pdev,
2172 sizeof(struct MR_LD_VF_AFFILIATION_111),
2173 new_affiliation_111,
2174 new_affiliation_111_h);
2175 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302176
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302177 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002178
2179 return retval;
2180}
2181
2182static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2183 int initial)
2184{
2185 struct megasas_cmd *cmd;
2186 struct megasas_dcmd_frame *dcmd;
2187 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2188 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2189 dma_addr_t new_affiliation_h;
2190 int i, j, retval = 0, found = 0, doscan = 0;
2191 u8 thisVf;
2192
2193 cmd = megasas_get_cmd(instance);
2194
2195 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002196 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2197 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002198 instance->host->host_no);
2199 return -ENOMEM;
2200 }
2201
2202 dcmd = &cmd->frame->dcmd;
2203
2204 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002205 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2206 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002207 megasas_return_cmd(instance, cmd);
2208 return -ENOMEM;
2209 }
2210
2211 if (initial)
2212 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2213 sizeof(struct MR_LD_VF_AFFILIATION));
2214 else {
2215 new_affiliation =
2216 pci_alloc_consistent(instance->pdev,
2217 (MAX_LOGICAL_DRIVES + 1) *
2218 sizeof(struct MR_LD_VF_AFFILIATION),
2219 &new_affiliation_h);
2220 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002221 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2222 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002223 instance->host->host_no);
2224 megasas_return_cmd(instance, cmd);
2225 return -ENOMEM;
2226 }
2227 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2228 sizeof(struct MR_LD_VF_AFFILIATION));
2229 }
2230
2231 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2232
2233 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302234 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002235 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302236 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002237 dcmd->timeout = 0;
2238 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302239 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2240 sizeof(struct MR_LD_VF_AFFILIATION));
2241 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002242
2243 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302244 dcmd->sgl.sge32[0].phys_addr =
2245 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002246 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302247 dcmd->sgl.sge32[0].phys_addr =
2248 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002249
Christoph Hellwig2213a462015-04-23 16:33:54 +05302250 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2251 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002252
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002253 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002254 "scsi%d\n", instance->host->host_no);
2255
Adam Radford4cbfea82014-07-09 15:17:56 -07002256
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302257 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002258 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2259 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002260 dcmd->cmd_status, instance->host->host_no);
2261 retval = 1; /* Do a scan if we couldn't get affiliation */
2262 goto out;
2263 }
2264
2265 if (!initial) {
2266 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002267 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2268 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002269 instance->host->host_no);
2270 retval = 1;
2271 goto out;
2272 }
2273 newmap = new_affiliation->map;
2274 savedmap = instance->vf_affiliation->map;
2275 thisVf = new_affiliation->thisVf;
2276 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2277 found = 0;
2278 for (j = 0; j < instance->vf_affiliation->ldCount;
2279 j++) {
2280 if (newmap->ref.targetId ==
2281 savedmap->ref.targetId) {
2282 found = 1;
2283 if (newmap->policy[thisVf] !=
2284 savedmap->policy[thisVf]) {
2285 doscan = 1;
2286 goto out;
2287 }
adam radford229fe472014-03-10 02:51:56 -07002288 }
2289 savedmap = (struct MR_LD_VF_MAP *)
2290 ((unsigned char *)savedmap +
2291 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002292 }
2293 if (!found && newmap->policy[thisVf] !=
2294 MR_LD_ACCESS_HIDDEN) {
2295 doscan = 1;
2296 goto out;
2297 }
2298 newmap = (struct MR_LD_VF_MAP *)
2299 ((unsigned char *)newmap + newmap->size);
2300 }
2301
2302 newmap = new_affiliation->map;
2303 savedmap = instance->vf_affiliation->map;
2304
2305 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2306 found = 0;
2307 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2308 if (savedmap->ref.targetId ==
2309 newmap->ref.targetId) {
2310 found = 1;
2311 if (savedmap->policy[thisVf] !=
2312 newmap->policy[thisVf]) {
2313 doscan = 1;
2314 goto out;
2315 }
2316 }
adam radford229fe472014-03-10 02:51:56 -07002317 newmap = (struct MR_LD_VF_MAP *)
2318 ((unsigned char *)newmap +
2319 newmap->size);
2320 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002321 if (!found && savedmap->policy[thisVf] !=
2322 MR_LD_ACCESS_HIDDEN) {
2323 doscan = 1;
2324 goto out;
2325 }
2326 savedmap = (struct MR_LD_VF_MAP *)
2327 ((unsigned char *)savedmap +
2328 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002329 }
2330 }
2331out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002332 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002333 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2334 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002335 memcpy(instance->vf_affiliation, new_affiliation,
2336 new_affiliation->size);
2337 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002338 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002339
2340 if (new_affiliation)
2341 pci_free_consistent(instance->pdev,
2342 (MAX_LOGICAL_DRIVES + 1) *
2343 sizeof(struct MR_LD_VF_AFFILIATION),
2344 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302345 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002346
2347 return retval;
2348}
2349
Adam Radford4cbfea82014-07-09 15:17:56 -07002350/* This function will get the current SR-IOV LD/VF affiliation */
2351static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2352 int initial)
2353{
2354 int retval;
2355
2356 if (instance->PlasmaFW111)
2357 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2358 else
2359 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2360 return retval;
2361}
2362
adam radford229fe472014-03-10 02:51:56 -07002363/* This function will tell FW to start the SR-IOV heartbeat */
2364int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2365 int initial)
2366{
2367 struct megasas_cmd *cmd;
2368 struct megasas_dcmd_frame *dcmd;
2369 int retval = 0;
2370
2371 cmd = megasas_get_cmd(instance);
2372
2373 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002374 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2375 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002376 instance->host->host_no);
2377 return -ENOMEM;
2378 }
2379
2380 dcmd = &cmd->frame->dcmd;
2381
2382 if (initial) {
2383 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002384 pci_zalloc_consistent(instance->pdev,
2385 sizeof(struct MR_CTRL_HB_HOST_MEM),
2386 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002387 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002388 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2389 " memory for heartbeat host memory for scsi%d\n",
2390 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002391 retval = -ENOMEM;
2392 goto out;
2393 }
adam radford229fe472014-03-10 02:51:56 -07002394 }
2395
2396 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2397
Christoph Hellwig2213a462015-04-23 16:33:54 +05302398 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002399 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302400 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002401 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302402 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002403 dcmd->timeout = 0;
2404 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302405 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2406 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2407 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2408 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002409
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002410 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002411 instance->host->host_no);
2412
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302413 if (instance->ctrl_context && !instance->mask_interrupts)
2414 retval = megasas_issue_blocked_cmd(instance, cmd,
2415 MEGASAS_ROUTINE_WAIT_TIME_VF);
2416 else
2417 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002418
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302419 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302420 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2421 "_MEM_ALLOC DCMD %s for scsi%d\n",
2422 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2423 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002424 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002425 }
2426
2427out:
2428 megasas_return_cmd(instance, cmd);
2429
2430 return retval;
2431}
2432
2433/* Handler for SR-IOV heartbeat */
2434void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2435{
2436 struct megasas_instance *instance =
2437 (struct megasas_instance *)instance_addr;
2438
2439 if (instance->hb_host_mem->HB.fwCounter !=
2440 instance->hb_host_mem->HB.driverCounter) {
2441 instance->hb_host_mem->HB.driverCounter =
2442 instance->hb_host_mem->HB.fwCounter;
2443 mod_timer(&instance->sriov_heartbeat_timer,
2444 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2445 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002446 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002447 "completed for scsi%d\n", instance->host->host_no);
2448 schedule_work(&instance->work_init);
2449 }
2450}
2451
bo yang7343eb62007-11-09 04:35:44 -05002452/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002453 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2454 * @instance: Adapter soft state
2455 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002456 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002457 * complete all its outstanding commands. Returns error if one or more IOs
2458 * are pending after this time period. It also marks the controller dead.
2459 */
2460static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2461{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302462 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002463 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002464 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002465 unsigned long flags;
2466 struct list_head clist_local;
2467 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002468 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002469
Sumit Saxenaccc75072016-01-28 21:04:33 +05302470 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2471 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2472 __func__, __LINE__);
2473 return FAILED;
2474 }
bo yang39a98552010-09-22 22:36:29 -04002475
Sumit Saxena8a01a412016-01-28 21:04:32 +05302476 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002477
2478 INIT_LIST_HEAD(&clist_local);
2479 spin_lock_irqsave(&instance->hba_lock, flags);
2480 list_splice_init(&instance->internal_reset_pending_q,
2481 &clist_local);
2482 spin_unlock_irqrestore(&instance->hba_lock, flags);
2483
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002484 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002485 for (i = 0; i < wait_time; i++) {
2486 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302487 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002488 break;
2489 }
2490
Sumit Saxena8a01a412016-01-28 21:04:32 +05302491 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002492 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302493 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002494 return FAILED;
2495 }
2496
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002497 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002498 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002499 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002500 struct megasas_cmd, list);
2501 list_del_init(&reset_cmd->list);
2502 if (reset_cmd->scmd) {
2503 reset_cmd->scmd->result = DID_RESET << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002504 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002505 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002506 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002507
2508 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2509 megasas_return_cmd(instance, reset_cmd);
2510 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002511 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002512 "reset queue\n",
2513 reset_cmd);
2514
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302515 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002516 instance->instancet->fire_cmd(instance,
2517 reset_cmd->frame_phys_addr,
2518 0, instance->reg_set);
2519 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002520 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002521 "cmds lst\n",
2522 reset_cmd);
2523 }
2524 reset_index++;
2525 }
2526
2527 return SUCCESS;
2528 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002529
adam radfordc007b8b2012-07-17 18:20:24 -07002530 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302531 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002532
2533 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002534 break;
2535
2536 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002537 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002538 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002539 /*
2540 * Call cmd completion routine. Cmd to be
2541 * be completed directly without depending on isr.
2542 */
2543 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002544 }
2545
2546 msleep(1000);
2547 }
2548
Yang, Bo707e09b2010-10-12 07:20:27 -06002549 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302550 outstanding = atomic_read(&instance->fw_outstanding);
2551 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002552
Sumit Saxenaccc75072016-01-28 21:04:33 +05302553 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2554 goto no_outstanding;
2555
2556 if (instance->disableOnlineCtrlReset)
2557 goto kill_hba_and_failed;
2558 do {
2559 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2560 dev_info(&instance->pdev->dev,
2561 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2562 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2563 if (i == 3)
2564 goto kill_hba_and_failed;
2565 megasas_do_ocr(instance);
2566
2567 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2568 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2569 __func__, __LINE__);
2570 return FAILED;
2571 }
2572 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2573 __func__, __LINE__);
2574
2575 for (sl = 0; sl < 10; sl++)
2576 msleep(500);
2577
2578 outstanding = atomic_read(&instance->fw_outstanding);
2579
2580 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2581 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2582 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002583 }
2584 i++;
2585 } while (i <= 3);
2586
Sumit Saxenaccc75072016-01-28 21:04:33 +05302587no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002588
Sumit Saxenaccc75072016-01-28 21:04:33 +05302589 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2590 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002591 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302592
2593kill_hba_and_failed:
2594
2595 /* Reset not supported, kill adapter */
2596 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2597 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2598 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2599 atomic_read(&instance->fw_outstanding));
2600 megasas_dump_pending_frames(instance);
2601 megaraid_sas_kill_hba(instance);
2602
2603 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002604}
2605
2606/**
2607 * megasas_generic_reset - Generic reset routine
2608 * @scmd: Mid-layer SCSI command
2609 *
2610 * This routine implements a generic reset handler for device, bus and host
2611 * reset requests. Device, bus and host specific reset handlers can use this
2612 * function after they do their specific tasks.
2613 */
2614static int megasas_generic_reset(struct scsi_cmnd *scmd)
2615{
2616 int ret_val;
2617 struct megasas_instance *instance;
2618
2619 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2620
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002621 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2622 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002623
Sumit Saxena8a01a412016-01-28 21:04:32 +05302624 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002625 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002626 return FAILED;
2627 }
2628
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002629 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002630 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002631 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002632 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002633 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002634
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002635 return ret_val;
2636}
2637
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002638/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002639 * megasas_reset_timer - quiesce the adapter if required
2640 * @scmd: scsi cmnd
2641 *
2642 * Sets the FW busy flag and reduces the host->can_queue if the
2643 * cmd has not been completed within the timeout period.
2644 */
2645static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002646blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002647{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002648 struct megasas_instance *instance;
2649 unsigned long flags;
2650
2651 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302652 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002653 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002654 }
2655
adam radfordf575c5d2011-10-13 16:01:12 -07002656 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002657 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2658 /* FW is busy, throttle IO */
2659 spin_lock_irqsave(instance->host->host_lock, flags);
2660
adam radfordc5daa6a2012-07-17 18:20:03 -07002661 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002662 instance->last_time = jiffies;
2663 instance->flag |= MEGASAS_FW_BUSY;
2664
2665 spin_unlock_irqrestore(instance->host->host_lock, flags);
2666 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002667 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002668}
2669
2670/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002671 * megasas_reset_device - Device reset handler entry point
2672 */
2673static int megasas_reset_device(struct scsi_cmnd *scmd)
2674{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002675 /*
2676 * First wait for all commands to complete
2677 */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002678 return megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002679}
2680
2681/**
2682 * megasas_reset_bus_host - Bus & host reset handler entry point
2683 */
2684static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2685{
2686 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002687 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002688
adam radford9c915a82010-12-21 13:34:31 -08002689 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002690
2691 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002692 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002693 */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302694 if (instance->ctrl_context)
adam radford229fe472014-03-10 02:51:56 -07002695 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002696 else
2697 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002698
2699 return ret;
2700}
2701
2702/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002703 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002704 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002705 * @bdev: block device
2706 * @capacity: drive capacity
2707 * @geom: geometry parameters
2708 */
2709static int
2710megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2711 sector_t capacity, int geom[])
2712{
2713 int heads;
2714 int sectors;
2715 sector_t cylinders;
2716 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002717
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002718 /* Default heads (64) & sectors (32) */
2719 heads = 64;
2720 sectors = 32;
2721
2722 tmp = heads * sectors;
2723 cylinders = capacity;
2724
2725 sector_div(cylinders, tmp);
2726
2727 /*
2728 * Handle extended translation size for logical drives > 1Gb
2729 */
2730
2731 if (capacity >= 0x200000) {
2732 heads = 255;
2733 sectors = 63;
2734 tmp = heads*sectors;
2735 cylinders = capacity;
2736 sector_div(cylinders, tmp);
2737 }
2738
2739 geom[0] = heads;
2740 geom[1] = sectors;
2741 geom[2] = cylinders;
2742
2743 return 0;
2744}
2745
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002746static void megasas_aen_polling(struct work_struct *work);
2747
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002748/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002749 * megasas_service_aen - Processes an event notification
2750 * @instance: Adapter soft state
2751 * @cmd: AEN command completed by the ISR
2752 *
2753 * For AEN, driver sends a command down to FW that is held by the FW till an
2754 * event occurs. When an event of interest occurs, FW completes the command
2755 * that it was previously holding.
2756 *
2757 * This routines sends SIGIO signal to processes that have registered with the
2758 * driver for AEN.
2759 */
2760static void
2761megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2762{
Yang, Boc3518832009-10-06 14:18:02 -06002763 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002764
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002765 /*
2766 * Don't signal app if it is just an aborted previously registered aen
2767 */
Yang, Boc3518832009-10-06 14:18:02 -06002768 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2769 spin_lock_irqsave(&poll_aen_lock, flags);
2770 megasas_poll_wait_aen = 1;
2771 spin_unlock_irqrestore(&poll_aen_lock, flags);
2772 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002773 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002774 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002775 else
2776 cmd->abort_aen = 0;
2777
2778 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302779
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302780 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002781
bo yang39a98552010-09-22 22:36:29 -04002782 if ((instance->unload == 0) &&
2783 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002784 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002785
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002786 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2787 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002788 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002789 } else {
2790 ev->instance = instance;
2791 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002792 INIT_DELAYED_WORK(&ev->hotplug_work,
2793 megasas_aen_polling);
2794 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002795 }
2796 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002797}
2798
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302799static ssize_t
2800megasas_fw_crash_buffer_store(struct device *cdev,
2801 struct device_attribute *attr, const char *buf, size_t count)
2802{
2803 struct Scsi_Host *shost = class_to_shost(cdev);
2804 struct megasas_instance *instance =
2805 (struct megasas_instance *) shost->hostdata;
2806 int val = 0;
2807 unsigned long flags;
2808
2809 if (kstrtoint(buf, 0, &val) != 0)
2810 return -EINVAL;
2811
2812 spin_lock_irqsave(&instance->crashdump_lock, flags);
2813 instance->fw_crash_buffer_offset = val;
2814 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2815 return strlen(buf);
2816}
2817
2818static ssize_t
2819megasas_fw_crash_buffer_show(struct device *cdev,
2820 struct device_attribute *attr, char *buf)
2821{
2822 struct Scsi_Host *shost = class_to_shost(cdev);
2823 struct megasas_instance *instance =
2824 (struct megasas_instance *) shost->hostdata;
2825 u32 size;
2826 unsigned long buff_addr;
2827 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2828 unsigned long src_addr;
2829 unsigned long flags;
2830 u32 buff_offset;
2831
2832 spin_lock_irqsave(&instance->crashdump_lock, flags);
2833 buff_offset = instance->fw_crash_buffer_offset;
2834 if (!instance->crash_dump_buf &&
2835 !((instance->fw_crash_state == AVAILABLE) ||
2836 (instance->fw_crash_state == COPYING))) {
2837 dev_err(&instance->pdev->dev,
2838 "Firmware crash dump is not available\n");
2839 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2840 return -EINVAL;
2841 }
2842
2843 buff_addr = (unsigned long) buf;
2844
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002845 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302846 dev_err(&instance->pdev->dev,
2847 "Firmware crash dump offset is out of range\n");
2848 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2849 return 0;
2850 }
2851
2852 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2853 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2854
2855 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2856 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002857 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302858 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2859
2860 return size;
2861}
2862
2863static ssize_t
2864megasas_fw_crash_buffer_size_show(struct device *cdev,
2865 struct device_attribute *attr, char *buf)
2866{
2867 struct Scsi_Host *shost = class_to_shost(cdev);
2868 struct megasas_instance *instance =
2869 (struct megasas_instance *) shost->hostdata;
2870
2871 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2872 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2873}
2874
2875static ssize_t
2876megasas_fw_crash_state_store(struct device *cdev,
2877 struct device_attribute *attr, const char *buf, size_t count)
2878{
2879 struct Scsi_Host *shost = class_to_shost(cdev);
2880 struct megasas_instance *instance =
2881 (struct megasas_instance *) shost->hostdata;
2882 int val = 0;
2883 unsigned long flags;
2884
2885 if (kstrtoint(buf, 0, &val) != 0)
2886 return -EINVAL;
2887
2888 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2889 dev_err(&instance->pdev->dev, "application updates invalid "
2890 "firmware crash state\n");
2891 return -EINVAL;
2892 }
2893
2894 instance->fw_crash_state = val;
2895
2896 if ((val == COPIED) || (val == COPY_ERROR)) {
2897 spin_lock_irqsave(&instance->crashdump_lock, flags);
2898 megasas_free_host_crash_buffer(instance);
2899 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2900 if (val == COPY_ERROR)
2901 dev_info(&instance->pdev->dev, "application failed to "
2902 "copy Firmware crash dump\n");
2903 else
2904 dev_info(&instance->pdev->dev, "Firmware crash dump "
2905 "copied successfully\n");
2906 }
2907 return strlen(buf);
2908}
2909
2910static ssize_t
2911megasas_fw_crash_state_show(struct device *cdev,
2912 struct device_attribute *attr, char *buf)
2913{
2914 struct Scsi_Host *shost = class_to_shost(cdev);
2915 struct megasas_instance *instance =
2916 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002917
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302918 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2919}
2920
2921static ssize_t
2922megasas_page_size_show(struct device *cdev,
2923 struct device_attribute *attr, char *buf)
2924{
2925 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2926}
2927
Sumit Saxena308ec452016-01-28 21:04:30 +05302928static ssize_t
2929megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
2930 char *buf)
2931{
2932 struct Scsi_Host *shost = class_to_shost(cdev);
2933 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
2934
2935 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
2936}
2937
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302938static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2939 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2940static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2941 megasas_fw_crash_buffer_size_show, NULL);
2942static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2943 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2944static DEVICE_ATTR(page_size, S_IRUGO,
2945 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05302946static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
2947 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302948
2949struct device_attribute *megaraid_host_attrs[] = {
2950 &dev_attr_fw_crash_buffer_size,
2951 &dev_attr_fw_crash_buffer,
2952 &dev_attr_fw_crash_state,
2953 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05302954 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302955 NULL,
2956};
2957
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002958/*
2959 * Scsi host template for megaraid_sas driver
2960 */
2961static struct scsi_host_template megasas_template = {
2962
2963 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05302964 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002965 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002966 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002967 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05302968 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002969 .queuecommand = megasas_queue_command,
2970 .eh_device_reset_handler = megasas_reset_device,
2971 .eh_bus_reset_handler = megasas_reset_bus_host,
2972 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002973 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302974 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002975 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002976 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002977 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002978 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002979};
2980
2981/**
2982 * megasas_complete_int_cmd - Completes an internal command
2983 * @instance: Adapter soft state
2984 * @cmd: Command to be completed
2985 *
2986 * The megasas_issue_blocked_cmd() function waits for a command to complete
2987 * after it issues a command. This function wakes up that waiting routine by
2988 * calling wake_up() on the wait queue.
2989 */
2990static void
2991megasas_complete_int_cmd(struct megasas_instance *instance,
2992 struct megasas_cmd *cmd)
2993{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302994 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002995 wake_up(&instance->int_cmd_wait_q);
2996}
2997
2998/**
2999 * megasas_complete_abort - Completes aborting a command
3000 * @instance: Adapter soft state
3001 * @cmd: Cmd that was issued to abort another cmd
3002 *
adam radford0d490162010-12-14 19:17:17 -08003003 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3004 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003005 * wakes up all functions waiting on the same wait queue.
3006 */
3007static void
3008megasas_complete_abort(struct megasas_instance *instance,
3009 struct megasas_cmd *cmd)
3010{
3011 if (cmd->sync_cmd) {
3012 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303013 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003014 wake_up(&instance->abort_cmd_wait_q);
3015 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003016}
3017
3018/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003019 * megasas_complete_cmd - Completes a command
3020 * @instance: Adapter soft state
3021 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003022 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003023 * SCSI mid-layer instead of the value returned
3024 * by the FW. This should be used if caller wants
3025 * an alternate status (as in the case of aborted
3026 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003027 */
adam radford9c915a82010-12-21 13:34:31 -08003028void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003029megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3030 u8 alt_status)
3031{
3032 int exception = 0;
3033 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003034 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003035 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303036 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003037
bo yang39a98552010-09-22 22:36:29 -04003038 /* flag for the retry reset */
3039 cmd->retry_for_fw_reset = 0;
3040
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003041 if (cmd->scmd)
3042 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003043
3044 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003045 case MFI_CMD_INVALID:
3046 /* Some older 1068 controller FW may keep a pended
3047 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3048 when booting the kdump kernel. Ignore this command to
3049 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003050 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3051 "completed\n");
3052 dev_warn(&instance->pdev->dev, "If you have a controller "
3053 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003054 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003055 case MFI_CMD_PD_SCSI_IO:
3056 case MFI_CMD_LD_SCSI_IO:
3057
3058 /*
3059 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3060 * issued either through an IO path or an IOCTL path. If it
3061 * was via IOCTL, we will send it to internal completion.
3062 */
3063 if (cmd->sync_cmd) {
3064 cmd->sync_cmd = 0;
3065 megasas_complete_int_cmd(instance, cmd);
3066 break;
3067 }
3068
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003069 case MFI_CMD_LD_READ:
3070 case MFI_CMD_LD_WRITE:
3071
3072 if (alt_status) {
3073 cmd->scmd->result = alt_status << 16;
3074 exception = 1;
3075 }
3076
3077 if (exception) {
3078
Sumant Patroe4a082c2006-05-30 12:03:37 -07003079 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003080
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003081 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003082 cmd->scmd->scsi_done(cmd->scmd);
3083 megasas_return_cmd(instance, cmd);
3084
3085 break;
3086 }
3087
3088 switch (hdr->cmd_status) {
3089
3090 case MFI_STAT_OK:
3091 cmd->scmd->result = DID_OK << 16;
3092 break;
3093
3094 case MFI_STAT_SCSI_IO_FAILED:
3095 case MFI_STAT_LD_INIT_IN_PROGRESS:
3096 cmd->scmd->result =
3097 (DID_ERROR << 16) | hdr->scsi_status;
3098 break;
3099
3100 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3101
3102 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3103
3104 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3105 memset(cmd->scmd->sense_buffer, 0,
3106 SCSI_SENSE_BUFFERSIZE);
3107 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3108 hdr->sense_len);
3109
3110 cmd->scmd->result |= DRIVER_SENSE << 24;
3111 }
3112
3113 break;
3114
3115 case MFI_STAT_LD_OFFLINE:
3116 case MFI_STAT_DEVICE_NOT_FOUND:
3117 cmd->scmd->result = DID_BAD_TARGET << 16;
3118 break;
3119
3120 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003121 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003122 hdr->cmd_status);
3123 cmd->scmd->result = DID_ERROR << 16;
3124 break;
3125 }
3126
Sumant Patroe4a082c2006-05-30 12:03:37 -07003127 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003128
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003129 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003130 cmd->scmd->scsi_done(cmd->scmd);
3131 megasas_return_cmd(instance, cmd);
3132
3133 break;
3134
3135 case MFI_CMD_SMP:
3136 case MFI_CMD_STP:
3137 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303138 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003139 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303140 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3141 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303142 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003143 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303144 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003145 if (cmd->frame->hdr.cmd_status != 0) {
3146 if (cmd->frame->hdr.cmd_status !=
3147 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003148 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003149 cmd->frame->hdr.cmd_status);
3150 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303151 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003152 spin_unlock_irqrestore(
3153 instance->host->host_lock,
3154 flags);
3155 break;
3156 }
3157 } else
3158 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303159 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303160
3161 /*
3162 * Set fast path IO to ZERO.
3163 * Validate Map will set proper value.
3164 * Meanwhile all IOs will go as LD IO.
3165 */
3166 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003167 fusion->fast_path_io = 1;
3168 else
3169 fusion->fast_path_io = 0;
3170 megasas_sync_map_info(instance);
3171 spin_unlock_irqrestore(instance->host->host_lock,
3172 flags);
3173 break;
3174 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303175 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3176 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003177 spin_lock_irqsave(&poll_aen_lock, flags);
3178 megasas_poll_wait_aen = 0;
3179 spin_unlock_irqrestore(&poll_aen_lock, flags);
3180 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003181
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303182 /* FW has an updated PD sequence */
3183 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3184 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3185
3186 spin_lock_irqsave(instance->host->host_lock, flags);
3187 status = cmd->frame->hdr.cmd_status;
3188 instance->jbod_seq_cmd = NULL;
3189 megasas_return_cmd(instance, cmd);
3190
3191 if (status == MFI_STAT_OK) {
3192 instance->pd_seq_map_id++;
3193 /* Re-register a pd sync seq num cmd */
3194 if (megasas_sync_pd_seq_num(instance, true))
3195 instance->use_seqnum_jbod_fp = false;
3196 } else
3197 instance->use_seqnum_jbod_fp = false;
3198
3199 spin_unlock_irqrestore(instance->host->host_lock, flags);
3200 break;
3201 }
3202
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003203 /*
3204 * See if got an event notification
3205 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303206 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003207 megasas_service_aen(instance, cmd);
3208 else
3209 megasas_complete_int_cmd(instance, cmd);
3210
3211 break;
3212
3213 case MFI_CMD_ABORT:
3214 /*
3215 * Cmd issued to abort another cmd returned
3216 */
3217 megasas_complete_abort(instance, cmd);
3218 break;
3219
3220 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003221 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003222 hdr->cmd);
3223 break;
3224 }
3225}
3226
3227/**
bo yang39a98552010-09-22 22:36:29 -04003228 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003229 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003230 * @instance: Adapter soft state
3231 */
3232static inline void
3233megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3234{
3235 struct megasas_cmd *cmd;
3236 struct list_head clist_local;
3237 union megasas_evt_class_locale class_locale;
3238 unsigned long flags;
3239 u32 seq_num;
3240
3241 INIT_LIST_HEAD(&clist_local);
3242 spin_lock_irqsave(&instance->hba_lock, flags);
3243 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3244 spin_unlock_irqrestore(&instance->hba_lock, flags);
3245
3246 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003247 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003248 struct megasas_cmd, list);
3249 list_del_init(&cmd->list);
3250
3251 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003252 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3253 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003254 cmd, cmd->scmd, cmd->sync_cmd);
3255
3256 cmd->retry_for_fw_reset++;
3257
3258 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003259 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003260 "was tried multiple times during reset."
3261 "Shutting down the HBA\n",
3262 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303263 instance->instancet->disable_intr(instance);
3264 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003265 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003266 return;
3267 }
3268 }
3269
3270 if (cmd->sync_cmd == 1) {
3271 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003272 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003273 "cmd attached to internal command!\n");
3274 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003275 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003276 "on the internal reset queue,"
3277 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303278 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003279 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003280 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003281 0, instance->reg_set);
3282 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003283 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003284 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003285 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003286
3287 atomic_inc(&instance->fw_outstanding);
3288 instance->instancet->fire_cmd(instance,
3289 cmd->frame_phys_addr,
3290 cmd->frame_count-1, instance->reg_set);
3291 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003292 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003293 "internal reset defer list while re-issue!!\n",
3294 cmd);
3295 }
3296 }
3297
3298 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003299 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003300 megasas_return_cmd(instance, instance->aen_cmd);
3301
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003302 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003303 }
3304
3305 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003306 * Initiate AEN (Asynchronous Event Notification)
3307 */
bo yang39a98552010-09-22 22:36:29 -04003308 seq_num = instance->last_seq_num;
3309 class_locale.members.reserved = 0;
3310 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3311 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3312
3313 megasas_register_aen(instance, seq_num, class_locale.word);
3314}
3315
3316/**
3317 * Move the internal reset pending commands to a deferred queue.
3318 *
3319 * We move the commands pending at internal reset time to a
3320 * pending queue. This queue would be flushed after successful
3321 * completion of the internal reset sequence. if the internal reset
3322 * did not complete in time, the kernel reset handler would flush
3323 * these commands.
3324 **/
3325static void
3326megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3327{
3328 struct megasas_cmd *cmd;
3329 int i;
3330 u32 max_cmd = instance->max_fw_cmds;
3331 u32 defer_index;
3332 unsigned long flags;
3333
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003334 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303335 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003336 for (i = 0; i < max_cmd; i++) {
3337 cmd = instance->cmd_list[i];
3338 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003339 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003340 "on the defer queue as internal\n",
3341 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3342
3343 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003344 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003345 " moving this cmd:%p, %d %p, it was"
3346 "discovered on some list?\n",
3347 cmd, cmd->sync_cmd, cmd->scmd);
3348
3349 list_del_init(&cmd->list);
3350 }
3351 defer_index++;
3352 list_add_tail(&cmd->list,
3353 &instance->internal_reset_pending_q);
3354 }
3355 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303356 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003357}
3358
3359
3360static void
3361process_fw_state_change_wq(struct work_struct *work)
3362{
3363 struct megasas_instance *instance =
3364 container_of(work, struct megasas_instance, work_init);
3365 u32 wait;
3366 unsigned long flags;
3367
Sumit Saxena8a01a412016-01-28 21:04:32 +05303368 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003369 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303370 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003371 return ;
3372 }
3373
Sumit Saxena8a01a412016-01-28 21:04:32 +05303374 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003375 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003376 "state, restarting it...\n");
3377
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303378 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003379 atomic_set(&instance->fw_outstanding, 0);
3380
3381 atomic_set(&instance->fw_reset_no_pci_access, 1);
3382 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003383 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003384
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003385 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003386 "initiating next stage...\n");
3387
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003388 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003389 "state 2 starting...\n");
3390
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003391 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003392 for (wait = 0; wait < 30; wait++) {
3393 msleep(1000);
3394 }
3395
adam radford058a8fa2011-10-08 18:14:27 -07003396 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003397 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003398
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303399 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003400 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003401 return ;
3402 }
3403
3404 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3405 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3406 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3407 ) {
3408 *instance->consumer = *instance->producer;
3409 } else {
3410 *instance->consumer = 0;
3411 *instance->producer = 0;
3412 }
3413
3414 megasas_issue_init_mfi(instance);
3415
3416 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303417 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003418 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303419 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003420
3421 megasas_issue_pending_cmds_again(instance);
3422 instance->issuepend_done = 1;
3423 }
bo yang39a98552010-09-22 22:36:29 -04003424}
3425
3426/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003427 * megasas_deplete_reply_queue - Processes all completed commands
3428 * @instance: Adapter soft state
3429 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003430 * SCSI mid-layer instead of the status
3431 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003432 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003433 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003434static int
bo yang39a98552010-09-22 22:36:29 -04003435megasas_deplete_reply_queue(struct megasas_instance *instance,
3436 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003437{
bo yang39a98552010-09-22 22:36:29 -04003438 u32 mfiStatus;
3439 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003440
bo yang39a98552010-09-22 22:36:29 -04003441 if ((mfiStatus = instance->instancet->check_reset(instance,
3442 instance->reg_set)) == 1) {
3443 return IRQ_HANDLED;
3444 }
3445
3446 if ((mfiStatus = instance->instancet->clear_intr(
3447 instance->reg_set)
3448 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003449 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003450 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003451 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003452 }
3453
3454 instance->mfiStatus = mfiStatus;
3455
3456 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3457 fw_state = instance->instancet->read_fw_status_reg(
3458 instance->reg_set) & MFI_STATE_MASK;
3459
3460 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003461 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003462 fw_state);
3463 }
3464
3465 if ((fw_state == MFI_STATE_FAULT) &&
3466 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003467 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003468
3469 if ((instance->pdev->device ==
3470 PCI_DEVICE_ID_LSI_SAS1064R) ||
3471 (instance->pdev->device ==
3472 PCI_DEVICE_ID_DELL_PERC5) ||
3473 (instance->pdev->device ==
3474 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3475
3476 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303477 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003478 }
3479
3480
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303481 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303482 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003483 instance->issuepend_done = 0;
3484
3485 atomic_set(&instance->fw_outstanding, 0);
3486 megasas_internal_reset_defer_cmds(instance);
3487
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003488 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303489 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003490
3491 schedule_work(&instance->work_init);
3492 return IRQ_HANDLED;
3493
3494 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003495 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003496 fw_state, instance->disableOnlineCtrlReset);
3497 }
3498 }
3499
Sumant Patro5d018ad2006-10-03 13:13:18 -07003500 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003501 return IRQ_HANDLED;
3502}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003503/**
3504 * megasas_isr - isr entry point
3505 */
David Howells7d12e782006-10-05 14:55:46 +01003506static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003507{
adam radfordc8e858f2011-10-08 18:15:13 -07003508 struct megasas_irq_context *irq_context = devp;
3509 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003510 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003511 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003512
adam radfordc8e858f2011-10-08 18:15:13 -07003513 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003514 return IRQ_HANDLED;
3515
bo yang39a98552010-09-22 22:36:29 -04003516 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003517 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003518 spin_unlock_irqrestore(&instance->hba_lock, flags);
3519
3520 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003521}
3522
3523/**
3524 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003525 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003526 *
3527 * During the initialization, FW passes can potentially be in any one of
3528 * several possible states. If the FW in operational, waiting-for-handshake
3529 * states, driver must take steps to bring it to ready state. Otherwise, it
3530 * has to wait for the ready state.
3531 */
adam radford9c915a82010-12-21 13:34:31 -08003532int
adam radford058a8fa2011-10-08 18:14:27 -07003533megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003534{
3535 int i;
3536 u8 max_wait;
3537 u32 fw_state;
3538 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003539 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003540
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003541 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3542 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003543
Sumant Patroe3bbff92006-10-03 12:28:49 -07003544 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003545 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003546 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003547
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003548 while (fw_state != MFI_STATE_READY) {
3549
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003550 switch (fw_state) {
3551
3552 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003553 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003554 if (ocr) {
3555 max_wait = MEGASAS_RESET_WAIT_TIME;
3556 cur_state = MFI_STATE_FAULT;
3557 break;
3558 } else
3559 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003560
3561 case MFI_STATE_WAIT_HANDSHAKE:
3562 /*
3563 * Set the CLR bit in inbound doorbell
3564 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003565 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003566 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3567 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003568 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303569 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003570 writel(
3571 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003572 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303573 else
Yang, Bo87911122009-10-06 14:31:54 -06003574 writel(
3575 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3576 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003577
Yang, Bo7218df62009-10-06 14:52:20 -06003578 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003579 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3580 break;
3581
Sumant Patroe3bbff92006-10-03 12:28:49 -07003582 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003583 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003584 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3585 (instance->pdev->device ==
3586 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303587 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003588 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003589 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303590 else
Yang, Bo87911122009-10-06 14:31:54 -06003591 writel(MFI_INIT_HOTPLUG,
3592 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003593
Yang, Bo7218df62009-10-06 14:52:20 -06003594 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003595 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3596 break;
3597
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003598 case MFI_STATE_OPERATIONAL:
3599 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003600 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003601 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303602 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003603 if ((instance->pdev->device ==
3604 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3605 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003606 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303607 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003608 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003609 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303610
3611 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003612 for (i = 0; i < (10 * 1000); i += 20) {
3613 if (readl(
3614 &instance->
3615 reg_set->
3616 doorbell) & 1)
3617 msleep(20);
3618 else
3619 break;
3620 }
3621 }
Yang, Bo87911122009-10-06 14:31:54 -06003622 } else
3623 writel(MFI_RESET_FLAGS,
3624 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003625
Yang, Bo7218df62009-10-06 14:52:20 -06003626 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003627 cur_state = MFI_STATE_OPERATIONAL;
3628 break;
3629
3630 case MFI_STATE_UNDEFINED:
3631 /*
3632 * This state should not last for more than 2 seconds
3633 */
Yang, Bo7218df62009-10-06 14:52:20 -06003634 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003635 cur_state = MFI_STATE_UNDEFINED;
3636 break;
3637
3638 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003639 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003640 cur_state = MFI_STATE_BB_INIT;
3641 break;
3642
3643 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003644 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003645 cur_state = MFI_STATE_FW_INIT;
3646 break;
3647
3648 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003649 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003650 cur_state = MFI_STATE_FW_INIT_2;
3651 break;
3652
3653 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003654 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003655 cur_state = MFI_STATE_DEVICE_SCAN;
3656 break;
3657
3658 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003659 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003660 cur_state = MFI_STATE_FLUSH_CACHE;
3661 break;
3662
3663 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003664 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003665 fw_state);
3666 return -ENODEV;
3667 }
3668
3669 /*
3670 * The cur_state should not last for more than max_wait secs
3671 */
3672 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003673 curr_abs_state = instance->instancet->
3674 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003675
Yang, Bo7218df62009-10-06 14:52:20 -06003676 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003677 msleep(1);
3678 } else
3679 break;
3680 }
3681
3682 /*
3683 * Return error if fw_state hasn't changed after max_wait
3684 */
Yang, Bo7218df62009-10-06 14:52:20 -06003685 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003686 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003687 "in %d secs\n", fw_state, max_wait);
3688 return -ENODEV;
3689 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003690
3691 abs_state = curr_abs_state;
3692 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003693 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003694 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003695
3696 return 0;
3697}
3698
3699/**
3700 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3701 * @instance: Adapter soft state
3702 */
3703static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3704{
3705 int i;
adam radford9c915a82010-12-21 13:34:31 -08003706 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003707 struct megasas_cmd *cmd;
3708
3709 if (!instance->frame_dma_pool)
3710 return;
3711
3712 /*
3713 * Return all frames to pool
3714 */
3715 for (i = 0; i < max_cmd; i++) {
3716
3717 cmd = instance->cmd_list[i];
3718
3719 if (cmd->frame)
3720 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3721 cmd->frame_phys_addr);
3722
3723 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003724 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003725 cmd->sense_phys_addr);
3726 }
3727
3728 /*
3729 * Now destroy the pool itself
3730 */
3731 pci_pool_destroy(instance->frame_dma_pool);
3732 pci_pool_destroy(instance->sense_dma_pool);
3733
3734 instance->frame_dma_pool = NULL;
3735 instance->sense_dma_pool = NULL;
3736}
3737
3738/**
3739 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3740 * @instance: Adapter soft state
3741 *
3742 * Each command packet has an embedded DMA memory buffer that is used for
3743 * filling MFI frame and the SG list that immediately follows the frame. This
3744 * function creates those DMA memory buffers for each command packet by using
3745 * PCI pool facility.
3746 */
3747static int megasas_create_frame_pool(struct megasas_instance *instance)
3748{
3749 int i;
3750 u32 max_cmd;
3751 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003752 u32 total_sz;
3753 u32 frame_count;
3754 struct megasas_cmd *cmd;
3755
adam radford9c915a82010-12-21 13:34:31 -08003756 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003757
3758 /*
3759 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3760 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3761 */
3762 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3763 sizeof(struct megasas_sge32);
3764
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003765 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003766 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003767
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003768 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303769 * For MFI controllers.
3770 * max_num_sge = 60
3771 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3772 * Total 960 byte (15 MFI frame of 64 byte)
3773 *
3774 * Fusion adapter require only 3 extra frame.
3775 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3776 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3777 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003778 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303779 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003780 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3781 /*
3782 * Use DMA pool facility provided by PCI layer
3783 */
3784 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303785 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003786
3787 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003788 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003789 return -ENOMEM;
3790 }
3791
3792 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3793 instance->pdev, 128, 4, 0);
3794
3795 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003796 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003797
3798 pci_pool_destroy(instance->frame_dma_pool);
3799 instance->frame_dma_pool = NULL;
3800
3801 return -ENOMEM;
3802 }
3803
3804 /*
3805 * Allocate and attach a frame to each of the commands in cmd_list.
3806 * By making cmd->index as the context instead of the &cmd, we can
3807 * always use 32bit context regardless of the architecture
3808 */
3809 for (i = 0; i < max_cmd; i++) {
3810
3811 cmd = instance->cmd_list[i];
3812
3813 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3814 GFP_KERNEL, &cmd->frame_phys_addr);
3815
3816 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3817 GFP_KERNEL, &cmd->sense_phys_addr);
3818
3819 /*
3820 * megasas_teardown_frame_pool() takes care of freeing
3821 * whatever has been allocated
3822 */
3823 if (!cmd->frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003824 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003825 megasas_teardown_frame_pool(instance);
3826 return -ENOMEM;
3827 }
3828
Yang, Bo707e09b2010-10-12 07:20:27 -06003829 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303830 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003831 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303832 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003833 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003834 }
3835
3836 return 0;
3837}
3838
3839/**
3840 * megasas_free_cmds - Free all the cmds in the free cmd pool
3841 * @instance: Adapter soft state
3842 */
adam radford9c915a82010-12-21 13:34:31 -08003843void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003844{
3845 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003846
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003847 /* First free the MFI frame pool */
3848 megasas_teardown_frame_pool(instance);
3849
3850 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003851 for (i = 0; i < instance->max_mfi_cmds; i++)
3852
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003853 kfree(instance->cmd_list[i]);
3854
3855 /* Free the cmd_list buffer itself */
3856 kfree(instance->cmd_list);
3857 instance->cmd_list = NULL;
3858
3859 INIT_LIST_HEAD(&instance->cmd_pool);
3860}
3861
3862/**
3863 * megasas_alloc_cmds - Allocates the command packets
3864 * @instance: Adapter soft state
3865 *
3866 * Each command that is issued to the FW, whether IO commands from the OS or
3867 * internal commands like IOCTLs, are wrapped in local data structure called
3868 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3869 * the FW.
3870 *
3871 * Each frame has a 32-bit field called context (tag). This context is used
3872 * to get back the megasas_cmd from the frame when a frame gets completed in
3873 * the ISR. Typically the address of the megasas_cmd itself would be used as
3874 * the context. But we wanted to keep the differences between 32 and 64 bit
3875 * systems to the mininum. We always use 32 bit integers for the context. In
3876 * this driver, the 32 bit values are the indices into an array cmd_list.
3877 * This array is used only to look up the megasas_cmd given the context. The
3878 * free commands themselves are maintained in a linked list called cmd_pool.
3879 */
adam radford9c915a82010-12-21 13:34:31 -08003880int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003881{
3882 int i;
3883 int j;
3884 u32 max_cmd;
3885 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303886 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003887
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303888 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003889 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003890
3891 /*
3892 * instance->cmd_list is an array of struct megasas_cmd pointers.
3893 * Allocate the dynamic array first and then allocate individual
3894 * commands.
3895 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003896 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003897
3898 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003899 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003900 return -ENOMEM;
3901 }
3902
adam radford9c915a82010-12-21 13:34:31 -08003903 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003904
3905 for (i = 0; i < max_cmd; i++) {
3906 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3907 GFP_KERNEL);
3908
3909 if (!instance->cmd_list[i]) {
3910
3911 for (j = 0; j < i; j++)
3912 kfree(instance->cmd_list[j]);
3913
3914 kfree(instance->cmd_list);
3915 instance->cmd_list = NULL;
3916
3917 return -ENOMEM;
3918 }
3919 }
3920
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003921 for (i = 0; i < max_cmd; i++) {
3922 cmd = instance->cmd_list[i];
3923 memset(cmd, 0, sizeof(struct megasas_cmd));
3924 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003925 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003926 cmd->instance = instance;
3927
3928 list_add_tail(&cmd->list, &instance->cmd_pool);
3929 }
3930
3931 /*
3932 * Create a frame pool and assign one frame to each cmd
3933 */
3934 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003935 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003936 megasas_free_cmds(instance);
3937 }
3938
3939 return 0;
3940}
3941
Yang, Bo81e403c2009-10-06 14:27:54 -06003942/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303943 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
3944 * @instance: Adapter soft state
3945 *
3946 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
3947 * or FW is not under OCR.
3948 */
3949inline int
3950dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
3951
3952 if (!instance->ctrl_context)
3953 return KILL_ADAPTER;
3954 else if (instance->unload ||
3955 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
3956 return IGNORE_TIMEOUT;
3957 else
3958 return INITIATE_OCR;
3959}
3960
Sumit Saxena2216c302016-01-28 21:04:26 +05303961static int
3962megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
3963{
3964 int ret;
3965 struct megasas_cmd *cmd;
3966 struct megasas_dcmd_frame *dcmd;
3967
3968 cmd = megasas_get_cmd(instance);
3969
3970 if (!cmd) {
3971 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
3972 return -ENOMEM;
3973 }
3974
3975 dcmd = &cmd->frame->dcmd;
3976
3977 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
3978 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3979
3980 dcmd->mbox.s[0] = cpu_to_le16(device_id);
3981 dcmd->cmd = MFI_CMD_DCMD;
3982 dcmd->cmd_status = 0xFF;
3983 dcmd->sge_count = 1;
3984 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3985 dcmd->timeout = 0;
3986 dcmd->pad_0 = 0;
3987 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
3988 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
3989 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
3990 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
3991
3992 if (instance->ctrl_context && !instance->mask_interrupts)
3993 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
3994 else
3995 ret = megasas_issue_polled(instance, cmd);
3996
3997 switch (ret) {
3998 case DCMD_SUCCESS:
3999 instance->pd_list[device_id].interface =
4000 instance->pd_info->state.ddf.pdType.intf;
4001 break;
4002
4003 case DCMD_TIMEOUT:
4004
4005 switch (dcmd_timeout_ocr_possible(instance)) {
4006 case INITIATE_OCR:
4007 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4008 megasas_reset_fusion(instance->host,
4009 MFI_IO_TIMEOUT_OCR);
4010 break;
4011 case KILL_ADAPTER:
4012 megaraid_sas_kill_hba(instance);
4013 break;
4014 case IGNORE_TIMEOUT:
4015 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4016 __func__, __LINE__);
4017 break;
4018 }
4019
4020 break;
4021 }
4022
4023 if (ret != DCMD_TIMEOUT)
4024 megasas_return_cmd(instance, cmd);
4025
4026 return ret;
4027}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304028/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004029 * megasas_get_pd_list_info - Returns FW's pd_list structure
4030 * @instance: Adapter soft state
4031 * @pd_list: pd_list structure
4032 *
4033 * Issues an internal command (DCMD) to get the FW's controller PD
4034 * list structure. This information is mainly used to find out SYSTEM
4035 * supported by the FW.
4036 */
4037static int
4038megasas_get_pd_list(struct megasas_instance *instance)
4039{
4040 int ret = 0, pd_index = 0;
4041 struct megasas_cmd *cmd;
4042 struct megasas_dcmd_frame *dcmd;
4043 struct MR_PD_LIST *ci;
4044 struct MR_PD_ADDRESS *pd_addr;
4045 dma_addr_t ci_h = 0;
4046
4047 cmd = megasas_get_cmd(instance);
4048
4049 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004050 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004051 return -ENOMEM;
4052 }
4053
4054 dcmd = &cmd->frame->dcmd;
4055
4056 ci = pci_alloc_consistent(instance->pdev,
4057 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4058
4059 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004060 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004061 megasas_return_cmd(instance, cmd);
4062 return -ENOMEM;
4063 }
4064
4065 memset(ci, 0, sizeof(*ci));
4066 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4067
4068 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4069 dcmd->mbox.b[1] = 0;
4070 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304071 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004072 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304073 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004074 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004075 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304076 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4077 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4078 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4079 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004080
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304081 if (instance->ctrl_context && !instance->mask_interrupts)
4082 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304083 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304084 else
4085 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004086
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304087 switch (ret) {
4088 case DCMD_FAILED:
4089 megaraid_sas_kill_hba(instance);
4090 break;
4091 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004092
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304093 switch (dcmd_timeout_ocr_possible(instance)) {
4094 case INITIATE_OCR:
4095 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4096 /*
4097 * DCMD failed from AEN path.
4098 * AEN path already hold reset_mutex to avoid PCI access
4099 * while OCR is in progress.
4100 */
4101 mutex_unlock(&instance->reset_mutex);
4102 megasas_reset_fusion(instance->host,
4103 MFI_IO_TIMEOUT_OCR);
4104 mutex_lock(&instance->reset_mutex);
4105 break;
4106 case KILL_ADAPTER:
4107 megaraid_sas_kill_hba(instance);
4108 break;
4109 case IGNORE_TIMEOUT:
4110 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4111 __func__, __LINE__);
4112 break;
4113 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004114
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304115 break;
4116
4117 case DCMD_SUCCESS:
4118 pd_addr = ci->addr;
4119
4120 if ((le32_to_cpu(ci->count) >
4121 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4122 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004123
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304124 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304125 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004126
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304127 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304128 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304129 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304130 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304131 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304132 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304133 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004134 pd_addr++;
4135 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304136
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304137 memcpy(instance->pd_list, instance->local_pd_list,
4138 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304139 break;
4140
Yang, Bo81e403c2009-10-06 14:27:54 -06004141 }
4142
4143 pci_free_consistent(instance->pdev,
4144 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4145 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304146
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304147 if (ret != DCMD_TIMEOUT)
4148 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004149
4150 return ret;
4151}
4152
Yang, Bobdc6fb82009-12-06 08:30:19 -07004153/*
4154 * megasas_get_ld_list_info - Returns FW's ld_list structure
4155 * @instance: Adapter soft state
4156 * @ld_list: ld_list structure
4157 *
4158 * Issues an internal command (DCMD) to get the FW's controller PD
4159 * list structure. This information is mainly used to find out SYSTEM
4160 * supported by the FW.
4161 */
4162static int
4163megasas_get_ld_list(struct megasas_instance *instance)
4164{
4165 int ret = 0, ld_index = 0, ids = 0;
4166 struct megasas_cmd *cmd;
4167 struct megasas_dcmd_frame *dcmd;
4168 struct MR_LD_LIST *ci;
4169 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304170 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004171
4172 cmd = megasas_get_cmd(instance);
4173
4174 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004175 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004176 return -ENOMEM;
4177 }
4178
4179 dcmd = &cmd->frame->dcmd;
4180
4181 ci = pci_alloc_consistent(instance->pdev,
4182 sizeof(struct MR_LD_LIST),
4183 &ci_h);
4184
4185 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004186 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004187 megasas_return_cmd(instance, cmd);
4188 return -ENOMEM;
4189 }
4190
4191 memset(ci, 0, sizeof(*ci));
4192 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4193
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304194 if (instance->supportmax256vd)
4195 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004196 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304197 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004198 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304199 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004200 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304201 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4202 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4203 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4204 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004205 dcmd->pad_0 = 0;
4206
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304207 if (instance->ctrl_context && !instance->mask_interrupts)
4208 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304209 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304210 else
4211 ret = megasas_issue_polled(instance, cmd);
4212
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304213 ld_count = le32_to_cpu(ci->ldCount);
4214
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304215 switch (ret) {
4216 case DCMD_FAILED:
4217 megaraid_sas_kill_hba(instance);
4218 break;
4219 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004220
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304221 switch (dcmd_timeout_ocr_possible(instance)) {
4222 case INITIATE_OCR:
4223 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4224 /*
4225 * DCMD failed from AEN path.
4226 * AEN path already hold reset_mutex to avoid PCI access
4227 * while OCR is in progress.
4228 */
4229 mutex_unlock(&instance->reset_mutex);
4230 megasas_reset_fusion(instance->host,
4231 MFI_IO_TIMEOUT_OCR);
4232 mutex_lock(&instance->reset_mutex);
4233 break;
4234 case KILL_ADAPTER:
4235 megaraid_sas_kill_hba(instance);
4236 break;
4237 case IGNORE_TIMEOUT:
4238 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4239 __func__, __LINE__);
4240 break;
4241 }
4242
4243 break;
4244
4245 case DCMD_SUCCESS:
4246 if (ld_count > instance->fw_supported_vd_count)
4247 break;
4248
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304249 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004250
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304251 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004252 if (ci->ldList[ld_index].state != 0) {
4253 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304254 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004255 }
4256 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304257
4258 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004259 }
4260
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304261 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004262
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304263 if (ret != DCMD_TIMEOUT)
4264 megasas_return_cmd(instance, cmd);
4265
Yang, Bobdc6fb82009-12-06 08:30:19 -07004266 return ret;
4267}
4268
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004269/**
adam radford21c9e162013-09-06 15:27:14 -07004270 * megasas_ld_list_query - Returns FW's ld_list structure
4271 * @instance: Adapter soft state
4272 * @ld_list: ld_list structure
4273 *
4274 * Issues an internal command (DCMD) to get the FW's controller PD
4275 * list structure. This information is mainly used to find out SYSTEM
4276 * supported by the FW.
4277 */
4278static int
4279megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4280{
4281 int ret = 0, ld_index = 0, ids = 0;
4282 struct megasas_cmd *cmd;
4283 struct megasas_dcmd_frame *dcmd;
4284 struct MR_LD_TARGETID_LIST *ci;
4285 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304286 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004287
4288 cmd = megasas_get_cmd(instance);
4289
4290 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004291 dev_warn(&instance->pdev->dev,
4292 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004293 return -ENOMEM;
4294 }
4295
4296 dcmd = &cmd->frame->dcmd;
4297
4298 ci = pci_alloc_consistent(instance->pdev,
4299 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4300
4301 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004302 dev_warn(&instance->pdev->dev,
4303 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004304 megasas_return_cmd(instance, cmd);
4305 return -ENOMEM;
4306 }
4307
4308 memset(ci, 0, sizeof(*ci));
4309 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4310
4311 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304312 if (instance->supportmax256vd)
4313 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004314
4315 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304316 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004317 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304318 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004319 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304320 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4321 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4322 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4323 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004324 dcmd->pad_0 = 0;
4325
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304326 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304327 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304328 else
4329 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004330
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304331 switch (ret) {
4332 case DCMD_FAILED:
4333 dev_info(&instance->pdev->dev,
4334 "DCMD not supported by firmware - %s %d\n",
4335 __func__, __LINE__);
4336 ret = megasas_get_ld_list(instance);
4337 break;
4338 case DCMD_TIMEOUT:
4339 switch (dcmd_timeout_ocr_possible(instance)) {
4340 case INITIATE_OCR:
4341 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4342 /*
4343 * DCMD failed from AEN path.
4344 * AEN path already hold reset_mutex to avoid PCI access
4345 * while OCR is in progress.
4346 */
4347 mutex_unlock(&instance->reset_mutex);
4348 megasas_reset_fusion(instance->host,
4349 MFI_IO_TIMEOUT_OCR);
4350 mutex_lock(&instance->reset_mutex);
4351 break;
4352 case KILL_ADAPTER:
4353 megaraid_sas_kill_hba(instance);
4354 break;
4355 case IGNORE_TIMEOUT:
4356 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4357 __func__, __LINE__);
4358 break;
4359 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304360
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304361 break;
4362 case DCMD_SUCCESS:
4363 tgtid_count = le32_to_cpu(ci->count);
4364
4365 if ((tgtid_count > (instance->fw_supported_vd_count)))
4366 break;
4367
adam radford21c9e162013-09-06 15:27:14 -07004368 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304369 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004370 ids = ci->targetId[ld_index];
4371 instance->ld_ids[ids] = ci->targetId[ld_index];
4372 }
4373
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304374 break;
adam radford21c9e162013-09-06 15:27:14 -07004375 }
4376
4377 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304378 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004379
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304380 if (ret != DCMD_TIMEOUT)
4381 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004382
4383 return ret;
4384}
4385
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304386/*
4387 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4388 * instance : Controller's instance
4389*/
4390static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4391{
4392 struct fusion_context *fusion;
4393 u32 old_map_sz;
4394 u32 new_map_sz;
4395
4396 fusion = instance->ctrl_context;
4397 /* For MFI based controllers return dummy success */
4398 if (!fusion)
4399 return;
4400
4401 instance->supportmax256vd =
4402 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4403 /* Below is additional check to address future FW enhancement */
4404 if (instance->ctrl_info->max_lds > 64)
4405 instance->supportmax256vd = 1;
4406
4407 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4408 * MEGASAS_MAX_DEV_PER_CHANNEL;
4409 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4410 * MEGASAS_MAX_DEV_PER_CHANNEL;
4411 if (instance->supportmax256vd) {
4412 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4413 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4414 } else {
4415 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4416 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4417 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304418
4419 dev_info(&instance->pdev->dev,
4420 "firmware type\t: %s\n",
4421 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4422 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304423
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004424 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304425 (sizeof(struct MR_LD_SPAN_MAP) *
4426 (instance->fw_supported_vd_count - 1));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004427 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4428 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304429 (sizeof(struct MR_LD_SPAN_MAP) *
4430 (instance->drv_supported_vd_count - 1));
4431
4432 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4433
4434
4435 if (instance->supportmax256vd)
4436 fusion->current_map_sz = new_map_sz;
4437 else
4438 fusion->current_map_sz = old_map_sz;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304439}
4440
adam radford21c9e162013-09-06 15:27:14 -07004441/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004442 * megasas_get_controller_info - Returns FW's controller structure
4443 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004444 *
4445 * Issues an internal command (DCMD) to get the FW's controller structure.
4446 * This information is mainly used to find out the maximum IO transfer per
4447 * command supported by the FW.
4448 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304449int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304450megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004451{
4452 int ret = 0;
4453 struct megasas_cmd *cmd;
4454 struct megasas_dcmd_frame *dcmd;
4455 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304456 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004457 dma_addr_t ci_h = 0;
4458
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304459 ctrl_info = instance->ctrl_info;
4460
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004461 cmd = megasas_get_cmd(instance);
4462
4463 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004464 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004465 return -ENOMEM;
4466 }
4467
4468 dcmd = &cmd->frame->dcmd;
4469
4470 ci = pci_alloc_consistent(instance->pdev,
4471 sizeof(struct megasas_ctrl_info), &ci_h);
4472
4473 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004474 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004475 megasas_return_cmd(instance, cmd);
4476 return -ENOMEM;
4477 }
4478
4479 memset(ci, 0, sizeof(*ci));
4480 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4481
4482 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304483 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004484 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304485 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004486 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004487 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304488 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4489 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4490 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4491 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304492 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004493
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304494 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304495 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304496 else
4497 ret = megasas_issue_polled(instance, cmd);
4498
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304499 switch (ret) {
4500 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004501 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304502 /* Save required controller information in
4503 * CPU endianness format.
4504 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304505 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4506 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4507 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304508
4509 /* Update the latest Ext VD info.
4510 * From Init path, store current firmware details.
4511 * From OCR path, detect any firmware properties changes.
4512 * in case of Firmware upgrade without system reboot.
4513 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304514 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304515 instance->use_seqnum_jbod_fp =
4516 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304517
4518 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304519 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4520 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304521 "controller type\t: %s(%dMB)\n",
4522 instance->is_imr ? "iMR" : "MR",
4523 le16_to_cpu(ctrl_info->memory_size));
4524
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304525 instance->disableOnlineCtrlReset =
4526 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304527 instance->secure_jbod_support =
4528 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304529 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4530 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304531 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4532 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304533 break;
4534
4535 case DCMD_TIMEOUT:
4536 switch (dcmd_timeout_ocr_possible(instance)) {
4537 case INITIATE_OCR:
4538 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4539 megasas_reset_fusion(instance->host,
4540 MFI_IO_TIMEOUT_OCR);
4541 break;
4542 case KILL_ADAPTER:
4543 megaraid_sas_kill_hba(instance);
4544 break;
4545 case IGNORE_TIMEOUT:
4546 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4547 __func__, __LINE__);
4548 break;
4549 }
4550 case DCMD_FAILED:
4551 megaraid_sas_kill_hba(instance);
4552 break;
4553
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304554 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004555
4556 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4557 ci, ci_h);
4558
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304559 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304560
4561
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004562 return ret;
4563}
4564
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304565/*
4566 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4567 * to firmware
4568 *
4569 * @instance: Adapter soft state
4570 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4571 MR_CRASH_BUF_TURN_OFF = 0
4572 MR_CRASH_BUF_TURN_ON = 1
4573 * @return 0 on success non-zero on failure.
4574 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4575 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4576 * that driver supports crash dump feature. This DCMD will be sent only if
4577 * crash dump feature is supported by the FW.
4578 *
4579 */
4580int megasas_set_crash_dump_params(struct megasas_instance *instance,
4581 u8 crash_buf_state)
4582{
4583 int ret = 0;
4584 struct megasas_cmd *cmd;
4585 struct megasas_dcmd_frame *dcmd;
4586
4587 cmd = megasas_get_cmd(instance);
4588
4589 if (!cmd) {
4590 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4591 return -ENOMEM;
4592 }
4593
4594
4595 dcmd = &cmd->frame->dcmd;
4596
4597 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4598 dcmd->mbox.b[0] = crash_buf_state;
4599 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304600 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304601 dcmd->sge_count = 1;
4602 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4603 dcmd->timeout = 0;
4604 dcmd->pad_0 = 0;
4605 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4606 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4607 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4608 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4609
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304610 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304611 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304612 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304613 ret = megasas_issue_polled(instance, cmd);
4614
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304615 if (ret == DCMD_TIMEOUT) {
4616 switch (dcmd_timeout_ocr_possible(instance)) {
4617 case INITIATE_OCR:
4618 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4619 megasas_reset_fusion(instance->host,
4620 MFI_IO_TIMEOUT_OCR);
4621 break;
4622 case KILL_ADAPTER:
4623 megaraid_sas_kill_hba(instance);
4624 break;
4625 case IGNORE_TIMEOUT:
4626 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4627 __func__, __LINE__);
4628 break;
4629 }
4630 } else
4631 megasas_return_cmd(instance, cmd);
4632
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304633 return ret;
4634}
4635
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004636/**
bo yang31ea7082007-11-07 12:09:50 -05004637 * megasas_issue_init_mfi - Initializes the FW
4638 * @instance: Adapter soft state
4639 *
4640 * Issues the INIT MFI cmd
4641 */
4642static int
4643megasas_issue_init_mfi(struct megasas_instance *instance)
4644{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304645 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004646 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004647 struct megasas_init_frame *init_frame;
4648 struct megasas_init_queue_info *initq_info;
4649 dma_addr_t init_frame_h;
4650 dma_addr_t initq_info_h;
4651
4652 /*
4653 * Prepare a init frame. Note the init frame points to queue info
4654 * structure. Each frame has SGL allocated after first 64 bytes. For
4655 * this frame - since we don't need any SGL - we use SGL's space as
4656 * queue info structure
4657 *
4658 * We will not get a NULL command below. We just created the pool.
4659 */
4660 cmd = megasas_get_cmd(instance);
4661
4662 init_frame = (struct megasas_init_frame *)cmd->frame;
4663 initq_info = (struct megasas_init_queue_info *)
4664 ((unsigned long)init_frame + 64);
4665
4666 init_frame_h = cmd->frame_phys_addr;
4667 initq_info_h = init_frame_h + 64;
4668
4669 context = init_frame->context;
4670 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4671 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4672 init_frame->context = context;
4673
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304674 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4675 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004676
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304677 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4678 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004679
4680 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304681 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304682 init_frame->queue_info_new_phys_addr_lo =
4683 cpu_to_le32(lower_32_bits(initq_info_h));
4684 init_frame->queue_info_new_phys_addr_hi =
4685 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004686
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304687 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004688
4689 /*
4690 * disable the intr before firing the init frame to FW
4691 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304692 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004693
4694 /*
4695 * Issue the init frame in polled mode
4696 */
4697
4698 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004699 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004700 megasas_return_cmd(instance, cmd);
4701 goto fail_fw_init;
4702 }
4703
4704 megasas_return_cmd(instance, cmd);
4705
4706 return 0;
4707
4708fail_fw_init:
4709 return -EINVAL;
4710}
4711
adam radfordcd50ba82010-12-21 10:23:23 -08004712static u32
4713megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004714{
adam radfordcd50ba82010-12-21 10:23:23 -08004715 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004716 u32 context_sz;
4717 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004718
4719 reg_set = instance->reg_set;
4720
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004721 /*
4722 * Get various operational parameters from status register
4723 */
Sumant Patro1341c932006-01-25 12:02:40 -08004724 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004725 /*
4726 * Reduce the max supported cmds by 1. This is to ensure that the
4727 * reply_q_sz (1 more than the max cmd that driver may send)
4728 * does not exceed max cmds that the FW can support
4729 */
4730 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004731 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004732 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004733 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004734 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304735 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4736 * are reserved for IOCTL + driver's internal DCMDs.
4737 */
4738 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4739 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4740 instance->max_scsi_cmds = (instance->max_fw_cmds -
4741 MEGASAS_SKINNY_INT_CMDS);
4742 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4743 } else {
4744 instance->max_scsi_cmds = (instance->max_fw_cmds -
4745 MEGASAS_INT_CMDS);
4746 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4747 }
4748
Sumit Saxena308ec452016-01-28 21:04:30 +05304749 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304750 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004751 * Create a pool of commands
4752 */
4753 if (megasas_alloc_cmds(instance))
4754 goto fail_alloc_cmds;
4755
4756 /*
4757 * Allocate memory for reply queue. Length of reply queue should
4758 * be _one_ more than the maximum commands handled by the firmware.
4759 *
4760 * Note: When FW completes commands, it places corresponding contex
4761 * values in this circular reply queue. This circular queue is a fairly
4762 * typical producer-consumer queue. FW is the producer (of completed
4763 * commands) and the driver is the consumer.
4764 */
4765 context_sz = sizeof(u32);
4766 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4767
4768 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4769 reply_q_sz,
4770 &instance->reply_queue_h);
4771
4772 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004773 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004774 goto fail_reply_queue;
4775 }
4776
bo yang31ea7082007-11-07 12:09:50 -05004777 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004778 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004779
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304780 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304781 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4782 "Fail from %s %d\n", instance->unique_id,
4783 __func__, __LINE__);
4784 goto fail_fw_init;
4785 }
4786
bo yang39a98552010-09-22 22:36:29 -04004787 instance->fw_support_ieee = 0;
4788 instance->fw_support_ieee =
4789 (instance->instancet->read_fw_status_reg(reg_set) &
4790 0x04000000);
4791
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004792 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004793 instance->fw_support_ieee);
4794
4795 if (instance->fw_support_ieee)
4796 instance->flag_ieee = 1;
4797
adam radfordcd50ba82010-12-21 10:23:23 -08004798 return 0;
4799
4800fail_fw_init:
4801
4802 pci_free_consistent(instance->pdev, reply_q_sz,
4803 instance->reply_queue, instance->reply_queue_h);
4804fail_reply_queue:
4805 megasas_free_cmds(instance);
4806
4807fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004808 return 1;
4809}
4810
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304811/*
4812 * megasas_setup_irqs_msix - register legacy interrupts.
4813 * @instance: Adapter soft state
4814 *
4815 * Do not enable interrupt, only setup ISRs.
4816 *
4817 * Return 0 on success.
4818 */
4819static int
4820megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4821{
4822 struct pci_dev *pdev;
4823
4824 pdev = instance->pdev;
4825 instance->irq_context[0].instance = instance;
4826 instance->irq_context[0].MSIxIndex = 0;
4827 if (request_irq(pdev->irq, instance->instancet->service_isr,
4828 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4829 dev_err(&instance->pdev->dev,
4830 "Failed to register IRQ from %s %d\n",
4831 __func__, __LINE__);
4832 return -1;
4833 }
4834 return 0;
4835}
4836
4837/**
4838 * megasas_setup_irqs_msix - register MSI-x interrupts.
4839 * @instance: Adapter soft state
4840 * @is_probe: Driver probe check
4841 *
4842 * Do not enable interrupt, only setup ISRs.
4843 *
4844 * Return 0 on success.
4845 */
4846static int
4847megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4848{
4849 int i, j, cpu;
4850 struct pci_dev *pdev;
4851
4852 pdev = instance->pdev;
4853
4854 /* Try MSI-x */
4855 cpu = cpumask_first(cpu_online_mask);
4856 for (i = 0; i < instance->msix_vectors; i++) {
4857 instance->irq_context[i].instance = instance;
4858 instance->irq_context[i].MSIxIndex = i;
4859 if (request_irq(instance->msixentry[i].vector,
4860 instance->instancet->service_isr, 0, "megasas",
4861 &instance->irq_context[i])) {
4862 dev_err(&instance->pdev->dev,
4863 "Failed to register IRQ for vector %d.\n", i);
4864 for (j = 0; j < i; j++) {
4865 if (smp_affinity_enable)
4866 irq_set_affinity_hint(
4867 instance->msixentry[j].vector, NULL);
4868 free_irq(instance->msixentry[j].vector,
4869 &instance->irq_context[j]);
4870 }
4871 /* Retry irq register for IO_APIC*/
4872 instance->msix_vectors = 0;
4873 if (is_probe)
4874 return megasas_setup_irqs_ioapic(instance);
4875 else
4876 return -1;
4877 }
4878 if (smp_affinity_enable) {
4879 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4880 get_cpu_mask(cpu)))
4881 dev_err(&instance->pdev->dev,
4882 "Failed to set affinity hint"
4883 " for cpu %d\n", cpu);
4884 cpu = cpumask_next(cpu, cpu_online_mask);
4885 }
4886 }
4887 return 0;
4888}
4889
4890/*
4891 * megasas_destroy_irqs- unregister interrupts.
4892 * @instance: Adapter soft state
4893 * return: void
4894 */
4895static void
4896megasas_destroy_irqs(struct megasas_instance *instance) {
4897
4898 int i;
4899
4900 if (instance->msix_vectors)
4901 for (i = 0; i < instance->msix_vectors; i++) {
4902 if (smp_affinity_enable)
4903 irq_set_affinity_hint(
4904 instance->msixentry[i].vector, NULL);
4905 free_irq(instance->msixentry[i].vector,
4906 &instance->irq_context[i]);
4907 }
4908 else
4909 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4910}
4911
adam radfordcd50ba82010-12-21 10:23:23 -08004912/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304913 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4914 * @instance: Adapter soft state
4915 * @is_probe: Driver probe check
4916 *
4917 * Return 0 on success.
4918 */
4919void
4920megasas_setup_jbod_map(struct megasas_instance *instance)
4921{
4922 int i;
4923 struct fusion_context *fusion = instance->ctrl_context;
4924 u32 pd_seq_map_sz;
4925
4926 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4927 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4928
4929 if (reset_devices || !fusion ||
4930 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4931 dev_info(&instance->pdev->dev,
4932 "Jbod map is not supported %s %d\n",
4933 __func__, __LINE__);
4934 instance->use_seqnum_jbod_fp = false;
4935 return;
4936 }
4937
4938 if (fusion->pd_seq_sync[0])
4939 goto skip_alloc;
4940
4941 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4942 fusion->pd_seq_sync[i] = dma_alloc_coherent
4943 (&instance->pdev->dev, pd_seq_map_sz,
4944 &fusion->pd_seq_phys[i], GFP_KERNEL);
4945 if (!fusion->pd_seq_sync[i]) {
4946 dev_err(&instance->pdev->dev,
4947 "Failed to allocate memory from %s %d\n",
4948 __func__, __LINE__);
4949 if (i == 1) {
4950 dma_free_coherent(&instance->pdev->dev,
4951 pd_seq_map_sz, fusion->pd_seq_sync[0],
4952 fusion->pd_seq_phys[0]);
4953 fusion->pd_seq_sync[0] = NULL;
4954 }
4955 instance->use_seqnum_jbod_fp = false;
4956 return;
4957 }
4958 }
4959
4960skip_alloc:
4961 if (!megasas_sync_pd_seq_num(instance, false) &&
4962 !megasas_sync_pd_seq_num(instance, true))
4963 instance->use_seqnum_jbod_fp = true;
4964 else
4965 instance->use_seqnum_jbod_fp = false;
4966}
4967
4968/**
adam radfordcd50ba82010-12-21 10:23:23 -08004969 * megasas_init_fw - Initializes the FW
4970 * @instance: Adapter soft state
4971 *
4972 * This is the main function for initializing firmware
4973 */
4974
4975static int megasas_init_fw(struct megasas_instance *instance)
4976{
4977 u32 max_sectors_1;
4978 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304979 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04004980 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08004981 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304982 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08004983 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304984 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07004985 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05304986 struct fusion_context *fusion;
4987
4988 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08004989
4990 /* Find first memory bar */
4991 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4992 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
adam radfordcd50ba82010-12-21 10:23:23 -08004993 if (pci_request_selected_regions(instance->pdev, instance->bar,
4994 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004995 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08004996 return -EBUSY;
4997 }
4998
Ben Collins11f8a7b2013-09-13 12:46:44 -04004999 base_addr = pci_resource_start(instance->pdev, instance->bar);
5000 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005001
5002 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005003 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005004 goto fail_ioremap;
5005 }
5006
5007 reg_set = instance->reg_set;
5008
5009 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08005010 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005011 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005012 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305013 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305014 case PCI_DEVICE_ID_LSI_INTRUDER:
5015 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305016 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5017 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005018 instance->instancet = &megasas_instance_template_fusion;
5019 break;
adam radfordcd50ba82010-12-21 10:23:23 -08005020 case PCI_DEVICE_ID_LSI_SAS1078R:
5021 case PCI_DEVICE_ID_LSI_SAS1078DE:
5022 instance->instancet = &megasas_instance_template_ppc;
5023 break;
5024 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5025 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5026 instance->instancet = &megasas_instance_template_gen2;
5027 break;
5028 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5029 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5030 instance->instancet = &megasas_instance_template_skinny;
5031 break;
5032 case PCI_DEVICE_ID_LSI_SAS1064R:
5033 case PCI_DEVICE_ID_DELL_PERC5:
5034 default:
5035 instance->instancet = &megasas_instance_template_xscale;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05305036 instance->allow_fw_scan = 1;
adam radfordcd50ba82010-12-21 10:23:23 -08005037 break;
5038 }
5039
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305040 if (megasas_transition_to_ready(instance, 0)) {
5041 atomic_set(&instance->fw_reset_no_pci_access, 1);
5042 instance->instancet->adp_reset
5043 (instance, instance->reg_set);
5044 atomic_set(&instance->fw_reset_no_pci_access, 0);
5045 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005046 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305047 __func__);
5048
5049 /*waitting for about 30 second before retry*/
5050 ssleep(30);
5051
5052 if (megasas_transition_to_ready(instance, 0))
5053 goto fail_ready_state;
5054 }
adam radfordcd50ba82010-12-21 10:23:23 -08005055
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305056 /*
5057 * MSI-X host index 0 is common for all adapter.
5058 * It is used for all MPT based Adapters.
5059 */
5060 instance->reply_post_host_index_addr[0] =
Christoph Hellwig8a232bb2015-04-23 16:32:39 +05305061 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305062 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5063
adam radford3f1abce2011-05-11 18:33:47 -07005064 /* Check if MSI-X is supported while in ready state */
5065 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5066 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005067 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305068 scratch_pad_2 = readl
5069 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005070 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305071 if (fusion) {
5072 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5073 instance->msix_vectors = (scratch_pad_2
5074 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5075 fw_msix_count = instance->msix_vectors;
5076 } else { /* Invader series supports more than 8 MSI-x vectors*/
5077 instance->msix_vectors = ((scratch_pad_2
5078 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5079 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sumit Saxena179ac142016-01-28 21:04:28 +05305080 if (rdpq_enable)
5081 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5082 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305083 fw_msix_count = instance->msix_vectors;
5084 /* Save 1-15 reply post index address to local memory
5085 * Index 0 is already saved from reg offset
5086 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5087 */
5088 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5089 instance->reply_post_host_index_addr[loop] =
5090 (u32 __iomem *)
5091 ((u8 __iomem *)instance->reg_set +
5092 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5093 + (loop * 0x10));
5094 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305095 }
5096 if (msix_vectors)
5097 instance->msix_vectors = min(msix_vectors,
5098 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305099 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005100 instance->msix_vectors = 1;
5101 /* Don't bother allocating more MSI-X vectors than cpus */
5102 instance->msix_vectors = min(instance->msix_vectors,
5103 (unsigned int)num_online_cpus());
5104 for (i = 0; i < instance->msix_vectors; i++)
5105 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005106 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
5107 1, instance->msix_vectors);
Jiang Liuc12de882014-11-03 20:44:20 +08005108 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005109 instance->msix_vectors = i;
5110 else
adam radfordc8e858f2011-10-08 18:15:13 -07005111 instance->msix_vectors = 0;
5112 }
adam radford3f1abce2011-05-11 18:33:47 -07005113
Tomas Henzl258c3af2015-06-02 16:09:46 +05305114 dev_info(&instance->pdev->dev,
5115 "firmware supports msix\t: (%d)", fw_msix_count);
5116 dev_info(&instance->pdev->dev,
5117 "current msix/online cpus\t: (%d/%d)\n",
5118 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305119 dev_info(&instance->pdev->dev,
5120 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305121
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305122 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5123 (unsigned long)instance);
5124
Tomas Henzl258c3af2015-06-02 16:09:46 +05305125 if (instance->msix_vectors ?
5126 megasas_setup_irqs_msix(instance, 1) :
5127 megasas_setup_irqs_ioapic(instance))
5128 goto fail_setup_irqs;
5129
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305130 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5131 GFP_KERNEL);
5132 if (instance->ctrl_info == NULL)
5133 goto fail_init_adapter;
5134
5135 /*
5136 * Below are default value for legacy Firmware.
5137 * non-fusion based controllers
5138 */
5139 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5140 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005141 /* Get operational params, sge flags, send init cmd to controller */
5142 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005143 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005144
Tomas Henzl258c3af2015-06-02 16:09:46 +05305145
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305146 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005147
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005148 dev_err(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005149
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305150 megasas_setup_jbod_map(instance);
5151
bo yang39a98552010-09-22 22:36:29 -04005152 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005153 * the following function will get the PD LIST.
5154 */
5155 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005156 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005157 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005158 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305159 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005160 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005161
Yang, Bobdc6fb82009-12-06 08:30:19 -07005162 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07005163 if (megasas_ld_list_query(instance,
5164 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5165 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07005166
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005167 /*
5168 * Compute the max allowed sectors per IO: The controller info has two
5169 * limits on max sectors. Driver should use the minimum of these two.
5170 *
5171 * 1 << stripe_sz_ops.min = max sectors per strip
5172 *
5173 * Note that older firmwares ( < FW ver 30) didn't report information
5174 * to calculate max_sectors_1. So the number ended up as zero always.
5175 */
bo yang14faea92007-11-09 04:14:00 -05005176 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305177 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005178
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305179 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5180 le16_to_cpu(ctrl_info->max_strips_per_io);
5181 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005182
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005183 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305184
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305185 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5186 instance->passive = ctrl_info->cluster.passive;
5187 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305188 instance->UnevenSpanSupport =
5189 ctrl_info->adapterOperations2.supportUnevenSpans;
5190 if (instance->UnevenSpanSupport) {
5191 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305192 if (MR_ValidateMapInfo(instance))
5193 fusion->fast_path_io = 1;
5194 else
5195 fusion->fast_path_io = 0;
5196
5197 }
5198 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305199 instance->requestorId = ctrl_info->iov.requestorId;
5200 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5201 if (!ctrl_info->adapterOperations2.activePassive)
5202 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305203
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305204 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5205 instance->PlasmaFW111 ? "1.11" : "new");
5206
5207 if (instance->PlasmaFW111) {
5208 iovPtr = (struct IOV_111 *)
5209 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5210 instance->requestorId = iovPtr->requestorId;
5211 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305212 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305213 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5214 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305215 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305216
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305217 instance->crash_dump_fw_support =
5218 ctrl_info->adapterOperations3.supportCrashDump;
5219 instance->crash_dump_drv_support =
5220 (instance->crash_dump_fw_support &&
5221 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305222 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305223 megasas_set_crash_dump_params(instance,
5224 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005225
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305226 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305227 if (instance->crash_dump_buf)
5228 pci_free_consistent(instance->pdev,
5229 CRASH_DMA_BUF_SIZE,
5230 instance->crash_dump_buf,
5231 instance->crash_dump_h);
5232 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005233 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305234
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305235
5236 dev_info(&instance->pdev->dev,
5237 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5238 le16_to_cpu(ctrl_info->pci.vendor_id),
5239 le16_to_cpu(ctrl_info->pci.device_id),
5240 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5241 le16_to_cpu(ctrl_info->pci.sub_device_id));
5242 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5243 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305244 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5245 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305246 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5247 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305248
5249
bo yang14faea92007-11-09 04:14:00 -05005250 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305251 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005252 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5253 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005254
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305255 /* Check for valid throttlequeuedepth module parameter */
5256 if (throttlequeuedepth &&
5257 throttlequeuedepth <= instance->max_scsi_cmds)
5258 instance->throttlequeuedepth = throttlequeuedepth;
5259 else
5260 instance->throttlequeuedepth =
5261 MEGASAS_THROTTLE_QUEUE_DEPTH;
5262
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305263 if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
5264 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5265
5266 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5267 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005268
adam radford229fe472014-03-10 02:51:56 -07005269 /* Launch SR-IOV heartbeat timer */
5270 if (instance->requestorId) {
5271 if (!megasas_sriov_start_heartbeat(instance, 1))
5272 megasas_start_timer(instance,
5273 &instance->sriov_heartbeat_timer,
5274 megasas_sriov_heartbeat_handler,
5275 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5276 else
5277 instance->skip_heartbeat_timer_del = 1;
5278 }
5279
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005280 return 0;
5281
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305282fail_get_pd_list:
5283 instance->instancet->disable_intr(instance);
adam radfordeb1b1232011-02-24 20:55:56 -08005284fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305285 megasas_destroy_irqs(instance);
5286fail_setup_irqs:
5287 if (instance->msix_vectors)
5288 pci_disable_msix(instance->pdev);
5289 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005290fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305291 kfree(instance->ctrl_info);
5292 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005293 iounmap(instance->reg_set);
5294
5295 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08005296 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005297
5298 return -EINVAL;
5299}
5300
5301/**
5302 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005303 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005304 */
5305static void megasas_release_mfi(struct megasas_instance *instance)
5306{
adam radford9c915a82010-12-21 13:34:31 -08005307 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005308
adam radford9c915a82010-12-21 13:34:31 -08005309 if (instance->reply_queue)
5310 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005311 instance->reply_queue, instance->reply_queue_h);
5312
5313 megasas_free_cmds(instance);
5314
5315 iounmap(instance->reg_set);
5316
adam radfordb6d5d882010-12-14 18:56:07 -08005317 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005318}
5319
5320/**
5321 * megasas_get_seq_num - Gets latest event sequence numbers
5322 * @instance: Adapter soft state
5323 * @eli: FW event log sequence numbers information
5324 *
5325 * FW maintains a log of all events in a non-volatile area. Upper layers would
5326 * usually find out the latest sequence number of the events, the seq number at
5327 * the boot etc. They would "read" all the events below the latest seq number
5328 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5329 * number), they would subsribe to AEN (asynchronous event notification) and
5330 * wait for the events to happen.
5331 */
5332static int
5333megasas_get_seq_num(struct megasas_instance *instance,
5334 struct megasas_evt_log_info *eli)
5335{
5336 struct megasas_cmd *cmd;
5337 struct megasas_dcmd_frame *dcmd;
5338 struct megasas_evt_log_info *el_info;
5339 dma_addr_t el_info_h = 0;
5340
5341 cmd = megasas_get_cmd(instance);
5342
5343 if (!cmd) {
5344 return -ENOMEM;
5345 }
5346
5347 dcmd = &cmd->frame->dcmd;
5348 el_info = pci_alloc_consistent(instance->pdev,
5349 sizeof(struct megasas_evt_log_info),
5350 &el_info_h);
5351
5352 if (!el_info) {
5353 megasas_return_cmd(instance, cmd);
5354 return -ENOMEM;
5355 }
5356
5357 memset(el_info, 0, sizeof(*el_info));
5358 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5359
5360 dcmd->cmd = MFI_CMD_DCMD;
5361 dcmd->cmd_status = 0x0;
5362 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305363 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005364 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005365 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305366 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5367 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5368 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5369 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005370
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305371 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5372 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305373 /*
5374 * Copy the data back into callers buffer
5375 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305376 eli->newest_seq_num = el_info->newest_seq_num;
5377 eli->oldest_seq_num = el_info->oldest_seq_num;
5378 eli->clear_seq_num = el_info->clear_seq_num;
5379 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5380 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305381 } else
5382 dev_err(&instance->pdev->dev, "DCMD failed "
5383 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005384
5385 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5386 el_info, el_info_h);
5387
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305388 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005389
5390 return 0;
5391}
5392
5393/**
5394 * megasas_register_aen - Registers for asynchronous event notification
5395 * @instance: Adapter soft state
5396 * @seq_num: The starting sequence number
5397 * @class_locale: Class of the event
5398 *
5399 * This function subscribes for AEN for events beyond the @seq_num. It requests
5400 * to be notified if and only if the event is of type @class_locale
5401 */
5402static int
5403megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5404 u32 class_locale_word)
5405{
5406 int ret_val;
5407 struct megasas_cmd *cmd;
5408 struct megasas_dcmd_frame *dcmd;
5409 union megasas_evt_class_locale curr_aen;
5410 union megasas_evt_class_locale prev_aen;
5411
5412 /*
5413 * If there an AEN pending already (aen_cmd), check if the
5414 * class_locale of that pending AEN is inclusive of the new
5415 * AEN request we currently have. If it is, then we don't have
5416 * to do anything. In other words, whichever events the current
5417 * AEN request is subscribing to, have already been subscribed
5418 * to.
5419 *
5420 * If the old_cmd is _not_ inclusive, then we have to abort
5421 * that command, form a class_locale that is superset of both
5422 * old and current and re-issue to the FW
5423 */
5424
5425 curr_aen.word = class_locale_word;
5426
5427 if (instance->aen_cmd) {
5428
Christoph Hellwiga9555532015-04-23 16:34:24 +05305429 prev_aen.word =
5430 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005431
5432 /*
5433 * A class whose enum value is smaller is inclusive of all
5434 * higher values. If a PROGRESS (= -1) was previously
5435 * registered, then a new registration requests for higher
5436 * classes need not be sent to FW. They are automatically
5437 * included.
5438 *
5439 * Locale numbers don't have such hierarchy. They are bitmap
5440 * values
5441 */
5442 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305443 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005444 curr_aen.members.locale)) {
5445 /*
5446 * Previously issued event registration includes
5447 * current request. Nothing to do.
5448 */
5449 return 0;
5450 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305451 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005452
5453 if (prev_aen.members.class < curr_aen.members.class)
5454 curr_aen.members.class = prev_aen.members.class;
5455
5456 instance->aen_cmd->abort_aen = 1;
5457 ret_val = megasas_issue_blocked_abort_cmd(instance,
5458 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305459 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005460
5461 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005462 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005463 "previous AEN command\n");
5464 return ret_val;
5465 }
5466 }
5467 }
5468
5469 cmd = megasas_get_cmd(instance);
5470
5471 if (!cmd)
5472 return -ENOMEM;
5473
5474 dcmd = &cmd->frame->dcmd;
5475
5476 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5477
5478 /*
5479 * Prepare DCMD for aen registration
5480 */
5481 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5482
5483 dcmd->cmd = MFI_CMD_DCMD;
5484 dcmd->cmd_status = 0x0;
5485 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305486 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005487 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005488 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305489 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5490 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5491 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005492 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305493 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5494 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5495 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005496
Yang, Bof4c9a132009-10-06 14:43:28 -06005497 if (instance->aen_cmd != NULL) {
5498 megasas_return_cmd(instance, cmd);
5499 return 0;
5500 }
5501
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005502 /*
5503 * Store reference to the cmd used to register for AEN. When an
5504 * application wants us to register for AEN, we have to abort this
5505 * cmd and re-register with a new EVENT LOCALE supplied by that app
5506 */
5507 instance->aen_cmd = cmd;
5508
5509 /*
5510 * Issue the aen registration frame
5511 */
adam radford9c915a82010-12-21 13:34:31 -08005512 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005513
5514 return 0;
5515}
5516
5517/**
5518 * megasas_start_aen - Subscribes to AEN during driver load time
5519 * @instance: Adapter soft state
5520 */
5521static int megasas_start_aen(struct megasas_instance *instance)
5522{
5523 struct megasas_evt_log_info eli;
5524 union megasas_evt_class_locale class_locale;
5525
5526 /*
5527 * Get the latest sequence number from FW
5528 */
5529 memset(&eli, 0, sizeof(eli));
5530
5531 if (megasas_get_seq_num(instance, &eli))
5532 return -1;
5533
5534 /*
5535 * Register AEN with FW for latest sequence number plus 1
5536 */
5537 class_locale.members.reserved = 0;
5538 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5539 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5540
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305541 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305542 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305543 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005544}
5545
5546/**
5547 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5548 * @instance: Adapter soft state
5549 */
5550static int megasas_io_attach(struct megasas_instance *instance)
5551{
5552 struct Scsi_Host *host = instance->host;
5553
5554 /*
5555 * Export parameters required by SCSI mid-layer
5556 */
5557 host->irq = instance->pdev->irq;
5558 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305559 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005560 host->this_id = instance->init_id;
5561 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005562
5563 if (instance->fw_support_ieee)
5564 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5565
Yang, Bo1fd10682010-10-12 07:18:50 -06005566 /*
5567 * Check if the module parameter value for max_sectors can be used
5568 */
5569 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5570 instance->max_sectors_per_req = max_sectors;
5571 else {
5572 if (max_sectors) {
5573 if (((instance->pdev->device ==
5574 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5575 (instance->pdev->device ==
5576 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5577 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5578 instance->max_sectors_per_req = max_sectors;
5579 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005580 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005581 "and <= %d (or < 1MB for GEN2 controller)\n",
5582 instance->max_sectors_per_req);
5583 }
5584 }
5585 }
5586
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005587 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005588 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005589 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5590 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5591 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005592 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005593
adam radford9c915a82010-12-21 13:34:31 -08005594 /* Fusion only supports host reset */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305595 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005596 host->hostt->eh_device_reset_handler = NULL;
5597 host->hostt->eh_bus_reset_handler = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05305598 host->hostt->eh_target_reset_handler = megasas_reset_target_fusion;
5599 host->hostt->eh_abort_handler = megasas_task_abort_fusion;
adam radford9c915a82010-12-21 13:34:31 -08005600 }
5601
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005602 /*
5603 * Notify the mid-layer about the new controller
5604 */
5605 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305606 dev_err(&instance->pdev->dev,
5607 "Failed to add host from %s %d\n",
5608 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005609 return -ENODEV;
5610 }
5611
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005612 return 0;
5613}
5614
bo yang31ea7082007-11-07 12:09:50 -05005615static int
5616megasas_set_dma_mask(struct pci_dev *pdev)
5617{
5618 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005619 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005620 */
5621 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005622 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005623
Yang Hongyang284901a2009-04-06 19:01:15 -07005624 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005625 goto fail_set_dma_mask;
5626 }
5627 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005628 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005629 goto fail_set_dma_mask;
5630 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305631 /*
5632 * Ensure that all data structures are allocated in 32-bit
5633 * memory.
5634 */
5635 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5636 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5637 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5638 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5639 dev_info(&pdev->dev, "set 32bit DMA mask"
5640 "and 32 bit consistent mask\n");
5641 else
5642 goto fail_set_dma_mask;
5643 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305644
bo yang31ea7082007-11-07 12:09:50 -05005645 return 0;
5646
5647fail_set_dma_mask:
5648 return 1;
5649}
5650
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005651/**
5652 * megasas_probe_one - PCI hotplug entry point
5653 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005654 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005655 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005656static int megasas_probe_one(struct pci_dev *pdev,
5657 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005658{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305659 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005660 struct Scsi_Host *host;
5661 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005662 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305663 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005664
5665 /* Reset MSI-X in the kdump kernel */
5666 if (reset_devices) {
5667 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5668 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005669 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005670 &control);
5671 if (control & PCI_MSIX_FLAGS_ENABLE) {
5672 dev_info(&pdev->dev, "resetting MSI-X\n");
5673 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005674 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005675 control &
5676 ~PCI_MSIX_FLAGS_ENABLE);
5677 }
5678 }
5679 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005680
5681 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005682 * PCI prepping: enable device set bus mastering and dma mask
5683 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005684 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005685
5686 if (rval) {
5687 return rval;
5688 }
5689
5690 pci_set_master(pdev);
5691
bo yang31ea7082007-11-07 12:09:50 -05005692 if (megasas_set_dma_mask(pdev))
5693 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005694
5695 host = scsi_host_alloc(&megasas_template,
5696 sizeof(struct megasas_instance));
5697
5698 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005699 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005700 goto fail_alloc_instance;
5701 }
5702
5703 instance = (struct megasas_instance *)host->hostdata;
5704 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005705 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08005706 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005707
adam radford9c915a82010-12-21 13:34:31 -08005708 switch (instance->pdev->device) {
5709 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005710 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005711 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305712 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305713 case PCI_DEVICE_ID_LSI_INTRUDER:
5714 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305715 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5716 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005717 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305718 instance->ctrl_context_pages =
5719 get_order(sizeof(struct fusion_context));
5720 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5721 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005722 if (!instance->ctrl_context) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005723 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005724 "memory for Fusion context info\n");
5725 goto fail_alloc_dma_buf;
5726 }
5727 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305728 memset(fusion, 0,
5729 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305730 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5731 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5732 fusion->adapter_type = THUNDERBOLT_SERIES;
5733 else
5734 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08005735 }
5736 break;
5737 default: /* For all other supported controllers */
5738
5739 instance->producer =
5740 pci_alloc_consistent(pdev, sizeof(u32),
5741 &instance->producer_h);
5742 instance->consumer =
5743 pci_alloc_consistent(pdev, sizeof(u32),
5744 &instance->consumer_h);
5745
5746 if (!instance->producer || !instance->consumer) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005747 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
adam radford9c915a82010-12-21 13:34:31 -08005748 "memory for producer, consumer\n");
5749 goto fail_alloc_dma_buf;
5750 }
5751
5752 *instance->producer = 0;
5753 *instance->consumer = 0;
5754 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005755 }
5756
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305757 instance->system_info_buf = pci_zalloc_consistent(pdev,
5758 sizeof(struct MR_DRV_SYSTEM_INFO),
5759 &instance->system_info_h);
5760
5761 if (!instance->system_info_buf)
5762 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5763
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305764 /* Crash dump feature related initialisation*/
5765 instance->drv_buf_index = 0;
5766 instance->drv_buf_alloc = 0;
5767 instance->crash_dump_fw_support = 0;
5768 instance->crash_dump_app_support = 0;
5769 instance->fw_crash_state = UNAVAILABLE;
5770 spin_lock_init(&instance->crashdump_lock);
5771 instance->crash_dump_buf = NULL;
5772
5773 if (!reset_devices)
5774 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5775 CRASH_DMA_BUF_SIZE,
5776 &instance->crash_dump_h);
5777 if (!instance->crash_dump_buf)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005778 dev_err(&pdev->dev, "Can't allocate Firmware "
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305779 "crash dump DMA buffer\n");
5780
Yang, Boc3518832009-10-06 14:18:02 -06005781 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005782 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005783 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005784 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05305785 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305786 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005787
5788 instance->evt_detail = pci_alloc_consistent(pdev,
5789 sizeof(struct
5790 megasas_evt_detail),
5791 &instance->evt_detail_h);
5792
5793 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005794 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005795 "event detail structure\n");
5796 goto fail_alloc_dma_buf;
5797 }
5798
Sumit Saxena2216c302016-01-28 21:04:26 +05305799 instance->pd_info = pci_alloc_consistent(pdev,
5800 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
5801
5802 if (!instance->pd_info)
5803 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
5804
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005805 /*
5806 * Initialize locks and queues
5807 */
5808 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005809 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005810
Sumant Patroe4a082c2006-05-30 12:03:37 -07005811 atomic_set(&instance->fw_outstanding,0);
5812
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005813 init_waitqueue_head(&instance->int_cmd_wait_q);
5814 init_waitqueue_head(&instance->abort_cmd_wait_q);
5815
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305816 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005817 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005818 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005819
adam radford9c915a82010-12-21 13:34:31 -08005820 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05305821 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005822
5823 /*
5824 * Initialize PCI related and misc parameters
5825 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005826 instance->host = host;
5827 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5828 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305829 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005830
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305831
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005832 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305833 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005834 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005835
Sumant Patro658dced2006-10-03 13:09:14 -07005836 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005837 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005838 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005839 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005840 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305841 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005842
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305843 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005844 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305845 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5846 } else
adam radford9c915a82010-12-21 13:34:31 -08005847 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005848
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005849 /*
adam radford0a770662011-02-24 20:56:12 -08005850 * Initialize MFI Firmware
5851 */
5852 if (megasas_init_fw(instance))
5853 goto fail_init_mfi;
5854
adam radford229fe472014-03-10 02:51:56 -07005855 if (instance->requestorId) {
5856 if (instance->PlasmaFW111) {
5857 instance->vf_affiliation_111 =
5858 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5859 &instance->vf_affiliation_111_h);
5860 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005861 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005862 "memory for VF affiliation buffer\n");
5863 } else {
5864 instance->vf_affiliation =
5865 pci_alloc_consistent(pdev,
5866 (MAX_LOGICAL_DRIVES + 1) *
5867 sizeof(struct MR_LD_VF_AFFILIATION),
5868 &instance->vf_affiliation_h);
5869 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005870 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005871 "memory for VF affiliation buffer\n");
5872 }
5873 }
5874
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005875 /*
5876 * Store instance in PCI softstate
5877 */
5878 pci_set_drvdata(pdev, instance);
5879
5880 /*
5881 * Add this controller to megasas_mgmt_info structure so that it
5882 * can be exported to management applications
5883 */
5884 megasas_mgmt_info.count++;
5885 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5886 megasas_mgmt_info.max_index++;
5887
5888 /*
adam radford541f90b2011-05-11 18:34:29 -07005889 * Register with SCSI mid-layer
5890 */
5891 if (megasas_io_attach(instance))
5892 goto fail_io_attach;
5893
5894 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305895 /*
5896 * Trigger SCSI to scan our drives
5897 */
5898 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005899
5900 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005901 * Initiate AEN (Asynchronous Event Notification)
5902 */
5903 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005904 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005905 goto fail_start_aen;
5906 }
5907
Adam Radford9ea81f82014-07-09 15:17:57 -07005908 /* Get current SR-IOV LD/VF affiliation */
5909 if (instance->requestorId)
5910 megasas_get_ld_vf_affiliation(instance, 1);
5911
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005912 return 0;
5913
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005914fail_start_aen:
5915fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005916 megasas_mgmt_info.count--;
5917 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5918 megasas_mgmt_info.max_index--;
5919
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305920 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305921 megasas_destroy_irqs(instance);
5922
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305923 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08005924 megasas_release_fusion(instance);
5925 else
5926 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005927 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005928 pci_disable_msix(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305929fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005930fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005931 if (instance->evt_detail)
5932 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5933 instance->evt_detail,
5934 instance->evt_detail_h);
5935
Sumit Saxena2216c302016-01-28 21:04:26 +05305936 if (instance->pd_info)
5937 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
5938 instance->pd_info,
5939 instance->pd_info_h);
adam radfordeb1b1232011-02-24 20:55:56 -08005940 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005941 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5942 instance->producer_h);
5943 if (instance->consumer)
5944 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5945 instance->consumer_h);
5946 scsi_host_put(host);
5947
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005948fail_alloc_instance:
5949fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005950 pci_disable_device(pdev);
5951
5952 return -ENODEV;
5953}
5954
5955/**
5956 * megasas_flush_cache - Requests FW to flush all its caches
5957 * @instance: Adapter soft state
5958 */
5959static void megasas_flush_cache(struct megasas_instance *instance)
5960{
5961 struct megasas_cmd *cmd;
5962 struct megasas_dcmd_frame *dcmd;
5963
Sumit Saxena8a01a412016-01-28 21:04:32 +05305964 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04005965 return;
5966
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005967 cmd = megasas_get_cmd(instance);
5968
5969 if (!cmd)
5970 return;
5971
5972 dcmd = &cmd->frame->dcmd;
5973
5974 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5975
5976 dcmd->cmd = MFI_CMD_DCMD;
5977 dcmd->cmd_status = 0x0;
5978 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305979 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005980 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005981 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005982 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305983 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005984 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5985
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305986 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
5987 != DCMD_SUCCESS) {
5988 dev_err(&instance->pdev->dev,
5989 "return from %s %d\n", __func__, __LINE__);
5990 return;
5991 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005992
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305993 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005994}
5995
5996/**
5997 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5998 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05005999 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006000 */
bo yang31ea7082007-11-07 12:09:50 -05006001static void megasas_shutdown_controller(struct megasas_instance *instance,
6002 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006003{
6004 struct megasas_cmd *cmd;
6005 struct megasas_dcmd_frame *dcmd;
6006
Sumit Saxena8a01a412016-01-28 21:04:32 +05306007 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006008 return;
6009
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006010 cmd = megasas_get_cmd(instance);
6011
6012 if (!cmd)
6013 return;
6014
6015 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306016 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306017 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006018 if (instance->map_update_cmd)
6019 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306020 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306021 if (instance->jbod_seq_cmd)
6022 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306023 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306024
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006025 dcmd = &cmd->frame->dcmd;
6026
6027 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6028
6029 dcmd->cmd = MFI_CMD_DCMD;
6030 dcmd->cmd_status = 0x0;
6031 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306032 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006033 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006034 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006035 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306036 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006037
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306038 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6039 != DCMD_SUCCESS) {
6040 dev_err(&instance->pdev->dev,
6041 "return from %s %d\n", __func__, __LINE__);
6042 return;
6043 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006044
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306045 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006046}
6047
Jiri Slaby33139b22008-05-01 17:56:02 +02006048#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006049/**
bo yangad84db22007-11-09 04:40:16 -05006050 * megasas_suspend - driver suspend entry point
6051 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006052 * @state: PCI power state to suspend routine
6053 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006054static int
bo yang31ea7082007-11-07 12:09:50 -05006055megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6056{
6057 struct Scsi_Host *host;
6058 struct megasas_instance *instance;
6059
6060 instance = pci_get_drvdata(pdev);
6061 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006062 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006063
adam radford229fe472014-03-10 02:51:56 -07006064 /* Shutdown SR-IOV heartbeat timer */
6065 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6066 del_timer_sync(&instance->sriov_heartbeat_timer);
6067
bo yang31ea7082007-11-07 12:09:50 -05006068 megasas_flush_cache(instance);
6069 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006070
6071 /* cancel the delayed work if this work still in queue */
6072 if (instance->ev != NULL) {
6073 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006074 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006075 instance->ev = NULL;
6076 }
6077
bo yang31ea7082007-11-07 12:09:50 -05006078 tasklet_kill(&instance->isr_tasklet);
6079
6080 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306081 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006082
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306083 megasas_destroy_irqs(instance);
6084
adam radfordc8e858f2011-10-08 18:15:13 -07006085 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08006086 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006087
6088 pci_save_state(pdev);
6089 pci_disable_device(pdev);
6090
6091 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6092
6093 return 0;
6094}
6095
6096/**
6097 * megasas_resume- driver resume entry point
6098 * @pdev: PCI device structure
6099 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006100static int
bo yang31ea7082007-11-07 12:09:50 -05006101megasas_resume(struct pci_dev *pdev)
6102{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306103 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006104 struct Scsi_Host *host;
6105 struct megasas_instance *instance;
6106
6107 instance = pci_get_drvdata(pdev);
6108 host = instance->host;
6109 pci_set_power_state(pdev, PCI_D0);
6110 pci_enable_wake(pdev, PCI_D0, 0);
6111 pci_restore_state(pdev);
6112
6113 /*
6114 * PCI prepping: enable device set bus mastering and dma mask
6115 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006116 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006117
6118 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006119 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006120 return rval;
6121 }
6122
6123 pci_set_master(pdev);
6124
6125 if (megasas_set_dma_mask(pdev))
6126 goto fail_set_dma_mask;
6127
6128 /*
6129 * Initialize MFI Firmware
6130 */
6131
bo yang31ea7082007-11-07 12:09:50 -05006132 atomic_set(&instance->fw_outstanding, 0);
6133
6134 /*
6135 * We expect the FW state to be READY
6136 */
adam radford058a8fa2011-10-08 18:14:27 -07006137 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006138 goto fail_ready_state;
6139
adam radford3f1abce2011-05-11 18:33:47 -07006140 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02006141 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02006142 pci_enable_msix_exact(instance->pdev, instance->msixentry,
6143 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02006144 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006145
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306146 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006147 megasas_reset_reply_desc(instance);
6148 if (megasas_ioc_init_fusion(instance)) {
6149 megasas_free_cmds(instance);
6150 megasas_free_cmds_fusion(instance);
6151 goto fail_init_mfi;
6152 }
6153 if (!megasas_get_map_info(instance))
6154 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306155 } else {
adam radford9c915a82010-12-21 13:34:31 -08006156 *instance->producer = 0;
6157 *instance->consumer = 0;
6158 if (megasas_issue_init_mfi(instance))
6159 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006160 }
bo yang31ea7082007-11-07 12:09:50 -05006161
adam radford9c915a82010-12-21 13:34:31 -08006162 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6163 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006164
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306165 if (instance->msix_vectors ?
6166 megasas_setup_irqs_msix(instance, 0) :
6167 megasas_setup_irqs_ioapic(instance))
6168 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006169
adam radford229fe472014-03-10 02:51:56 -07006170 /* Re-launch SR-IOV heartbeat timer */
6171 if (instance->requestorId) {
6172 if (!megasas_sriov_start_heartbeat(instance, 0))
6173 megasas_start_timer(instance,
6174 &instance->sriov_heartbeat_timer,
6175 megasas_sriov_heartbeat_handler,
6176 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306177 else {
adam radford229fe472014-03-10 02:51:56 -07006178 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306179 goto fail_init_mfi;
6180 }
adam radford229fe472014-03-10 02:51:56 -07006181 }
6182
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306183 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306184 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006185 instance->unload = 0;
6186
adam radford541f90b2011-05-11 18:34:29 -07006187 /*
6188 * Initiate AEN (Asynchronous Event Notification)
6189 */
6190 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006191 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006192
bo yang31ea7082007-11-07 12:09:50 -05006193 return 0;
6194
bo yang31ea7082007-11-07 12:09:50 -05006195fail_init_mfi:
6196 if (instance->evt_detail)
6197 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6198 instance->evt_detail,
6199 instance->evt_detail_h);
6200
Sumit Saxena2216c302016-01-28 21:04:26 +05306201 if (instance->pd_info)
6202 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6203 instance->pd_info,
6204 instance->pd_info_h);
bo yang31ea7082007-11-07 12:09:50 -05006205 if (instance->producer)
6206 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6207 instance->producer_h);
6208 if (instance->consumer)
6209 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6210 instance->consumer_h);
6211 scsi_host_put(host);
6212
6213fail_set_dma_mask:
6214fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006215fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006216
6217 pci_disable_device(pdev);
6218
6219 return -ENODEV;
6220}
Jiri Slaby33139b22008-05-01 17:56:02 +02006221#else
6222#define megasas_suspend NULL
6223#define megasas_resume NULL
6224#endif
bo yang31ea7082007-11-07 12:09:50 -05006225
6226/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006227 * megasas_detach_one - PCI hot"un"plug entry point
6228 * @pdev: PCI device structure
6229 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006230static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006231{
6232 int i;
6233 struct Scsi_Host *host;
6234 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006235 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306236 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006237
6238 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006239 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006240 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006241 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006242
adam radford229fe472014-03-10 02:51:56 -07006243 /* Shutdown SR-IOV heartbeat timer */
6244 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6245 del_timer_sync(&instance->sriov_heartbeat_timer);
6246
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306247 if (instance->fw_crash_state != UNAVAILABLE)
6248 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006249 scsi_remove_host(instance->host);
6250 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006251 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006252
6253 /* cancel the delayed work if this work still in queue*/
6254 if (instance->ev != NULL) {
6255 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006256 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006257 instance->ev = NULL;
6258 }
6259
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306260 /* cancel all wait events */
6261 wake_up_all(&instance->int_cmd_wait_q);
6262
Sumant Patro5d018ad2006-10-03 13:13:18 -07006263 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006264
6265 /*
6266 * Take the instance off the instance array. Note that we will not
6267 * decrement the max_index. We let this array be sparse array
6268 */
6269 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6270 if (megasas_mgmt_info.instance[i] == instance) {
6271 megasas_mgmt_info.count--;
6272 megasas_mgmt_info.instance[i] = NULL;
6273
6274 break;
6275 }
6276 }
6277
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306278 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006279
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306280 megasas_destroy_irqs(instance);
6281
adam radfordc8e858f2011-10-08 18:15:13 -07006282 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08006283 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006284
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306285 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006286 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306287 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6288 (sizeof(struct MR_PD_CFG_SEQ) *
6289 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306290 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006291 if (fusion->ld_map[i])
6292 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306293 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006294 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306295 fusion->ld_map_phys[i]);
6296 if (fusion->ld_drv_map[i])
6297 free_pages((ulong)fusion->ld_drv_map[i],
6298 fusion->drv_map_pages);
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006299 if (fusion->pd_seq_sync[i])
6300 dma_free_coherent(&instance->pdev->dev,
6301 pd_seq_map_sz,
6302 fusion->pd_seq_sync[i],
6303 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306304 }
6305 free_pages((ulong)instance->ctrl_context,
6306 instance->ctrl_context_pages);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306307 } else {
adam radford9c915a82010-12-21 13:34:31 -08006308 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006309 pci_free_consistent(pdev, sizeof(u32),
6310 instance->producer,
6311 instance->producer_h);
6312 pci_free_consistent(pdev, sizeof(u32),
6313 instance->consumer,
6314 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08006315 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006316
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306317 kfree(instance->ctrl_info);
6318
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05306319 if (instance->evt_detail)
6320 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6321 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07006322
Sumit Saxena2216c302016-01-28 21:04:26 +05306323 if (instance->pd_info)
6324 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6325 instance->pd_info,
6326 instance->pd_info_h);
adam radford229fe472014-03-10 02:51:56 -07006327 if (instance->vf_affiliation)
6328 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6329 sizeof(struct MR_LD_VF_AFFILIATION),
6330 instance->vf_affiliation,
6331 instance->vf_affiliation_h);
6332
6333 if (instance->vf_affiliation_111)
6334 pci_free_consistent(pdev,
6335 sizeof(struct MR_LD_VF_AFFILIATION_111),
6336 instance->vf_affiliation_111,
6337 instance->vf_affiliation_111_h);
6338
6339 if (instance->hb_host_mem)
6340 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6341 instance->hb_host_mem,
6342 instance->hb_host_mem_h);
6343
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306344 if (instance->crash_dump_buf)
6345 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6346 instance->crash_dump_buf, instance->crash_dump_h);
6347
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306348 if (instance->system_info_buf)
6349 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6350 instance->system_info_buf, instance->system_info_h);
6351
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006352 scsi_host_put(host);
6353
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006354 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006355}
6356
6357/**
6358 * megasas_shutdown - Shutdown entry point
6359 * @device: Generic device structure
6360 */
6361static void megasas_shutdown(struct pci_dev *pdev)
6362{
6363 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006364
Yang, Bo0c79e682009-10-06 14:47:35 -06006365 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006366 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006367 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306368 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306369 megasas_destroy_irqs(instance);
6370
adam radfordc8e858f2011-10-08 18:15:13 -07006371 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07006372 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006373}
6374
6375/**
6376 * megasas_mgmt_open - char node "open" entry point
6377 */
6378static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6379{
6380 /*
6381 * Allow only those users with admin rights
6382 */
6383 if (!capable(CAP_SYS_ADMIN))
6384 return -EACCES;
6385
6386 return 0;
6387}
6388
6389/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006390 * megasas_mgmt_fasync - Async notifier registration from applications
6391 *
6392 * This function adds the calling process to a driver global queue. When an
6393 * event occurs, SIGIO will be sent to all processes in this queue.
6394 */
6395static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6396{
6397 int rc;
6398
Arjan van de Ven0b950672006-01-11 13:16:10 +01006399 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006400
6401 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6402
Arjan van de Ven0b950672006-01-11 13:16:10 +01006403 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006404
6405 if (rc >= 0) {
6406 /* For sanity check when we get ioctl */
6407 filep->private_data = filep;
6408 return 0;
6409 }
6410
6411 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6412
6413 return rc;
6414}
6415
6416/**
Yang, Boc3518832009-10-06 14:18:02 -06006417 * megasas_mgmt_poll - char node "poll" entry point
6418 * */
6419static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6420{
6421 unsigned int mask;
6422 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006423
Yang, Boc3518832009-10-06 14:18:02 -06006424 poll_wait(file, &megasas_poll_wait, wait);
6425 spin_lock_irqsave(&poll_aen_lock, flags);
6426 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006427 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006428 else
6429 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306430 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006431 spin_unlock_irqrestore(&poll_aen_lock, flags);
6432 return mask;
6433}
6434
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306435/*
6436 * megasas_set_crash_dump_params_ioctl:
6437 * Send CRASH_DUMP_MODE DCMD to all controllers
6438 * @cmd: MFI command frame
6439 */
6440
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006441static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306442{
6443 struct megasas_instance *local_instance;
6444 int i, error = 0;
6445 int crash_support;
6446
6447 crash_support = cmd->frame->dcmd.mbox.w[0];
6448
6449 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6450 local_instance = megasas_mgmt_info.instance[i];
6451 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05306452 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306453 MEGASAS_HBA_OPERATIONAL) &&
6454 !megasas_set_crash_dump_params(local_instance,
6455 crash_support)) {
6456 local_instance->crash_dump_app_support =
6457 crash_support;
6458 dev_info(&local_instance->pdev->dev,
6459 "Application firmware crash "
6460 "dump mode set success\n");
6461 error = 0;
6462 } else {
6463 dev_info(&local_instance->pdev->dev,
6464 "Application firmware crash "
6465 "dump mode set failed\n");
6466 error = -1;
6467 }
6468 }
6469 }
6470 return error;
6471}
6472
Yang, Boc3518832009-10-06 14:18:02 -06006473/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006474 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6475 * @instance: Adapter soft state
6476 * @argp: User's ioctl packet
6477 */
6478static int
6479megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6480 struct megasas_iocpacket __user * user_ioc,
6481 struct megasas_iocpacket *ioc)
6482{
6483 struct megasas_sge32 *kern_sge32;
6484 struct megasas_cmd *cmd;
6485 void *kbuff_arr[MAX_IOCTL_SGE];
6486 dma_addr_t buf_handle = 0;
6487 int error = 0, i;
6488 void *sense = NULL;
6489 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006490 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006491
6492 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6493
6494 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006495 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006496 ioc->sge_count, MAX_IOCTL_SGE);
6497 return -EINVAL;
6498 }
6499
6500 cmd = megasas_get_cmd(instance);
6501 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006502 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006503 return -ENOMEM;
6504 }
6505
6506 /*
6507 * User's IOCTL packet has 2 frames (maximum). Copy those two
6508 * frames into our cmd's frames. cmd->frame's context will get
6509 * overwritten when we copy from user's frames. So set that value
6510 * alone separately
6511 */
6512 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306513 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006514 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306515 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6516 MFI_FRAME_SGL64 |
6517 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006518
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306519 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6520 error = megasas_set_crash_dump_params_ioctl(cmd);
6521 megasas_return_cmd(instance, cmd);
6522 return error;
6523 }
6524
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006525 /*
6526 * The management interface between applications and the fw uses
6527 * MFI frames. E.g, RAID configuration changes, LD property changes
6528 * etc are accomplishes through different kinds of MFI frames. The
6529 * driver needs to care only about substituting user buffers with
6530 * kernel buffers in SGLs. The location of SGL is embedded in the
6531 * struct iocpacket itself.
6532 */
6533 kern_sge32 = (struct megasas_sge32 *)
6534 ((unsigned long)cmd->frame + ioc->sgl_off);
6535
6536 /*
6537 * For each user buffer, create a mirror buffer and copy in
6538 */
6539 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006540 if (!ioc->sgl[i].iov_len)
6541 continue;
6542
Sumant Patro9f35fa82007-02-14 12:55:45 -08006543 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006544 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006545 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006546 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006547 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6548 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006549 error = -ENOMEM;
6550 goto out;
6551 }
6552
6553 /*
6554 * We don't change the dma_coherent_mask, so
6555 * pci_alloc_consistent only returns 32bit addresses
6556 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306557 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6558 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006559
6560 /*
6561 * We created a kernel buffer corresponding to the
6562 * user buffer. Now copy in from the user buffer
6563 */
6564 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6565 (u32) (ioc->sgl[i].iov_len))) {
6566 error = -EFAULT;
6567 goto out;
6568 }
6569 }
6570
6571 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006572 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6573 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006574 if (!sense) {
6575 error = -ENOMEM;
6576 goto out;
6577 }
6578
6579 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006580 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306581 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006582 }
6583
6584 /*
6585 * Set the sync_cmd flag so that the ISR knows not to complete this
6586 * cmd to the SCSI mid-layer
6587 */
6588 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306589 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6590 cmd->sync_cmd = 0;
6591 dev_err(&instance->pdev->dev,
6592 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6593 __func__, __LINE__, cmd->frame->dcmd.opcode,
6594 cmd->cmd_status_drv);
6595 return -EBUSY;
6596 }
6597
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006598 cmd->sync_cmd = 0;
6599
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306600 if (instance->unload == 1) {
6601 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6602 "don't submit data to application\n");
6603 goto out;
6604 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006605 /*
6606 * copy out the kernel buffers to user buffers
6607 */
6608 for (i = 0; i < ioc->sge_count; i++) {
6609 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6610 ioc->sgl[i].iov_len)) {
6611 error = -EFAULT;
6612 goto out;
6613 }
6614 }
6615
6616 /*
6617 * copy out the sense
6618 */
6619 if (ioc->sense_len) {
6620 /*
bo yangb70a41e2008-03-18 03:13:06 -04006621 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006622 * sense buffer address
6623 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006624 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6625 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006626
bo yangb70a41e2008-03-18 03:13:06 -04006627 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6628 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006629 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05006630 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006631 error = -EFAULT;
6632 goto out;
6633 }
6634 }
6635
6636 /*
6637 * copy the status codes returned by the fw
6638 */
6639 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6640 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006641 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006642 error = -EFAULT;
6643 }
6644
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006645out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006646 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006647 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006648 sense, sense_handle);
6649 }
6650
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006651 for (i = 0; i < ioc->sge_count; i++) {
6652 if (kbuff_arr[i])
6653 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306654 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006655 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306656 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306657 kbuff_arr[i] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006658 }
6659
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306660 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006661 return error;
6662}
6663
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006664static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6665{
6666 struct megasas_iocpacket __user *user_ioc =
6667 (struct megasas_iocpacket __user *)arg;
6668 struct megasas_iocpacket *ioc;
6669 struct megasas_instance *instance;
6670 int error;
bo yang39a98552010-09-22 22:36:29 -04006671 int i;
6672 unsigned long flags;
6673 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006674
6675 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6676 if (!ioc)
6677 return -ENOMEM;
6678
6679 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6680 error = -EFAULT;
6681 goto out_kfree_ioc;
6682 }
6683
6684 instance = megasas_lookup_instance(ioc->host_no);
6685 if (!instance) {
6686 error = -ENODEV;
6687 goto out_kfree_ioc;
6688 }
6689
adam radford229fe472014-03-10 02:51:56 -07006690 /* Adjust ioctl wait time for VF mode */
6691 if (instance->requestorId)
6692 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6693
6694 /* Block ioctls in VF mode */
6695 if (instance->requestorId && !allow_vf_ioctls) {
6696 error = -ENODEV;
6697 goto out_kfree_ioc;
6698 }
6699
Sumit Saxena8a01a412016-01-28 21:04:32 +05306700 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006701 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006702 error = -ENODEV;
6703 goto out_kfree_ioc;
6704 }
6705
6706 if (instance->unload == 1) {
6707 error = -ENODEV;
6708 goto out_kfree_ioc;
6709 }
6710
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006711 if (down_interruptible(&instance->ioctl_sem)) {
6712 error = -ERESTARTSYS;
6713 goto out_kfree_ioc;
6714 }
bo yang39a98552010-09-22 22:36:29 -04006715
6716 for (i = 0; i < wait_time; i++) {
6717
6718 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306719 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006720 spin_unlock_irqrestore(&instance->hba_lock, flags);
6721 break;
6722 }
6723 spin_unlock_irqrestore(&instance->hba_lock, flags);
6724
6725 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006726 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04006727 "for controller reset to finish\n");
6728 }
6729
6730 msleep(1000);
6731 }
6732
6733 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306734 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006735 spin_unlock_irqrestore(&instance->hba_lock, flags);
6736
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006737 dev_err(&instance->pdev->dev, "timed out while"
bo yang39a98552010-09-22 22:36:29 -04006738 "waiting for HBA to recover\n");
6739 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006740 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006741 }
6742 spin_unlock_irqrestore(&instance->hba_lock, flags);
6743
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006744 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006745out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006746 up(&instance->ioctl_sem);
6747
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006748out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006749 kfree(ioc);
6750 return error;
6751}
6752
6753static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6754{
6755 struct megasas_instance *instance;
6756 struct megasas_aen aen;
6757 int error;
bo yang39a98552010-09-22 22:36:29 -04006758 int i;
6759 unsigned long flags;
6760 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006761
6762 if (file->private_data != file) {
6763 printk(KERN_DEBUG "megasas: fasync_helper was not "
6764 "called first\n");
6765 return -EINVAL;
6766 }
6767
6768 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6769 return -EFAULT;
6770
6771 instance = megasas_lookup_instance(aen.host_no);
6772
6773 if (!instance)
6774 return -ENODEV;
6775
Sumit Saxena8a01a412016-01-28 21:04:32 +05306776 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04006777 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006778 }
6779
6780 if (instance->unload == 1) {
6781 return -ENODEV;
6782 }
6783
bo yang39a98552010-09-22 22:36:29 -04006784 for (i = 0; i < wait_time; i++) {
6785
6786 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306787 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006788 spin_unlock_irqrestore(&instance->hba_lock,
6789 flags);
6790 break;
6791 }
6792
6793 spin_unlock_irqrestore(&instance->hba_lock, flags);
6794
6795 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006796 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04006797 "controller reset to finish\n");
6798 }
6799
6800 msleep(1000);
6801 }
6802
6803 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306804 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006805 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006806 dev_err(&instance->pdev->dev, "timed out while waiting"
6807 "for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04006808 return -ENODEV;
6809 }
6810 spin_unlock_irqrestore(&instance->hba_lock, flags);
6811
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306812 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006813 error = megasas_register_aen(instance, aen.seq_num,
6814 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306815 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006816 return error;
6817}
6818
6819/**
6820 * megasas_mgmt_ioctl - char node ioctl entry point
6821 */
6822static long
6823megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6824{
6825 switch (cmd) {
6826 case MEGASAS_IOC_FIRMWARE:
6827 return megasas_mgmt_ioctl_fw(file, arg);
6828
6829 case MEGASAS_IOC_GET_AEN:
6830 return megasas_mgmt_ioctl_aen(file, arg);
6831 }
6832
6833 return -ENOTTY;
6834}
6835
6836#ifdef CONFIG_COMPAT
6837static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6838{
6839 struct compat_megasas_iocpacket __user *cioc =
6840 (struct compat_megasas_iocpacket __user *)arg;
6841 struct megasas_iocpacket __user *ioc =
6842 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6843 int i;
6844 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006845 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306846 u32 local_sense_off;
6847 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306848 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006849
Jeff Garzik83aabc12006-10-04 06:34:03 -04006850 if (clear_user(ioc, sizeof(*ioc)))
6851 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006852
6853 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6854 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6855 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6856 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6857 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6858 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6859 return -EFAULT;
6860
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006861 /*
6862 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6863 * sense_len is not null, so prepare the 64bit value under
6864 * the same condition.
6865 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306866 if (get_user(local_sense_off, &ioc->sense_off) ||
6867 get_user(local_sense_len, &ioc->sense_len) ||
6868 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306869 return -EFAULT;
6870
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306871 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006872 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306873 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006874 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306875 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006876 if (get_user(ptr, sense_cioc_ptr) ||
6877 put_user(compat_ptr(ptr), sense_ioc_ptr))
6878 return -EFAULT;
6879 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006880
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006881 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006882 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6883 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6884 copy_in_user(&ioc->sgl[i].iov_len,
6885 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6886 return -EFAULT;
6887 }
6888
6889 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6890
6891 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6892 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6893 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6894 return -EFAULT;
6895 }
6896 return error;
6897}
6898
6899static long
6900megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6901 unsigned long arg)
6902{
6903 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006904 case MEGASAS_IOC_FIRMWARE32:
6905 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006906 case MEGASAS_IOC_GET_AEN:
6907 return megasas_mgmt_ioctl_aen(file, arg);
6908 }
6909
6910 return -ENOTTY;
6911}
6912#endif
6913
6914/*
6915 * File operations structure for management interface
6916 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006917static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006918 .owner = THIS_MODULE,
6919 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006920 .fasync = megasas_mgmt_fasync,
6921 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006922 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006923#ifdef CONFIG_COMPAT
6924 .compat_ioctl = megasas_mgmt_compat_ioctl,
6925#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006926 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006927};
6928
6929/*
6930 * PCI hotplug support registration structure
6931 */
6932static struct pci_driver megasas_pci_driver = {
6933
6934 .name = "megaraid_sas",
6935 .id_table = megasas_pci_table,
6936 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006937 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006938 .suspend = megasas_suspend,
6939 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006940 .shutdown = megasas_shutdown,
6941};
6942
6943/*
6944 * Sysfs driver attributes
6945 */
6946static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6947{
6948 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6949 MEGASAS_VERSION);
6950}
6951
6952static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6953
6954static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306955megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6956{
6957 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6958 MEGASAS_RELDATE);
6959}
6960
6961static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6962
6963static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06006964megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6965{
6966 return sprintf(buf, "%u\n", support_poll_for_event);
6967}
6968
6969static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6970 megasas_sysfs_show_support_poll_for_event, NULL);
6971
Yang, Bo837f5fe2010-10-11 06:59:20 -06006972 static ssize_t
6973megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6974{
6975 return sprintf(buf, "%u\n", support_device_change);
6976}
6977
6978static DRIVER_ATTR(support_device_change, S_IRUGO,
6979 megasas_sysfs_show_support_device_change, NULL);
6980
Yang, Bo72c4fd32009-10-06 14:20:59 -06006981static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07006982megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6983{
bo yangad84db22007-11-09 04:40:16 -05006984 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07006985}
6986
6987static ssize_t
6988megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6989{
6990 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006991
6992 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07006993 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6994 retval = -EINVAL;
6995 }
6996 return retval;
6997}
6998
Joe Malicki66dca9b2008-08-14 17:14:48 -04006999static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05007000 megasas_sysfs_set_dbg_lvl);
7001
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007002static void
7003megasas_aen_polling(struct work_struct *work)
7004{
7005 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007006 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007007 struct megasas_instance *instance = ev->instance;
7008 union megasas_evt_class_locale class_locale;
7009 struct Scsi_Host *host;
7010 struct scsi_device *sdev1;
7011 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007012 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007013 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007014 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007015 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307016 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007017
7018 if (!instance) {
7019 printk(KERN_ERR "invalid instance!\n");
7020 kfree(ev);
7021 return;
7022 }
adam radford229fe472014-03-10 02:51:56 -07007023
7024 /* Adjust event workqueue thread wait time for VF mode */
7025 if (instance->requestorId)
7026 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7027
7028 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307029 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007030
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007031 instance->ev = NULL;
7032 host = instance->host;
7033 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307034 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007035
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307036 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307037
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007038 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307039 case MR_EVT_PD_REMOVED:
7040 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307041 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307042 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007043 break;
7044
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307045 case MR_EVT_LD_OFFLINE:
7046 case MR_EVT_CFG_CLEARED:
7047 case MR_EVT_LD_DELETED:
7048 case MR_EVT_LD_CREATED:
7049 if (!instance->requestorId ||
7050 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7051 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007052
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307053 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307054 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007055
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307056 break;
7057
7058 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7059 case MR_EVT_FOREIGN_CFG_IMPORTED:
7060 case MR_EVT_LD_STATE_CHANGE:
7061 dcmd_ret = megasas_get_pd_list(instance);
7062
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307063 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307064 break;
7065
7066 if (!instance->requestorId ||
7067 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7068 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7069
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307070 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307071 break;
7072
7073 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7074 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7075 instance->host->host_no);
7076 break;
7077
7078 case MR_EVT_CTRL_PROP_CHANGED:
7079 dcmd_ret = megasas_get_ctrl_info(instance);
7080 break;
7081 default:
7082 doscan = 0;
7083 break;
7084 }
7085 } else {
7086 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7087 mutex_unlock(&instance->reset_mutex);
7088 kfree(ev);
7089 return;
7090 }
7091
7092 mutex_unlock(&instance->reset_mutex);
7093
7094 if (doscan & SCAN_PD_CHANNEL) {
7095 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7096 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7097 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007098 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307099 if (instance->pd_list[pd_index].driveState ==
7100 MR_PD_STATE_SYSTEM) {
7101 if (!sdev1)
7102 scsi_add_device(host, i, j, 0);
7103 else
Yang, Boc9786842009-12-06 08:39:25 -07007104 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007105 } else {
7106 if (sdev1) {
7107 scsi_remove_device(sdev1);
7108 scsi_device_put(sdev1);
7109 }
7110 }
Yang, Boc9786842009-12-06 08:39:25 -07007111 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007112 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007113 }
7114
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307115 if (doscan & SCAN_VD_CHANNEL) {
7116 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7117 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7118 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7119 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7120 if (instance->ld_ids[ld_index] != 0xff) {
7121 if (!sdev1)
7122 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7123 else
7124 scsi_device_put(sdev1);
7125 } else {
7126 if (sdev1) {
7127 scsi_remove_device(sdev1);
7128 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007129 }
7130 }
7131 }
7132 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007133 }
7134
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307135 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307136 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7137 else
7138 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007139
7140 /* Register AEN with FW for latest sequence number plus 1 */
7141 class_locale.members.reserved = 0;
7142 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7143 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307144
7145 if (instance->aen_cmd != NULL) {
7146 kfree(ev);
7147 return;
7148 }
7149
7150 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007151 error = megasas_register_aen(instance, seq_num,
7152 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007153 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307154 dev_err(&instance->pdev->dev,
7155 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007156
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307157 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007158 kfree(ev);
7159}
7160
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007161/**
7162 * megasas_init - Driver load entry point
7163 */
7164static int __init megasas_init(void)
7165{
7166 int rval;
7167
7168 /*
7169 * Announce driver version and other information
7170 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307171 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007172
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007173 spin_lock_init(&poll_aen_lock);
7174
Yang, Bo72c4fd32009-10-06 14:20:59 -06007175 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007176 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007177
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007178 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7179
7180 /*
7181 * Register character device node
7182 */
7183 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7184
7185 if (rval < 0) {
7186 printk(KERN_DEBUG "megasas: failed to open device node\n");
7187 return rval;
7188 }
7189
7190 megasas_mgmt_majorno = rval;
7191
7192 /*
7193 * Register ourselves as PCI hotplug module
7194 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007195 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007196
7197 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007198 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007199 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007200 }
7201
Jeff Garzik83aabc12006-10-04 06:34:03 -04007202 rval = driver_create_file(&megasas_pci_driver.driver,
7203 &driver_attr_version);
7204 if (rval)
7205 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007206
7207 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307208 &driver_attr_release_date);
7209 if (rval)
7210 goto err_dcf_rel_date;
7211
7212 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007213 &driver_attr_support_poll_for_event);
7214 if (rval)
7215 goto err_dcf_support_poll_for_event;
7216
Jeff Garzik83aabc12006-10-04 06:34:03 -04007217 rval = driver_create_file(&megasas_pci_driver.driver,
7218 &driver_attr_dbg_lvl);
7219 if (rval)
7220 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007221 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007222 &driver_attr_support_device_change);
7223 if (rval)
7224 goto err_dcf_support_device_change;
7225
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007226 return rval;
bo yangad84db22007-11-09 04:40:16 -05007227
Yang, Bo837f5fe2010-10-11 06:59:20 -06007228err_dcf_support_device_change:
7229 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007230 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007231err_dcf_dbg_lvl:
7232 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007233 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007234err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307235 driver_remove_file(&megasas_pci_driver.driver,
7236 &driver_attr_release_date);
7237err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007238 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7239err_dcf_attr_ver:
7240 pci_unregister_driver(&megasas_pci_driver);
7241err_pcidrv:
7242 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007243 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007244}
7245
7246/**
7247 * megasas_exit - Driver unload entry point
7248 */
7249static void __exit megasas_exit(void)
7250{
Sumant Patro658dced2006-10-03 13:09:14 -07007251 driver_remove_file(&megasas_pci_driver.driver,
7252 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007253 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007254 &driver_attr_support_poll_for_event);
7255 driver_remove_file(&megasas_pci_driver.driver,
7256 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307257 driver_remove_file(&megasas_pci_driver.driver,
7258 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007259 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007260
7261 pci_unregister_driver(&megasas_pci_driver);
7262 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7263}
7264
7265module_init(megasas_init);
7266module_exit(megasas_exit);