blob: 57cf4e3b6c48ca0cacf86108b0477e2fef8438eb [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
adam radfordc007b8b2012-07-17 18:20:24 -070086int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87module_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
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040095MODULE_LICENSE("GPL");
96MODULE_VERSION(MEGASAS_VERSION);
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +053097MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
98MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040099
adam radford058a8fa2011-10-08 18:14:27 -0700100int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400101static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700102static int megasas_ld_list_query(struct megasas_instance *instance,
103 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400104static int megasas_issue_init_mfi(struct megasas_instance *instance);
105static int megasas_register_aen(struct megasas_instance *instance,
106 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400107/*
108 * PCI ID table for all supported controllers
109 */
110static struct pci_device_id megasas_pci_table[] = {
111
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200112 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
113 /* xscale IOP */
114 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
115 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400116 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
117 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700118 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
119 /* gen2*/
120 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
121 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600122 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
123 /* skinny*/
124 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
125 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
127 /* xscale IOP, vega */
128 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
129 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
131 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
133 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
135 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
137 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
139 /* Intruder */
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
141 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
143 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200144 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400145};
146
147MODULE_DEVICE_TABLE(pci, megasas_pci_table);
148
149static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700150struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400151static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100152static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400153
Yang, Boc3518832009-10-06 14:18:02 -0600154static int megasas_poll_wait_aen;
155static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600156static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800157u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600158static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700159
Yang, Boc3518832009-10-06 14:18:02 -0600160/* define lock for aen poll */
161spinlock_t poll_aen_lock;
162
adam radford9c915a82010-12-21 13:34:31 -0800163void
bo yang7343eb62007-11-09 04:35:44 -0500164megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
165 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800166static u32
167megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
168static int
169megasas_adp_reset_gen2(struct megasas_instance *instance,
170 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800171static irqreturn_t megasas_isr(int irq, void *devp);
172static u32
173megasas_init_adapter_mfi(struct megasas_instance *instance);
174u32
175megasas_build_and_issue_cmd(struct megasas_instance *instance,
176 struct scsi_cmnd *scmd);
177static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800178void
179megasas_release_fusion(struct megasas_instance *instance);
180int
181megasas_ioc_init_fusion(struct megasas_instance *instance);
182void
183megasas_free_cmds_fusion(struct megasas_instance *instance);
184u8
185megasas_get_map_info(struct megasas_instance *instance);
186int
187megasas_sync_map_info(struct megasas_instance *instance);
188int
adam radford229fe472014-03-10 02:51:56 -0700189wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
190 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800191void megasas_reset_reply_desc(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -0800192void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700193static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
194 int initial);
195int megasas_check_mpio_paths(struct megasas_instance *instance,
196 struct scsi_cmnd *scmd);
adam radfordcd50ba82010-12-21 10:23:23 -0800197
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530198int
adam radfordcd50ba82010-12-21 10:23:23 -0800199megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
200{
201 instance->instancet->fire_cmd(instance,
202 cmd->frame_phys_addr, 0, instance->reg_set);
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530203 return 0;
adam radfordcd50ba82010-12-21 10:23:23 -0800204}
bo yang7343eb62007-11-09 04:35:44 -0500205
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400206/**
207 * megasas_get_cmd - Get a command from the free pool
208 * @instance: Adapter soft state
209 *
210 * Returns a free command from the pool
211 */
adam radford9c915a82010-12-21 13:34:31 -0800212struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400213 *instance)
214{
215 unsigned long flags;
216 struct megasas_cmd *cmd = NULL;
217
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530218 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400219
220 if (!list_empty(&instance->cmd_pool)) {
221 cmd = list_entry((&instance->cmd_pool)->next,
222 struct megasas_cmd, list);
223 list_del_init(&cmd->list);
224 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500225 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400226 }
227
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530228 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400229 return cmd;
230}
231
232/**
233 * megasas_return_cmd - Return a cmd to free command pool
234 * @instance: Adapter soft state
235 * @cmd: Command packet to be returned to free command pool
236 */
adam radford9c915a82010-12-21 13:34:31 -0800237inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400238megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
239{
240 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530241 u32 blk_tags;
242 struct megasas_cmd_fusion *cmd_fusion;
243 struct fusion_context *fusion = instance->ctrl_context;
244
245 /* This flag is used only for fusion adapter.
246 * Wait for Interrupt for Polled mode DCMD
247 */
248 if (cmd->flags & DRV_DCMD_POLLED_MODE)
249 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400250
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530251 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400252
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530253 if (fusion) {
254 blk_tags = instance->max_scsi_cmds + cmd->index;
255 cmd_fusion = fusion->cmd_list[blk_tags];
256 megasas_return_cmd_fusion(instance, cmd_fusion);
257 }
258 cmd->scmd = NULL;
259 cmd->frame_count = 0;
260 cmd->flags = 0;
261 if (!fusion && reset_devices)
262 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
263 list_add(&cmd->list, (&instance->cmd_pool)->next);
264
265 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
266
267}
Sumant Patro1341c932006-01-25 12:02:40 -0800268
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530269static const char *
270format_timestamp(uint32_t timestamp)
271{
272 static char buffer[32];
273
274 if ((timestamp & 0xff000000) == 0xff000000)
275 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
276 0x00ffffff);
277 else
278 snprintf(buffer, sizeof(buffer), "%us", timestamp);
279 return buffer;
280}
281
282static const char *
283format_class(int8_t class)
284{
285 static char buffer[6];
286
287 switch (class) {
288 case MFI_EVT_CLASS_DEBUG:
289 return "debug";
290 case MFI_EVT_CLASS_PROGRESS:
291 return "progress";
292 case MFI_EVT_CLASS_INFO:
293 return "info";
294 case MFI_EVT_CLASS_WARNING:
295 return "WARN";
296 case MFI_EVT_CLASS_CRITICAL:
297 return "CRIT";
298 case MFI_EVT_CLASS_FATAL:
299 return "FATAL";
300 case MFI_EVT_CLASS_DEAD:
301 return "DEAD";
302 default:
303 snprintf(buffer, sizeof(buffer), "%d", class);
304 return buffer;
305 }
306}
307
308/**
309 * megasas_decode_evt: Decode FW AEN event and print critical event
310 * for information.
311 * @instance: Adapter soft state
312 */
313static void
314megasas_decode_evt(struct megasas_instance *instance)
315{
316 struct megasas_evt_detail *evt_detail = instance->evt_detail;
317 union megasas_evt_class_locale class_locale;
318 class_locale.word = le32_to_cpu(evt_detail->cl.word);
319
320 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
321 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
322 le32_to_cpu(evt_detail->seq_num),
323 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
324 (class_locale.members.locale),
325 format_class(class_locale.members.class),
326 evt_detail->description);
327}
328
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400329/**
adam radford0d490162010-12-14 19:17:17 -0800330* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800331* (deviceid : 1064R, PERC5) controllers
332*/
333
334/**
335 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400336 * @regs: MFI register set
337 */
338static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530339megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400340{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530341 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500342
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530343 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400344 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400345
346 /* Dummy readl to force pci flush */
347 readl(&regs->outbound_intr_mask);
348}
349
350/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700351 * megasas_disable_intr_xscale -Disables interrupt
352 * @regs: MFI register set
353 */
354static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530355megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700356{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530357 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700358 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500359
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530360 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700361 writel(mask, &regs->outbound_intr_mask);
362 /* Dummy readl to force pci flush */
363 readl(&regs->outbound_intr_mask);
364}
365
366/**
Sumant Patro1341c932006-01-25 12:02:40 -0800367 * megasas_read_fw_status_reg_xscale - returns the current FW status value
368 * @regs: MFI register set
369 */
370static u32
371megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
372{
373 return readl(&(regs)->outbound_msg_0);
374}
375/**
376 * megasas_clear_interrupt_xscale - Check & clear interrupt
377 * @regs: MFI register set
378 */
adam radford0d490162010-12-14 19:17:17 -0800379static int
Sumant Patro1341c932006-01-25 12:02:40 -0800380megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
381{
382 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400383 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500384
Sumant Patro1341c932006-01-25 12:02:40 -0800385 /*
386 * Check if it is our interrupt
387 */
388 status = readl(&regs->outbound_intr_status);
389
bo yang39a98552010-09-22 22:36:29 -0400390 if (status & MFI_OB_INTR_STATUS_MASK)
391 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
392 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
393 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800394
395 /*
396 * Clear the interrupt by writing back the same value
397 */
bo yang39a98552010-09-22 22:36:29 -0400398 if (mfiStatus)
399 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800400
Yang, Bo06f579d2008-08-10 12:42:37 -0700401 /* Dummy readl to force pci flush */
402 readl(&regs->outbound_intr_status);
403
bo yang39a98552010-09-22 22:36:29 -0400404 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800405}
406
407/**
408 * megasas_fire_cmd_xscale - Sends command to the FW
409 * @frame_phys_addr : Physical address of cmd
410 * @frame_count : Number of frames for the command
411 * @regs : MFI register set
412 */
adam radford0d490162010-12-14 19:17:17 -0800413static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600414megasas_fire_cmd_xscale(struct megasas_instance *instance,
415 dma_addr_t frame_phys_addr,
416 u32 frame_count,
417 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800418{
bo yang39a98552010-09-22 22:36:29 -0400419 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500420
bo yang39a98552010-09-22 22:36:29 -0400421 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800422 writel((frame_phys_addr >> 3)|(frame_count),
423 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400424 spin_unlock_irqrestore(&instance->hba_lock, flags);
425}
426
427/**
428 * megasas_adp_reset_xscale - For controller reset
429 * @regs: MFI register set
430 */
431static int
432megasas_adp_reset_xscale(struct megasas_instance *instance,
433 struct megasas_register_set __iomem *regs)
434{
435 u32 i;
436 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500437
bo yang39a98552010-09-22 22:36:29 -0400438 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
439
440 for (i = 0; i < 3; i++)
441 msleep(1000); /* sleep for 3 secs */
442 pcidata = 0;
443 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500444 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400445 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500446 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400447 pcidata &= ~0x2;
448 pci_write_config_dword(instance->pdev,
449 MFI_1068_PCSR_OFFSET, pcidata);
450
451 for (i = 0; i < 2; i++)
452 msleep(1000); /* need to wait 2 secs again */
453
454 pcidata = 0;
455 pci_read_config_dword(instance->pdev,
456 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500457 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400458 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500459 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400460 pcidata = 0;
461 pci_write_config_dword(instance->pdev,
462 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
463 }
464 }
465 return 0;
466}
467
468/**
469 * megasas_check_reset_xscale - For controller reset check
470 * @regs: MFI register set
471 */
472static int
473megasas_check_reset_xscale(struct megasas_instance *instance,
474 struct megasas_register_set __iomem *regs)
475{
bo yang39a98552010-09-22 22:36:29 -0400476 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530477 (le32_to_cpu(*instance->consumer) ==
478 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400479 return 1;
bo yang39a98552010-09-22 22:36:29 -0400480 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800481}
482
483static struct megasas_instance_template megasas_instance_template_xscale = {
484
485 .fire_cmd = megasas_fire_cmd_xscale,
486 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700487 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800488 .clear_intr = megasas_clear_intr_xscale,
489 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400490 .adp_reset = megasas_adp_reset_xscale,
491 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800492 .service_isr = megasas_isr,
493 .tasklet = megasas_complete_cmd_dpc,
494 .init_adapter = megasas_init_adapter_mfi,
495 .build_and_issue_cmd = megasas_build_and_issue_cmd,
496 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800497};
498
499/**
adam radford0d490162010-12-14 19:17:17 -0800500* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800501* to xscale (deviceid : 1064R, PERC5) controllers
502*/
503
504/**
adam radford0d490162010-12-14 19:17:17 -0800505* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500506* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800507*/
508
509/**
510 * megasas_enable_intr_ppc - Enables interrupts
511 * @regs: MFI register set
512 */
513static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530514megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800515{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530516 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500517
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530518 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800519 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800520
bo yang39a98552010-09-22 22:36:29 -0400521 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800522
523 /* Dummy readl to force pci flush */
524 readl(&regs->outbound_intr_mask);
525}
526
527/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700528 * megasas_disable_intr_ppc - Disable interrupt
529 * @regs: MFI register set
530 */
531static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530532megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700533{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530534 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700535 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500536
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530537 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700538 writel(mask, &regs->outbound_intr_mask);
539 /* Dummy readl to force pci flush */
540 readl(&regs->outbound_intr_mask);
541}
542
543/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800544 * megasas_read_fw_status_reg_ppc - returns the current FW status value
545 * @regs: MFI register set
546 */
547static u32
548megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
549{
550 return readl(&(regs)->outbound_scratch_pad);
551}
552
553/**
554 * megasas_clear_interrupt_ppc - Check & clear interrupt
555 * @regs: MFI register set
556 */
adam radford0d490162010-12-14 19:17:17 -0800557static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800558megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
559{
adam radford3cc6851f92011-05-11 18:34:52 -0700560 u32 status, mfiStatus = 0;
561
Sumant Patrof9876f02006-02-03 15:34:35 -0800562 /*
563 * Check if it is our interrupt
564 */
565 status = readl(&regs->outbound_intr_status);
566
adam radford3cc6851f92011-05-11 18:34:52 -0700567 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
568 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
569
570 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
571 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800572
573 /*
574 * Clear the interrupt by writing back the same value
575 */
576 writel(status, &regs->outbound_doorbell_clear);
577
Yang, Bo06f579d2008-08-10 12:42:37 -0700578 /* Dummy readl to force pci flush */
579 readl(&regs->outbound_doorbell_clear);
580
adam radford3cc6851f92011-05-11 18:34:52 -0700581 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800582}
adam radford3cc6851f92011-05-11 18:34:52 -0700583
Sumant Patrof9876f02006-02-03 15:34:35 -0800584/**
585 * megasas_fire_cmd_ppc - Sends command to the FW
586 * @frame_phys_addr : Physical address of cmd
587 * @frame_count : Number of frames for the command
588 * @regs : MFI register set
589 */
adam radford0d490162010-12-14 19:17:17 -0800590static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600591megasas_fire_cmd_ppc(struct megasas_instance *instance,
592 dma_addr_t frame_phys_addr,
593 u32 frame_count,
594 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800595{
bo yang39a98552010-09-22 22:36:29 -0400596 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500597
bo yang39a98552010-09-22 22:36:29 -0400598 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800599 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800600 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400601 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800602}
603
bo yang39a98552010-09-22 22:36:29 -0400604/**
bo yang39a98552010-09-22 22:36:29 -0400605 * megasas_check_reset_ppc - For controller reset check
606 * @regs: MFI register set
607 */
608static int
609megasas_check_reset_ppc(struct megasas_instance *instance,
610 struct megasas_register_set __iomem *regs)
611{
adam radford3cc6851f92011-05-11 18:34:52 -0700612 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
613 return 1;
614
bo yang39a98552010-09-22 22:36:29 -0400615 return 0;
616}
adam radford3cc6851f92011-05-11 18:34:52 -0700617
Sumant Patrof9876f02006-02-03 15:34:35 -0800618static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800619
Sumant Patrof9876f02006-02-03 15:34:35 -0800620 .fire_cmd = megasas_fire_cmd_ppc,
621 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700622 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800623 .clear_intr = megasas_clear_intr_ppc,
624 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700625 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400626 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800627 .service_isr = megasas_isr,
628 .tasklet = megasas_complete_cmd_dpc,
629 .init_adapter = megasas_init_adapter_mfi,
630 .build_and_issue_cmd = megasas_build_and_issue_cmd,
631 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800632};
633
634/**
Yang, Bo87911122009-10-06 14:31:54 -0600635 * megasas_enable_intr_skinny - Enables interrupts
636 * @regs: MFI register set
637 */
638static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530639megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600640{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530641 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500642
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530643 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600644 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
645
646 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
647
648 /* Dummy readl to force pci flush */
649 readl(&regs->outbound_intr_mask);
650}
651
652/**
653 * megasas_disable_intr_skinny - Disables interrupt
654 * @regs: MFI register set
655 */
656static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530657megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600658{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530659 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600660 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500661
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530662 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600663 writel(mask, &regs->outbound_intr_mask);
664 /* Dummy readl to force pci flush */
665 readl(&regs->outbound_intr_mask);
666}
667
668/**
669 * megasas_read_fw_status_reg_skinny - returns the current FW status value
670 * @regs: MFI register set
671 */
672static u32
673megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
674{
675 return readl(&(regs)->outbound_scratch_pad);
676}
677
678/**
679 * megasas_clear_interrupt_skinny - Check & clear interrupt
680 * @regs: MFI register set
681 */
682static int
683megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
684{
685 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800686 u32 mfiStatus = 0;
687
Yang, Bo87911122009-10-06 14:31:54 -0600688 /*
689 * Check if it is our interrupt
690 */
691 status = readl(&regs->outbound_intr_status);
692
693 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400694 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600695 }
696
697 /*
adam radfordebf054b2011-02-24 20:57:15 -0800698 * Check if it is our interrupt
699 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600700 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800701 MFI_STATE_FAULT) {
702 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
703 } else
704 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
705
706 /*
Yang, Bo87911122009-10-06 14:31:54 -0600707 * Clear the interrupt by writing back the same value
708 */
709 writel(status, &regs->outbound_intr_status);
710
711 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500712 * dummy read to flush PCI
713 */
Yang, Bo87911122009-10-06 14:31:54 -0600714 readl(&regs->outbound_intr_status);
715
adam radfordebf054b2011-02-24 20:57:15 -0800716 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600717}
718
719/**
720 * megasas_fire_cmd_skinny - Sends command to the FW
721 * @frame_phys_addr : Physical address of cmd
722 * @frame_count : Number of frames for the command
723 * @regs : MFI register set
724 */
725static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600726megasas_fire_cmd_skinny(struct megasas_instance *instance,
727 dma_addr_t frame_phys_addr,
728 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600729 struct megasas_register_set __iomem *regs)
730{
Yang, Bo0c79e682009-10-06 14:47:35 -0600731 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500732
bo yang39a98552010-09-22 22:36:29 -0400733 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530734 writel(upper_32_bits(frame_phys_addr),
735 &(regs)->inbound_high_queue_port);
736 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
737 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400738 spin_unlock_irqrestore(&instance->hba_lock, flags);
739}
740
741/**
bo yang39a98552010-09-22 22:36:29 -0400742 * megasas_check_reset_skinny - For controller reset check
743 * @regs: MFI register set
744 */
745static int
746megasas_check_reset_skinny(struct megasas_instance *instance,
747 struct megasas_register_set __iomem *regs)
748{
adam radford3cc6851f92011-05-11 18:34:52 -0700749 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
750 return 1;
751
bo yang39a98552010-09-22 22:36:29 -0400752 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600753}
754
755static struct megasas_instance_template megasas_instance_template_skinny = {
756
757 .fire_cmd = megasas_fire_cmd_skinny,
758 .enable_intr = megasas_enable_intr_skinny,
759 .disable_intr = megasas_disable_intr_skinny,
760 .clear_intr = megasas_clear_intr_skinny,
761 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800762 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400763 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800764 .service_isr = megasas_isr,
765 .tasklet = megasas_complete_cmd_dpc,
766 .init_adapter = megasas_init_adapter_mfi,
767 .build_and_issue_cmd = megasas_build_and_issue_cmd,
768 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600769};
770
771
772/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700773* The following functions are defined for gen2 (deviceid : 0x78 0x79)
774* controllers
775*/
776
777/**
778 * megasas_enable_intr_gen2 - Enables interrupts
779 * @regs: MFI register set
780 */
781static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530782megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700783{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530784 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500785
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530786 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700787 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
788
789 /* write ~0x00000005 (4 & 1) to the intr mask*/
790 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
791
792 /* Dummy readl to force pci flush */
793 readl(&regs->outbound_intr_mask);
794}
795
796/**
797 * megasas_disable_intr_gen2 - Disables interrupt
798 * @regs: MFI register set
799 */
800static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530801megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700802{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530803 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700804 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500805
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530806 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700807 writel(mask, &regs->outbound_intr_mask);
808 /* Dummy readl to force pci flush */
809 readl(&regs->outbound_intr_mask);
810}
811
812/**
813 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
814 * @regs: MFI register set
815 */
816static u32
817megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
818{
819 return readl(&(regs)->outbound_scratch_pad);
820}
821
822/**
823 * megasas_clear_interrupt_gen2 - Check & clear interrupt
824 * @regs: MFI register set
825 */
826static int
827megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
828{
829 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400830 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500831
Yang, Bo6610a6b2008-08-10 12:42:38 -0700832 /*
833 * Check if it is our interrupt
834 */
835 status = readl(&regs->outbound_intr_status);
836
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530837 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400838 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
839 }
840 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
841 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
842 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700843
844 /*
845 * Clear the interrupt by writing back the same value
846 */
bo yang39a98552010-09-22 22:36:29 -0400847 if (mfiStatus)
848 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700849
850 /* Dummy readl to force pci flush */
851 readl(&regs->outbound_intr_status);
852
bo yang39a98552010-09-22 22:36:29 -0400853 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700854}
855/**
856 * megasas_fire_cmd_gen2 - Sends command to the FW
857 * @frame_phys_addr : Physical address of cmd
858 * @frame_count : Number of frames for the command
859 * @regs : MFI register set
860 */
861static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600862megasas_fire_cmd_gen2(struct megasas_instance *instance,
863 dma_addr_t frame_phys_addr,
864 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700865 struct megasas_register_set __iomem *regs)
866{
bo yang39a98552010-09-22 22:36:29 -0400867 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500868
bo yang39a98552010-09-22 22:36:29 -0400869 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700870 writel((frame_phys_addr | (frame_count<<1))|1,
871 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400872 spin_unlock_irqrestore(&instance->hba_lock, flags);
873}
874
875/**
876 * megasas_adp_reset_gen2 - For controller reset
877 * @regs: MFI register set
878 */
879static int
880megasas_adp_reset_gen2(struct megasas_instance *instance,
881 struct megasas_register_set __iomem *reg_set)
882{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500883 u32 retry = 0 ;
884 u32 HostDiag;
885 u32 __iomem *seq_offset = &reg_set->seq_offset;
886 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400887
adam radfordebf054b2011-02-24 20:57:15 -0800888 if (instance->instancet == &megasas_instance_template_skinny) {
889 seq_offset = &reg_set->fusion_seq_offset;
890 hostdiag_offset = &reg_set->fusion_host_diag;
891 }
892
893 writel(0, seq_offset);
894 writel(4, seq_offset);
895 writel(0xb, seq_offset);
896 writel(2, seq_offset);
897 writel(7, seq_offset);
898 writel(0xd, seq_offset);
899
bo yang39a98552010-09-22 22:36:29 -0400900 msleep(1000);
901
adam radfordebf054b2011-02-24 20:57:15 -0800902 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400903
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500904 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400905 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800906 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500907 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400908 retry, HostDiag);
909
910 if (retry++ >= 100)
911 return 1;
912
913 }
914
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500915 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400916
adam radfordebf054b2011-02-24 20:57:15 -0800917 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400918
919 ssleep(10);
920
adam radfordebf054b2011-02-24 20:57:15 -0800921 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500922 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400923 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800924 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500925 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400926 retry, HostDiag);
927
928 if (retry++ >= 1000)
929 return 1;
930
931 }
932 return 0;
933}
934
935/**
936 * megasas_check_reset_gen2 - For controller reset check
937 * @regs: MFI register set
938 */
939static int
940megasas_check_reset_gen2(struct megasas_instance *instance,
941 struct megasas_register_set __iomem *regs)
942{
Yang, Bo707e09b2010-10-12 07:20:27 -0600943 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
944 return 1;
945 }
946
bo yang39a98552010-09-22 22:36:29 -0400947 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700948}
949
950static struct megasas_instance_template megasas_instance_template_gen2 = {
951
952 .fire_cmd = megasas_fire_cmd_gen2,
953 .enable_intr = megasas_enable_intr_gen2,
954 .disable_intr = megasas_disable_intr_gen2,
955 .clear_intr = megasas_clear_intr_gen2,
956 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400957 .adp_reset = megasas_adp_reset_gen2,
958 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800959 .service_isr = megasas_isr,
960 .tasklet = megasas_complete_cmd_dpc,
961 .init_adapter = megasas_init_adapter_mfi,
962 .build_and_issue_cmd = megasas_build_and_issue_cmd,
963 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700964};
965
966/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800967* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400968* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800969*/
970
adam radford9c915a82010-12-21 13:34:31 -0800971/*
972 * Template added for TB (Fusion)
973 */
974extern struct megasas_instance_template megasas_instance_template_fusion;
975
Sumant Patrof9876f02006-02-03 15:34:35 -0800976/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400977 * megasas_issue_polled - Issues a polling command
978 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800979 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400980 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530981 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400982 */
adam radford9c915a82010-12-21 13:34:31 -0800983int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400984megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
985{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400986 struct megasas_header *frame_hdr = &cmd->frame->hdr;
987
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530988 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530989 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400990
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530991 if ((instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) ||
992 (instance->instancet->issue_dcmd(instance, cmd))) {
993 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
994 __func__, __LINE__);
995 return DCMD_NOT_FIRED;
996 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400997
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530998 return wait_and_poll(instance, cmd, instance->requestorId ?
999 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001000}
1001
1002/**
1003 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1004 * @instance: Adapter soft state
1005 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301006 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001007 *
1008 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001009 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001010 * Used to issue ioctl commands.
1011 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301012int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001013megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301014 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001015{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301016 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301017 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001018
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301019 if ((instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) ||
1020 (instance->instancet->issue_dcmd(instance, cmd))) {
1021 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1022 __func__, __LINE__);
1023 return DCMD_NOT_FIRED;
1024 }
1025
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301026 if (timeout) {
1027 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301028 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301029 if (!ret) {
1030 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1031 __func__, __LINE__);
1032 return DCMD_TIMEOUT;
1033 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301034 } else
1035 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301036 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001037
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301038 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301039 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001040}
1041
1042/**
1043 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1044 * @instance: Adapter soft state
1045 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301046 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001047 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301048 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001049 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001050 * cmd and waits for return status.
1051 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001052 */
1053static int
1054megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301055 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001056{
1057 struct megasas_cmd *cmd;
1058 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301059 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001060
1061 cmd = megasas_get_cmd(instance);
1062
1063 if (!cmd)
1064 return -1;
1065
1066 abort_fr = &cmd->frame->abort;
1067
1068 /*
1069 * Prepare and issue the abort frame
1070 */
1071 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301072 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301073 abort_fr->flags = cpu_to_le16(0);
1074 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1075 abort_fr->abort_mfi_phys_addr_lo =
1076 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1077 abort_fr->abort_mfi_phys_addr_hi =
1078 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001079
1080 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301081 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001082
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301083 if ((instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) ||
1084 (instance->instancet->issue_dcmd(instance, cmd))) {
1085 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1086 __func__, __LINE__);
1087 return DCMD_NOT_FIRED;
1088 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001089
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301090 if (timeout) {
1091 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301092 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301093 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301094 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1095 __func__, __LINE__);
1096 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301097 }
1098 } else
1099 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301100 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301101
bo yang39a98552010-09-22 22:36:29 -04001102 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001103
1104 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301105 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1106 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001107}
1108
1109/**
1110 * megasas_make_sgl32 - Prepares 32-bit SGL
1111 * @instance: Adapter soft state
1112 * @scp: SCSI command from the mid-layer
1113 * @mfi_sgl: SGL to be filled in
1114 *
1115 * If successful, this function returns the number of SG elements. Otherwise,
1116 * it returnes -1.
1117 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001118static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001119megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1120 union megasas_sgl *mfi_sgl)
1121{
1122 int i;
1123 int sge_count;
1124 struct scatterlist *os_sgl;
1125
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001126 sge_count = scsi_dma_map(scp);
1127 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001128
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001129 if (sge_count) {
1130 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301131 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1132 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001133 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001134 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135 return sge_count;
1136}
1137
1138/**
1139 * megasas_make_sgl64 - Prepares 64-bit SGL
1140 * @instance: Adapter soft state
1141 * @scp: SCSI command from the mid-layer
1142 * @mfi_sgl: SGL to be filled in
1143 *
1144 * If successful, this function returns the number of SG elements. Otherwise,
1145 * it returnes -1.
1146 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001147static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001148megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1149 union megasas_sgl *mfi_sgl)
1150{
1151 int i;
1152 int sge_count;
1153 struct scatterlist *os_sgl;
1154
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001155 sge_count = scsi_dma_map(scp);
1156 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001157
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001158 if (sge_count) {
1159 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301160 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1161 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001162 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001163 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001164 return sge_count;
1165}
1166
Yang, Bof4c9a132009-10-06 14:43:28 -06001167/**
1168 * megasas_make_sgl_skinny - Prepares IEEE SGL
1169 * @instance: Adapter soft state
1170 * @scp: SCSI command from the mid-layer
1171 * @mfi_sgl: SGL to be filled in
1172 *
1173 * If successful, this function returns the number of SG elements. Otherwise,
1174 * it returnes -1.
1175 */
1176static int
1177megasas_make_sgl_skinny(struct megasas_instance *instance,
1178 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1179{
1180 int i;
1181 int sge_count;
1182 struct scatterlist *os_sgl;
1183
1184 sge_count = scsi_dma_map(scp);
1185
1186 if (sge_count) {
1187 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301188 mfi_sgl->sge_skinny[i].length =
1189 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001190 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301191 cpu_to_le64(sg_dma_address(os_sgl));
1192 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001193 }
1194 }
1195 return sge_count;
1196}
1197
Sumant Patrob1df99d2006-10-03 12:40:47 -07001198 /**
1199 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001200 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001201 * @sge_count : number of sg elements
1202 *
1203 * Returns the number of frames required for numnber of sge's (sge_count)
1204 */
1205
Yang, Bof4c9a132009-10-06 14:43:28 -06001206static u32 megasas_get_frame_count(struct megasas_instance *instance,
1207 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001208{
1209 int num_cnt;
1210 int sge_bytes;
1211 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001212 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001213
1214 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1215 sizeof(struct megasas_sge32);
1216
Yang, Bof4c9a132009-10-06 14:43:28 -06001217 if (instance->flag_ieee) {
1218 sge_sz = sizeof(struct megasas_sge_skinny);
1219 }
1220
Sumant Patrob1df99d2006-10-03 12:40:47 -07001221 /*
bo yangd532dbe2008-03-17 03:36:43 -04001222 * Main frame can contain 2 SGEs for 64-bit SGLs and
1223 * 3 SGEs for 32-bit SGLs for ldio &
1224 * 1 SGEs for 64-bit SGLs and
1225 * 2 SGEs for 32-bit SGLs for pthru frame
1226 */
1227 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001228 if (instance->flag_ieee == 1) {
1229 num_cnt = sge_count - 1;
1230 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001231 num_cnt = sge_count - 1;
1232 else
1233 num_cnt = sge_count - 2;
1234 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001235 if (instance->flag_ieee == 1) {
1236 num_cnt = sge_count - 1;
1237 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001238 num_cnt = sge_count - 2;
1239 else
1240 num_cnt = sge_count - 3;
1241 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001242
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001243 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001244 sge_bytes = sge_sz * num_cnt;
1245
1246 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1247 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1248 }
1249 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001250 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001251
1252 if (frame_count > 7)
1253 frame_count = 8;
1254 return frame_count;
1255}
1256
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001257/**
1258 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1259 * @instance: Adapter soft state
1260 * @scp: SCSI command
1261 * @cmd: Command to be prepared in
1262 *
1263 * This function prepares CDB commands. These are typcially pass-through
1264 * commands to the devices.
1265 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001266static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001267megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1268 struct megasas_cmd *cmd)
1269{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001270 u32 is_logical;
1271 u32 device_id;
1272 u16 flags = 0;
1273 struct megasas_pthru_frame *pthru;
1274
1275 is_logical = MEGASAS_IS_LOGICAL(scp);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301276 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001277 pthru = (struct megasas_pthru_frame *)cmd->frame;
1278
1279 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1280 flags = MFI_FRAME_DIR_WRITE;
1281 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1282 flags = MFI_FRAME_DIR_READ;
1283 else if (scp->sc_data_direction == PCI_DMA_NONE)
1284 flags = MFI_FRAME_DIR_NONE;
1285
Yang, Bof4c9a132009-10-06 14:43:28 -06001286 if (instance->flag_ieee == 1) {
1287 flags |= MFI_FRAME_IEEE;
1288 }
1289
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001290 /*
1291 * Prepare the DCDB frame
1292 */
1293 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1294 pthru->cmd_status = 0x0;
1295 pthru->scsi_status = 0x0;
1296 pthru->target_id = device_id;
1297 pthru->lun = scp->device->lun;
1298 pthru->cdb_len = scp->cmd_len;
1299 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001300 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301301 pthru->flags = cpu_to_le16(flags);
1302 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001303
1304 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1305
1306 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001307 * If the command is for the tape device, set the
1308 * pthru timeout to the os layer timeout value.
1309 */
Yang, Bo8d568252009-10-06 14:12:21 -06001310 if (scp->device->type == TYPE_TAPE) {
1311 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301312 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001313 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301314 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001315 }
1316
1317 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001318 * Construct SGL
1319 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001320 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301321 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001322 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1323 &pthru->sgl);
1324 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301325 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001326 pthru->sge_count = megasas_make_sgl64(instance, scp,
1327 &pthru->sgl);
1328 } else
1329 pthru->sge_count = megasas_make_sgl32(instance, scp,
1330 &pthru->sgl);
1331
Yang, Bobdc6fb82009-12-06 08:30:19 -07001332 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001333 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001334 pthru->sge_count);
1335 return 0;
1336 }
1337
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001338 /*
1339 * Sense info specific
1340 */
1341 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301342 pthru->sense_buf_phys_addr_hi =
1343 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1344 pthru->sense_buf_phys_addr_lo =
1345 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001346
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001347 /*
1348 * Compute the total number of frames this command consumes. FW uses
1349 * this number to pull sufficient number of frames from host memory.
1350 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001351 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001352 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001353
1354 return cmd->frame_count;
1355}
1356
1357/**
1358 * megasas_build_ldio - Prepares IOs to logical devices
1359 * @instance: Adapter soft state
1360 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001361 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001362 *
1363 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1364 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001365static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001366megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1367 struct megasas_cmd *cmd)
1368{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001369 u32 device_id;
1370 u8 sc = scp->cmnd[0];
1371 u16 flags = 0;
1372 struct megasas_io_frame *ldio;
1373
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301374 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001375 ldio = (struct megasas_io_frame *)cmd->frame;
1376
1377 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1378 flags = MFI_FRAME_DIR_WRITE;
1379 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1380 flags = MFI_FRAME_DIR_READ;
1381
Yang, Bof4c9a132009-10-06 14:43:28 -06001382 if (instance->flag_ieee == 1) {
1383 flags |= MFI_FRAME_IEEE;
1384 }
1385
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001386 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001387 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001388 */
1389 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1390 ldio->cmd_status = 0x0;
1391 ldio->scsi_status = 0x0;
1392 ldio->target_id = device_id;
1393 ldio->timeout = 0;
1394 ldio->reserved_0 = 0;
1395 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301396 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001397 ldio->start_lba_hi = 0;
1398 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1399
1400 /*
1401 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1402 */
1403 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301404 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1405 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1406 ((u32) scp->cmnd[2] << 8) |
1407 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001408
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301409 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001410 }
1411
1412 /*
1413 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1414 */
1415 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301416 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1417 ((u32) scp->cmnd[7] << 8));
1418 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1419 ((u32) scp->cmnd[3] << 16) |
1420 ((u32) scp->cmnd[4] << 8) |
1421 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001422 }
1423
1424 /*
1425 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1426 */
1427 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301428 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1429 ((u32) scp->cmnd[7] << 16) |
1430 ((u32) scp->cmnd[8] << 8) |
1431 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001432
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301433 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1434 ((u32) scp->cmnd[3] << 16) |
1435 ((u32) scp->cmnd[4] << 8) |
1436 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001437 }
1438
1439 /*
1440 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1441 */
1442 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301443 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1444 ((u32) scp->cmnd[11] << 16) |
1445 ((u32) scp->cmnd[12] << 8) |
1446 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001447
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301448 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1449 ((u32) scp->cmnd[7] << 16) |
1450 ((u32) scp->cmnd[8] << 8) |
1451 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001452
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301453 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1454 ((u32) scp->cmnd[3] << 16) |
1455 ((u32) scp->cmnd[4] << 8) |
1456 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001457
1458 }
1459
1460 /*
1461 * Construct SGL
1462 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001463 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301464 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001465 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1466 &ldio->sgl);
1467 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301468 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001469 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1470 } else
1471 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1472
Yang, Bobdc6fb82009-12-06 08:30:19 -07001473 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001474 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001475 ldio->sge_count);
1476 return 0;
1477 }
1478
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001479 /*
1480 * Sense info specific
1481 */
1482 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1483 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301484 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001485
Sumant Patrob1df99d2006-10-03 12:40:47 -07001486 /*
1487 * Compute the total number of frames this command consumes. FW uses
1488 * this number to pull sufficient number of frames from host memory.
1489 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001490 cmd->frame_count = megasas_get_frame_count(instance,
1491 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001492
1493 return cmd->frame_count;
1494}
1495
1496/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301497 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1498 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001499 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001500 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001501 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301502inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001503{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301504 int ret;
1505
Sumant Patrocb59aa62006-01-25 11:53:25 -08001506 switch (cmd->cmnd[0]) {
1507 case READ_10:
1508 case WRITE_10:
1509 case READ_12:
1510 case WRITE_12:
1511 case READ_6:
1512 case WRITE_6:
1513 case READ_16:
1514 case WRITE_16:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301515 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1516 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1517 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001518 default:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301519 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1520 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001521 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301522 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001523}
1524
Sumant Patro658dced2006-10-03 13:09:14 -07001525 /**
1526 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001527 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001528 * @instance: Adapter soft state
1529 */
1530static inline void
1531megasas_dump_pending_frames(struct megasas_instance *instance)
1532{
1533 struct megasas_cmd *cmd;
1534 int i,n;
1535 union megasas_sgl *mfi_sgl;
1536 struct megasas_io_frame *ldio;
1537 struct megasas_pthru_frame *pthru;
1538 u32 sgcount;
1539 u32 max_cmd = instance->max_fw_cmds;
1540
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001541 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1542 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 -07001543 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001544 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 -07001545 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001546 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 -07001547
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001548 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001549 for (i = 0; i < max_cmd; i++) {
1550 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001551 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001552 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001553 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 +05301554 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001555 ldio = (struct megasas_io_frame *)cmd->frame;
1556 mfi_sgl = &ldio->sgl;
1557 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001558 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 +05301559 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1560 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1561 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1562 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001563 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001564 pthru = (struct megasas_pthru_frame *) cmd->frame;
1565 mfi_sgl = &pthru->sgl;
1566 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001567 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 +05301568 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1569 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1570 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1571 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001572 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001573 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1574 for (n = 0; n < sgcount; n++) {
1575 if (IS_DMA64)
1576 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1577 le32_to_cpu(mfi_sgl->sge64[n].length),
1578 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1579 else
1580 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1581 le32_to_cpu(mfi_sgl->sge32[n].length),
1582 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001583 }
1584 }
Sumant Patro658dced2006-10-03 13:09:14 -07001585 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001586 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001587 for (i = 0; i < max_cmd; i++) {
1588
1589 cmd = instance->cmd_list[i];
1590
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001591 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001592 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001593 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001594 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001595}
1596
adam radfordcd50ba82010-12-21 10:23:23 -08001597u32
1598megasas_build_and_issue_cmd(struct megasas_instance *instance,
1599 struct scsi_cmnd *scmd)
1600{
1601 struct megasas_cmd *cmd;
1602 u32 frame_count;
1603
1604 cmd = megasas_get_cmd(instance);
1605 if (!cmd)
1606 return SCSI_MLQUEUE_HOST_BUSY;
1607
1608 /*
1609 * Logical drive command
1610 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301611 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001612 frame_count = megasas_build_ldio(instance, scmd, cmd);
1613 else
1614 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1615
1616 if (!frame_count)
1617 goto out_return_cmd;
1618
1619 cmd->scmd = scmd;
1620 scmd->SCp.ptr = (char *)cmd;
1621
1622 /*
1623 * Issue the command to the FW
1624 */
1625 atomic_inc(&instance->fw_outstanding);
1626
1627 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1628 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001629
1630 return 0;
1631out_return_cmd:
1632 megasas_return_cmd(instance, cmd);
1633 return 1;
1634}
1635
1636
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001637/**
1638 * megasas_queue_command - Queue entry point
1639 * @scmd: SCSI command to be queued
1640 * @done: Callback entry point
1641 */
1642static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301643megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001644{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001645 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001646 unsigned long flags;
Sumit Saxena18365b12016-01-28 21:04:25 +05301647 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001648
1649 instance = (struct megasas_instance *)
1650 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001651
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301652 if (instance->unload == 1) {
1653 scmd->result = DID_NO_CONNECT << 16;
1654 scmd->scsi_done(scmd);
1655 return 0;
1656 }
1657
bo yang39a98552010-09-22 22:36:29 -04001658 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001659 return SCSI_MLQUEUE_HOST_BUSY;
1660
bo yang39a98552010-09-22 22:36:29 -04001661 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301662
adam radford229fe472014-03-10 02:51:56 -07001663 /* Check for an mpio path and adjust behavior */
1664 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1665 if (megasas_check_mpio_paths(instance, scmd) ==
1666 (DID_RESET << 16)) {
1667 spin_unlock_irqrestore(&instance->hba_lock, flags);
1668 return SCSI_MLQUEUE_HOST_BUSY;
1669 } else {
1670 spin_unlock_irqrestore(&instance->hba_lock, flags);
1671 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301672 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001673 return 0;
1674 }
1675 }
1676
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301677 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1678 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford229fe472014-03-10 02:51:56 -07001679 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301680 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301681 return 0;
1682 }
1683
Sumit Saxena18365b12016-01-28 21:04:25 +05301684 mr_device_priv_data = scmd->device->hostdata;
1685 if (!mr_device_priv_data) {
1686 spin_unlock_irqrestore(&instance->hba_lock, flags);
1687 scmd->result = DID_NO_CONNECT << 16;
1688 scmd->scsi_done(scmd);
1689 return 0;
1690 }
1691
bo yang39a98552010-09-22 22:36:29 -04001692 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1693 spin_unlock_irqrestore(&instance->hba_lock, flags);
1694 return SCSI_MLQUEUE_HOST_BUSY;
1695 }
1696
Sumit Saxena18365b12016-01-28 21:04:25 +05301697 if (mr_device_priv_data->tm_busy) {
1698 spin_unlock_irqrestore(&instance->hba_lock, flags);
1699 return SCSI_MLQUEUE_DEVICE_BUSY;
1700 }
1701
bo yang39a98552010-09-22 22:36:29 -04001702 spin_unlock_irqrestore(&instance->hba_lock, flags);
1703
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001704 scmd->result = 0;
1705
Sumant Patrocb59aa62006-01-25 11:53:25 -08001706 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301707 (scmd->device->id >= instance->fw_supported_vd_count ||
1708 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001709 scmd->result = DID_BAD_TARGET << 16;
1710 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001711 }
1712
Sumant Patro02b01e02007-02-14 13:00:55 -08001713 switch (scmd->cmnd[0]) {
1714 case SYNCHRONIZE_CACHE:
1715 /*
1716 * FW takes care of flush cache on its own
1717 * No need to send it down
1718 */
1719 scmd->result = DID_OK << 16;
1720 goto out_done;
1721 default:
1722 break;
1723 }
1724
adam radfordcd50ba82010-12-21 10:23:23 -08001725 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001726 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001727 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001728 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001729
1730 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001731
Sumant Patrocb59aa62006-01-25 11:53:25 -08001732 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301733 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001734 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001735}
1736
Yang, Bo044833b2009-10-06 14:33:06 -06001737static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1738{
1739 int i;
1740
1741 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1742
1743 if ((megasas_mgmt_info.instance[i]) &&
1744 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1745 return megasas_mgmt_info.instance[i];
1746 }
1747
1748 return NULL;
1749}
1750
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301751/*
Sumit Saxena18365b12016-01-28 21:04:25 +05301752* megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301753*
1754* @sdev: OS provided scsi device
1755*
1756* Returns void
1757*/
Sumit Saxena18365b12016-01-28 21:04:25 +05301758void megasas_update_sdev_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301759{
Sumit Saxena18365b12016-01-28 21:04:25 +05301760 u16 pd_index = 0;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301761 u32 device_id, ld;
1762 struct megasas_instance *instance;
1763 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301764 struct MR_PRIV_DEVICE *mr_device_priv_data;
1765 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301766 struct MR_LD_RAID *raid;
1767 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1768
1769 instance = megasas_lookup_instance(sdev->host->host_no);
1770 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301771 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301772
1773 if (!fusion)
1774 return;
1775
Sumit Saxena18365b12016-01-28 21:04:25 +05301776 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1777 instance->use_seqnum_jbod_fp) {
1778 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1779 sdev->id;
1780 pd_sync = (void *)fusion->pd_seq_sync
1781 [(instance->pd_seq_map_id - 1) & 1];
1782 mr_device_priv_data->is_tm_capable =
1783 pd_sync->seq[pd_index].capability.tmCapable;
1784 } else {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301785 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1786 + sdev->id;
1787 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1788 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1789 raid = MR_LdRaidGet(ld, local_map_ptr);
1790
1791 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Sumit Saxena18365b12016-01-28 21:04:25 +05301792 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1793 mr_device_priv_data->is_tm_capable =
1794 raid->capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301795 }
1796}
1797
Sumit Saxena18365b12016-01-28 21:04:25 +05301798
Christoph Hellwig147aab62006-02-17 12:13:48 +01001799static int megasas_slave_configure(struct scsi_device *sdev)
1800{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301801 u16 pd_index = 0;
1802 struct megasas_instance *instance;
1803
1804 instance = megasas_lookup_instance(sdev->host->host_no);
1805 if (instance->allow_fw_scan) {
1806 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1807 sdev->type == TYPE_DISK) {
1808 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1809 sdev->id;
1810 if (instance->pd_list[pd_index].driveState !=
1811 MR_PD_STATE_SYSTEM)
1812 return -ENXIO;
1813 }
1814 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301815 megasas_update_sdev_properties(sdev);
1816
Yang, Bo044833b2009-10-06 14:33:06 -06001817 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001818 * The RAID firmware may require extended timeouts.
1819 */
Yang, Bo044833b2009-10-06 14:33:06 -06001820 blk_queue_rq_timeout(sdev->request_queue,
1821 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301822
Yang, Bo044833b2009-10-06 14:33:06 -06001823 return 0;
1824}
1825
1826static int megasas_slave_alloc(struct scsi_device *sdev)
1827{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001828 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001829 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301830 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001831
Yang, Bo044833b2009-10-06 14:33:06 -06001832 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301833 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001834 /*
1835 * Open the OS scan to the SYSTEM PD
1836 */
1837 pd_index =
1838 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1839 sdev->id;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301840 if ((instance->allow_fw_scan || instance->pd_list[pd_index].driveState ==
1841 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301842 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001843 }
1844 return -ENXIO;
1845 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301846
1847scan_target:
1848 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1849 GFP_KERNEL);
1850 if (!mr_device_priv_data)
1851 return -ENOMEM;
1852 sdev->hostdata = mr_device_priv_data;
Christoph Hellwig147aab62006-02-17 12:13:48 +01001853 return 0;
1854}
1855
Sumit Saxena18365b12016-01-28 21:04:25 +05301856static void megasas_slave_destroy(struct scsi_device *sdev)
1857{
1858 kfree(sdev->hostdata);
1859 sdev->hostdata = NULL;
1860}
1861
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301862/*
1863* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1864* kill adapter
1865* @instance: Adapter soft state
1866*
1867*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301868static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301869{
1870 int i;
1871 struct megasas_cmd *cmd_mfi;
1872 struct megasas_cmd_fusion *cmd_fusion;
1873 struct fusion_context *fusion = instance->ctrl_context;
1874
1875 /* Find all outstanding ioctls */
1876 if (fusion) {
1877 for (i = 0; i < instance->max_fw_cmds; i++) {
1878 cmd_fusion = fusion->cmd_list[i];
1879 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1880 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1881 if (cmd_mfi->sync_cmd &&
1882 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1883 megasas_complete_cmd(instance,
1884 cmd_mfi, DID_OK);
1885 }
1886 }
1887 } else {
1888 for (i = 0; i < instance->max_fw_cmds; i++) {
1889 cmd_mfi = instance->cmd_list[i];
1890 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1891 MFI_CMD_ABORT)
1892 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1893 }
1894 }
1895}
1896
1897
adam radford9c915a82010-12-21 13:34:31 -08001898void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001899{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301900 /* Set critical error to block I/O & ioctls in case caller didn't */
1901 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1902 /* Wait 1 second to ensure IO or ioctls in build have posted */
1903 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001904 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301905 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301906 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001907 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001908 /* Flush */
1909 readl(&instance->reg_set->doorbell);
1910 if (instance->mpio && instance->requestorId)
1911 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001912 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301913 writel(MFI_STOP_ADP,
1914 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001915 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301916 /* Complete outstanding ioctls when adapter is killed */
1917 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001918}
1919
1920 /**
1921 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1922 * restored to max value
1923 * @instance: Adapter soft state
1924 *
1925 */
1926void
1927megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1928{
1929 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301930
adam radford9c915a82010-12-21 13:34:31 -08001931 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001932 && time_after(jiffies, instance->last_time + 5 * HZ)
1933 && atomic_read(&instance->fw_outstanding) <
1934 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001935
1936 spin_lock_irqsave(instance->host->host_lock, flags);
1937 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001938
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301939 instance->host->can_queue = instance->max_scsi_cmds;
adam radford9c915a82010-12-21 13:34:31 -08001940 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001941 }
1942}
1943
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001944/**
bo yang7343eb62007-11-09 04:35:44 -05001945 * megasas_complete_cmd_dpc - Returns FW's controller structure
1946 * @instance_addr: Address of adapter soft state
1947 *
1948 * Tasklet to complete cmds
1949 */
1950static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1951{
1952 u32 producer;
1953 u32 consumer;
1954 u32 context;
1955 struct megasas_cmd *cmd;
1956 struct megasas_instance *instance =
1957 (struct megasas_instance *)instance_addr;
1958 unsigned long flags;
1959
1960 /* If we have already declared adapter dead, donot complete cmds */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001961 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05001962 return;
1963
1964 spin_lock_irqsave(&instance->completion_lock, flags);
1965
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301966 producer = le32_to_cpu(*instance->producer);
1967 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001968
1969 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301970 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001971 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001972 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04001973 context);
1974 BUG();
1975 }
bo yang7343eb62007-11-09 04:35:44 -05001976
1977 cmd = instance->cmd_list[context];
1978
1979 megasas_complete_cmd(instance, cmd, DID_OK);
1980
1981 consumer++;
1982 if (consumer == (instance->max_fw_cmds + 1)) {
1983 consumer = 0;
1984 }
1985 }
1986
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301987 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05001988
1989 spin_unlock_irqrestore(&instance->completion_lock, flags);
1990
1991 /*
1992 * Check if we can restore can_queue
1993 */
adam radford9c915a82010-12-21 13:34:31 -08001994 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001995}
1996
adam radford229fe472014-03-10 02:51:56 -07001997/**
1998 * megasas_start_timer - Initializes a timer object
1999 * @instance: Adapter soft state
2000 * @timer: timer object to be initialized
2001 * @fn: timer function
2002 * @interval: time interval between timer function call
2003 *
2004 */
2005void megasas_start_timer(struct megasas_instance *instance,
2006 struct timer_list *timer,
2007 void *fn, unsigned long interval)
2008{
2009 init_timer(timer);
2010 timer->expires = jiffies + interval;
2011 timer->data = (unsigned long)instance;
2012 timer->function = fn;
2013 add_timer(timer);
2014}
2015
Yang, Bo707e09b2010-10-12 07:20:27 -06002016static void
2017megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2018
2019static void
2020process_fw_state_change_wq(struct work_struct *work);
2021
2022void megasas_do_ocr(struct megasas_instance *instance)
2023{
2024 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2025 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2026 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302027 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002028 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302029 instance->instancet->disable_intr(instance);
Yang, Bo707e09b2010-10-12 07:20:27 -06002030 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2031 instance->issuepend_done = 0;
2032
2033 atomic_set(&instance->fw_outstanding, 0);
2034 megasas_internal_reset_defer_cmds(instance);
2035 process_fw_state_change_wq(&instance->work_init);
2036}
2037
Adam Radford4cbfea82014-07-09 15:17:56 -07002038static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2039 int initial)
adam radford229fe472014-03-10 02:51:56 -07002040{
2041 struct megasas_cmd *cmd;
2042 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002043 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002044 dma_addr_t new_affiliation_111_h;
2045 int ld, retval = 0;
2046 u8 thisVf;
2047
2048 cmd = megasas_get_cmd(instance);
2049
2050 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002051 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2052 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002053 instance->host->host_no);
2054 return -ENOMEM;
2055 }
2056
2057 dcmd = &cmd->frame->dcmd;
2058
Adam Radford4cbfea82014-07-09 15:17:56 -07002059 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002060 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2061 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002062 megasas_return_cmd(instance, cmd);
2063 return -ENOMEM;
2064 }
2065
2066 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002067 memset(instance->vf_affiliation_111, 0,
2068 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002069 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002070 new_affiliation_111 =
2071 pci_alloc_consistent(instance->pdev,
2072 sizeof(struct MR_LD_VF_AFFILIATION_111),
2073 &new_affiliation_111_h);
2074 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002075 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2076 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002077 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002078 megasas_return_cmd(instance, cmd);
2079 return -ENOMEM;
2080 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002081 memset(new_affiliation_111, 0,
2082 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002083 }
2084
2085 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2086
2087 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302088 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002089 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302090 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002091 dcmd->timeout = 0;
2092 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302093 dcmd->data_xfer_len =
2094 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2095 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002096
Adam Radford4cbfea82014-07-09 15:17:56 -07002097 if (initial)
2098 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302099 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002100 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302101 dcmd->sgl.sge32[0].phys_addr =
2102 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002103
Christoph Hellwig2213a462015-04-23 16:33:54 +05302104 dcmd->sgl.sge32[0].length = cpu_to_le32(
2105 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002106
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002107 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002108 "scsi%d\n", instance->host->host_no);
2109
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302110 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002111 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2112 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002113 dcmd->cmd_status, instance->host->host_no);
2114 retval = 1; /* Do a scan if we couldn't get affiliation */
2115 goto out;
2116 }
2117
2118 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002119 thisVf = new_affiliation_111->thisVf;
2120 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2121 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2122 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002123 dev_warn(&instance->pdev->dev, "SR-IOV: "
2124 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002125 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002126 memcpy(instance->vf_affiliation_111,
2127 new_affiliation_111,
2128 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002129 retval = 1;
2130 goto out;
2131 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002132 }
2133out:
2134 if (new_affiliation_111) {
2135 pci_free_consistent(instance->pdev,
2136 sizeof(struct MR_LD_VF_AFFILIATION_111),
2137 new_affiliation_111,
2138 new_affiliation_111_h);
2139 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302140
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302141 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002142
2143 return retval;
2144}
2145
2146static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2147 int initial)
2148{
2149 struct megasas_cmd *cmd;
2150 struct megasas_dcmd_frame *dcmd;
2151 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2152 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2153 dma_addr_t new_affiliation_h;
2154 int i, j, retval = 0, found = 0, doscan = 0;
2155 u8 thisVf;
2156
2157 cmd = megasas_get_cmd(instance);
2158
2159 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002160 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2161 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002162 instance->host->host_no);
2163 return -ENOMEM;
2164 }
2165
2166 dcmd = &cmd->frame->dcmd;
2167
2168 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002169 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2170 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002171 megasas_return_cmd(instance, cmd);
2172 return -ENOMEM;
2173 }
2174
2175 if (initial)
2176 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2177 sizeof(struct MR_LD_VF_AFFILIATION));
2178 else {
2179 new_affiliation =
2180 pci_alloc_consistent(instance->pdev,
2181 (MAX_LOGICAL_DRIVES + 1) *
2182 sizeof(struct MR_LD_VF_AFFILIATION),
2183 &new_affiliation_h);
2184 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002185 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2186 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002187 instance->host->host_no);
2188 megasas_return_cmd(instance, cmd);
2189 return -ENOMEM;
2190 }
2191 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2192 sizeof(struct MR_LD_VF_AFFILIATION));
2193 }
2194
2195 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2196
2197 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302198 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002199 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302200 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002201 dcmd->timeout = 0;
2202 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302203 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2204 sizeof(struct MR_LD_VF_AFFILIATION));
2205 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002206
2207 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302208 dcmd->sgl.sge32[0].phys_addr =
2209 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002210 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302211 dcmd->sgl.sge32[0].phys_addr =
2212 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002213
Christoph Hellwig2213a462015-04-23 16:33:54 +05302214 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2215 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002216
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002217 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002218 "scsi%d\n", instance->host->host_no);
2219
Adam Radford4cbfea82014-07-09 15:17:56 -07002220
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302221 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002222 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2223 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002224 dcmd->cmd_status, instance->host->host_no);
2225 retval = 1; /* Do a scan if we couldn't get affiliation */
2226 goto out;
2227 }
2228
2229 if (!initial) {
2230 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002231 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2232 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002233 instance->host->host_no);
2234 retval = 1;
2235 goto out;
2236 }
2237 newmap = new_affiliation->map;
2238 savedmap = instance->vf_affiliation->map;
2239 thisVf = new_affiliation->thisVf;
2240 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2241 found = 0;
2242 for (j = 0; j < instance->vf_affiliation->ldCount;
2243 j++) {
2244 if (newmap->ref.targetId ==
2245 savedmap->ref.targetId) {
2246 found = 1;
2247 if (newmap->policy[thisVf] !=
2248 savedmap->policy[thisVf]) {
2249 doscan = 1;
2250 goto out;
2251 }
adam radford229fe472014-03-10 02:51:56 -07002252 }
2253 savedmap = (struct MR_LD_VF_MAP *)
2254 ((unsigned char *)savedmap +
2255 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002256 }
2257 if (!found && newmap->policy[thisVf] !=
2258 MR_LD_ACCESS_HIDDEN) {
2259 doscan = 1;
2260 goto out;
2261 }
2262 newmap = (struct MR_LD_VF_MAP *)
2263 ((unsigned char *)newmap + newmap->size);
2264 }
2265
2266 newmap = new_affiliation->map;
2267 savedmap = instance->vf_affiliation->map;
2268
2269 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2270 found = 0;
2271 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2272 if (savedmap->ref.targetId ==
2273 newmap->ref.targetId) {
2274 found = 1;
2275 if (savedmap->policy[thisVf] !=
2276 newmap->policy[thisVf]) {
2277 doscan = 1;
2278 goto out;
2279 }
2280 }
adam radford229fe472014-03-10 02:51:56 -07002281 newmap = (struct MR_LD_VF_MAP *)
2282 ((unsigned char *)newmap +
2283 newmap->size);
2284 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002285 if (!found && savedmap->policy[thisVf] !=
2286 MR_LD_ACCESS_HIDDEN) {
2287 doscan = 1;
2288 goto out;
2289 }
2290 savedmap = (struct MR_LD_VF_MAP *)
2291 ((unsigned char *)savedmap +
2292 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002293 }
2294 }
2295out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002296 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002297 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2298 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002299 memcpy(instance->vf_affiliation, new_affiliation,
2300 new_affiliation->size);
2301 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002302 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002303
2304 if (new_affiliation)
2305 pci_free_consistent(instance->pdev,
2306 (MAX_LOGICAL_DRIVES + 1) *
2307 sizeof(struct MR_LD_VF_AFFILIATION),
2308 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302309 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002310
2311 return retval;
2312}
2313
Adam Radford4cbfea82014-07-09 15:17:56 -07002314/* This function will get the current SR-IOV LD/VF affiliation */
2315static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2316 int initial)
2317{
2318 int retval;
2319
2320 if (instance->PlasmaFW111)
2321 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2322 else
2323 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2324 return retval;
2325}
2326
adam radford229fe472014-03-10 02:51:56 -07002327/* This function will tell FW to start the SR-IOV heartbeat */
2328int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2329 int initial)
2330{
2331 struct megasas_cmd *cmd;
2332 struct megasas_dcmd_frame *dcmd;
2333 int retval = 0;
2334
2335 cmd = megasas_get_cmd(instance);
2336
2337 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002338 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2339 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002340 instance->host->host_no);
2341 return -ENOMEM;
2342 }
2343
2344 dcmd = &cmd->frame->dcmd;
2345
2346 if (initial) {
2347 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002348 pci_zalloc_consistent(instance->pdev,
2349 sizeof(struct MR_CTRL_HB_HOST_MEM),
2350 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002351 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002352 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2353 " memory for heartbeat host memory for scsi%d\n",
2354 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002355 retval = -ENOMEM;
2356 goto out;
2357 }
adam radford229fe472014-03-10 02:51:56 -07002358 }
2359
2360 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2361
Christoph Hellwig2213a462015-04-23 16:33:54 +05302362 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002363 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302364 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002365 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302366 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002367 dcmd->timeout = 0;
2368 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302369 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2370 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2371 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2372 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002373
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002374 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002375 instance->host->host_no);
2376
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302377 if (instance->ctrl_context && !instance->mask_interrupts)
2378 retval = megasas_issue_blocked_cmd(instance, cmd,
2379 MEGASAS_ROUTINE_WAIT_TIME_VF);
2380 else
2381 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002382
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302383 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302384 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2385 "_MEM_ALLOC DCMD %s for scsi%d\n",
2386 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2387 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002388 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002389 }
2390
2391out:
2392 megasas_return_cmd(instance, cmd);
2393
2394 return retval;
2395}
2396
2397/* Handler for SR-IOV heartbeat */
2398void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2399{
2400 struct megasas_instance *instance =
2401 (struct megasas_instance *)instance_addr;
2402
2403 if (instance->hb_host_mem->HB.fwCounter !=
2404 instance->hb_host_mem->HB.driverCounter) {
2405 instance->hb_host_mem->HB.driverCounter =
2406 instance->hb_host_mem->HB.fwCounter;
2407 mod_timer(&instance->sriov_heartbeat_timer,
2408 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2409 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002410 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002411 "completed for scsi%d\n", instance->host->host_no);
2412 schedule_work(&instance->work_init);
2413 }
2414}
2415
bo yang7343eb62007-11-09 04:35:44 -05002416/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002417 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2418 * @instance: Adapter soft state
2419 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002420 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002421 * complete all its outstanding commands. Returns error if one or more IOs
2422 * are pending after this time period. It also marks the controller dead.
2423 */
2424static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2425{
2426 int i;
bo yang39a98552010-09-22 22:36:29 -04002427 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002428 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002429 u8 adprecovery;
2430 unsigned long flags;
2431 struct list_head clist_local;
2432 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002433 u32 fw_state;
2434 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04002435
2436 spin_lock_irqsave(&instance->hba_lock, flags);
2437 adprecovery = instance->adprecovery;
2438 spin_unlock_irqrestore(&instance->hba_lock, flags);
2439
2440 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2441
2442 INIT_LIST_HEAD(&clist_local);
2443 spin_lock_irqsave(&instance->hba_lock, flags);
2444 list_splice_init(&instance->internal_reset_pending_q,
2445 &clist_local);
2446 spin_unlock_irqrestore(&instance->hba_lock, flags);
2447
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002448 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002449 for (i = 0; i < wait_time; i++) {
2450 msleep(1000);
2451 spin_lock_irqsave(&instance->hba_lock, flags);
2452 adprecovery = instance->adprecovery;
2453 spin_unlock_irqrestore(&instance->hba_lock, flags);
2454 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2455 break;
2456 }
2457
2458 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002459 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
bo yang39a98552010-09-22 22:36:29 -04002460 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002461 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
bo yang39a98552010-09-22 22:36:29 -04002462 spin_unlock_irqrestore(&instance->hba_lock, flags);
2463 return FAILED;
2464 }
2465
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002466 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002467 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002468 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002469 struct megasas_cmd, list);
2470 list_del_init(&reset_cmd->list);
2471 if (reset_cmd->scmd) {
2472 reset_cmd->scmd->result = DID_RESET << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002473 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002474 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002475 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002476
2477 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2478 megasas_return_cmd(instance, reset_cmd);
2479 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002480 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002481 "reset queue\n",
2482 reset_cmd);
2483
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302484 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002485 instance->instancet->fire_cmd(instance,
2486 reset_cmd->frame_phys_addr,
2487 0, instance->reg_set);
2488 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002489 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002490 "cmds lst\n",
2491 reset_cmd);
2492 }
2493 reset_index++;
2494 }
2495
2496 return SUCCESS;
2497 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002498
adam radfordc007b8b2012-07-17 18:20:24 -07002499 for (i = 0; i < resetwaittime; i++) {
Sumant Patroe4a082c2006-05-30 12:03:37 -07002500 int outstanding = atomic_read(&instance->fw_outstanding);
2501
2502 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002503 break;
2504
2505 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002506 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002507 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002508 /*
2509 * Call cmd completion routine. Cmd to be
2510 * be completed directly without depending on isr.
2511 */
2512 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002513 }
2514
2515 msleep(1000);
2516 }
2517
Yang, Bo707e09b2010-10-12 07:20:27 -06002518 i = 0;
2519 kill_adapter_flag = 0;
2520 do {
2521 fw_state = instance->instancet->read_fw_status_reg(
2522 instance->reg_set) & MFI_STATE_MASK;
2523 if ((fw_state == MFI_STATE_FAULT) &&
2524 (instance->disableOnlineCtrlReset == 0)) {
2525 if (i == 3) {
2526 kill_adapter_flag = 2;
2527 break;
2528 }
2529 megasas_do_ocr(instance);
2530 kill_adapter_flag = 1;
2531
2532 /* wait for 1 secs to let FW finish the pending cmds */
2533 msleep(1000);
2534 }
2535 i++;
2536 } while (i <= 3);
2537
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002538 if (atomic_read(&instance->fw_outstanding) && !kill_adapter_flag) {
Yang, Bo707e09b2010-10-12 07:20:27 -06002539 if (instance->disableOnlineCtrlReset == 0) {
Yang, Bo707e09b2010-10-12 07:20:27 -06002540 megasas_do_ocr(instance);
2541
2542 /* wait for 5 secs to let FW finish the pending cmds */
2543 for (i = 0; i < wait_time; i++) {
2544 int outstanding =
2545 atomic_read(&instance->fw_outstanding);
2546 if (!outstanding)
2547 return SUCCESS;
2548 msleep(1000);
2549 }
2550 }
2551 }
2552
2553 if (atomic_read(&instance->fw_outstanding) ||
2554 (kill_adapter_flag == 2)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002555 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002556 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002557 * Send signal to FW to stop processing any pending cmds.
2558 * The controller will be taken offline by the OS now.
2559 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002560 if ((instance->pdev->device ==
2561 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2562 (instance->pdev->device ==
2563 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2564 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08002565 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002566 } else {
2567 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07002568 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002569 }
Sumant Patro658dced2006-10-03 13:09:14 -07002570 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04002571 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002572 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
bo yang39a98552010-09-22 22:36:29 -04002573 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002574 return FAILED;
2575 }
2576
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002577 dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
bo yang39a98552010-09-22 22:36:29 -04002578
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002579 return SUCCESS;
2580}
2581
2582/**
2583 * megasas_generic_reset - Generic reset routine
2584 * @scmd: Mid-layer SCSI command
2585 *
2586 * This routine implements a generic reset handler for device, bus and host
2587 * reset requests. Device, bus and host specific reset handlers can use this
2588 * function after they do their specific tasks.
2589 */
2590static int megasas_generic_reset(struct scsi_cmnd *scmd)
2591{
2592 int ret_val;
2593 struct megasas_instance *instance;
2594
2595 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2596
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002597 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2598 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002599
bo yang39a98552010-09-22 22:36:29 -04002600 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002601 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002602 return FAILED;
2603 }
2604
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002605 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002606 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002607 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002608 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002609 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002610
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002611 return ret_val;
2612}
2613
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002614/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002615 * megasas_reset_timer - quiesce the adapter if required
2616 * @scmd: scsi cmnd
2617 *
2618 * Sets the FW busy flag and reduces the host->can_queue if the
2619 * cmd has not been completed within the timeout period.
2620 */
2621static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002622blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002623{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002624 struct megasas_instance *instance;
2625 unsigned long flags;
2626
2627 if (time_after(jiffies, scmd->jiffies_at_alloc +
2628 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002629 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002630 }
2631
adam radfordf575c5d2011-10-13 16:01:12 -07002632 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002633 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2634 /* FW is busy, throttle IO */
2635 spin_lock_irqsave(instance->host->host_lock, flags);
2636
adam radfordc5daa6a2012-07-17 18:20:03 -07002637 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002638 instance->last_time = jiffies;
2639 instance->flag |= MEGASAS_FW_BUSY;
2640
2641 spin_unlock_irqrestore(instance->host->host_lock, flags);
2642 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002643 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002644}
2645
2646/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002647 * megasas_reset_device - Device reset handler entry point
2648 */
2649static int megasas_reset_device(struct scsi_cmnd *scmd)
2650{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002651 /*
2652 * First wait for all commands to complete
2653 */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002654 return megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002655}
2656
2657/**
2658 * megasas_reset_bus_host - Bus & host reset handler entry point
2659 */
2660static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2661{
2662 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002663 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002664
adam radford9c915a82010-12-21 13:34:31 -08002665 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002666
2667 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002668 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002669 */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302670 if (instance->ctrl_context)
adam radford229fe472014-03-10 02:51:56 -07002671 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002672 else
2673 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002674
2675 return ret;
2676}
2677
2678/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002679 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002680 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002681 * @bdev: block device
2682 * @capacity: drive capacity
2683 * @geom: geometry parameters
2684 */
2685static int
2686megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2687 sector_t capacity, int geom[])
2688{
2689 int heads;
2690 int sectors;
2691 sector_t cylinders;
2692 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002693
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002694 /* Default heads (64) & sectors (32) */
2695 heads = 64;
2696 sectors = 32;
2697
2698 tmp = heads * sectors;
2699 cylinders = capacity;
2700
2701 sector_div(cylinders, tmp);
2702
2703 /*
2704 * Handle extended translation size for logical drives > 1Gb
2705 */
2706
2707 if (capacity >= 0x200000) {
2708 heads = 255;
2709 sectors = 63;
2710 tmp = heads*sectors;
2711 cylinders = capacity;
2712 sector_div(cylinders, tmp);
2713 }
2714
2715 geom[0] = heads;
2716 geom[1] = sectors;
2717 geom[2] = cylinders;
2718
2719 return 0;
2720}
2721
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002722static void megasas_aen_polling(struct work_struct *work);
2723
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002724/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002725 * megasas_service_aen - Processes an event notification
2726 * @instance: Adapter soft state
2727 * @cmd: AEN command completed by the ISR
2728 *
2729 * For AEN, driver sends a command down to FW that is held by the FW till an
2730 * event occurs. When an event of interest occurs, FW completes the command
2731 * that it was previously holding.
2732 *
2733 * This routines sends SIGIO signal to processes that have registered with the
2734 * driver for AEN.
2735 */
2736static void
2737megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2738{
Yang, Boc3518832009-10-06 14:18:02 -06002739 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002740
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002741 /*
2742 * Don't signal app if it is just an aborted previously registered aen
2743 */
Yang, Boc3518832009-10-06 14:18:02 -06002744 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2745 spin_lock_irqsave(&poll_aen_lock, flags);
2746 megasas_poll_wait_aen = 1;
2747 spin_unlock_irqrestore(&poll_aen_lock, flags);
2748 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002749 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002750 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002751 else
2752 cmd->abort_aen = 0;
2753
2754 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302755
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302756 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002757
bo yang39a98552010-09-22 22:36:29 -04002758 if ((instance->unload == 0) &&
2759 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002760 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002761
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002762 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2763 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002764 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002765 } else {
2766 ev->instance = instance;
2767 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002768 INIT_DELAYED_WORK(&ev->hotplug_work,
2769 megasas_aen_polling);
2770 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002771 }
2772 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002773}
2774
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302775static ssize_t
2776megasas_fw_crash_buffer_store(struct device *cdev,
2777 struct device_attribute *attr, const char *buf, size_t count)
2778{
2779 struct Scsi_Host *shost = class_to_shost(cdev);
2780 struct megasas_instance *instance =
2781 (struct megasas_instance *) shost->hostdata;
2782 int val = 0;
2783 unsigned long flags;
2784
2785 if (kstrtoint(buf, 0, &val) != 0)
2786 return -EINVAL;
2787
2788 spin_lock_irqsave(&instance->crashdump_lock, flags);
2789 instance->fw_crash_buffer_offset = val;
2790 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2791 return strlen(buf);
2792}
2793
2794static ssize_t
2795megasas_fw_crash_buffer_show(struct device *cdev,
2796 struct device_attribute *attr, char *buf)
2797{
2798 struct Scsi_Host *shost = class_to_shost(cdev);
2799 struct megasas_instance *instance =
2800 (struct megasas_instance *) shost->hostdata;
2801 u32 size;
2802 unsigned long buff_addr;
2803 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2804 unsigned long src_addr;
2805 unsigned long flags;
2806 u32 buff_offset;
2807
2808 spin_lock_irqsave(&instance->crashdump_lock, flags);
2809 buff_offset = instance->fw_crash_buffer_offset;
2810 if (!instance->crash_dump_buf &&
2811 !((instance->fw_crash_state == AVAILABLE) ||
2812 (instance->fw_crash_state == COPYING))) {
2813 dev_err(&instance->pdev->dev,
2814 "Firmware crash dump is not available\n");
2815 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2816 return -EINVAL;
2817 }
2818
2819 buff_addr = (unsigned long) buf;
2820
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002821 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302822 dev_err(&instance->pdev->dev,
2823 "Firmware crash dump offset is out of range\n");
2824 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2825 return 0;
2826 }
2827
2828 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2829 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2830
2831 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2832 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002833 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302834 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2835
2836 return size;
2837}
2838
2839static ssize_t
2840megasas_fw_crash_buffer_size_show(struct device *cdev,
2841 struct device_attribute *attr, char *buf)
2842{
2843 struct Scsi_Host *shost = class_to_shost(cdev);
2844 struct megasas_instance *instance =
2845 (struct megasas_instance *) shost->hostdata;
2846
2847 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2848 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2849}
2850
2851static ssize_t
2852megasas_fw_crash_state_store(struct device *cdev,
2853 struct device_attribute *attr, const char *buf, size_t count)
2854{
2855 struct Scsi_Host *shost = class_to_shost(cdev);
2856 struct megasas_instance *instance =
2857 (struct megasas_instance *) shost->hostdata;
2858 int val = 0;
2859 unsigned long flags;
2860
2861 if (kstrtoint(buf, 0, &val) != 0)
2862 return -EINVAL;
2863
2864 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2865 dev_err(&instance->pdev->dev, "application updates invalid "
2866 "firmware crash state\n");
2867 return -EINVAL;
2868 }
2869
2870 instance->fw_crash_state = val;
2871
2872 if ((val == COPIED) || (val == COPY_ERROR)) {
2873 spin_lock_irqsave(&instance->crashdump_lock, flags);
2874 megasas_free_host_crash_buffer(instance);
2875 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2876 if (val == COPY_ERROR)
2877 dev_info(&instance->pdev->dev, "application failed to "
2878 "copy Firmware crash dump\n");
2879 else
2880 dev_info(&instance->pdev->dev, "Firmware crash dump "
2881 "copied successfully\n");
2882 }
2883 return strlen(buf);
2884}
2885
2886static ssize_t
2887megasas_fw_crash_state_show(struct device *cdev,
2888 struct device_attribute *attr, char *buf)
2889{
2890 struct Scsi_Host *shost = class_to_shost(cdev);
2891 struct megasas_instance *instance =
2892 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002893
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302894 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2895}
2896
2897static ssize_t
2898megasas_page_size_show(struct device *cdev,
2899 struct device_attribute *attr, char *buf)
2900{
2901 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2902}
2903
2904static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2905 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2906static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2907 megasas_fw_crash_buffer_size_show, NULL);
2908static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2909 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2910static DEVICE_ATTR(page_size, S_IRUGO,
2911 megasas_page_size_show, NULL);
2912
2913struct device_attribute *megaraid_host_attrs[] = {
2914 &dev_attr_fw_crash_buffer_size,
2915 &dev_attr_fw_crash_buffer,
2916 &dev_attr_fw_crash_state,
2917 &dev_attr_page_size,
2918 NULL,
2919};
2920
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002921/*
2922 * Scsi host template for megaraid_sas driver
2923 */
2924static struct scsi_host_template megasas_template = {
2925
2926 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05302927 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002928 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002929 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002930 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05302931 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002932 .queuecommand = megasas_queue_command,
2933 .eh_device_reset_handler = megasas_reset_device,
2934 .eh_bus_reset_handler = megasas_reset_bus_host,
2935 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002936 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302937 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002938 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002939 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002940 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002941 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002942};
2943
2944/**
2945 * megasas_complete_int_cmd - Completes an internal command
2946 * @instance: Adapter soft state
2947 * @cmd: Command to be completed
2948 *
2949 * The megasas_issue_blocked_cmd() function waits for a command to complete
2950 * after it issues a command. This function wakes up that waiting routine by
2951 * calling wake_up() on the wait queue.
2952 */
2953static void
2954megasas_complete_int_cmd(struct megasas_instance *instance,
2955 struct megasas_cmd *cmd)
2956{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302957 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002958 wake_up(&instance->int_cmd_wait_q);
2959}
2960
2961/**
2962 * megasas_complete_abort - Completes aborting a command
2963 * @instance: Adapter soft state
2964 * @cmd: Cmd that was issued to abort another cmd
2965 *
adam radford0d490162010-12-14 19:17:17 -08002966 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2967 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002968 * wakes up all functions waiting on the same wait queue.
2969 */
2970static void
2971megasas_complete_abort(struct megasas_instance *instance,
2972 struct megasas_cmd *cmd)
2973{
2974 if (cmd->sync_cmd) {
2975 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302976 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002977 wake_up(&instance->abort_cmd_wait_q);
2978 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002979}
2980
2981/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002982 * megasas_complete_cmd - Completes a command
2983 * @instance: Adapter soft state
2984 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002985 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002986 * SCSI mid-layer instead of the value returned
2987 * by the FW. This should be used if caller wants
2988 * an alternate status (as in the case of aborted
2989 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002990 */
adam radford9c915a82010-12-21 13:34:31 -08002991void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002992megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2993 u8 alt_status)
2994{
2995 int exception = 0;
2996 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002997 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002998 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302999 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003000
bo yang39a98552010-09-22 22:36:29 -04003001 /* flag for the retry reset */
3002 cmd->retry_for_fw_reset = 0;
3003
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003004 if (cmd->scmd)
3005 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003006
3007 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003008 case MFI_CMD_INVALID:
3009 /* Some older 1068 controller FW may keep a pended
3010 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3011 when booting the kdump kernel. Ignore this command to
3012 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003013 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3014 "completed\n");
3015 dev_warn(&instance->pdev->dev, "If you have a controller "
3016 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003017 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003018 case MFI_CMD_PD_SCSI_IO:
3019 case MFI_CMD_LD_SCSI_IO:
3020
3021 /*
3022 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3023 * issued either through an IO path or an IOCTL path. If it
3024 * was via IOCTL, we will send it to internal completion.
3025 */
3026 if (cmd->sync_cmd) {
3027 cmd->sync_cmd = 0;
3028 megasas_complete_int_cmd(instance, cmd);
3029 break;
3030 }
3031
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003032 case MFI_CMD_LD_READ:
3033 case MFI_CMD_LD_WRITE:
3034
3035 if (alt_status) {
3036 cmd->scmd->result = alt_status << 16;
3037 exception = 1;
3038 }
3039
3040 if (exception) {
3041
Sumant Patroe4a082c2006-05-30 12:03:37 -07003042 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003043
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003044 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003045 cmd->scmd->scsi_done(cmd->scmd);
3046 megasas_return_cmd(instance, cmd);
3047
3048 break;
3049 }
3050
3051 switch (hdr->cmd_status) {
3052
3053 case MFI_STAT_OK:
3054 cmd->scmd->result = DID_OK << 16;
3055 break;
3056
3057 case MFI_STAT_SCSI_IO_FAILED:
3058 case MFI_STAT_LD_INIT_IN_PROGRESS:
3059 cmd->scmd->result =
3060 (DID_ERROR << 16) | hdr->scsi_status;
3061 break;
3062
3063 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3064
3065 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3066
3067 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3068 memset(cmd->scmd->sense_buffer, 0,
3069 SCSI_SENSE_BUFFERSIZE);
3070 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3071 hdr->sense_len);
3072
3073 cmd->scmd->result |= DRIVER_SENSE << 24;
3074 }
3075
3076 break;
3077
3078 case MFI_STAT_LD_OFFLINE:
3079 case MFI_STAT_DEVICE_NOT_FOUND:
3080 cmd->scmd->result = DID_BAD_TARGET << 16;
3081 break;
3082
3083 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003084 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003085 hdr->cmd_status);
3086 cmd->scmd->result = DID_ERROR << 16;
3087 break;
3088 }
3089
Sumant Patroe4a082c2006-05-30 12:03:37 -07003090 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003091
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003092 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003093 cmd->scmd->scsi_done(cmd->scmd);
3094 megasas_return_cmd(instance, cmd);
3095
3096 break;
3097
3098 case MFI_CMD_SMP:
3099 case MFI_CMD_STP:
3100 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303101 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003102 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303103 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3104 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303105 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003106 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303107 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003108 if (cmd->frame->hdr.cmd_status != 0) {
3109 if (cmd->frame->hdr.cmd_status !=
3110 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003111 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003112 cmd->frame->hdr.cmd_status);
3113 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303114 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003115 spin_unlock_irqrestore(
3116 instance->host->host_lock,
3117 flags);
3118 break;
3119 }
3120 } else
3121 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303122 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303123
3124 /*
3125 * Set fast path IO to ZERO.
3126 * Validate Map will set proper value.
3127 * Meanwhile all IOs will go as LD IO.
3128 */
3129 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003130 fusion->fast_path_io = 1;
3131 else
3132 fusion->fast_path_io = 0;
3133 megasas_sync_map_info(instance);
3134 spin_unlock_irqrestore(instance->host->host_lock,
3135 flags);
3136 break;
3137 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303138 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3139 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003140 spin_lock_irqsave(&poll_aen_lock, flags);
3141 megasas_poll_wait_aen = 0;
3142 spin_unlock_irqrestore(&poll_aen_lock, flags);
3143 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003144
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303145 /* FW has an updated PD sequence */
3146 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3147 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3148
3149 spin_lock_irqsave(instance->host->host_lock, flags);
3150 status = cmd->frame->hdr.cmd_status;
3151 instance->jbod_seq_cmd = NULL;
3152 megasas_return_cmd(instance, cmd);
3153
3154 if (status == MFI_STAT_OK) {
3155 instance->pd_seq_map_id++;
3156 /* Re-register a pd sync seq num cmd */
3157 if (megasas_sync_pd_seq_num(instance, true))
3158 instance->use_seqnum_jbod_fp = false;
3159 } else
3160 instance->use_seqnum_jbod_fp = false;
3161
3162 spin_unlock_irqrestore(instance->host->host_lock, flags);
3163 break;
3164 }
3165
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003166 /*
3167 * See if got an event notification
3168 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303169 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003170 megasas_service_aen(instance, cmd);
3171 else
3172 megasas_complete_int_cmd(instance, cmd);
3173
3174 break;
3175
3176 case MFI_CMD_ABORT:
3177 /*
3178 * Cmd issued to abort another cmd returned
3179 */
3180 megasas_complete_abort(instance, cmd);
3181 break;
3182
3183 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003184 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003185 hdr->cmd);
3186 break;
3187 }
3188}
3189
3190/**
bo yang39a98552010-09-22 22:36:29 -04003191 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003192 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003193 * @instance: Adapter soft state
3194 */
3195static inline void
3196megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3197{
3198 struct megasas_cmd *cmd;
3199 struct list_head clist_local;
3200 union megasas_evt_class_locale class_locale;
3201 unsigned long flags;
3202 u32 seq_num;
3203
3204 INIT_LIST_HEAD(&clist_local);
3205 spin_lock_irqsave(&instance->hba_lock, flags);
3206 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3207 spin_unlock_irqrestore(&instance->hba_lock, flags);
3208
3209 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003210 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003211 struct megasas_cmd, list);
3212 list_del_init(&cmd->list);
3213
3214 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003215 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3216 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003217 cmd, cmd->scmd, cmd->sync_cmd);
3218
3219 cmd->retry_for_fw_reset++;
3220
3221 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003222 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003223 "was tried multiple times during reset."
3224 "Shutting down the HBA\n",
3225 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303226 instance->instancet->disable_intr(instance);
3227 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003228 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003229 return;
3230 }
3231 }
3232
3233 if (cmd->sync_cmd == 1) {
3234 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003235 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003236 "cmd attached to internal command!\n");
3237 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003238 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003239 "on the internal reset queue,"
3240 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303241 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003242 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003243 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003244 0, instance->reg_set);
3245 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003246 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003247 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003248 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003249
3250 atomic_inc(&instance->fw_outstanding);
3251 instance->instancet->fire_cmd(instance,
3252 cmd->frame_phys_addr,
3253 cmd->frame_count-1, instance->reg_set);
3254 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003255 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003256 "internal reset defer list while re-issue!!\n",
3257 cmd);
3258 }
3259 }
3260
3261 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003262 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003263 megasas_return_cmd(instance, instance->aen_cmd);
3264
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003265 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003266 }
3267
3268 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003269 * Initiate AEN (Asynchronous Event Notification)
3270 */
bo yang39a98552010-09-22 22:36:29 -04003271 seq_num = instance->last_seq_num;
3272 class_locale.members.reserved = 0;
3273 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3274 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3275
3276 megasas_register_aen(instance, seq_num, class_locale.word);
3277}
3278
3279/**
3280 * Move the internal reset pending commands to a deferred queue.
3281 *
3282 * We move the commands pending at internal reset time to a
3283 * pending queue. This queue would be flushed after successful
3284 * completion of the internal reset sequence. if the internal reset
3285 * did not complete in time, the kernel reset handler would flush
3286 * these commands.
3287 **/
3288static void
3289megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3290{
3291 struct megasas_cmd *cmd;
3292 int i;
3293 u32 max_cmd = instance->max_fw_cmds;
3294 u32 defer_index;
3295 unsigned long flags;
3296
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003297 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303298 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003299 for (i = 0; i < max_cmd; i++) {
3300 cmd = instance->cmd_list[i];
3301 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003302 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003303 "on the defer queue as internal\n",
3304 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3305
3306 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003307 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003308 " moving this cmd:%p, %d %p, it was"
3309 "discovered on some list?\n",
3310 cmd, cmd->sync_cmd, cmd->scmd);
3311
3312 list_del_init(&cmd->list);
3313 }
3314 defer_index++;
3315 list_add_tail(&cmd->list,
3316 &instance->internal_reset_pending_q);
3317 }
3318 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303319 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003320}
3321
3322
3323static void
3324process_fw_state_change_wq(struct work_struct *work)
3325{
3326 struct megasas_instance *instance =
3327 container_of(work, struct megasas_instance, work_init);
3328 u32 wait;
3329 unsigned long flags;
3330
3331 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003332 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
bo yang39a98552010-09-22 22:36:29 -04003333 instance->adprecovery);
3334 return ;
3335 }
3336
3337 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003338 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003339 "state, restarting it...\n");
3340
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303341 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003342 atomic_set(&instance->fw_outstanding, 0);
3343
3344 atomic_set(&instance->fw_reset_no_pci_access, 1);
3345 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003346 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003347
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003348 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003349 "initiating next stage...\n");
3350
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003351 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003352 "state 2 starting...\n");
3353
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003354 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003355 for (wait = 0; wait < 30; wait++) {
3356 msleep(1000);
3357 }
3358
adam radford058a8fa2011-10-08 18:14:27 -07003359 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003360 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003361
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303362 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003363 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003364 return ;
3365 }
3366
3367 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3368 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3369 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3370 ) {
3371 *instance->consumer = *instance->producer;
3372 } else {
3373 *instance->consumer = 0;
3374 *instance->producer = 0;
3375 }
3376
3377 megasas_issue_init_mfi(instance);
3378
3379 spin_lock_irqsave(&instance->hba_lock, flags);
3380 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3381 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303382 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003383
3384 megasas_issue_pending_cmds_again(instance);
3385 instance->issuepend_done = 1;
3386 }
bo yang39a98552010-09-22 22:36:29 -04003387}
3388
3389/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003390 * megasas_deplete_reply_queue - Processes all completed commands
3391 * @instance: Adapter soft state
3392 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003393 * SCSI mid-layer instead of the status
3394 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003395 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003396 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003397static int
bo yang39a98552010-09-22 22:36:29 -04003398megasas_deplete_reply_queue(struct megasas_instance *instance,
3399 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003400{
bo yang39a98552010-09-22 22:36:29 -04003401 u32 mfiStatus;
3402 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003403
bo yang39a98552010-09-22 22:36:29 -04003404 if ((mfiStatus = instance->instancet->check_reset(instance,
3405 instance->reg_set)) == 1) {
3406 return IRQ_HANDLED;
3407 }
3408
3409 if ((mfiStatus = instance->instancet->clear_intr(
3410 instance->reg_set)
3411 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003412 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003413 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003414 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003415 }
3416
3417 instance->mfiStatus = mfiStatus;
3418
3419 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3420 fw_state = instance->instancet->read_fw_status_reg(
3421 instance->reg_set) & MFI_STATE_MASK;
3422
3423 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003424 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003425 fw_state);
3426 }
3427
3428 if ((fw_state == MFI_STATE_FAULT) &&
3429 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003430 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003431
3432 if ((instance->pdev->device ==
3433 PCI_DEVICE_ID_LSI_SAS1064R) ||
3434 (instance->pdev->device ==
3435 PCI_DEVICE_ID_DELL_PERC5) ||
3436 (instance->pdev->device ==
3437 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3438
3439 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303440 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003441 }
3442
3443
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303444 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003445 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3446 instance->issuepend_done = 0;
3447
3448 atomic_set(&instance->fw_outstanding, 0);
3449 megasas_internal_reset_defer_cmds(instance);
3450
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003451 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
bo yang39a98552010-09-22 22:36:29 -04003452 fw_state, instance->adprecovery);
3453
3454 schedule_work(&instance->work_init);
3455 return IRQ_HANDLED;
3456
3457 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003458 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003459 fw_state, instance->disableOnlineCtrlReset);
3460 }
3461 }
3462
Sumant Patro5d018ad2006-10-03 13:13:18 -07003463 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003464 return IRQ_HANDLED;
3465}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003466/**
3467 * megasas_isr - isr entry point
3468 */
David Howells7d12e782006-10-05 14:55:46 +01003469static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003470{
adam radfordc8e858f2011-10-08 18:15:13 -07003471 struct megasas_irq_context *irq_context = devp;
3472 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003473 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003474 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003475
adam radfordc8e858f2011-10-08 18:15:13 -07003476 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003477 return IRQ_HANDLED;
3478
bo yang39a98552010-09-22 22:36:29 -04003479 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003480 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003481 spin_unlock_irqrestore(&instance->hba_lock, flags);
3482
3483 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003484}
3485
3486/**
3487 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003488 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003489 *
3490 * During the initialization, FW passes can potentially be in any one of
3491 * several possible states. If the FW in operational, waiting-for-handshake
3492 * states, driver must take steps to bring it to ready state. Otherwise, it
3493 * has to wait for the ready state.
3494 */
adam radford9c915a82010-12-21 13:34:31 -08003495int
adam radford058a8fa2011-10-08 18:14:27 -07003496megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003497{
3498 int i;
3499 u8 max_wait;
3500 u32 fw_state;
3501 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003502 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003503
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003504 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3505 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003506
Sumant Patroe3bbff92006-10-03 12:28:49 -07003507 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003508 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003509 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003510
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003511 while (fw_state != MFI_STATE_READY) {
3512
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003513 switch (fw_state) {
3514
3515 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003516 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003517 if (ocr) {
3518 max_wait = MEGASAS_RESET_WAIT_TIME;
3519 cur_state = MFI_STATE_FAULT;
3520 break;
3521 } else
3522 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003523
3524 case MFI_STATE_WAIT_HANDSHAKE:
3525 /*
3526 * Set the CLR bit in inbound doorbell
3527 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003528 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003529 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3530 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003531 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303532 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003533 writel(
3534 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003535 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303536 else
Yang, Bo87911122009-10-06 14:31:54 -06003537 writel(
3538 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3539 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003540
Yang, Bo7218df62009-10-06 14:52:20 -06003541 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003542 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3543 break;
3544
Sumant Patroe3bbff92006-10-03 12:28:49 -07003545 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003546 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003547 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3548 (instance->pdev->device ==
3549 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303550 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003551 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003552 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303553 else
Yang, Bo87911122009-10-06 14:31:54 -06003554 writel(MFI_INIT_HOTPLUG,
3555 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003556
Yang, Bo7218df62009-10-06 14:52:20 -06003557 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003558 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3559 break;
3560
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003561 case MFI_STATE_OPERATIONAL:
3562 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003563 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003564 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303565 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003566 if ((instance->pdev->device ==
3567 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3568 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003569 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303570 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003571 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003572 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303573
3574 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003575 for (i = 0; i < (10 * 1000); i += 20) {
3576 if (readl(
3577 &instance->
3578 reg_set->
3579 doorbell) & 1)
3580 msleep(20);
3581 else
3582 break;
3583 }
3584 }
Yang, Bo87911122009-10-06 14:31:54 -06003585 } else
3586 writel(MFI_RESET_FLAGS,
3587 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003588
Yang, Bo7218df62009-10-06 14:52:20 -06003589 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003590 cur_state = MFI_STATE_OPERATIONAL;
3591 break;
3592
3593 case MFI_STATE_UNDEFINED:
3594 /*
3595 * This state should not last for more than 2 seconds
3596 */
Yang, Bo7218df62009-10-06 14:52:20 -06003597 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003598 cur_state = MFI_STATE_UNDEFINED;
3599 break;
3600
3601 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003602 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003603 cur_state = MFI_STATE_BB_INIT;
3604 break;
3605
3606 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003607 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003608 cur_state = MFI_STATE_FW_INIT;
3609 break;
3610
3611 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003612 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003613 cur_state = MFI_STATE_FW_INIT_2;
3614 break;
3615
3616 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003617 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003618 cur_state = MFI_STATE_DEVICE_SCAN;
3619 break;
3620
3621 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003622 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003623 cur_state = MFI_STATE_FLUSH_CACHE;
3624 break;
3625
3626 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003627 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003628 fw_state);
3629 return -ENODEV;
3630 }
3631
3632 /*
3633 * The cur_state should not last for more than max_wait secs
3634 */
3635 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003636 curr_abs_state = instance->instancet->
3637 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003638
Yang, Bo7218df62009-10-06 14:52:20 -06003639 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003640 msleep(1);
3641 } else
3642 break;
3643 }
3644
3645 /*
3646 * Return error if fw_state hasn't changed after max_wait
3647 */
Yang, Bo7218df62009-10-06 14:52:20 -06003648 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003649 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003650 "in %d secs\n", fw_state, max_wait);
3651 return -ENODEV;
3652 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003653
3654 abs_state = curr_abs_state;
3655 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003656 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003657 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003658
3659 return 0;
3660}
3661
3662/**
3663 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3664 * @instance: Adapter soft state
3665 */
3666static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3667{
3668 int i;
adam radford9c915a82010-12-21 13:34:31 -08003669 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003670 struct megasas_cmd *cmd;
3671
3672 if (!instance->frame_dma_pool)
3673 return;
3674
3675 /*
3676 * Return all frames to pool
3677 */
3678 for (i = 0; i < max_cmd; i++) {
3679
3680 cmd = instance->cmd_list[i];
3681
3682 if (cmd->frame)
3683 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3684 cmd->frame_phys_addr);
3685
3686 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003687 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003688 cmd->sense_phys_addr);
3689 }
3690
3691 /*
3692 * Now destroy the pool itself
3693 */
3694 pci_pool_destroy(instance->frame_dma_pool);
3695 pci_pool_destroy(instance->sense_dma_pool);
3696
3697 instance->frame_dma_pool = NULL;
3698 instance->sense_dma_pool = NULL;
3699}
3700
3701/**
3702 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3703 * @instance: Adapter soft state
3704 *
3705 * Each command packet has an embedded DMA memory buffer that is used for
3706 * filling MFI frame and the SG list that immediately follows the frame. This
3707 * function creates those DMA memory buffers for each command packet by using
3708 * PCI pool facility.
3709 */
3710static int megasas_create_frame_pool(struct megasas_instance *instance)
3711{
3712 int i;
3713 u32 max_cmd;
3714 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003715 u32 total_sz;
3716 u32 frame_count;
3717 struct megasas_cmd *cmd;
3718
adam radford9c915a82010-12-21 13:34:31 -08003719 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003720
3721 /*
3722 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3723 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3724 */
3725 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3726 sizeof(struct megasas_sge32);
3727
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003728 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003729 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003730
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003731 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303732 * For MFI controllers.
3733 * max_num_sge = 60
3734 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3735 * Total 960 byte (15 MFI frame of 64 byte)
3736 *
3737 * Fusion adapter require only 3 extra frame.
3738 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3739 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3740 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003741 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303742 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003743 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3744 /*
3745 * Use DMA pool facility provided by PCI layer
3746 */
3747 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303748 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003749
3750 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003751 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003752 return -ENOMEM;
3753 }
3754
3755 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3756 instance->pdev, 128, 4, 0);
3757
3758 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003759 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003760
3761 pci_pool_destroy(instance->frame_dma_pool);
3762 instance->frame_dma_pool = NULL;
3763
3764 return -ENOMEM;
3765 }
3766
3767 /*
3768 * Allocate and attach a frame to each of the commands in cmd_list.
3769 * By making cmd->index as the context instead of the &cmd, we can
3770 * always use 32bit context regardless of the architecture
3771 */
3772 for (i = 0; i < max_cmd; i++) {
3773
3774 cmd = instance->cmd_list[i];
3775
3776 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3777 GFP_KERNEL, &cmd->frame_phys_addr);
3778
3779 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3780 GFP_KERNEL, &cmd->sense_phys_addr);
3781
3782 /*
3783 * megasas_teardown_frame_pool() takes care of freeing
3784 * whatever has been allocated
3785 */
3786 if (!cmd->frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003787 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003788 megasas_teardown_frame_pool(instance);
3789 return -ENOMEM;
3790 }
3791
Yang, Bo707e09b2010-10-12 07:20:27 -06003792 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303793 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003794 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303795 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003796 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003797 }
3798
3799 return 0;
3800}
3801
3802/**
3803 * megasas_free_cmds - Free all the cmds in the free cmd pool
3804 * @instance: Adapter soft state
3805 */
adam radford9c915a82010-12-21 13:34:31 -08003806void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003807{
3808 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003809
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003810 /* First free the MFI frame pool */
3811 megasas_teardown_frame_pool(instance);
3812
3813 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003814 for (i = 0; i < instance->max_mfi_cmds; i++)
3815
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003816 kfree(instance->cmd_list[i]);
3817
3818 /* Free the cmd_list buffer itself */
3819 kfree(instance->cmd_list);
3820 instance->cmd_list = NULL;
3821
3822 INIT_LIST_HEAD(&instance->cmd_pool);
3823}
3824
3825/**
3826 * megasas_alloc_cmds - Allocates the command packets
3827 * @instance: Adapter soft state
3828 *
3829 * Each command that is issued to the FW, whether IO commands from the OS or
3830 * internal commands like IOCTLs, are wrapped in local data structure called
3831 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3832 * the FW.
3833 *
3834 * Each frame has a 32-bit field called context (tag). This context is used
3835 * to get back the megasas_cmd from the frame when a frame gets completed in
3836 * the ISR. Typically the address of the megasas_cmd itself would be used as
3837 * the context. But we wanted to keep the differences between 32 and 64 bit
3838 * systems to the mininum. We always use 32 bit integers for the context. In
3839 * this driver, the 32 bit values are the indices into an array cmd_list.
3840 * This array is used only to look up the megasas_cmd given the context. The
3841 * free commands themselves are maintained in a linked list called cmd_pool.
3842 */
adam radford9c915a82010-12-21 13:34:31 -08003843int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003844{
3845 int i;
3846 int j;
3847 u32 max_cmd;
3848 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303849 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003850
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303851 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003852 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003853
3854 /*
3855 * instance->cmd_list is an array of struct megasas_cmd pointers.
3856 * Allocate the dynamic array first and then allocate individual
3857 * commands.
3858 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003859 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003860
3861 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003862 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003863 return -ENOMEM;
3864 }
3865
adam radford9c915a82010-12-21 13:34:31 -08003866 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003867
3868 for (i = 0; i < max_cmd; i++) {
3869 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3870 GFP_KERNEL);
3871
3872 if (!instance->cmd_list[i]) {
3873
3874 for (j = 0; j < i; j++)
3875 kfree(instance->cmd_list[j]);
3876
3877 kfree(instance->cmd_list);
3878 instance->cmd_list = NULL;
3879
3880 return -ENOMEM;
3881 }
3882 }
3883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003884 for (i = 0; i < max_cmd; i++) {
3885 cmd = instance->cmd_list[i];
3886 memset(cmd, 0, sizeof(struct megasas_cmd));
3887 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003888 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003889 cmd->instance = instance;
3890
3891 list_add_tail(&cmd->list, &instance->cmd_pool);
3892 }
3893
3894 /*
3895 * Create a frame pool and assign one frame to each cmd
3896 */
3897 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003898 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003899 megasas_free_cmds(instance);
3900 }
3901
3902 return 0;
3903}
3904
Yang, Bo81e403c2009-10-06 14:27:54 -06003905/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303906 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
3907 * @instance: Adapter soft state
3908 *
3909 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
3910 * or FW is not under OCR.
3911 */
3912inline int
3913dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
3914
3915 if (!instance->ctrl_context)
3916 return KILL_ADAPTER;
3917 else if (instance->unload ||
3918 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
3919 return IGNORE_TIMEOUT;
3920 else
3921 return INITIATE_OCR;
3922}
3923
3924/*
Yang, Bo81e403c2009-10-06 14:27:54 -06003925 * megasas_get_pd_list_info - Returns FW's pd_list structure
3926 * @instance: Adapter soft state
3927 * @pd_list: pd_list structure
3928 *
3929 * Issues an internal command (DCMD) to get the FW's controller PD
3930 * list structure. This information is mainly used to find out SYSTEM
3931 * supported by the FW.
3932 */
3933static int
3934megasas_get_pd_list(struct megasas_instance *instance)
3935{
3936 int ret = 0, pd_index = 0;
3937 struct megasas_cmd *cmd;
3938 struct megasas_dcmd_frame *dcmd;
3939 struct MR_PD_LIST *ci;
3940 struct MR_PD_ADDRESS *pd_addr;
3941 dma_addr_t ci_h = 0;
3942
3943 cmd = megasas_get_cmd(instance);
3944
3945 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003946 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06003947 return -ENOMEM;
3948 }
3949
3950 dcmd = &cmd->frame->dcmd;
3951
3952 ci = pci_alloc_consistent(instance->pdev,
3953 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3954
3955 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003956 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06003957 megasas_return_cmd(instance, cmd);
3958 return -ENOMEM;
3959 }
3960
3961 memset(ci, 0, sizeof(*ci));
3962 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3963
3964 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3965 dcmd->mbox.b[1] = 0;
3966 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303967 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06003968 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303969 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06003970 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003971 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303972 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3973 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3974 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3975 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06003976
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303977 if (instance->ctrl_context && !instance->mask_interrupts)
3978 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303979 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303980 else
3981 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003982
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303983 switch (ret) {
3984 case DCMD_FAILED:
3985 megaraid_sas_kill_hba(instance);
3986 break;
3987 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06003988
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303989 switch (dcmd_timeout_ocr_possible(instance)) {
3990 case INITIATE_OCR:
3991 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
3992 /*
3993 * DCMD failed from AEN path.
3994 * AEN path already hold reset_mutex to avoid PCI access
3995 * while OCR is in progress.
3996 */
3997 mutex_unlock(&instance->reset_mutex);
3998 megasas_reset_fusion(instance->host,
3999 MFI_IO_TIMEOUT_OCR);
4000 mutex_lock(&instance->reset_mutex);
4001 break;
4002 case KILL_ADAPTER:
4003 megaraid_sas_kill_hba(instance);
4004 break;
4005 case IGNORE_TIMEOUT:
4006 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4007 __func__, __LINE__);
4008 break;
4009 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004010
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304011 break;
4012
4013 case DCMD_SUCCESS:
4014 pd_addr = ci->addr;
4015
4016 if ((le32_to_cpu(ci->count) >
4017 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4018 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004019
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304020 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304021 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004022
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304023 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304024 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304025 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304026 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304027 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304028 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304029 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004030 pd_addr++;
4031 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304032
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304033 memcpy(instance->pd_list, instance->local_pd_list,
4034 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304035 break;
4036
Yang, Bo81e403c2009-10-06 14:27:54 -06004037 }
4038
4039 pci_free_consistent(instance->pdev,
4040 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4041 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304042
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304043 if (ret != DCMD_TIMEOUT)
4044 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004045
4046 return ret;
4047}
4048
Yang, Bobdc6fb82009-12-06 08:30:19 -07004049/*
4050 * megasas_get_ld_list_info - Returns FW's ld_list structure
4051 * @instance: Adapter soft state
4052 * @ld_list: ld_list structure
4053 *
4054 * Issues an internal command (DCMD) to get the FW's controller PD
4055 * list structure. This information is mainly used to find out SYSTEM
4056 * supported by the FW.
4057 */
4058static int
4059megasas_get_ld_list(struct megasas_instance *instance)
4060{
4061 int ret = 0, ld_index = 0, ids = 0;
4062 struct megasas_cmd *cmd;
4063 struct megasas_dcmd_frame *dcmd;
4064 struct MR_LD_LIST *ci;
4065 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304066 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004067
4068 cmd = megasas_get_cmd(instance);
4069
4070 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004071 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004072 return -ENOMEM;
4073 }
4074
4075 dcmd = &cmd->frame->dcmd;
4076
4077 ci = pci_alloc_consistent(instance->pdev,
4078 sizeof(struct MR_LD_LIST),
4079 &ci_h);
4080
4081 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004082 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004083 megasas_return_cmd(instance, cmd);
4084 return -ENOMEM;
4085 }
4086
4087 memset(ci, 0, sizeof(*ci));
4088 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4089
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304090 if (instance->supportmax256vd)
4091 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004092 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304093 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004094 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304095 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004096 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304097 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4098 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4099 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4100 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004101 dcmd->pad_0 = 0;
4102
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304103 if (instance->ctrl_context && !instance->mask_interrupts)
4104 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304105 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304106 else
4107 ret = megasas_issue_polled(instance, cmd);
4108
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304109 ld_count = le32_to_cpu(ci->ldCount);
4110
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304111 switch (ret) {
4112 case DCMD_FAILED:
4113 megaraid_sas_kill_hba(instance);
4114 break;
4115 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004116
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304117 switch (dcmd_timeout_ocr_possible(instance)) {
4118 case INITIATE_OCR:
4119 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4120 /*
4121 * DCMD failed from AEN path.
4122 * AEN path already hold reset_mutex to avoid PCI access
4123 * while OCR is in progress.
4124 */
4125 mutex_unlock(&instance->reset_mutex);
4126 megasas_reset_fusion(instance->host,
4127 MFI_IO_TIMEOUT_OCR);
4128 mutex_lock(&instance->reset_mutex);
4129 break;
4130 case KILL_ADAPTER:
4131 megaraid_sas_kill_hba(instance);
4132 break;
4133 case IGNORE_TIMEOUT:
4134 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4135 __func__, __LINE__);
4136 break;
4137 }
4138
4139 break;
4140
4141 case DCMD_SUCCESS:
4142 if (ld_count > instance->fw_supported_vd_count)
4143 break;
4144
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304145 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004146
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304147 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004148 if (ci->ldList[ld_index].state != 0) {
4149 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304150 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004151 }
4152 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304153
4154 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004155 }
4156
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304157 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004158
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304159 if (ret != DCMD_TIMEOUT)
4160 megasas_return_cmd(instance, cmd);
4161
Yang, Bobdc6fb82009-12-06 08:30:19 -07004162 return ret;
4163}
4164
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004165/**
adam radford21c9e162013-09-06 15:27:14 -07004166 * megasas_ld_list_query - Returns FW's ld_list structure
4167 * @instance: Adapter soft state
4168 * @ld_list: ld_list structure
4169 *
4170 * Issues an internal command (DCMD) to get the FW's controller PD
4171 * list structure. This information is mainly used to find out SYSTEM
4172 * supported by the FW.
4173 */
4174static int
4175megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4176{
4177 int ret = 0, ld_index = 0, ids = 0;
4178 struct megasas_cmd *cmd;
4179 struct megasas_dcmd_frame *dcmd;
4180 struct MR_LD_TARGETID_LIST *ci;
4181 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304182 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004183
4184 cmd = megasas_get_cmd(instance);
4185
4186 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004187 dev_warn(&instance->pdev->dev,
4188 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004189 return -ENOMEM;
4190 }
4191
4192 dcmd = &cmd->frame->dcmd;
4193
4194 ci = pci_alloc_consistent(instance->pdev,
4195 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4196
4197 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004198 dev_warn(&instance->pdev->dev,
4199 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004200 megasas_return_cmd(instance, cmd);
4201 return -ENOMEM;
4202 }
4203
4204 memset(ci, 0, sizeof(*ci));
4205 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4206
4207 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304208 if (instance->supportmax256vd)
4209 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004210
4211 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304212 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004213 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304214 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004215 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304216 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4217 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4218 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4219 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004220 dcmd->pad_0 = 0;
4221
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304222 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304223 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304224 else
4225 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004226
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304227 switch (ret) {
4228 case DCMD_FAILED:
4229 dev_info(&instance->pdev->dev,
4230 "DCMD not supported by firmware - %s %d\n",
4231 __func__, __LINE__);
4232 ret = megasas_get_ld_list(instance);
4233 break;
4234 case DCMD_TIMEOUT:
4235 switch (dcmd_timeout_ocr_possible(instance)) {
4236 case INITIATE_OCR:
4237 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4238 /*
4239 * DCMD failed from AEN path.
4240 * AEN path already hold reset_mutex to avoid PCI access
4241 * while OCR is in progress.
4242 */
4243 mutex_unlock(&instance->reset_mutex);
4244 megasas_reset_fusion(instance->host,
4245 MFI_IO_TIMEOUT_OCR);
4246 mutex_lock(&instance->reset_mutex);
4247 break;
4248 case KILL_ADAPTER:
4249 megaraid_sas_kill_hba(instance);
4250 break;
4251 case IGNORE_TIMEOUT:
4252 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4253 __func__, __LINE__);
4254 break;
4255 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304256
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304257 break;
4258 case DCMD_SUCCESS:
4259 tgtid_count = le32_to_cpu(ci->count);
4260
4261 if ((tgtid_count > (instance->fw_supported_vd_count)))
4262 break;
4263
adam radford21c9e162013-09-06 15:27:14 -07004264 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304265 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004266 ids = ci->targetId[ld_index];
4267 instance->ld_ids[ids] = ci->targetId[ld_index];
4268 }
4269
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304270 break;
adam radford21c9e162013-09-06 15:27:14 -07004271 }
4272
4273 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304274 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004275
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304276 if (ret != DCMD_TIMEOUT)
4277 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004278
4279 return ret;
4280}
4281
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304282/*
4283 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4284 * instance : Controller's instance
4285*/
4286static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4287{
4288 struct fusion_context *fusion;
4289 u32 old_map_sz;
4290 u32 new_map_sz;
4291
4292 fusion = instance->ctrl_context;
4293 /* For MFI based controllers return dummy success */
4294 if (!fusion)
4295 return;
4296
4297 instance->supportmax256vd =
4298 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4299 /* Below is additional check to address future FW enhancement */
4300 if (instance->ctrl_info->max_lds > 64)
4301 instance->supportmax256vd = 1;
4302
4303 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4304 * MEGASAS_MAX_DEV_PER_CHANNEL;
4305 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4306 * MEGASAS_MAX_DEV_PER_CHANNEL;
4307 if (instance->supportmax256vd) {
4308 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4309 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4310 } else {
4311 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4312 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4313 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304314
4315 dev_info(&instance->pdev->dev,
4316 "firmware type\t: %s\n",
4317 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4318 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304319
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004320 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304321 (sizeof(struct MR_LD_SPAN_MAP) *
4322 (instance->fw_supported_vd_count - 1));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004323 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4324 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304325 (sizeof(struct MR_LD_SPAN_MAP) *
4326 (instance->drv_supported_vd_count - 1));
4327
4328 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4329
4330
4331 if (instance->supportmax256vd)
4332 fusion->current_map_sz = new_map_sz;
4333 else
4334 fusion->current_map_sz = old_map_sz;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304335}
4336
adam radford21c9e162013-09-06 15:27:14 -07004337/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004338 * megasas_get_controller_info - Returns FW's controller structure
4339 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004340 *
4341 * Issues an internal command (DCMD) to get the FW's controller structure.
4342 * This information is mainly used to find out the maximum IO transfer per
4343 * command supported by the FW.
4344 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304345int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304346megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004347{
4348 int ret = 0;
4349 struct megasas_cmd *cmd;
4350 struct megasas_dcmd_frame *dcmd;
4351 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304352 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004353 dma_addr_t ci_h = 0;
4354
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304355 ctrl_info = instance->ctrl_info;
4356
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004357 cmd = megasas_get_cmd(instance);
4358
4359 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004360 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004361 return -ENOMEM;
4362 }
4363
4364 dcmd = &cmd->frame->dcmd;
4365
4366 ci = pci_alloc_consistent(instance->pdev,
4367 sizeof(struct megasas_ctrl_info), &ci_h);
4368
4369 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004370 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004371 megasas_return_cmd(instance, cmd);
4372 return -ENOMEM;
4373 }
4374
4375 memset(ci, 0, sizeof(*ci));
4376 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4377
4378 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304379 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004380 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304381 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004382 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004383 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304384 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4385 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4386 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4387 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304388 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004389
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304390 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304391 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304392 else
4393 ret = megasas_issue_polled(instance, cmd);
4394
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304395 switch (ret) {
4396 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004397 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304398 /* Save required controller information in
4399 * CPU endianness format.
4400 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304401 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4402 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4403 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304404
4405 /* Update the latest Ext VD info.
4406 * From Init path, store current firmware details.
4407 * From OCR path, detect any firmware properties changes.
4408 * in case of Firmware upgrade without system reboot.
4409 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304410 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304411 instance->use_seqnum_jbod_fp =
4412 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304413
4414 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304415 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4416 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304417 "controller type\t: %s(%dMB)\n",
4418 instance->is_imr ? "iMR" : "MR",
4419 le16_to_cpu(ctrl_info->memory_size));
4420
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304421 instance->disableOnlineCtrlReset =
4422 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304423 instance->secure_jbod_support =
4424 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304425 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4426 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304427 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4428 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304429 break;
4430
4431 case DCMD_TIMEOUT:
4432 switch (dcmd_timeout_ocr_possible(instance)) {
4433 case INITIATE_OCR:
4434 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4435 megasas_reset_fusion(instance->host,
4436 MFI_IO_TIMEOUT_OCR);
4437 break;
4438 case KILL_ADAPTER:
4439 megaraid_sas_kill_hba(instance);
4440 break;
4441 case IGNORE_TIMEOUT:
4442 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4443 __func__, __LINE__);
4444 break;
4445 }
4446 case DCMD_FAILED:
4447 megaraid_sas_kill_hba(instance);
4448 break;
4449
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304450 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004451
4452 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4453 ci, ci_h);
4454
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304455 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304456
4457
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004458 return ret;
4459}
4460
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304461/*
4462 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4463 * to firmware
4464 *
4465 * @instance: Adapter soft state
4466 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4467 MR_CRASH_BUF_TURN_OFF = 0
4468 MR_CRASH_BUF_TURN_ON = 1
4469 * @return 0 on success non-zero on failure.
4470 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4471 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4472 * that driver supports crash dump feature. This DCMD will be sent only if
4473 * crash dump feature is supported by the FW.
4474 *
4475 */
4476int megasas_set_crash_dump_params(struct megasas_instance *instance,
4477 u8 crash_buf_state)
4478{
4479 int ret = 0;
4480 struct megasas_cmd *cmd;
4481 struct megasas_dcmd_frame *dcmd;
4482
4483 cmd = megasas_get_cmd(instance);
4484
4485 if (!cmd) {
4486 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4487 return -ENOMEM;
4488 }
4489
4490
4491 dcmd = &cmd->frame->dcmd;
4492
4493 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4494 dcmd->mbox.b[0] = crash_buf_state;
4495 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304496 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304497 dcmd->sge_count = 1;
4498 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4499 dcmd->timeout = 0;
4500 dcmd->pad_0 = 0;
4501 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4502 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4503 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4504 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4505
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304506 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304507 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304508 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304509 ret = megasas_issue_polled(instance, cmd);
4510
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304511 if (ret == DCMD_TIMEOUT) {
4512 switch (dcmd_timeout_ocr_possible(instance)) {
4513 case INITIATE_OCR:
4514 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4515 megasas_reset_fusion(instance->host,
4516 MFI_IO_TIMEOUT_OCR);
4517 break;
4518 case KILL_ADAPTER:
4519 megaraid_sas_kill_hba(instance);
4520 break;
4521 case IGNORE_TIMEOUT:
4522 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4523 __func__, __LINE__);
4524 break;
4525 }
4526 } else
4527 megasas_return_cmd(instance, cmd);
4528
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304529 return ret;
4530}
4531
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004532/**
bo yang31ea7082007-11-07 12:09:50 -05004533 * megasas_issue_init_mfi - Initializes the FW
4534 * @instance: Adapter soft state
4535 *
4536 * Issues the INIT MFI cmd
4537 */
4538static int
4539megasas_issue_init_mfi(struct megasas_instance *instance)
4540{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304541 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004542 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004543 struct megasas_init_frame *init_frame;
4544 struct megasas_init_queue_info *initq_info;
4545 dma_addr_t init_frame_h;
4546 dma_addr_t initq_info_h;
4547
4548 /*
4549 * Prepare a init frame. Note the init frame points to queue info
4550 * structure. Each frame has SGL allocated after first 64 bytes. For
4551 * this frame - since we don't need any SGL - we use SGL's space as
4552 * queue info structure
4553 *
4554 * We will not get a NULL command below. We just created the pool.
4555 */
4556 cmd = megasas_get_cmd(instance);
4557
4558 init_frame = (struct megasas_init_frame *)cmd->frame;
4559 initq_info = (struct megasas_init_queue_info *)
4560 ((unsigned long)init_frame + 64);
4561
4562 init_frame_h = cmd->frame_phys_addr;
4563 initq_info_h = init_frame_h + 64;
4564
4565 context = init_frame->context;
4566 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4567 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4568 init_frame->context = context;
4569
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304570 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4571 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004572
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304573 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4574 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004575
4576 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304577 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304578 init_frame->queue_info_new_phys_addr_lo =
4579 cpu_to_le32(lower_32_bits(initq_info_h));
4580 init_frame->queue_info_new_phys_addr_hi =
4581 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004582
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304583 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004584
4585 /*
4586 * disable the intr before firing the init frame to FW
4587 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304588 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004589
4590 /*
4591 * Issue the init frame in polled mode
4592 */
4593
4594 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004595 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004596 megasas_return_cmd(instance, cmd);
4597 goto fail_fw_init;
4598 }
4599
4600 megasas_return_cmd(instance, cmd);
4601
4602 return 0;
4603
4604fail_fw_init:
4605 return -EINVAL;
4606}
4607
adam radfordcd50ba82010-12-21 10:23:23 -08004608static u32
4609megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004610{
adam radfordcd50ba82010-12-21 10:23:23 -08004611 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004612 u32 context_sz;
4613 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004614
4615 reg_set = instance->reg_set;
4616
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004617 /*
4618 * Get various operational parameters from status register
4619 */
Sumant Patro1341c932006-01-25 12:02:40 -08004620 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004621 /*
4622 * Reduce the max supported cmds by 1. This is to ensure that the
4623 * reply_q_sz (1 more than the max cmd that driver may send)
4624 * does not exceed max cmds that the FW can support
4625 */
4626 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004627 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004628 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004629 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004630 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304631 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4632 * are reserved for IOCTL + driver's internal DCMDs.
4633 */
4634 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4635 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4636 instance->max_scsi_cmds = (instance->max_fw_cmds -
4637 MEGASAS_SKINNY_INT_CMDS);
4638 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4639 } else {
4640 instance->max_scsi_cmds = (instance->max_fw_cmds -
4641 MEGASAS_INT_CMDS);
4642 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4643 }
4644
4645 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004646 * Create a pool of commands
4647 */
4648 if (megasas_alloc_cmds(instance))
4649 goto fail_alloc_cmds;
4650
4651 /*
4652 * Allocate memory for reply queue. Length of reply queue should
4653 * be _one_ more than the maximum commands handled by the firmware.
4654 *
4655 * Note: When FW completes commands, it places corresponding contex
4656 * values in this circular reply queue. This circular queue is a fairly
4657 * typical producer-consumer queue. FW is the producer (of completed
4658 * commands) and the driver is the consumer.
4659 */
4660 context_sz = sizeof(u32);
4661 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4662
4663 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4664 reply_q_sz,
4665 &instance->reply_queue_h);
4666
4667 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004668 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004669 goto fail_reply_queue;
4670 }
4671
bo yang31ea7082007-11-07 12:09:50 -05004672 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004673 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004674
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304675 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304676 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4677 "Fail from %s %d\n", instance->unique_id,
4678 __func__, __LINE__);
4679 goto fail_fw_init;
4680 }
4681
bo yang39a98552010-09-22 22:36:29 -04004682 instance->fw_support_ieee = 0;
4683 instance->fw_support_ieee =
4684 (instance->instancet->read_fw_status_reg(reg_set) &
4685 0x04000000);
4686
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004687 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004688 instance->fw_support_ieee);
4689
4690 if (instance->fw_support_ieee)
4691 instance->flag_ieee = 1;
4692
adam radfordcd50ba82010-12-21 10:23:23 -08004693 return 0;
4694
4695fail_fw_init:
4696
4697 pci_free_consistent(instance->pdev, reply_q_sz,
4698 instance->reply_queue, instance->reply_queue_h);
4699fail_reply_queue:
4700 megasas_free_cmds(instance);
4701
4702fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004703 return 1;
4704}
4705
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304706/*
4707 * megasas_setup_irqs_msix - register legacy interrupts.
4708 * @instance: Adapter soft state
4709 *
4710 * Do not enable interrupt, only setup ISRs.
4711 *
4712 * Return 0 on success.
4713 */
4714static int
4715megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4716{
4717 struct pci_dev *pdev;
4718
4719 pdev = instance->pdev;
4720 instance->irq_context[0].instance = instance;
4721 instance->irq_context[0].MSIxIndex = 0;
4722 if (request_irq(pdev->irq, instance->instancet->service_isr,
4723 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4724 dev_err(&instance->pdev->dev,
4725 "Failed to register IRQ from %s %d\n",
4726 __func__, __LINE__);
4727 return -1;
4728 }
4729 return 0;
4730}
4731
4732/**
4733 * megasas_setup_irqs_msix - register MSI-x interrupts.
4734 * @instance: Adapter soft state
4735 * @is_probe: Driver probe check
4736 *
4737 * Do not enable interrupt, only setup ISRs.
4738 *
4739 * Return 0 on success.
4740 */
4741static int
4742megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4743{
4744 int i, j, cpu;
4745 struct pci_dev *pdev;
4746
4747 pdev = instance->pdev;
4748
4749 /* Try MSI-x */
4750 cpu = cpumask_first(cpu_online_mask);
4751 for (i = 0; i < instance->msix_vectors; i++) {
4752 instance->irq_context[i].instance = instance;
4753 instance->irq_context[i].MSIxIndex = i;
4754 if (request_irq(instance->msixentry[i].vector,
4755 instance->instancet->service_isr, 0, "megasas",
4756 &instance->irq_context[i])) {
4757 dev_err(&instance->pdev->dev,
4758 "Failed to register IRQ for vector %d.\n", i);
4759 for (j = 0; j < i; j++) {
4760 if (smp_affinity_enable)
4761 irq_set_affinity_hint(
4762 instance->msixentry[j].vector, NULL);
4763 free_irq(instance->msixentry[j].vector,
4764 &instance->irq_context[j]);
4765 }
4766 /* Retry irq register for IO_APIC*/
4767 instance->msix_vectors = 0;
4768 if (is_probe)
4769 return megasas_setup_irqs_ioapic(instance);
4770 else
4771 return -1;
4772 }
4773 if (smp_affinity_enable) {
4774 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4775 get_cpu_mask(cpu)))
4776 dev_err(&instance->pdev->dev,
4777 "Failed to set affinity hint"
4778 " for cpu %d\n", cpu);
4779 cpu = cpumask_next(cpu, cpu_online_mask);
4780 }
4781 }
4782 return 0;
4783}
4784
4785/*
4786 * megasas_destroy_irqs- unregister interrupts.
4787 * @instance: Adapter soft state
4788 * return: void
4789 */
4790static void
4791megasas_destroy_irqs(struct megasas_instance *instance) {
4792
4793 int i;
4794
4795 if (instance->msix_vectors)
4796 for (i = 0; i < instance->msix_vectors; i++) {
4797 if (smp_affinity_enable)
4798 irq_set_affinity_hint(
4799 instance->msixentry[i].vector, NULL);
4800 free_irq(instance->msixentry[i].vector,
4801 &instance->irq_context[i]);
4802 }
4803 else
4804 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4805}
4806
adam radfordcd50ba82010-12-21 10:23:23 -08004807/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304808 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4809 * @instance: Adapter soft state
4810 * @is_probe: Driver probe check
4811 *
4812 * Return 0 on success.
4813 */
4814void
4815megasas_setup_jbod_map(struct megasas_instance *instance)
4816{
4817 int i;
4818 struct fusion_context *fusion = instance->ctrl_context;
4819 u32 pd_seq_map_sz;
4820
4821 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4822 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4823
4824 if (reset_devices || !fusion ||
4825 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4826 dev_info(&instance->pdev->dev,
4827 "Jbod map is not supported %s %d\n",
4828 __func__, __LINE__);
4829 instance->use_seqnum_jbod_fp = false;
4830 return;
4831 }
4832
4833 if (fusion->pd_seq_sync[0])
4834 goto skip_alloc;
4835
4836 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4837 fusion->pd_seq_sync[i] = dma_alloc_coherent
4838 (&instance->pdev->dev, pd_seq_map_sz,
4839 &fusion->pd_seq_phys[i], GFP_KERNEL);
4840 if (!fusion->pd_seq_sync[i]) {
4841 dev_err(&instance->pdev->dev,
4842 "Failed to allocate memory from %s %d\n",
4843 __func__, __LINE__);
4844 if (i == 1) {
4845 dma_free_coherent(&instance->pdev->dev,
4846 pd_seq_map_sz, fusion->pd_seq_sync[0],
4847 fusion->pd_seq_phys[0]);
4848 fusion->pd_seq_sync[0] = NULL;
4849 }
4850 instance->use_seqnum_jbod_fp = false;
4851 return;
4852 }
4853 }
4854
4855skip_alloc:
4856 if (!megasas_sync_pd_seq_num(instance, false) &&
4857 !megasas_sync_pd_seq_num(instance, true))
4858 instance->use_seqnum_jbod_fp = true;
4859 else
4860 instance->use_seqnum_jbod_fp = false;
4861}
4862
4863/**
adam radfordcd50ba82010-12-21 10:23:23 -08004864 * megasas_init_fw - Initializes the FW
4865 * @instance: Adapter soft state
4866 *
4867 * This is the main function for initializing firmware
4868 */
4869
4870static int megasas_init_fw(struct megasas_instance *instance)
4871{
4872 u32 max_sectors_1;
4873 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304874 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04004875 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08004876 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304877 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08004878 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304879 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07004880 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05304881 struct fusion_context *fusion;
4882
4883 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08004884
4885 /* Find first memory bar */
4886 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4887 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
adam radfordcd50ba82010-12-21 10:23:23 -08004888 if (pci_request_selected_regions(instance->pdev, instance->bar,
4889 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004890 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08004891 return -EBUSY;
4892 }
4893
Ben Collins11f8a7b2013-09-13 12:46:44 -04004894 base_addr = pci_resource_start(instance->pdev, instance->bar);
4895 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08004896
4897 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004898 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08004899 goto fail_ioremap;
4900 }
4901
4902 reg_set = instance->reg_set;
4903
4904 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08004905 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07004906 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07004907 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304908 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05304909 case PCI_DEVICE_ID_LSI_INTRUDER:
4910 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05304911 case PCI_DEVICE_ID_LSI_CUTLASS_52:
4912 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08004913 instance->instancet = &megasas_instance_template_fusion;
4914 break;
adam radfordcd50ba82010-12-21 10:23:23 -08004915 case PCI_DEVICE_ID_LSI_SAS1078R:
4916 case PCI_DEVICE_ID_LSI_SAS1078DE:
4917 instance->instancet = &megasas_instance_template_ppc;
4918 break;
4919 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4920 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4921 instance->instancet = &megasas_instance_template_gen2;
4922 break;
4923 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4924 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4925 instance->instancet = &megasas_instance_template_skinny;
4926 break;
4927 case PCI_DEVICE_ID_LSI_SAS1064R:
4928 case PCI_DEVICE_ID_DELL_PERC5:
4929 default:
4930 instance->instancet = &megasas_instance_template_xscale;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05304931 instance->allow_fw_scan = 1;
adam radfordcd50ba82010-12-21 10:23:23 -08004932 break;
4933 }
4934
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05304935 if (megasas_transition_to_ready(instance, 0)) {
4936 atomic_set(&instance->fw_reset_no_pci_access, 1);
4937 instance->instancet->adp_reset
4938 (instance, instance->reg_set);
4939 atomic_set(&instance->fw_reset_no_pci_access, 0);
4940 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004941 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05304942 __func__);
4943
4944 /*waitting for about 30 second before retry*/
4945 ssleep(30);
4946
4947 if (megasas_transition_to_ready(instance, 0))
4948 goto fail_ready_state;
4949 }
adam radfordcd50ba82010-12-21 10:23:23 -08004950
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304951 /*
4952 * MSI-X host index 0 is common for all adapter.
4953 * It is used for all MPT based Adapters.
4954 */
4955 instance->reply_post_host_index_addr[0] =
Christoph Hellwig8a232bb2015-04-23 16:32:39 +05304956 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304957 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4958
adam radford3f1abce2011-05-11 18:33:47 -07004959 /* Check if MSI-X is supported while in ready state */
4960 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4961 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07004962 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304963 scratch_pad_2 = readl
4964 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07004965 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05304966 if (fusion) {
4967 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
4968 instance->msix_vectors = (scratch_pad_2
4969 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4970 fw_msix_count = instance->msix_vectors;
4971 } else { /* Invader series supports more than 8 MSI-x vectors*/
4972 instance->msix_vectors = ((scratch_pad_2
4973 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4974 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4975 fw_msix_count = instance->msix_vectors;
4976 /* Save 1-15 reply post index address to local memory
4977 * Index 0 is already saved from reg offset
4978 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4979 */
4980 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4981 instance->reply_post_host_index_addr[loop] =
4982 (u32 __iomem *)
4983 ((u8 __iomem *)instance->reg_set +
4984 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4985 + (loop * 0x10));
4986 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304987 }
4988 if (msix_vectors)
4989 instance->msix_vectors = min(msix_vectors,
4990 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05304991 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07004992 instance->msix_vectors = 1;
4993 /* Don't bother allocating more MSI-X vectors than cpus */
4994 instance->msix_vectors = min(instance->msix_vectors,
4995 (unsigned int)num_online_cpus());
4996 for (i = 0; i < instance->msix_vectors; i++)
4997 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004998 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4999 1, instance->msix_vectors);
Jiang Liuc12de882014-11-03 20:44:20 +08005000 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005001 instance->msix_vectors = i;
5002 else
adam radfordc8e858f2011-10-08 18:15:13 -07005003 instance->msix_vectors = 0;
5004 }
adam radford3f1abce2011-05-11 18:33:47 -07005005
Tomas Henzl258c3af2015-06-02 16:09:46 +05305006 dev_info(&instance->pdev->dev,
5007 "firmware supports msix\t: (%d)", fw_msix_count);
5008 dev_info(&instance->pdev->dev,
5009 "current msix/online cpus\t: (%d/%d)\n",
5010 instance->msix_vectors, (unsigned int)num_online_cpus());
5011
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305012 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5013 (unsigned long)instance);
5014
Tomas Henzl258c3af2015-06-02 16:09:46 +05305015 if (instance->msix_vectors ?
5016 megasas_setup_irqs_msix(instance, 1) :
5017 megasas_setup_irqs_ioapic(instance))
5018 goto fail_setup_irqs;
5019
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305020 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5021 GFP_KERNEL);
5022 if (instance->ctrl_info == NULL)
5023 goto fail_init_adapter;
5024
5025 /*
5026 * Below are default value for legacy Firmware.
5027 * non-fusion based controllers
5028 */
5029 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5030 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005031 /* Get operational params, sge flags, send init cmd to controller */
5032 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005033 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005034
Tomas Henzl258c3af2015-06-02 16:09:46 +05305035
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305036 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005037
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005038 dev_err(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005039
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305040 megasas_setup_jbod_map(instance);
5041
bo yang39a98552010-09-22 22:36:29 -04005042 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005043 * the following function will get the PD LIST.
5044 */
5045 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005046 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005047 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005048 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305049 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005050 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005051
Yang, Bobdc6fb82009-12-06 08:30:19 -07005052 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07005053 if (megasas_ld_list_query(instance,
5054 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5055 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07005056
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005057 /*
5058 * Compute the max allowed sectors per IO: The controller info has two
5059 * limits on max sectors. Driver should use the minimum of these two.
5060 *
5061 * 1 << stripe_sz_ops.min = max sectors per strip
5062 *
5063 * Note that older firmwares ( < FW ver 30) didn't report information
5064 * to calculate max_sectors_1. So the number ended up as zero always.
5065 */
bo yang14faea92007-11-09 04:14:00 -05005066 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305067 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005068
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305069 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5070 le16_to_cpu(ctrl_info->max_strips_per_io);
5071 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005072
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005073 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305074
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305075 instance->mpio = ctrl_info->adapterOperations2.mpio;
5076 instance->UnevenSpanSupport =
5077 ctrl_info->adapterOperations2.supportUnevenSpans;
5078 if (instance->UnevenSpanSupport) {
5079 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305080 if (MR_ValidateMapInfo(instance))
5081 fusion->fast_path_io = 1;
5082 else
5083 fusion->fast_path_io = 0;
5084
5085 }
5086 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305087 instance->requestorId = ctrl_info->iov.requestorId;
5088 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5089 if (!ctrl_info->adapterOperations2.activePassive)
5090 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305091
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305092 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5093 instance->PlasmaFW111 ? "1.11" : "new");
5094
5095 if (instance->PlasmaFW111) {
5096 iovPtr = (struct IOV_111 *)
5097 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5098 instance->requestorId = iovPtr->requestorId;
5099 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305100 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305101 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5102 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305103 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305104
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305105 instance->crash_dump_fw_support =
5106 ctrl_info->adapterOperations3.supportCrashDump;
5107 instance->crash_dump_drv_support =
5108 (instance->crash_dump_fw_support &&
5109 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305110 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305111 megasas_set_crash_dump_params(instance,
5112 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005113
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305114 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305115 if (instance->crash_dump_buf)
5116 pci_free_consistent(instance->pdev,
5117 CRASH_DMA_BUF_SIZE,
5118 instance->crash_dump_buf,
5119 instance->crash_dump_h);
5120 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005121 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305122
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305123
5124 dev_info(&instance->pdev->dev,
5125 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5126 le16_to_cpu(ctrl_info->pci.vendor_id),
5127 le16_to_cpu(ctrl_info->pci.device_id),
5128 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5129 le16_to_cpu(ctrl_info->pci.sub_device_id));
5130 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5131 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305132 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5133 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305134 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5135 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305136
5137
bo yang14faea92007-11-09 04:14:00 -05005138 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305139 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005140 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5141 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005142
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305143 /* Check for valid throttlequeuedepth module parameter */
5144 if (throttlequeuedepth &&
5145 throttlequeuedepth <= instance->max_scsi_cmds)
5146 instance->throttlequeuedepth = throttlequeuedepth;
5147 else
5148 instance->throttlequeuedepth =
5149 MEGASAS_THROTTLE_QUEUE_DEPTH;
5150
bo yangad84db22007-11-09 04:40:16 -05005151
adam radford229fe472014-03-10 02:51:56 -07005152 /* Launch SR-IOV heartbeat timer */
5153 if (instance->requestorId) {
5154 if (!megasas_sriov_start_heartbeat(instance, 1))
5155 megasas_start_timer(instance,
5156 &instance->sriov_heartbeat_timer,
5157 megasas_sriov_heartbeat_handler,
5158 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5159 else
5160 instance->skip_heartbeat_timer_del = 1;
5161 }
5162
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005163 return 0;
5164
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305165fail_get_pd_list:
5166 instance->instancet->disable_intr(instance);
adam radfordeb1b1232011-02-24 20:55:56 -08005167fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305168 megasas_destroy_irqs(instance);
5169fail_setup_irqs:
5170 if (instance->msix_vectors)
5171 pci_disable_msix(instance->pdev);
5172 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005173fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305174 kfree(instance->ctrl_info);
5175 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005176 iounmap(instance->reg_set);
5177
5178 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08005179 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005180
5181 return -EINVAL;
5182}
5183
5184/**
5185 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005186 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005187 */
5188static void megasas_release_mfi(struct megasas_instance *instance)
5189{
adam radford9c915a82010-12-21 13:34:31 -08005190 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005191
adam radford9c915a82010-12-21 13:34:31 -08005192 if (instance->reply_queue)
5193 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005194 instance->reply_queue, instance->reply_queue_h);
5195
5196 megasas_free_cmds(instance);
5197
5198 iounmap(instance->reg_set);
5199
adam radfordb6d5d882010-12-14 18:56:07 -08005200 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005201}
5202
5203/**
5204 * megasas_get_seq_num - Gets latest event sequence numbers
5205 * @instance: Adapter soft state
5206 * @eli: FW event log sequence numbers information
5207 *
5208 * FW maintains a log of all events in a non-volatile area. Upper layers would
5209 * usually find out the latest sequence number of the events, the seq number at
5210 * the boot etc. They would "read" all the events below the latest seq number
5211 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5212 * number), they would subsribe to AEN (asynchronous event notification) and
5213 * wait for the events to happen.
5214 */
5215static int
5216megasas_get_seq_num(struct megasas_instance *instance,
5217 struct megasas_evt_log_info *eli)
5218{
5219 struct megasas_cmd *cmd;
5220 struct megasas_dcmd_frame *dcmd;
5221 struct megasas_evt_log_info *el_info;
5222 dma_addr_t el_info_h = 0;
5223
5224 cmd = megasas_get_cmd(instance);
5225
5226 if (!cmd) {
5227 return -ENOMEM;
5228 }
5229
5230 dcmd = &cmd->frame->dcmd;
5231 el_info = pci_alloc_consistent(instance->pdev,
5232 sizeof(struct megasas_evt_log_info),
5233 &el_info_h);
5234
5235 if (!el_info) {
5236 megasas_return_cmd(instance, cmd);
5237 return -ENOMEM;
5238 }
5239
5240 memset(el_info, 0, sizeof(*el_info));
5241 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5242
5243 dcmd->cmd = MFI_CMD_DCMD;
5244 dcmd->cmd_status = 0x0;
5245 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305246 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005247 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005248 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305249 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5250 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5251 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5252 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005253
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305254 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5255 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305256 /*
5257 * Copy the data back into callers buffer
5258 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305259 eli->newest_seq_num = el_info->newest_seq_num;
5260 eli->oldest_seq_num = el_info->oldest_seq_num;
5261 eli->clear_seq_num = el_info->clear_seq_num;
5262 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5263 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305264 } else
5265 dev_err(&instance->pdev->dev, "DCMD failed "
5266 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005267
5268 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5269 el_info, el_info_h);
5270
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305271 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005272
5273 return 0;
5274}
5275
5276/**
5277 * megasas_register_aen - Registers for asynchronous event notification
5278 * @instance: Adapter soft state
5279 * @seq_num: The starting sequence number
5280 * @class_locale: Class of the event
5281 *
5282 * This function subscribes for AEN for events beyond the @seq_num. It requests
5283 * to be notified if and only if the event is of type @class_locale
5284 */
5285static int
5286megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5287 u32 class_locale_word)
5288{
5289 int ret_val;
5290 struct megasas_cmd *cmd;
5291 struct megasas_dcmd_frame *dcmd;
5292 union megasas_evt_class_locale curr_aen;
5293 union megasas_evt_class_locale prev_aen;
5294
5295 /*
5296 * If there an AEN pending already (aen_cmd), check if the
5297 * class_locale of that pending AEN is inclusive of the new
5298 * AEN request we currently have. If it is, then we don't have
5299 * to do anything. In other words, whichever events the current
5300 * AEN request is subscribing to, have already been subscribed
5301 * to.
5302 *
5303 * If the old_cmd is _not_ inclusive, then we have to abort
5304 * that command, form a class_locale that is superset of both
5305 * old and current and re-issue to the FW
5306 */
5307
5308 curr_aen.word = class_locale_word;
5309
5310 if (instance->aen_cmd) {
5311
Christoph Hellwiga9555532015-04-23 16:34:24 +05305312 prev_aen.word =
5313 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005314
5315 /*
5316 * A class whose enum value is smaller is inclusive of all
5317 * higher values. If a PROGRESS (= -1) was previously
5318 * registered, then a new registration requests for higher
5319 * classes need not be sent to FW. They are automatically
5320 * included.
5321 *
5322 * Locale numbers don't have such hierarchy. They are bitmap
5323 * values
5324 */
5325 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305326 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005327 curr_aen.members.locale)) {
5328 /*
5329 * Previously issued event registration includes
5330 * current request. Nothing to do.
5331 */
5332 return 0;
5333 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305334 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005335
5336 if (prev_aen.members.class < curr_aen.members.class)
5337 curr_aen.members.class = prev_aen.members.class;
5338
5339 instance->aen_cmd->abort_aen = 1;
5340 ret_val = megasas_issue_blocked_abort_cmd(instance,
5341 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305342 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005343
5344 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005345 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005346 "previous AEN command\n");
5347 return ret_val;
5348 }
5349 }
5350 }
5351
5352 cmd = megasas_get_cmd(instance);
5353
5354 if (!cmd)
5355 return -ENOMEM;
5356
5357 dcmd = &cmd->frame->dcmd;
5358
5359 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5360
5361 /*
5362 * Prepare DCMD for aen registration
5363 */
5364 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5365
5366 dcmd->cmd = MFI_CMD_DCMD;
5367 dcmd->cmd_status = 0x0;
5368 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305369 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005370 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005371 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305372 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5373 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5374 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005375 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305376 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5377 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5378 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005379
Yang, Bof4c9a132009-10-06 14:43:28 -06005380 if (instance->aen_cmd != NULL) {
5381 megasas_return_cmd(instance, cmd);
5382 return 0;
5383 }
5384
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005385 /*
5386 * Store reference to the cmd used to register for AEN. When an
5387 * application wants us to register for AEN, we have to abort this
5388 * cmd and re-register with a new EVENT LOCALE supplied by that app
5389 */
5390 instance->aen_cmd = cmd;
5391
5392 /*
5393 * Issue the aen registration frame
5394 */
adam radford9c915a82010-12-21 13:34:31 -08005395 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005396
5397 return 0;
5398}
5399
5400/**
5401 * megasas_start_aen - Subscribes to AEN during driver load time
5402 * @instance: Adapter soft state
5403 */
5404static int megasas_start_aen(struct megasas_instance *instance)
5405{
5406 struct megasas_evt_log_info eli;
5407 union megasas_evt_class_locale class_locale;
5408
5409 /*
5410 * Get the latest sequence number from FW
5411 */
5412 memset(&eli, 0, sizeof(eli));
5413
5414 if (megasas_get_seq_num(instance, &eli))
5415 return -1;
5416
5417 /*
5418 * Register AEN with FW for latest sequence number plus 1
5419 */
5420 class_locale.members.reserved = 0;
5421 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5422 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5423
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305424 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305425 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305426 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005427}
5428
5429/**
5430 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5431 * @instance: Adapter soft state
5432 */
5433static int megasas_io_attach(struct megasas_instance *instance)
5434{
5435 struct Scsi_Host *host = instance->host;
5436
5437 /*
5438 * Export parameters required by SCSI mid-layer
5439 */
5440 host->irq = instance->pdev->irq;
5441 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305442 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005443 host->this_id = instance->init_id;
5444 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005445
5446 if (instance->fw_support_ieee)
5447 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5448
Yang, Bo1fd10682010-10-12 07:18:50 -06005449 /*
5450 * Check if the module parameter value for max_sectors can be used
5451 */
5452 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5453 instance->max_sectors_per_req = max_sectors;
5454 else {
5455 if (max_sectors) {
5456 if (((instance->pdev->device ==
5457 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5458 (instance->pdev->device ==
5459 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5460 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5461 instance->max_sectors_per_req = max_sectors;
5462 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005463 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005464 "and <= %d (or < 1MB for GEN2 controller)\n",
5465 instance->max_sectors_per_req);
5466 }
5467 }
5468 }
5469
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005470 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005471 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005472 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5473 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5474 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005475 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005476
adam radford9c915a82010-12-21 13:34:31 -08005477 /* Fusion only supports host reset */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305478 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005479 host->hostt->eh_device_reset_handler = NULL;
5480 host->hostt->eh_bus_reset_handler = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05305481 host->hostt->eh_target_reset_handler = megasas_reset_target_fusion;
5482 host->hostt->eh_abort_handler = megasas_task_abort_fusion;
adam radford9c915a82010-12-21 13:34:31 -08005483 }
5484
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005485 /*
5486 * Notify the mid-layer about the new controller
5487 */
5488 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305489 dev_err(&instance->pdev->dev,
5490 "Failed to add host from %s %d\n",
5491 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005492 return -ENODEV;
5493 }
5494
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005495 return 0;
5496}
5497
bo yang31ea7082007-11-07 12:09:50 -05005498static int
5499megasas_set_dma_mask(struct pci_dev *pdev)
5500{
5501 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005502 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005503 */
5504 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005505 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005506
Yang Hongyang284901a2009-04-06 19:01:15 -07005507 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005508 goto fail_set_dma_mask;
5509 }
5510 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005511 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005512 goto fail_set_dma_mask;
5513 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305514 /*
5515 * Ensure that all data structures are allocated in 32-bit
5516 * memory.
5517 */
5518 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5519 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5520 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5521 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5522 dev_info(&pdev->dev, "set 32bit DMA mask"
5523 "and 32 bit consistent mask\n");
5524 else
5525 goto fail_set_dma_mask;
5526 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305527
bo yang31ea7082007-11-07 12:09:50 -05005528 return 0;
5529
5530fail_set_dma_mask:
5531 return 1;
5532}
5533
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005534/**
5535 * megasas_probe_one - PCI hotplug entry point
5536 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005537 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005538 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005539static int megasas_probe_one(struct pci_dev *pdev,
5540 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005541{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305542 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005543 struct Scsi_Host *host;
5544 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005545 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305546 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005547
5548 /* Reset MSI-X in the kdump kernel */
5549 if (reset_devices) {
5550 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5551 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005552 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005553 &control);
5554 if (control & PCI_MSIX_FLAGS_ENABLE) {
5555 dev_info(&pdev->dev, "resetting MSI-X\n");
5556 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005557 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005558 control &
5559 ~PCI_MSIX_FLAGS_ENABLE);
5560 }
5561 }
5562 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005563
5564 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005565 * PCI prepping: enable device set bus mastering and dma mask
5566 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005567 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005568
5569 if (rval) {
5570 return rval;
5571 }
5572
5573 pci_set_master(pdev);
5574
bo yang31ea7082007-11-07 12:09:50 -05005575 if (megasas_set_dma_mask(pdev))
5576 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005577
5578 host = scsi_host_alloc(&megasas_template,
5579 sizeof(struct megasas_instance));
5580
5581 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005582 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005583 goto fail_alloc_instance;
5584 }
5585
5586 instance = (struct megasas_instance *)host->hostdata;
5587 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005588 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08005589 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005590
adam radford9c915a82010-12-21 13:34:31 -08005591 switch (instance->pdev->device) {
5592 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005593 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005594 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305595 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305596 case PCI_DEVICE_ID_LSI_INTRUDER:
5597 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305598 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5599 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005600 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305601 instance->ctrl_context_pages =
5602 get_order(sizeof(struct fusion_context));
5603 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5604 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005605 if (!instance->ctrl_context) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005606 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005607 "memory for Fusion context info\n");
5608 goto fail_alloc_dma_buf;
5609 }
5610 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305611 memset(fusion, 0,
5612 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305613 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5614 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5615 fusion->adapter_type = THUNDERBOLT_SERIES;
5616 else
5617 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08005618 }
5619 break;
5620 default: /* For all other supported controllers */
5621
5622 instance->producer =
5623 pci_alloc_consistent(pdev, sizeof(u32),
5624 &instance->producer_h);
5625 instance->consumer =
5626 pci_alloc_consistent(pdev, sizeof(u32),
5627 &instance->consumer_h);
5628
5629 if (!instance->producer || !instance->consumer) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005630 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
adam radford9c915a82010-12-21 13:34:31 -08005631 "memory for producer, consumer\n");
5632 goto fail_alloc_dma_buf;
5633 }
5634
5635 *instance->producer = 0;
5636 *instance->consumer = 0;
5637 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005638 }
5639
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305640 instance->system_info_buf = pci_zalloc_consistent(pdev,
5641 sizeof(struct MR_DRV_SYSTEM_INFO),
5642 &instance->system_info_h);
5643
5644 if (!instance->system_info_buf)
5645 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5646
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305647 /* Crash dump feature related initialisation*/
5648 instance->drv_buf_index = 0;
5649 instance->drv_buf_alloc = 0;
5650 instance->crash_dump_fw_support = 0;
5651 instance->crash_dump_app_support = 0;
5652 instance->fw_crash_state = UNAVAILABLE;
5653 spin_lock_init(&instance->crashdump_lock);
5654 instance->crash_dump_buf = NULL;
5655
5656 if (!reset_devices)
5657 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5658 CRASH_DMA_BUF_SIZE,
5659 &instance->crash_dump_h);
5660 if (!instance->crash_dump_buf)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005661 dev_err(&pdev->dev, "Can't allocate Firmware "
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305662 "crash dump DMA buffer\n");
5663
Yang, Boc3518832009-10-06 14:18:02 -06005664 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005665 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005666 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005667 instance->issuepend_done = 1;
5668 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305669 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005670
5671 instance->evt_detail = pci_alloc_consistent(pdev,
5672 sizeof(struct
5673 megasas_evt_detail),
5674 &instance->evt_detail_h);
5675
5676 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005677 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005678 "event detail structure\n");
5679 goto fail_alloc_dma_buf;
5680 }
5681
5682 /*
5683 * Initialize locks and queues
5684 */
5685 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005686 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005687
Sumant Patroe4a082c2006-05-30 12:03:37 -07005688 atomic_set(&instance->fw_outstanding,0);
5689
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005690 init_waitqueue_head(&instance->int_cmd_wait_q);
5691 init_waitqueue_head(&instance->abort_cmd_wait_q);
5692
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305693 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005694 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005695 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005696
adam radford9c915a82010-12-21 13:34:31 -08005697 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005698
5699 /*
5700 * Initialize PCI related and misc parameters
5701 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005702 instance->host = host;
5703 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5704 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305705 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005706
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305707
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005708 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305709 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005710 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005711
Sumant Patro658dced2006-10-03 13:09:14 -07005712 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005713 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005714 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005715 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005716 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305717 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005718
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305719 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005720 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305721 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5722 } else
adam radford9c915a82010-12-21 13:34:31 -08005723 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005724
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005725 /*
adam radford0a770662011-02-24 20:56:12 -08005726 * Initialize MFI Firmware
5727 */
5728 if (megasas_init_fw(instance))
5729 goto fail_init_mfi;
5730
adam radford229fe472014-03-10 02:51:56 -07005731 if (instance->requestorId) {
5732 if (instance->PlasmaFW111) {
5733 instance->vf_affiliation_111 =
5734 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5735 &instance->vf_affiliation_111_h);
5736 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005737 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005738 "memory for VF affiliation buffer\n");
5739 } else {
5740 instance->vf_affiliation =
5741 pci_alloc_consistent(pdev,
5742 (MAX_LOGICAL_DRIVES + 1) *
5743 sizeof(struct MR_LD_VF_AFFILIATION),
5744 &instance->vf_affiliation_h);
5745 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005746 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005747 "memory for VF affiliation buffer\n");
5748 }
5749 }
5750
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005751 /*
5752 * Store instance in PCI softstate
5753 */
5754 pci_set_drvdata(pdev, instance);
5755
5756 /*
5757 * Add this controller to megasas_mgmt_info structure so that it
5758 * can be exported to management applications
5759 */
5760 megasas_mgmt_info.count++;
5761 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5762 megasas_mgmt_info.max_index++;
5763
5764 /*
adam radford541f90b2011-05-11 18:34:29 -07005765 * Register with SCSI mid-layer
5766 */
5767 if (megasas_io_attach(instance))
5768 goto fail_io_attach;
5769
5770 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305771 /*
5772 * Trigger SCSI to scan our drives
5773 */
5774 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005775
5776 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005777 * Initiate AEN (Asynchronous Event Notification)
5778 */
5779 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005780 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005781 goto fail_start_aen;
5782 }
5783
Adam Radford9ea81f82014-07-09 15:17:57 -07005784 /* Get current SR-IOV LD/VF affiliation */
5785 if (instance->requestorId)
5786 megasas_get_ld_vf_affiliation(instance, 1);
5787
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005788 return 0;
5789
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005790fail_start_aen:
5791fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005792 megasas_mgmt_info.count--;
5793 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5794 megasas_mgmt_info.max_index--;
5795
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305796 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305797 megasas_destroy_irqs(instance);
5798
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305799 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08005800 megasas_release_fusion(instance);
5801 else
5802 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005803 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005804 pci_disable_msix(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305805fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005806fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005807 if (instance->evt_detail)
5808 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5809 instance->evt_detail,
5810 instance->evt_detail_h);
5811
adam radfordeb1b1232011-02-24 20:55:56 -08005812 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005813 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5814 instance->producer_h);
5815 if (instance->consumer)
5816 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5817 instance->consumer_h);
5818 scsi_host_put(host);
5819
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005820fail_alloc_instance:
5821fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005822 pci_disable_device(pdev);
5823
5824 return -ENODEV;
5825}
5826
5827/**
5828 * megasas_flush_cache - Requests FW to flush all its caches
5829 * @instance: Adapter soft state
5830 */
5831static void megasas_flush_cache(struct megasas_instance *instance)
5832{
5833 struct megasas_cmd *cmd;
5834 struct megasas_dcmd_frame *dcmd;
5835
bo yang39a98552010-09-22 22:36:29 -04005836 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5837 return;
5838
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005839 cmd = megasas_get_cmd(instance);
5840
5841 if (!cmd)
5842 return;
5843
5844 dcmd = &cmd->frame->dcmd;
5845
5846 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5847
5848 dcmd->cmd = MFI_CMD_DCMD;
5849 dcmd->cmd_status = 0x0;
5850 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305851 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005852 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005853 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005854 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305855 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005856 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5857
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305858 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
5859 != DCMD_SUCCESS) {
5860 dev_err(&instance->pdev->dev,
5861 "return from %s %d\n", __func__, __LINE__);
5862 return;
5863 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005864
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305865 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005866}
5867
5868/**
5869 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5870 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05005871 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005872 */
bo yang31ea7082007-11-07 12:09:50 -05005873static void megasas_shutdown_controller(struct megasas_instance *instance,
5874 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005875{
5876 struct megasas_cmd *cmd;
5877 struct megasas_dcmd_frame *dcmd;
5878
bo yang39a98552010-09-22 22:36:29 -04005879 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5880 return;
5881
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005882 cmd = megasas_get_cmd(instance);
5883
5884 if (!cmd)
5885 return;
5886
5887 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305888 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305889 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08005890 if (instance->map_update_cmd)
5891 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305892 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305893 if (instance->jbod_seq_cmd)
5894 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305895 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305896
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005897 dcmd = &cmd->frame->dcmd;
5898
5899 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5900
5901 dcmd->cmd = MFI_CMD_DCMD;
5902 dcmd->cmd_status = 0x0;
5903 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305904 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005905 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005906 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005907 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305908 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005909
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305910 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
5911 != DCMD_SUCCESS) {
5912 dev_err(&instance->pdev->dev,
5913 "return from %s %d\n", __func__, __LINE__);
5914 return;
5915 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005916
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305917 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005918}
5919
Jiri Slaby33139b22008-05-01 17:56:02 +02005920#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005921/**
bo yangad84db22007-11-09 04:40:16 -05005922 * megasas_suspend - driver suspend entry point
5923 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05005924 * @state: PCI power state to suspend routine
5925 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005926static int
bo yang31ea7082007-11-07 12:09:50 -05005927megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5928{
5929 struct Scsi_Host *host;
5930 struct megasas_instance *instance;
5931
5932 instance = pci_get_drvdata(pdev);
5933 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06005934 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05005935
adam radford229fe472014-03-10 02:51:56 -07005936 /* Shutdown SR-IOV heartbeat timer */
5937 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5938 del_timer_sync(&instance->sriov_heartbeat_timer);
5939
bo yang31ea7082007-11-07 12:09:50 -05005940 megasas_flush_cache(instance);
5941 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005942
5943 /* cancel the delayed work if this work still in queue */
5944 if (instance->ev != NULL) {
5945 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005946 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005947 instance->ev = NULL;
5948 }
5949
bo yang31ea7082007-11-07 12:09:50 -05005950 tasklet_kill(&instance->isr_tasklet);
5951
5952 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305953 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005954
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305955 megasas_destroy_irqs(instance);
5956
adam radfordc8e858f2011-10-08 18:15:13 -07005957 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005958 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05005959
5960 pci_save_state(pdev);
5961 pci_disable_device(pdev);
5962
5963 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5964
5965 return 0;
5966}
5967
5968/**
5969 * megasas_resume- driver resume entry point
5970 * @pdev: PCI device structure
5971 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005972static int
bo yang31ea7082007-11-07 12:09:50 -05005973megasas_resume(struct pci_dev *pdev)
5974{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305975 int rval;
bo yang31ea7082007-11-07 12:09:50 -05005976 struct Scsi_Host *host;
5977 struct megasas_instance *instance;
5978
5979 instance = pci_get_drvdata(pdev);
5980 host = instance->host;
5981 pci_set_power_state(pdev, PCI_D0);
5982 pci_enable_wake(pdev, PCI_D0, 0);
5983 pci_restore_state(pdev);
5984
5985 /*
5986 * PCI prepping: enable device set bus mastering and dma mask
5987 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005988 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05005989
5990 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005991 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05005992 return rval;
5993 }
5994
5995 pci_set_master(pdev);
5996
5997 if (megasas_set_dma_mask(pdev))
5998 goto fail_set_dma_mask;
5999
6000 /*
6001 * Initialize MFI Firmware
6002 */
6003
bo yang31ea7082007-11-07 12:09:50 -05006004 atomic_set(&instance->fw_outstanding, 0);
6005
6006 /*
6007 * We expect the FW state to be READY
6008 */
adam radford058a8fa2011-10-08 18:14:27 -07006009 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006010 goto fail_ready_state;
6011
adam radford3f1abce2011-05-11 18:33:47 -07006012 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02006013 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02006014 pci_enable_msix_exact(instance->pdev, instance->msixentry,
6015 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02006016 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006017
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306018 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006019 megasas_reset_reply_desc(instance);
6020 if (megasas_ioc_init_fusion(instance)) {
6021 megasas_free_cmds(instance);
6022 megasas_free_cmds_fusion(instance);
6023 goto fail_init_mfi;
6024 }
6025 if (!megasas_get_map_info(instance))
6026 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306027 } else {
adam radford9c915a82010-12-21 13:34:31 -08006028 *instance->producer = 0;
6029 *instance->consumer = 0;
6030 if (megasas_issue_init_mfi(instance))
6031 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006032 }
bo yang31ea7082007-11-07 12:09:50 -05006033
adam radford9c915a82010-12-21 13:34:31 -08006034 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6035 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006036
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306037 if (instance->msix_vectors ?
6038 megasas_setup_irqs_msix(instance, 0) :
6039 megasas_setup_irqs_ioapic(instance))
6040 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006041
adam radford229fe472014-03-10 02:51:56 -07006042 /* Re-launch SR-IOV heartbeat timer */
6043 if (instance->requestorId) {
6044 if (!megasas_sriov_start_heartbeat(instance, 0))
6045 megasas_start_timer(instance,
6046 &instance->sriov_heartbeat_timer,
6047 megasas_sriov_heartbeat_handler,
6048 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306049 else {
adam radford229fe472014-03-10 02:51:56 -07006050 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306051 goto fail_init_mfi;
6052 }
adam radford229fe472014-03-10 02:51:56 -07006053 }
6054
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306055 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306056 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006057 instance->unload = 0;
6058
adam radford541f90b2011-05-11 18:34:29 -07006059 /*
6060 * Initiate AEN (Asynchronous Event Notification)
6061 */
6062 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006063 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006064
bo yang31ea7082007-11-07 12:09:50 -05006065 return 0;
6066
bo yang31ea7082007-11-07 12:09:50 -05006067fail_init_mfi:
6068 if (instance->evt_detail)
6069 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6070 instance->evt_detail,
6071 instance->evt_detail_h);
6072
6073 if (instance->producer)
6074 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6075 instance->producer_h);
6076 if (instance->consumer)
6077 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6078 instance->consumer_h);
6079 scsi_host_put(host);
6080
6081fail_set_dma_mask:
6082fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006083fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006084
6085 pci_disable_device(pdev);
6086
6087 return -ENODEV;
6088}
Jiri Slaby33139b22008-05-01 17:56:02 +02006089#else
6090#define megasas_suspend NULL
6091#define megasas_resume NULL
6092#endif
bo yang31ea7082007-11-07 12:09:50 -05006093
6094/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006095 * megasas_detach_one - PCI hot"un"plug entry point
6096 * @pdev: PCI device structure
6097 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006098static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006099{
6100 int i;
6101 struct Scsi_Host *host;
6102 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006103 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306104 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006105
6106 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006107 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006108 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006109 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006110
adam radford229fe472014-03-10 02:51:56 -07006111 /* Shutdown SR-IOV heartbeat timer */
6112 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6113 del_timer_sync(&instance->sriov_heartbeat_timer);
6114
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306115 if (instance->fw_crash_state != UNAVAILABLE)
6116 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006117 scsi_remove_host(instance->host);
6118 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006119 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006120
6121 /* cancel the delayed work if this work still in queue*/
6122 if (instance->ev != NULL) {
6123 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006124 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006125 instance->ev = NULL;
6126 }
6127
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306128 /* cancel all wait events */
6129 wake_up_all(&instance->int_cmd_wait_q);
6130
Sumant Patro5d018ad2006-10-03 13:13:18 -07006131 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006132
6133 /*
6134 * Take the instance off the instance array. Note that we will not
6135 * decrement the max_index. We let this array be sparse array
6136 */
6137 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6138 if (megasas_mgmt_info.instance[i] == instance) {
6139 megasas_mgmt_info.count--;
6140 megasas_mgmt_info.instance[i] = NULL;
6141
6142 break;
6143 }
6144 }
6145
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306146 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006147
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306148 megasas_destroy_irqs(instance);
6149
adam radfordc8e858f2011-10-08 18:15:13 -07006150 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08006151 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006152
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306153 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006154 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306155 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6156 (sizeof(struct MR_PD_CFG_SEQ) *
6157 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306158 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006159 if (fusion->ld_map[i])
6160 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306161 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006162 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306163 fusion->ld_map_phys[i]);
6164 if (fusion->ld_drv_map[i])
6165 free_pages((ulong)fusion->ld_drv_map[i],
6166 fusion->drv_map_pages);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306167 if (fusion->pd_seq_sync)
6168 dma_free_coherent(&instance->pdev->dev,
6169 pd_seq_map_sz,
6170 fusion->pd_seq_sync[i],
6171 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306172 }
6173 free_pages((ulong)instance->ctrl_context,
6174 instance->ctrl_context_pages);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306175 } else {
adam radford9c915a82010-12-21 13:34:31 -08006176 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006177 pci_free_consistent(pdev, sizeof(u32),
6178 instance->producer,
6179 instance->producer_h);
6180 pci_free_consistent(pdev, sizeof(u32),
6181 instance->consumer,
6182 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08006183 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006184
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306185 kfree(instance->ctrl_info);
6186
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05306187 if (instance->evt_detail)
6188 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6189 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07006190
6191 if (instance->vf_affiliation)
6192 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6193 sizeof(struct MR_LD_VF_AFFILIATION),
6194 instance->vf_affiliation,
6195 instance->vf_affiliation_h);
6196
6197 if (instance->vf_affiliation_111)
6198 pci_free_consistent(pdev,
6199 sizeof(struct MR_LD_VF_AFFILIATION_111),
6200 instance->vf_affiliation_111,
6201 instance->vf_affiliation_111_h);
6202
6203 if (instance->hb_host_mem)
6204 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6205 instance->hb_host_mem,
6206 instance->hb_host_mem_h);
6207
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306208 if (instance->crash_dump_buf)
6209 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6210 instance->crash_dump_buf, instance->crash_dump_h);
6211
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306212 if (instance->system_info_buf)
6213 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6214 instance->system_info_buf, instance->system_info_h);
6215
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006216 scsi_host_put(host);
6217
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006218 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006219}
6220
6221/**
6222 * megasas_shutdown - Shutdown entry point
6223 * @device: Generic device structure
6224 */
6225static void megasas_shutdown(struct pci_dev *pdev)
6226{
6227 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006228
Yang, Bo0c79e682009-10-06 14:47:35 -06006229 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006230 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006231 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306232 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306233 megasas_destroy_irqs(instance);
6234
adam radfordc8e858f2011-10-08 18:15:13 -07006235 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07006236 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006237}
6238
6239/**
6240 * megasas_mgmt_open - char node "open" entry point
6241 */
6242static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6243{
6244 /*
6245 * Allow only those users with admin rights
6246 */
6247 if (!capable(CAP_SYS_ADMIN))
6248 return -EACCES;
6249
6250 return 0;
6251}
6252
6253/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006254 * megasas_mgmt_fasync - Async notifier registration from applications
6255 *
6256 * This function adds the calling process to a driver global queue. When an
6257 * event occurs, SIGIO will be sent to all processes in this queue.
6258 */
6259static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6260{
6261 int rc;
6262
Arjan van de Ven0b950672006-01-11 13:16:10 +01006263 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006264
6265 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6266
Arjan van de Ven0b950672006-01-11 13:16:10 +01006267 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006268
6269 if (rc >= 0) {
6270 /* For sanity check when we get ioctl */
6271 filep->private_data = filep;
6272 return 0;
6273 }
6274
6275 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6276
6277 return rc;
6278}
6279
6280/**
Yang, Boc3518832009-10-06 14:18:02 -06006281 * megasas_mgmt_poll - char node "poll" entry point
6282 * */
6283static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6284{
6285 unsigned int mask;
6286 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006287
Yang, Boc3518832009-10-06 14:18:02 -06006288 poll_wait(file, &megasas_poll_wait, wait);
6289 spin_lock_irqsave(&poll_aen_lock, flags);
6290 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006291 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006292 else
6293 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306294 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006295 spin_unlock_irqrestore(&poll_aen_lock, flags);
6296 return mask;
6297}
6298
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306299/*
6300 * megasas_set_crash_dump_params_ioctl:
6301 * Send CRASH_DUMP_MODE DCMD to all controllers
6302 * @cmd: MFI command frame
6303 */
6304
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006305static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306306{
6307 struct megasas_instance *local_instance;
6308 int i, error = 0;
6309 int crash_support;
6310
6311 crash_support = cmd->frame->dcmd.mbox.w[0];
6312
6313 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6314 local_instance = megasas_mgmt_info.instance[i];
6315 if (local_instance && local_instance->crash_dump_drv_support) {
6316 if ((local_instance->adprecovery ==
6317 MEGASAS_HBA_OPERATIONAL) &&
6318 !megasas_set_crash_dump_params(local_instance,
6319 crash_support)) {
6320 local_instance->crash_dump_app_support =
6321 crash_support;
6322 dev_info(&local_instance->pdev->dev,
6323 "Application firmware crash "
6324 "dump mode set success\n");
6325 error = 0;
6326 } else {
6327 dev_info(&local_instance->pdev->dev,
6328 "Application firmware crash "
6329 "dump mode set failed\n");
6330 error = -1;
6331 }
6332 }
6333 }
6334 return error;
6335}
6336
Yang, Boc3518832009-10-06 14:18:02 -06006337/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006338 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6339 * @instance: Adapter soft state
6340 * @argp: User's ioctl packet
6341 */
6342static int
6343megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6344 struct megasas_iocpacket __user * user_ioc,
6345 struct megasas_iocpacket *ioc)
6346{
6347 struct megasas_sge32 *kern_sge32;
6348 struct megasas_cmd *cmd;
6349 void *kbuff_arr[MAX_IOCTL_SGE];
6350 dma_addr_t buf_handle = 0;
6351 int error = 0, i;
6352 void *sense = NULL;
6353 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006354 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006355
6356 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6357
6358 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006359 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006360 ioc->sge_count, MAX_IOCTL_SGE);
6361 return -EINVAL;
6362 }
6363
6364 cmd = megasas_get_cmd(instance);
6365 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006366 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006367 return -ENOMEM;
6368 }
6369
6370 /*
6371 * User's IOCTL packet has 2 frames (maximum). Copy those two
6372 * frames into our cmd's frames. cmd->frame's context will get
6373 * overwritten when we copy from user's frames. So set that value
6374 * alone separately
6375 */
6376 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306377 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006378 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306379 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6380 MFI_FRAME_SGL64 |
6381 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006382
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306383 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6384 error = megasas_set_crash_dump_params_ioctl(cmd);
6385 megasas_return_cmd(instance, cmd);
6386 return error;
6387 }
6388
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006389 /*
6390 * The management interface between applications and the fw uses
6391 * MFI frames. E.g, RAID configuration changes, LD property changes
6392 * etc are accomplishes through different kinds of MFI frames. The
6393 * driver needs to care only about substituting user buffers with
6394 * kernel buffers in SGLs. The location of SGL is embedded in the
6395 * struct iocpacket itself.
6396 */
6397 kern_sge32 = (struct megasas_sge32 *)
6398 ((unsigned long)cmd->frame + ioc->sgl_off);
6399
6400 /*
6401 * For each user buffer, create a mirror buffer and copy in
6402 */
6403 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006404 if (!ioc->sgl[i].iov_len)
6405 continue;
6406
Sumant Patro9f35fa82007-02-14 12:55:45 -08006407 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006408 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006409 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006410 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006411 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6412 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006413 error = -ENOMEM;
6414 goto out;
6415 }
6416
6417 /*
6418 * We don't change the dma_coherent_mask, so
6419 * pci_alloc_consistent only returns 32bit addresses
6420 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306421 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6422 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006423
6424 /*
6425 * We created a kernel buffer corresponding to the
6426 * user buffer. Now copy in from the user buffer
6427 */
6428 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6429 (u32) (ioc->sgl[i].iov_len))) {
6430 error = -EFAULT;
6431 goto out;
6432 }
6433 }
6434
6435 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006436 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6437 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006438 if (!sense) {
6439 error = -ENOMEM;
6440 goto out;
6441 }
6442
6443 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006444 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306445 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006446 }
6447
6448 /*
6449 * Set the sync_cmd flag so that the ISR knows not to complete this
6450 * cmd to the SCSI mid-layer
6451 */
6452 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306453 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6454 cmd->sync_cmd = 0;
6455 dev_err(&instance->pdev->dev,
6456 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6457 __func__, __LINE__, cmd->frame->dcmd.opcode,
6458 cmd->cmd_status_drv);
6459 return -EBUSY;
6460 }
6461
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006462 cmd->sync_cmd = 0;
6463
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306464 if (instance->unload == 1) {
6465 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6466 "don't submit data to application\n");
6467 goto out;
6468 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006469 /*
6470 * copy out the kernel buffers to user buffers
6471 */
6472 for (i = 0; i < ioc->sge_count; i++) {
6473 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6474 ioc->sgl[i].iov_len)) {
6475 error = -EFAULT;
6476 goto out;
6477 }
6478 }
6479
6480 /*
6481 * copy out the sense
6482 */
6483 if (ioc->sense_len) {
6484 /*
bo yangb70a41e2008-03-18 03:13:06 -04006485 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006486 * sense buffer address
6487 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006488 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6489 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006490
bo yangb70a41e2008-03-18 03:13:06 -04006491 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6492 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006493 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05006494 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006495 error = -EFAULT;
6496 goto out;
6497 }
6498 }
6499
6500 /*
6501 * copy the status codes returned by the fw
6502 */
6503 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6504 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006505 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006506 error = -EFAULT;
6507 }
6508
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006509out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006510 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006511 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006512 sense, sense_handle);
6513 }
6514
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006515 for (i = 0; i < ioc->sge_count; i++) {
6516 if (kbuff_arr[i])
6517 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306518 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006519 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306520 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306521 kbuff_arr[i] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006522 }
6523
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306524 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006525 return error;
6526}
6527
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006528static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6529{
6530 struct megasas_iocpacket __user *user_ioc =
6531 (struct megasas_iocpacket __user *)arg;
6532 struct megasas_iocpacket *ioc;
6533 struct megasas_instance *instance;
6534 int error;
bo yang39a98552010-09-22 22:36:29 -04006535 int i;
6536 unsigned long flags;
6537 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006538
6539 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6540 if (!ioc)
6541 return -ENOMEM;
6542
6543 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6544 error = -EFAULT;
6545 goto out_kfree_ioc;
6546 }
6547
6548 instance = megasas_lookup_instance(ioc->host_no);
6549 if (!instance) {
6550 error = -ENODEV;
6551 goto out_kfree_ioc;
6552 }
6553
adam radford229fe472014-03-10 02:51:56 -07006554 /* Adjust ioctl wait time for VF mode */
6555 if (instance->requestorId)
6556 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6557
6558 /* Block ioctls in VF mode */
6559 if (instance->requestorId && !allow_vf_ioctls) {
6560 error = -ENODEV;
6561 goto out_kfree_ioc;
6562 }
6563
bo yang39a98552010-09-22 22:36:29 -04006564 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006565 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006566 error = -ENODEV;
6567 goto out_kfree_ioc;
6568 }
6569
6570 if (instance->unload == 1) {
6571 error = -ENODEV;
6572 goto out_kfree_ioc;
6573 }
6574
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006575 if (down_interruptible(&instance->ioctl_sem)) {
6576 error = -ERESTARTSYS;
6577 goto out_kfree_ioc;
6578 }
bo yang39a98552010-09-22 22:36:29 -04006579
6580 for (i = 0; i < wait_time; i++) {
6581
6582 spin_lock_irqsave(&instance->hba_lock, flags);
6583 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6584 spin_unlock_irqrestore(&instance->hba_lock, flags);
6585 break;
6586 }
6587 spin_unlock_irqrestore(&instance->hba_lock, flags);
6588
6589 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006590 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04006591 "for controller reset to finish\n");
6592 }
6593
6594 msleep(1000);
6595 }
6596
6597 spin_lock_irqsave(&instance->hba_lock, flags);
6598 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6599 spin_unlock_irqrestore(&instance->hba_lock, flags);
6600
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006601 dev_err(&instance->pdev->dev, "timed out while"
bo yang39a98552010-09-22 22:36:29 -04006602 "waiting for HBA to recover\n");
6603 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006604 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006605 }
6606 spin_unlock_irqrestore(&instance->hba_lock, flags);
6607
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006608 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006609out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006610 up(&instance->ioctl_sem);
6611
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006612out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006613 kfree(ioc);
6614 return error;
6615}
6616
6617static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6618{
6619 struct megasas_instance *instance;
6620 struct megasas_aen aen;
6621 int error;
bo yang39a98552010-09-22 22:36:29 -04006622 int i;
6623 unsigned long flags;
6624 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006625
6626 if (file->private_data != file) {
6627 printk(KERN_DEBUG "megasas: fasync_helper was not "
6628 "called first\n");
6629 return -EINVAL;
6630 }
6631
6632 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6633 return -EFAULT;
6634
6635 instance = megasas_lookup_instance(aen.host_no);
6636
6637 if (!instance)
6638 return -ENODEV;
6639
bo yang39a98552010-09-22 22:36:29 -04006640 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6641 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006642 }
6643
6644 if (instance->unload == 1) {
6645 return -ENODEV;
6646 }
6647
bo yang39a98552010-09-22 22:36:29 -04006648 for (i = 0; i < wait_time; i++) {
6649
6650 spin_lock_irqsave(&instance->hba_lock, flags);
6651 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6652 spin_unlock_irqrestore(&instance->hba_lock,
6653 flags);
6654 break;
6655 }
6656
6657 spin_unlock_irqrestore(&instance->hba_lock, flags);
6658
6659 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006660 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04006661 "controller reset to finish\n");
6662 }
6663
6664 msleep(1000);
6665 }
6666
6667 spin_lock_irqsave(&instance->hba_lock, flags);
6668 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6669 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006670 dev_err(&instance->pdev->dev, "timed out while waiting"
6671 "for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04006672 return -ENODEV;
6673 }
6674 spin_unlock_irqrestore(&instance->hba_lock, flags);
6675
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306676 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006677 error = megasas_register_aen(instance, aen.seq_num,
6678 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306679 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006680 return error;
6681}
6682
6683/**
6684 * megasas_mgmt_ioctl - char node ioctl entry point
6685 */
6686static long
6687megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6688{
6689 switch (cmd) {
6690 case MEGASAS_IOC_FIRMWARE:
6691 return megasas_mgmt_ioctl_fw(file, arg);
6692
6693 case MEGASAS_IOC_GET_AEN:
6694 return megasas_mgmt_ioctl_aen(file, arg);
6695 }
6696
6697 return -ENOTTY;
6698}
6699
6700#ifdef CONFIG_COMPAT
6701static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6702{
6703 struct compat_megasas_iocpacket __user *cioc =
6704 (struct compat_megasas_iocpacket __user *)arg;
6705 struct megasas_iocpacket __user *ioc =
6706 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6707 int i;
6708 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006709 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306710 unsigned long local_raw_ptr;
6711 u32 local_sense_off;
6712 u32 local_sense_len;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006713
Jeff Garzik83aabc12006-10-04 06:34:03 -04006714 if (clear_user(ioc, sizeof(*ioc)))
6715 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006716
6717 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6718 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6719 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6720 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6721 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6722 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6723 return -EFAULT;
6724
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006725 /*
6726 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6727 * sense_len is not null, so prepare the 64bit value under
6728 * the same condition.
6729 */
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306730 if (get_user(local_raw_ptr, ioc->frame.raw) ||
6731 get_user(local_sense_off, &ioc->sense_off) ||
6732 get_user(local_sense_len, &ioc->sense_len))
6733 return -EFAULT;
6734
6735
6736 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006737 void __user **sense_ioc_ptr =
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306738 (void __user **)((u8*)local_raw_ptr + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006739 compat_uptr_t *sense_cioc_ptr =
6740 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6741 if (get_user(ptr, sense_cioc_ptr) ||
6742 put_user(compat_ptr(ptr), sense_ioc_ptr))
6743 return -EFAULT;
6744 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006745
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006746 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006747 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6748 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6749 copy_in_user(&ioc->sgl[i].iov_len,
6750 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6751 return -EFAULT;
6752 }
6753
6754 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6755
6756 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6757 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6758 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6759 return -EFAULT;
6760 }
6761 return error;
6762}
6763
6764static long
6765megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6766 unsigned long arg)
6767{
6768 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006769 case MEGASAS_IOC_FIRMWARE32:
6770 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006771 case MEGASAS_IOC_GET_AEN:
6772 return megasas_mgmt_ioctl_aen(file, arg);
6773 }
6774
6775 return -ENOTTY;
6776}
6777#endif
6778
6779/*
6780 * File operations structure for management interface
6781 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006782static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006783 .owner = THIS_MODULE,
6784 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006785 .fasync = megasas_mgmt_fasync,
6786 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006787 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006788#ifdef CONFIG_COMPAT
6789 .compat_ioctl = megasas_mgmt_compat_ioctl,
6790#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006791 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006792};
6793
6794/*
6795 * PCI hotplug support registration structure
6796 */
6797static struct pci_driver megasas_pci_driver = {
6798
6799 .name = "megaraid_sas",
6800 .id_table = megasas_pci_table,
6801 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006802 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006803 .suspend = megasas_suspend,
6804 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006805 .shutdown = megasas_shutdown,
6806};
6807
6808/*
6809 * Sysfs driver attributes
6810 */
6811static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6812{
6813 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6814 MEGASAS_VERSION);
6815}
6816
6817static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6818
6819static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306820megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6821{
6822 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6823 MEGASAS_RELDATE);
6824}
6825
6826static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6827
6828static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06006829megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6830{
6831 return sprintf(buf, "%u\n", support_poll_for_event);
6832}
6833
6834static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6835 megasas_sysfs_show_support_poll_for_event, NULL);
6836
Yang, Bo837f5fe2010-10-11 06:59:20 -06006837 static ssize_t
6838megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6839{
6840 return sprintf(buf, "%u\n", support_device_change);
6841}
6842
6843static DRIVER_ATTR(support_device_change, S_IRUGO,
6844 megasas_sysfs_show_support_device_change, NULL);
6845
Yang, Bo72c4fd32009-10-06 14:20:59 -06006846static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07006847megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6848{
bo yangad84db22007-11-09 04:40:16 -05006849 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07006850}
6851
6852static ssize_t
6853megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6854{
6855 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006856
6857 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07006858 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6859 retval = -EINVAL;
6860 }
6861 return retval;
6862}
6863
Joe Malicki66dca9b2008-08-14 17:14:48 -04006864static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05006865 megasas_sysfs_set_dbg_lvl);
6866
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006867static void
6868megasas_aen_polling(struct work_struct *work)
6869{
6870 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006871 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006872 struct megasas_instance *instance = ev->instance;
6873 union megasas_evt_class_locale class_locale;
6874 struct Scsi_Host *host;
6875 struct scsi_device *sdev1;
6876 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006877 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006878 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07006879 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006880 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306881 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006882
6883 if (!instance) {
6884 printk(KERN_ERR "invalid instance!\n");
6885 kfree(ev);
6886 return;
6887 }
adam radford229fe472014-03-10 02:51:56 -07006888
6889 /* Adjust event workqueue thread wait time for VF mode */
6890 if (instance->requestorId)
6891 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6892
6893 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306894 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07006895
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006896 instance->ev = NULL;
6897 host = instance->host;
6898 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05306899 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006900
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306901 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306902
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006903 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306904 case MR_EVT_PD_REMOVED:
6905 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306906 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306907 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07006908 break;
6909
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306910 case MR_EVT_LD_OFFLINE:
6911 case MR_EVT_CFG_CLEARED:
6912 case MR_EVT_LD_DELETED:
6913 case MR_EVT_LD_CREATED:
6914 if (!instance->requestorId ||
6915 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
6916 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07006917
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306918 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306919 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07006920
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306921 break;
6922
6923 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
6924 case MR_EVT_FOREIGN_CFG_IMPORTED:
6925 case MR_EVT_LD_STATE_CHANGE:
6926 dcmd_ret = megasas_get_pd_list(instance);
6927
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306928 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306929 break;
6930
6931 if (!instance->requestorId ||
6932 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
6933 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
6934
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306935 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306936 break;
6937
6938 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
6939 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
6940 instance->host->host_no);
6941 break;
6942
6943 case MR_EVT_CTRL_PROP_CHANGED:
6944 dcmd_ret = megasas_get_ctrl_info(instance);
6945 break;
6946 default:
6947 doscan = 0;
6948 break;
6949 }
6950 } else {
6951 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
6952 mutex_unlock(&instance->reset_mutex);
6953 kfree(ev);
6954 return;
6955 }
6956
6957 mutex_unlock(&instance->reset_mutex);
6958
6959 if (doscan & SCAN_PD_CHANNEL) {
6960 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6961 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6962 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006963 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306964 if (instance->pd_list[pd_index].driveState ==
6965 MR_PD_STATE_SYSTEM) {
6966 if (!sdev1)
6967 scsi_add_device(host, i, j, 0);
6968 else
Yang, Boc9786842009-12-06 08:39:25 -07006969 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006970 } else {
6971 if (sdev1) {
6972 scsi_remove_device(sdev1);
6973 scsi_device_put(sdev1);
6974 }
6975 }
Yang, Boc9786842009-12-06 08:39:25 -07006976 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006977 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006978 }
6979
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306980 if (doscan & SCAN_VD_CHANNEL) {
6981 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6982 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6983 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6984 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6985 if (instance->ld_ids[ld_index] != 0xff) {
6986 if (!sdev1)
6987 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6988 else
6989 scsi_device_put(sdev1);
6990 } else {
6991 if (sdev1) {
6992 scsi_remove_device(sdev1);
6993 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006994 }
6995 }
6996 }
6997 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006998 }
6999
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307000 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307001 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7002 else
7003 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007004
7005 /* Register AEN with FW for latest sequence number plus 1 */
7006 class_locale.members.reserved = 0;
7007 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7008 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307009
7010 if (instance->aen_cmd != NULL) {
7011 kfree(ev);
7012 return;
7013 }
7014
7015 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007016 error = megasas_register_aen(instance, seq_num,
7017 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007018 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307019 dev_err(&instance->pdev->dev,
7020 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007021
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307022 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007023 kfree(ev);
7024}
7025
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007026/**
7027 * megasas_init - Driver load entry point
7028 */
7029static int __init megasas_init(void)
7030{
7031 int rval;
7032
7033 /*
7034 * Announce driver version and other information
7035 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307036 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007037
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007038 spin_lock_init(&poll_aen_lock);
7039
Yang, Bo72c4fd32009-10-06 14:20:59 -06007040 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007041 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007042
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007043 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7044
7045 /*
7046 * Register character device node
7047 */
7048 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7049
7050 if (rval < 0) {
7051 printk(KERN_DEBUG "megasas: failed to open device node\n");
7052 return rval;
7053 }
7054
7055 megasas_mgmt_majorno = rval;
7056
7057 /*
7058 * Register ourselves as PCI hotplug module
7059 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007060 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007061
7062 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007063 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007064 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007065 }
7066
Jeff Garzik83aabc12006-10-04 06:34:03 -04007067 rval = driver_create_file(&megasas_pci_driver.driver,
7068 &driver_attr_version);
7069 if (rval)
7070 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007071
7072 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307073 &driver_attr_release_date);
7074 if (rval)
7075 goto err_dcf_rel_date;
7076
7077 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007078 &driver_attr_support_poll_for_event);
7079 if (rval)
7080 goto err_dcf_support_poll_for_event;
7081
Jeff Garzik83aabc12006-10-04 06:34:03 -04007082 rval = driver_create_file(&megasas_pci_driver.driver,
7083 &driver_attr_dbg_lvl);
7084 if (rval)
7085 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007086 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007087 &driver_attr_support_device_change);
7088 if (rval)
7089 goto err_dcf_support_device_change;
7090
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007091 return rval;
bo yangad84db22007-11-09 04:40:16 -05007092
Yang, Bo837f5fe2010-10-11 06:59:20 -06007093err_dcf_support_device_change:
7094 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007095 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007096err_dcf_dbg_lvl:
7097 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007098 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007099err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307100 driver_remove_file(&megasas_pci_driver.driver,
7101 &driver_attr_release_date);
7102err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007103 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7104err_dcf_attr_ver:
7105 pci_unregister_driver(&megasas_pci_driver);
7106err_pcidrv:
7107 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007108 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007109}
7110
7111/**
7112 * megasas_exit - Driver unload entry point
7113 */
7114static void __exit megasas_exit(void)
7115{
Sumant Patro658dced2006-10-03 13:09:14 -07007116 driver_remove_file(&megasas_pci_driver.driver,
7117 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007118 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007119 &driver_attr_support_poll_for_event);
7120 driver_remove_file(&megasas_pci_driver.driver,
7121 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307122 driver_remove_file(&megasas_pci_driver.driver,
7123 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007124 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007125
7126 pci_unregister_driver(&megasas_pci_driver);
7127 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7128}
7129
7130module_init(megasas_init);
7131module_exit(megasas_exit);