blob: 7e12a8535c39a940446f7ab551d65af8015826b0 [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 radford229fe472014-03-10 02:51:56 -0700192int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
adam radford9c915a82010-12-21 13:34:31 -0800193void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700194static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
195 int initial);
196int megasas_check_mpio_paths(struct megasas_instance *instance,
197 struct scsi_cmnd *scmd);
adam radfordcd50ba82010-12-21 10:23:23 -0800198
199void
200megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
201{
202 instance->instancet->fire_cmd(instance,
203 cmd->frame_phys_addr, 0, instance->reg_set);
204}
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{
adam radford229fe472014-03-10 02:51:56 -0700986 int seconds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400987 struct megasas_header *frame_hdr = &cmd->frame->hdr;
988
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530989 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
990 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991
992 /*
993 * Issue the frame using inbound queue port
994 */
adam radford9c915a82010-12-21 13:34:31 -0800995 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996
997 /*
998 * Wait for cmd_status to change
999 */
adam radford229fe472014-03-10 02:51:56 -07001000 if (instance->requestorId)
1001 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
1002 else
1003 seconds = MFI_POLL_TIMEOUT_SECS;
1004 return wait_and_poll(instance, cmd, seconds);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001005}
1006
1007/**
1008 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1009 * @instance: Adapter soft state
1010 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301011 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001012 *
1013 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001014 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001015 * Used to issue ioctl commands.
1016 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301017int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001018megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301019 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001020{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301021 int ret = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001022
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301023 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001024
adam radford9c915a82010-12-21 13:34:31 -08001025 instance->instancet->issue_dcmd(instance, cmd);
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.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301029 if (!ret)
1030 return 1;
1031 } else
1032 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301033 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001034
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301035 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1036 0 : 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001037}
1038
1039/**
1040 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1041 * @instance: Adapter soft state
1042 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301043 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001044 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301045 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001046 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001047 * cmd and waits for return status.
1048 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001049 */
1050static int
1051megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301052 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001053{
1054 struct megasas_cmd *cmd;
1055 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301056 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001057
1058 cmd = megasas_get_cmd(instance);
1059
1060 if (!cmd)
1061 return -1;
1062
1063 abort_fr = &cmd->frame->abort;
1064
1065 /*
1066 * Prepare and issue the abort frame
1067 */
1068 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301069 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301070 abort_fr->flags = cpu_to_le16(0);
1071 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1072 abort_fr->abort_mfi_phys_addr_lo =
1073 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1074 abort_fr->abort_mfi_phys_addr_hi =
1075 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001076
1077 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301078 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001079
adam radford9c915a82010-12-21 13:34:31 -08001080 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001081
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301082 if (timeout) {
1083 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301084 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301085 if (!ret) {
1086 dev_err(&instance->pdev->dev, "Command timedout"
1087 "from %s\n", __func__);
1088 return 1;
1089 }
1090 } else
1091 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301092 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301093
bo yang39a98552010-09-22 22:36:29 -04001094 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001095
1096 megasas_return_cmd(instance, cmd);
1097 return 0;
1098}
1099
1100/**
1101 * megasas_make_sgl32 - Prepares 32-bit SGL
1102 * @instance: Adapter soft state
1103 * @scp: SCSI command from the mid-layer
1104 * @mfi_sgl: SGL to be filled in
1105 *
1106 * If successful, this function returns the number of SG elements. Otherwise,
1107 * it returnes -1.
1108 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001109static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001110megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1111 union megasas_sgl *mfi_sgl)
1112{
1113 int i;
1114 int sge_count;
1115 struct scatterlist *os_sgl;
1116
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001117 sge_count = scsi_dma_map(scp);
1118 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001119
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001120 if (sge_count) {
1121 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301122 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1123 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001124 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001125 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001126 return sge_count;
1127}
1128
1129/**
1130 * megasas_make_sgl64 - Prepares 64-bit SGL
1131 * @instance: Adapter soft state
1132 * @scp: SCSI command from the mid-layer
1133 * @mfi_sgl: SGL to be filled in
1134 *
1135 * If successful, this function returns the number of SG elements. Otherwise,
1136 * it returnes -1.
1137 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001138static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001139megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1140 union megasas_sgl *mfi_sgl)
1141{
1142 int i;
1143 int sge_count;
1144 struct scatterlist *os_sgl;
1145
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001146 sge_count = scsi_dma_map(scp);
1147 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001148
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001149 if (sge_count) {
1150 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301151 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1152 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001153 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001154 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001155 return sge_count;
1156}
1157
Yang, Bof4c9a132009-10-06 14:43:28 -06001158/**
1159 * megasas_make_sgl_skinny - Prepares IEEE SGL
1160 * @instance: Adapter soft state
1161 * @scp: SCSI command from the mid-layer
1162 * @mfi_sgl: SGL to be filled in
1163 *
1164 * If successful, this function returns the number of SG elements. Otherwise,
1165 * it returnes -1.
1166 */
1167static int
1168megasas_make_sgl_skinny(struct megasas_instance *instance,
1169 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1170{
1171 int i;
1172 int sge_count;
1173 struct scatterlist *os_sgl;
1174
1175 sge_count = scsi_dma_map(scp);
1176
1177 if (sge_count) {
1178 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301179 mfi_sgl->sge_skinny[i].length =
1180 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001181 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301182 cpu_to_le64(sg_dma_address(os_sgl));
1183 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001184 }
1185 }
1186 return sge_count;
1187}
1188
Sumant Patrob1df99d2006-10-03 12:40:47 -07001189 /**
1190 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001191 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001192 * @sge_count : number of sg elements
1193 *
1194 * Returns the number of frames required for numnber of sge's (sge_count)
1195 */
1196
Yang, Bof4c9a132009-10-06 14:43:28 -06001197static u32 megasas_get_frame_count(struct megasas_instance *instance,
1198 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001199{
1200 int num_cnt;
1201 int sge_bytes;
1202 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001203 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001204
1205 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1206 sizeof(struct megasas_sge32);
1207
Yang, Bof4c9a132009-10-06 14:43:28 -06001208 if (instance->flag_ieee) {
1209 sge_sz = sizeof(struct megasas_sge_skinny);
1210 }
1211
Sumant Patrob1df99d2006-10-03 12:40:47 -07001212 /*
bo yangd532dbe2008-03-17 03:36:43 -04001213 * Main frame can contain 2 SGEs for 64-bit SGLs and
1214 * 3 SGEs for 32-bit SGLs for ldio &
1215 * 1 SGEs for 64-bit SGLs and
1216 * 2 SGEs for 32-bit SGLs for pthru frame
1217 */
1218 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001219 if (instance->flag_ieee == 1) {
1220 num_cnt = sge_count - 1;
1221 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001222 num_cnt = sge_count - 1;
1223 else
1224 num_cnt = sge_count - 2;
1225 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001226 if (instance->flag_ieee == 1) {
1227 num_cnt = sge_count - 1;
1228 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001229 num_cnt = sge_count - 2;
1230 else
1231 num_cnt = sge_count - 3;
1232 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001233
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001234 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001235 sge_bytes = sge_sz * num_cnt;
1236
1237 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1238 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1239 }
1240 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001241 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001242
1243 if (frame_count > 7)
1244 frame_count = 8;
1245 return frame_count;
1246}
1247
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001248/**
1249 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1250 * @instance: Adapter soft state
1251 * @scp: SCSI command
1252 * @cmd: Command to be prepared in
1253 *
1254 * This function prepares CDB commands. These are typcially pass-through
1255 * commands to the devices.
1256 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001257static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001258megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1259 struct megasas_cmd *cmd)
1260{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001261 u32 is_logical;
1262 u32 device_id;
1263 u16 flags = 0;
1264 struct megasas_pthru_frame *pthru;
1265
1266 is_logical = MEGASAS_IS_LOGICAL(scp);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301267 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001268 pthru = (struct megasas_pthru_frame *)cmd->frame;
1269
1270 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1271 flags = MFI_FRAME_DIR_WRITE;
1272 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1273 flags = MFI_FRAME_DIR_READ;
1274 else if (scp->sc_data_direction == PCI_DMA_NONE)
1275 flags = MFI_FRAME_DIR_NONE;
1276
Yang, Bof4c9a132009-10-06 14:43:28 -06001277 if (instance->flag_ieee == 1) {
1278 flags |= MFI_FRAME_IEEE;
1279 }
1280
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001281 /*
1282 * Prepare the DCDB frame
1283 */
1284 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1285 pthru->cmd_status = 0x0;
1286 pthru->scsi_status = 0x0;
1287 pthru->target_id = device_id;
1288 pthru->lun = scp->device->lun;
1289 pthru->cdb_len = scp->cmd_len;
1290 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001291 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301292 pthru->flags = cpu_to_le16(flags);
1293 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001294
1295 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1296
1297 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001298 * If the command is for the tape device, set the
1299 * pthru timeout to the os layer timeout value.
1300 */
Yang, Bo8d568252009-10-06 14:12:21 -06001301 if (scp->device->type == TYPE_TAPE) {
1302 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301303 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001304 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301305 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001306 }
1307
1308 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001309 * Construct SGL
1310 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001311 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301312 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001313 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1314 &pthru->sgl);
1315 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301316 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001317 pthru->sge_count = megasas_make_sgl64(instance, scp,
1318 &pthru->sgl);
1319 } else
1320 pthru->sge_count = megasas_make_sgl32(instance, scp,
1321 &pthru->sgl);
1322
Yang, Bobdc6fb82009-12-06 08:30:19 -07001323 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001324 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001325 pthru->sge_count);
1326 return 0;
1327 }
1328
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001329 /*
1330 * Sense info specific
1331 */
1332 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301333 pthru->sense_buf_phys_addr_hi =
1334 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1335 pthru->sense_buf_phys_addr_lo =
1336 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001337
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001338 /*
1339 * Compute the total number of frames this command consumes. FW uses
1340 * this number to pull sufficient number of frames from host memory.
1341 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001342 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001343 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001344
1345 return cmd->frame_count;
1346}
1347
1348/**
1349 * megasas_build_ldio - Prepares IOs to logical devices
1350 * @instance: Adapter soft state
1351 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001352 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001353 *
1354 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1355 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001356static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001357megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1358 struct megasas_cmd *cmd)
1359{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001360 u32 device_id;
1361 u8 sc = scp->cmnd[0];
1362 u16 flags = 0;
1363 struct megasas_io_frame *ldio;
1364
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301365 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001366 ldio = (struct megasas_io_frame *)cmd->frame;
1367
1368 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1369 flags = MFI_FRAME_DIR_WRITE;
1370 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1371 flags = MFI_FRAME_DIR_READ;
1372
Yang, Bof4c9a132009-10-06 14:43:28 -06001373 if (instance->flag_ieee == 1) {
1374 flags |= MFI_FRAME_IEEE;
1375 }
1376
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001377 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001378 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001379 */
1380 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1381 ldio->cmd_status = 0x0;
1382 ldio->scsi_status = 0x0;
1383 ldio->target_id = device_id;
1384 ldio->timeout = 0;
1385 ldio->reserved_0 = 0;
1386 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301387 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001388 ldio->start_lba_hi = 0;
1389 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1390
1391 /*
1392 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1393 */
1394 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301395 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1396 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1397 ((u32) scp->cmnd[2] << 8) |
1398 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001399
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301400 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001401 }
1402
1403 /*
1404 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1405 */
1406 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301407 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1408 ((u32) scp->cmnd[7] << 8));
1409 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1410 ((u32) scp->cmnd[3] << 16) |
1411 ((u32) scp->cmnd[4] << 8) |
1412 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001413 }
1414
1415 /*
1416 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1417 */
1418 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301419 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1420 ((u32) scp->cmnd[7] << 16) |
1421 ((u32) scp->cmnd[8] << 8) |
1422 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301424 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1425 ((u32) scp->cmnd[3] << 16) |
1426 ((u32) scp->cmnd[4] << 8) |
1427 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001428 }
1429
1430 /*
1431 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1432 */
1433 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301434 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1435 ((u32) scp->cmnd[11] << 16) |
1436 ((u32) scp->cmnd[12] << 8) |
1437 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001438
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301439 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1440 ((u32) scp->cmnd[7] << 16) |
1441 ((u32) scp->cmnd[8] << 8) |
1442 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001443
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301444 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1445 ((u32) scp->cmnd[3] << 16) |
1446 ((u32) scp->cmnd[4] << 8) |
1447 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001448
1449 }
1450
1451 /*
1452 * Construct SGL
1453 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001454 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301455 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001456 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1457 &ldio->sgl);
1458 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301459 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001460 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1461 } else
1462 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1463
Yang, Bobdc6fb82009-12-06 08:30:19 -07001464 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001465 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001466 ldio->sge_count);
1467 return 0;
1468 }
1469
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001470 /*
1471 * Sense info specific
1472 */
1473 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1474 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301475 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001476
Sumant Patrob1df99d2006-10-03 12:40:47 -07001477 /*
1478 * Compute the total number of frames this command consumes. FW uses
1479 * this number to pull sufficient number of frames from host memory.
1480 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001481 cmd->frame_count = megasas_get_frame_count(instance,
1482 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001483
1484 return cmd->frame_count;
1485}
1486
1487/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301488 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1489 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001490 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001491 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001492 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301493inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001494{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301495 int ret;
1496
Sumant Patrocb59aa62006-01-25 11:53:25 -08001497 switch (cmd->cmnd[0]) {
1498 case READ_10:
1499 case WRITE_10:
1500 case READ_12:
1501 case WRITE_12:
1502 case READ_6:
1503 case WRITE_6:
1504 case READ_16:
1505 case WRITE_16:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301506 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1507 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1508 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001509 default:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301510 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1511 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001512 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301513 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001514}
1515
Sumant Patro658dced2006-10-03 13:09:14 -07001516 /**
1517 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001518 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001519 * @instance: Adapter soft state
1520 */
1521static inline void
1522megasas_dump_pending_frames(struct megasas_instance *instance)
1523{
1524 struct megasas_cmd *cmd;
1525 int i,n;
1526 union megasas_sgl *mfi_sgl;
1527 struct megasas_io_frame *ldio;
1528 struct megasas_pthru_frame *pthru;
1529 u32 sgcount;
1530 u32 max_cmd = instance->max_fw_cmds;
1531
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001532 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1533 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 -07001534 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001535 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 -07001536 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001537 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 -07001538
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001539 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001540 for (i = 0; i < max_cmd; i++) {
1541 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001542 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001543 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001544 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 +05301545 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001546 ldio = (struct megasas_io_frame *)cmd->frame;
1547 mfi_sgl = &ldio->sgl;
1548 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001549 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 +05301550 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1551 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1552 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1553 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001554 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001555 pthru = (struct megasas_pthru_frame *) cmd->frame;
1556 mfi_sgl = &pthru->sgl;
1557 sgcount = pthru->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 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1560 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1561 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1562 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001563 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001564 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1565 for (n = 0; n < sgcount; n++) {
1566 if (IS_DMA64)
1567 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1568 le32_to_cpu(mfi_sgl->sge64[n].length),
1569 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1570 else
1571 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1572 le32_to_cpu(mfi_sgl->sge32[n].length),
1573 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001574 }
1575 }
Sumant Patro658dced2006-10-03 13:09:14 -07001576 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001577 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001578 for (i = 0; i < max_cmd; i++) {
1579
1580 cmd = instance->cmd_list[i];
1581
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001582 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001583 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001584 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001585 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001586}
1587
adam radfordcd50ba82010-12-21 10:23:23 -08001588u32
1589megasas_build_and_issue_cmd(struct megasas_instance *instance,
1590 struct scsi_cmnd *scmd)
1591{
1592 struct megasas_cmd *cmd;
1593 u32 frame_count;
1594
1595 cmd = megasas_get_cmd(instance);
1596 if (!cmd)
1597 return SCSI_MLQUEUE_HOST_BUSY;
1598
1599 /*
1600 * Logical drive command
1601 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301602 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001603 frame_count = megasas_build_ldio(instance, scmd, cmd);
1604 else
1605 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1606
1607 if (!frame_count)
1608 goto out_return_cmd;
1609
1610 cmd->scmd = scmd;
1611 scmd->SCp.ptr = (char *)cmd;
1612
1613 /*
1614 * Issue the command to the FW
1615 */
1616 atomic_inc(&instance->fw_outstanding);
1617
1618 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1619 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001620
1621 return 0;
1622out_return_cmd:
1623 megasas_return_cmd(instance, cmd);
1624 return 1;
1625}
1626
1627
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001628/**
1629 * megasas_queue_command - Queue entry point
1630 * @scmd: SCSI command to be queued
1631 * @done: Callback entry point
1632 */
1633static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301634megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001635{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001636 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001637 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001638
1639 instance = (struct megasas_instance *)
1640 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001641
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301642 if (instance->unload == 1) {
1643 scmd->result = DID_NO_CONNECT << 16;
1644 scmd->scsi_done(scmd);
1645 return 0;
1646 }
1647
bo yang39a98552010-09-22 22:36:29 -04001648 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001649 return SCSI_MLQUEUE_HOST_BUSY;
1650
bo yang39a98552010-09-22 22:36:29 -04001651 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301652
adam radford229fe472014-03-10 02:51:56 -07001653 /* Check for an mpio path and adjust behavior */
1654 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1655 if (megasas_check_mpio_paths(instance, scmd) ==
1656 (DID_RESET << 16)) {
1657 spin_unlock_irqrestore(&instance->hba_lock, flags);
1658 return SCSI_MLQUEUE_HOST_BUSY;
1659 } else {
1660 spin_unlock_irqrestore(&instance->hba_lock, flags);
1661 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301662 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001663 return 0;
1664 }
1665 }
1666
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301667 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1668 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford229fe472014-03-10 02:51:56 -07001669 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301670 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301671 return 0;
1672 }
1673
bo yang39a98552010-09-22 22:36:29 -04001674 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1675 spin_unlock_irqrestore(&instance->hba_lock, flags);
1676 return SCSI_MLQUEUE_HOST_BUSY;
1677 }
1678
1679 spin_unlock_irqrestore(&instance->hba_lock, flags);
1680
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001681 scmd->result = 0;
1682
Sumant Patrocb59aa62006-01-25 11:53:25 -08001683 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301684 (scmd->device->id >= instance->fw_supported_vd_count ||
1685 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001686 scmd->result = DID_BAD_TARGET << 16;
1687 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001688 }
1689
Sumant Patro02b01e02007-02-14 13:00:55 -08001690 switch (scmd->cmnd[0]) {
1691 case SYNCHRONIZE_CACHE:
1692 /*
1693 * FW takes care of flush cache on its own
1694 * No need to send it down
1695 */
1696 scmd->result = DID_OK << 16;
1697 goto out_done;
1698 default:
1699 break;
1700 }
1701
adam radfordcd50ba82010-12-21 10:23:23 -08001702 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001703 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001704 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001705 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001706
1707 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001708
Sumant Patrocb59aa62006-01-25 11:53:25 -08001709 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301710 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001711 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001712}
1713
Yang, Bo044833b2009-10-06 14:33:06 -06001714static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1715{
1716 int i;
1717
1718 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1719
1720 if ((megasas_mgmt_info.instance[i]) &&
1721 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1722 return megasas_mgmt_info.instance[i];
1723 }
1724
1725 return NULL;
1726}
1727
Christoph Hellwig147aab62006-02-17 12:13:48 +01001728static int megasas_slave_configure(struct scsi_device *sdev)
1729{
Yang, Bo044833b2009-10-06 14:33:06 -06001730 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001731 * The RAID firmware may require extended timeouts.
1732 */
Yang, Bo044833b2009-10-06 14:33:06 -06001733 blk_queue_rq_timeout(sdev->request_queue,
1734 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301735
Yang, Bo044833b2009-10-06 14:33:06 -06001736 return 0;
1737}
1738
1739static int megasas_slave_alloc(struct scsi_device *sdev)
1740{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001741 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001742 struct megasas_instance *instance ;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001743
Yang, Bo044833b2009-10-06 14:33:06 -06001744 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301745 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001746 /*
1747 * Open the OS scan to the SYSTEM PD
1748 */
1749 pd_index =
1750 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1751 sdev->id;
sumit.saxena@avagotech.com0d5b47a2015-10-15 13:39:24 +05301752 if ((instance->pd_list[pd_index].driveState ==
1753 MR_PD_STATE_SYSTEM) ||
1754 (instance->pd_list[pd_index].driveType != TYPE_DISK)) {
Yang, Bo044833b2009-10-06 14:33:06 -06001755 return 0;
1756 }
1757 return -ENXIO;
1758 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001759 return 0;
1760}
1761
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301762/*
1763* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1764* kill adapter
1765* @instance: Adapter soft state
1766*
1767*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301768static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301769{
1770 int i;
1771 struct megasas_cmd *cmd_mfi;
1772 struct megasas_cmd_fusion *cmd_fusion;
1773 struct fusion_context *fusion = instance->ctrl_context;
1774
1775 /* Find all outstanding ioctls */
1776 if (fusion) {
1777 for (i = 0; i < instance->max_fw_cmds; i++) {
1778 cmd_fusion = fusion->cmd_list[i];
1779 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1780 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1781 if (cmd_mfi->sync_cmd &&
1782 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1783 megasas_complete_cmd(instance,
1784 cmd_mfi, DID_OK);
1785 }
1786 }
1787 } else {
1788 for (i = 0; i < instance->max_fw_cmds; i++) {
1789 cmd_mfi = instance->cmd_list[i];
1790 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1791 MFI_CMD_ABORT)
1792 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1793 }
1794 }
1795}
1796
1797
adam radford9c915a82010-12-21 13:34:31 -08001798void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001799{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301800 /* Set critical error to block I/O & ioctls in case caller didn't */
1801 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1802 /* Wait 1 second to ensure IO or ioctls in build have posted */
1803 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001804 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301805 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301806 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001807 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001808 /* Flush */
1809 readl(&instance->reg_set->doorbell);
1810 if (instance->mpio && instance->requestorId)
1811 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001812 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301813 writel(MFI_STOP_ADP,
1814 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001815 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301816 /* Complete outstanding ioctls when adapter is killed */
1817 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001818}
1819
1820 /**
1821 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1822 * restored to max value
1823 * @instance: Adapter soft state
1824 *
1825 */
1826void
1827megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1828{
1829 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301830
adam radford9c915a82010-12-21 13:34:31 -08001831 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001832 && time_after(jiffies, instance->last_time + 5 * HZ)
1833 && atomic_read(&instance->fw_outstanding) <
1834 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001835
1836 spin_lock_irqsave(instance->host->host_lock, flags);
1837 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001838
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301839 instance->host->can_queue = instance->max_scsi_cmds;
adam radford9c915a82010-12-21 13:34:31 -08001840 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001841 }
1842}
1843
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001844/**
bo yang7343eb62007-11-09 04:35:44 -05001845 * megasas_complete_cmd_dpc - Returns FW's controller structure
1846 * @instance_addr: Address of adapter soft state
1847 *
1848 * Tasklet to complete cmds
1849 */
1850static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1851{
1852 u32 producer;
1853 u32 consumer;
1854 u32 context;
1855 struct megasas_cmd *cmd;
1856 struct megasas_instance *instance =
1857 (struct megasas_instance *)instance_addr;
1858 unsigned long flags;
1859
1860 /* If we have already declared adapter dead, donot complete cmds */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001861 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05001862 return;
1863
1864 spin_lock_irqsave(&instance->completion_lock, flags);
1865
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301866 producer = le32_to_cpu(*instance->producer);
1867 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001868
1869 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301870 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001871 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001872 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04001873 context);
1874 BUG();
1875 }
bo yang7343eb62007-11-09 04:35:44 -05001876
1877 cmd = instance->cmd_list[context];
1878
1879 megasas_complete_cmd(instance, cmd, DID_OK);
1880
1881 consumer++;
1882 if (consumer == (instance->max_fw_cmds + 1)) {
1883 consumer = 0;
1884 }
1885 }
1886
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301887 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05001888
1889 spin_unlock_irqrestore(&instance->completion_lock, flags);
1890
1891 /*
1892 * Check if we can restore can_queue
1893 */
adam radford9c915a82010-12-21 13:34:31 -08001894 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001895}
1896
adam radford229fe472014-03-10 02:51:56 -07001897/**
1898 * megasas_start_timer - Initializes a timer object
1899 * @instance: Adapter soft state
1900 * @timer: timer object to be initialized
1901 * @fn: timer function
1902 * @interval: time interval between timer function call
1903 *
1904 */
1905void megasas_start_timer(struct megasas_instance *instance,
1906 struct timer_list *timer,
1907 void *fn, unsigned long interval)
1908{
1909 init_timer(timer);
1910 timer->expires = jiffies + interval;
1911 timer->data = (unsigned long)instance;
1912 timer->function = fn;
1913 add_timer(timer);
1914}
1915
Yang, Bo707e09b2010-10-12 07:20:27 -06001916static void
1917megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1918
1919static void
1920process_fw_state_change_wq(struct work_struct *work);
1921
1922void megasas_do_ocr(struct megasas_instance *instance)
1923{
1924 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1925 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1926 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301927 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06001928 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301929 instance->instancet->disable_intr(instance);
Yang, Bo707e09b2010-10-12 07:20:27 -06001930 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1931 instance->issuepend_done = 0;
1932
1933 atomic_set(&instance->fw_outstanding, 0);
1934 megasas_internal_reset_defer_cmds(instance);
1935 process_fw_state_change_wq(&instance->work_init);
1936}
1937
Adam Radford4cbfea82014-07-09 15:17:56 -07001938static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1939 int initial)
adam radford229fe472014-03-10 02:51:56 -07001940{
1941 struct megasas_cmd *cmd;
1942 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07001943 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07001944 dma_addr_t new_affiliation_111_h;
1945 int ld, retval = 0;
1946 u8 thisVf;
1947
1948 cmd = megasas_get_cmd(instance);
1949
1950 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001951 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
1952 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07001953 instance->host->host_no);
1954 return -ENOMEM;
1955 }
1956
1957 dcmd = &cmd->frame->dcmd;
1958
Adam Radford4cbfea82014-07-09 15:17:56 -07001959 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001960 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
1961 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07001962 megasas_return_cmd(instance, cmd);
1963 return -ENOMEM;
1964 }
1965
1966 if (initial)
adam radford229fe472014-03-10 02:51:56 -07001967 memset(instance->vf_affiliation_111, 0,
1968 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001969 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07001970 new_affiliation_111 =
1971 pci_alloc_consistent(instance->pdev,
1972 sizeof(struct MR_LD_VF_AFFILIATION_111),
1973 &new_affiliation_111_h);
1974 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001975 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
1976 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07001977 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07001978 megasas_return_cmd(instance, cmd);
1979 return -ENOMEM;
1980 }
Adam Radford4cbfea82014-07-09 15:17:56 -07001981 memset(new_affiliation_111, 0,
1982 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001983 }
1984
1985 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1986
1987 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301988 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07001989 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05301990 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07001991 dcmd->timeout = 0;
1992 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05301993 dcmd->data_xfer_len =
1994 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
1995 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07001996
Adam Radford4cbfea82014-07-09 15:17:56 -07001997 if (initial)
1998 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05301999 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002000 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302001 dcmd->sgl.sge32[0].phys_addr =
2002 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002003
Christoph Hellwig2213a462015-04-23 16:33:54 +05302004 dcmd->sgl.sge32[0].length = cpu_to_le32(
2005 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002006
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002007 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002008 "scsi%d\n", instance->host->host_no);
2009
2010 megasas_issue_blocked_cmd(instance, cmd, 0);
2011
2012 if (dcmd->cmd_status) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002013 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2014 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002015 dcmd->cmd_status, instance->host->host_no);
2016 retval = 1; /* Do a scan if we couldn't get affiliation */
2017 goto out;
2018 }
2019
2020 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002021 thisVf = new_affiliation_111->thisVf;
2022 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2023 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2024 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002025 dev_warn(&instance->pdev->dev, "SR-IOV: "
2026 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002027 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002028 memcpy(instance->vf_affiliation_111,
2029 new_affiliation_111,
2030 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002031 retval = 1;
2032 goto out;
2033 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002034 }
2035out:
2036 if (new_affiliation_111) {
2037 pci_free_consistent(instance->pdev,
2038 sizeof(struct MR_LD_VF_AFFILIATION_111),
2039 new_affiliation_111,
2040 new_affiliation_111_h);
2041 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302042
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302043 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002044
2045 return retval;
2046}
2047
2048static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2049 int initial)
2050{
2051 struct megasas_cmd *cmd;
2052 struct megasas_dcmd_frame *dcmd;
2053 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2054 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2055 dma_addr_t new_affiliation_h;
2056 int i, j, retval = 0, found = 0, doscan = 0;
2057 u8 thisVf;
2058
2059 cmd = megasas_get_cmd(instance);
2060
2061 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002062 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2063 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002064 instance->host->host_no);
2065 return -ENOMEM;
2066 }
2067
2068 dcmd = &cmd->frame->dcmd;
2069
2070 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002071 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2072 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002073 megasas_return_cmd(instance, cmd);
2074 return -ENOMEM;
2075 }
2076
2077 if (initial)
2078 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2079 sizeof(struct MR_LD_VF_AFFILIATION));
2080 else {
2081 new_affiliation =
2082 pci_alloc_consistent(instance->pdev,
2083 (MAX_LOGICAL_DRIVES + 1) *
2084 sizeof(struct MR_LD_VF_AFFILIATION),
2085 &new_affiliation_h);
2086 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002087 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2088 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002089 instance->host->host_no);
2090 megasas_return_cmd(instance, cmd);
2091 return -ENOMEM;
2092 }
2093 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2094 sizeof(struct MR_LD_VF_AFFILIATION));
2095 }
2096
2097 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2098
2099 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302100 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002101 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302102 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002103 dcmd->timeout = 0;
2104 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302105 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2106 sizeof(struct MR_LD_VF_AFFILIATION));
2107 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002108
2109 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302110 dcmd->sgl.sge32[0].phys_addr =
2111 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002112 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302113 dcmd->sgl.sge32[0].phys_addr =
2114 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002115
Christoph Hellwig2213a462015-04-23 16:33:54 +05302116 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2117 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002118
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002119 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002120 "scsi%d\n", instance->host->host_no);
2121
2122 megasas_issue_blocked_cmd(instance, cmd, 0);
2123
2124 if (dcmd->cmd_status) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002125 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2126 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002127 dcmd->cmd_status, instance->host->host_no);
2128 retval = 1; /* Do a scan if we couldn't get affiliation */
2129 goto out;
2130 }
2131
2132 if (!initial) {
2133 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002134 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2135 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002136 instance->host->host_no);
2137 retval = 1;
2138 goto out;
2139 }
2140 newmap = new_affiliation->map;
2141 savedmap = instance->vf_affiliation->map;
2142 thisVf = new_affiliation->thisVf;
2143 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2144 found = 0;
2145 for (j = 0; j < instance->vf_affiliation->ldCount;
2146 j++) {
2147 if (newmap->ref.targetId ==
2148 savedmap->ref.targetId) {
2149 found = 1;
2150 if (newmap->policy[thisVf] !=
2151 savedmap->policy[thisVf]) {
2152 doscan = 1;
2153 goto out;
2154 }
adam radford229fe472014-03-10 02:51:56 -07002155 }
2156 savedmap = (struct MR_LD_VF_MAP *)
2157 ((unsigned char *)savedmap +
2158 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002159 }
2160 if (!found && newmap->policy[thisVf] !=
2161 MR_LD_ACCESS_HIDDEN) {
2162 doscan = 1;
2163 goto out;
2164 }
2165 newmap = (struct MR_LD_VF_MAP *)
2166 ((unsigned char *)newmap + newmap->size);
2167 }
2168
2169 newmap = new_affiliation->map;
2170 savedmap = instance->vf_affiliation->map;
2171
2172 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2173 found = 0;
2174 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2175 if (savedmap->ref.targetId ==
2176 newmap->ref.targetId) {
2177 found = 1;
2178 if (savedmap->policy[thisVf] !=
2179 newmap->policy[thisVf]) {
2180 doscan = 1;
2181 goto out;
2182 }
2183 }
adam radford229fe472014-03-10 02:51:56 -07002184 newmap = (struct MR_LD_VF_MAP *)
2185 ((unsigned char *)newmap +
2186 newmap->size);
2187 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002188 if (!found && savedmap->policy[thisVf] !=
2189 MR_LD_ACCESS_HIDDEN) {
2190 doscan = 1;
2191 goto out;
2192 }
2193 savedmap = (struct MR_LD_VF_MAP *)
2194 ((unsigned char *)savedmap +
2195 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002196 }
2197 }
2198out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002199 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002200 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2201 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002202 memcpy(instance->vf_affiliation, new_affiliation,
2203 new_affiliation->size);
2204 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002205 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002206
2207 if (new_affiliation)
2208 pci_free_consistent(instance->pdev,
2209 (MAX_LOGICAL_DRIVES + 1) *
2210 sizeof(struct MR_LD_VF_AFFILIATION),
2211 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302212 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002213
2214 return retval;
2215}
2216
Adam Radford4cbfea82014-07-09 15:17:56 -07002217/* This function will get the current SR-IOV LD/VF affiliation */
2218static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2219 int initial)
2220{
2221 int retval;
2222
2223 if (instance->PlasmaFW111)
2224 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2225 else
2226 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2227 return retval;
2228}
2229
adam radford229fe472014-03-10 02:51:56 -07002230/* This function will tell FW to start the SR-IOV heartbeat */
2231int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2232 int initial)
2233{
2234 struct megasas_cmd *cmd;
2235 struct megasas_dcmd_frame *dcmd;
2236 int retval = 0;
2237
2238 cmd = megasas_get_cmd(instance);
2239
2240 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002241 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2242 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002243 instance->host->host_no);
2244 return -ENOMEM;
2245 }
2246
2247 dcmd = &cmd->frame->dcmd;
2248
2249 if (initial) {
2250 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002251 pci_zalloc_consistent(instance->pdev,
2252 sizeof(struct MR_CTRL_HB_HOST_MEM),
2253 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002254 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002255 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2256 " memory for heartbeat host memory for scsi%d\n",
2257 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002258 retval = -ENOMEM;
2259 goto out;
2260 }
adam radford229fe472014-03-10 02:51:56 -07002261 }
2262
2263 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2264
Christoph Hellwig2213a462015-04-23 16:33:54 +05302265 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002266 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302267 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002268 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302269 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002270 dcmd->timeout = 0;
2271 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302272 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2273 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2274 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2275 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002276
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002277 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002278 instance->host->host_no);
2279
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302280 if (instance->ctrl_context && !instance->mask_interrupts)
2281 retval = megasas_issue_blocked_cmd(instance, cmd,
2282 MEGASAS_ROUTINE_WAIT_TIME_VF);
2283 else
2284 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002285
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302286 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302287 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2288 "_MEM_ALLOC DCMD %s for scsi%d\n",
2289 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2290 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002291 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002292 }
2293
2294out:
2295 megasas_return_cmd(instance, cmd);
2296
2297 return retval;
2298}
2299
2300/* Handler for SR-IOV heartbeat */
2301void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2302{
2303 struct megasas_instance *instance =
2304 (struct megasas_instance *)instance_addr;
2305
2306 if (instance->hb_host_mem->HB.fwCounter !=
2307 instance->hb_host_mem->HB.driverCounter) {
2308 instance->hb_host_mem->HB.driverCounter =
2309 instance->hb_host_mem->HB.fwCounter;
2310 mod_timer(&instance->sriov_heartbeat_timer,
2311 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2312 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002313 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002314 "completed for scsi%d\n", instance->host->host_no);
2315 schedule_work(&instance->work_init);
2316 }
2317}
2318
bo yang7343eb62007-11-09 04:35:44 -05002319/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002320 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2321 * @instance: Adapter soft state
2322 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002323 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002324 * complete all its outstanding commands. Returns error if one or more IOs
2325 * are pending after this time period. It also marks the controller dead.
2326 */
2327static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2328{
2329 int i;
bo yang39a98552010-09-22 22:36:29 -04002330 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002331 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002332 u8 adprecovery;
2333 unsigned long flags;
2334 struct list_head clist_local;
2335 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002336 u32 fw_state;
2337 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04002338
2339 spin_lock_irqsave(&instance->hba_lock, flags);
2340 adprecovery = instance->adprecovery;
2341 spin_unlock_irqrestore(&instance->hba_lock, flags);
2342
2343 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2344
2345 INIT_LIST_HEAD(&clist_local);
2346 spin_lock_irqsave(&instance->hba_lock, flags);
2347 list_splice_init(&instance->internal_reset_pending_q,
2348 &clist_local);
2349 spin_unlock_irqrestore(&instance->hba_lock, flags);
2350
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002351 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002352 for (i = 0; i < wait_time; i++) {
2353 msleep(1000);
2354 spin_lock_irqsave(&instance->hba_lock, flags);
2355 adprecovery = instance->adprecovery;
2356 spin_unlock_irqrestore(&instance->hba_lock, flags);
2357 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2358 break;
2359 }
2360
2361 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002362 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
bo yang39a98552010-09-22 22:36:29 -04002363 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002364 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
bo yang39a98552010-09-22 22:36:29 -04002365 spin_unlock_irqrestore(&instance->hba_lock, flags);
2366 return FAILED;
2367 }
2368
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002369 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002370 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002371 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002372 struct megasas_cmd, list);
2373 list_del_init(&reset_cmd->list);
2374 if (reset_cmd->scmd) {
2375 reset_cmd->scmd->result = DID_RESET << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002376 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002377 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002378 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002379
2380 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2381 megasas_return_cmd(instance, reset_cmd);
2382 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002383 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002384 "reset queue\n",
2385 reset_cmd);
2386
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302387 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002388 instance->instancet->fire_cmd(instance,
2389 reset_cmd->frame_phys_addr,
2390 0, instance->reg_set);
2391 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002392 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002393 "cmds lst\n",
2394 reset_cmd);
2395 }
2396 reset_index++;
2397 }
2398
2399 return SUCCESS;
2400 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002401
adam radfordc007b8b2012-07-17 18:20:24 -07002402 for (i = 0; i < resetwaittime; i++) {
Sumant Patroe4a082c2006-05-30 12:03:37 -07002403 int outstanding = atomic_read(&instance->fw_outstanding);
2404
2405 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002406 break;
2407
2408 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002409 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002410 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002411 /*
2412 * Call cmd completion routine. Cmd to be
2413 * be completed directly without depending on isr.
2414 */
2415 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002416 }
2417
2418 msleep(1000);
2419 }
2420
Yang, Bo707e09b2010-10-12 07:20:27 -06002421 i = 0;
2422 kill_adapter_flag = 0;
2423 do {
2424 fw_state = instance->instancet->read_fw_status_reg(
2425 instance->reg_set) & MFI_STATE_MASK;
2426 if ((fw_state == MFI_STATE_FAULT) &&
2427 (instance->disableOnlineCtrlReset == 0)) {
2428 if (i == 3) {
2429 kill_adapter_flag = 2;
2430 break;
2431 }
2432 megasas_do_ocr(instance);
2433 kill_adapter_flag = 1;
2434
2435 /* wait for 1 secs to let FW finish the pending cmds */
2436 msleep(1000);
2437 }
2438 i++;
2439 } while (i <= 3);
2440
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002441 if (atomic_read(&instance->fw_outstanding) && !kill_adapter_flag) {
Yang, Bo707e09b2010-10-12 07:20:27 -06002442 if (instance->disableOnlineCtrlReset == 0) {
Yang, Bo707e09b2010-10-12 07:20:27 -06002443 megasas_do_ocr(instance);
2444
2445 /* wait for 5 secs to let FW finish the pending cmds */
2446 for (i = 0; i < wait_time; i++) {
2447 int outstanding =
2448 atomic_read(&instance->fw_outstanding);
2449 if (!outstanding)
2450 return SUCCESS;
2451 msleep(1000);
2452 }
2453 }
2454 }
2455
2456 if (atomic_read(&instance->fw_outstanding) ||
2457 (kill_adapter_flag == 2)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002458 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002459 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002460 * Send signal to FW to stop processing any pending cmds.
2461 * The controller will be taken offline by the OS now.
2462 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002463 if ((instance->pdev->device ==
2464 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2465 (instance->pdev->device ==
2466 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2467 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08002468 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002469 } else {
2470 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07002471 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002472 }
Sumant Patro658dced2006-10-03 13:09:14 -07002473 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04002474 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002475 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
bo yang39a98552010-09-22 22:36:29 -04002476 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002477 return FAILED;
2478 }
2479
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002480 dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
bo yang39a98552010-09-22 22:36:29 -04002481
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002482 return SUCCESS;
2483}
2484
2485/**
2486 * megasas_generic_reset - Generic reset routine
2487 * @scmd: Mid-layer SCSI command
2488 *
2489 * This routine implements a generic reset handler for device, bus and host
2490 * reset requests. Device, bus and host specific reset handlers can use this
2491 * function after they do their specific tasks.
2492 */
2493static int megasas_generic_reset(struct scsi_cmnd *scmd)
2494{
2495 int ret_val;
2496 struct megasas_instance *instance;
2497
2498 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2499
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002500 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2501 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002502
bo yang39a98552010-09-22 22:36:29 -04002503 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002504 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002505 return FAILED;
2506 }
2507
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002508 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002509 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002510 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002511 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002512 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002513
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002514 return ret_val;
2515}
2516
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002517/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002518 * megasas_reset_timer - quiesce the adapter if required
2519 * @scmd: scsi cmnd
2520 *
2521 * Sets the FW busy flag and reduces the host->can_queue if the
2522 * cmd has not been completed within the timeout period.
2523 */
2524static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002525blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002526{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002527 struct megasas_instance *instance;
2528 unsigned long flags;
2529
2530 if (time_after(jiffies, scmd->jiffies_at_alloc +
2531 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002532 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002533 }
2534
adam radfordf575c5d2011-10-13 16:01:12 -07002535 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002536 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2537 /* FW is busy, throttle IO */
2538 spin_lock_irqsave(instance->host->host_lock, flags);
2539
adam radfordc5daa6a2012-07-17 18:20:03 -07002540 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002541 instance->last_time = jiffies;
2542 instance->flag |= MEGASAS_FW_BUSY;
2543
2544 spin_unlock_irqrestore(instance->host->host_lock, flags);
2545 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002546 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002547}
2548
2549/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002550 * megasas_reset_device - Device reset handler entry point
2551 */
2552static int megasas_reset_device(struct scsi_cmnd *scmd)
2553{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002554 /*
2555 * First wait for all commands to complete
2556 */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002557 return megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002558}
2559
2560/**
2561 * megasas_reset_bus_host - Bus & host reset handler entry point
2562 */
2563static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2564{
2565 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002566 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002567
adam radford9c915a82010-12-21 13:34:31 -08002568 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002569
2570 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002571 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002572 */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302573 if (instance->ctrl_context)
adam radford229fe472014-03-10 02:51:56 -07002574 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002575 else
2576 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002577
2578 return ret;
2579}
2580
2581/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002582 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002583 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002584 * @bdev: block device
2585 * @capacity: drive capacity
2586 * @geom: geometry parameters
2587 */
2588static int
2589megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2590 sector_t capacity, int geom[])
2591{
2592 int heads;
2593 int sectors;
2594 sector_t cylinders;
2595 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002596
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002597 /* Default heads (64) & sectors (32) */
2598 heads = 64;
2599 sectors = 32;
2600
2601 tmp = heads * sectors;
2602 cylinders = capacity;
2603
2604 sector_div(cylinders, tmp);
2605
2606 /*
2607 * Handle extended translation size for logical drives > 1Gb
2608 */
2609
2610 if (capacity >= 0x200000) {
2611 heads = 255;
2612 sectors = 63;
2613 tmp = heads*sectors;
2614 cylinders = capacity;
2615 sector_div(cylinders, tmp);
2616 }
2617
2618 geom[0] = heads;
2619 geom[1] = sectors;
2620 geom[2] = cylinders;
2621
2622 return 0;
2623}
2624
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002625static void megasas_aen_polling(struct work_struct *work);
2626
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002627/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002628 * megasas_service_aen - Processes an event notification
2629 * @instance: Adapter soft state
2630 * @cmd: AEN command completed by the ISR
2631 *
2632 * For AEN, driver sends a command down to FW that is held by the FW till an
2633 * event occurs. When an event of interest occurs, FW completes the command
2634 * that it was previously holding.
2635 *
2636 * This routines sends SIGIO signal to processes that have registered with the
2637 * driver for AEN.
2638 */
2639static void
2640megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2641{
Yang, Boc3518832009-10-06 14:18:02 -06002642 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002643
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002644 /*
2645 * Don't signal app if it is just an aborted previously registered aen
2646 */
Yang, Boc3518832009-10-06 14:18:02 -06002647 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2648 spin_lock_irqsave(&poll_aen_lock, flags);
2649 megasas_poll_wait_aen = 1;
2650 spin_unlock_irqrestore(&poll_aen_lock, flags);
2651 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002652 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002653 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002654 else
2655 cmd->abort_aen = 0;
2656
2657 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302658
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302659 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002660
bo yang39a98552010-09-22 22:36:29 -04002661 if ((instance->unload == 0) &&
2662 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002663 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002664
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002665 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2666 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002667 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002668 } else {
2669 ev->instance = instance;
2670 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002671 INIT_DELAYED_WORK(&ev->hotplug_work,
2672 megasas_aen_polling);
2673 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002674 }
2675 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002676}
2677
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302678static ssize_t
2679megasas_fw_crash_buffer_store(struct device *cdev,
2680 struct device_attribute *attr, const char *buf, size_t count)
2681{
2682 struct Scsi_Host *shost = class_to_shost(cdev);
2683 struct megasas_instance *instance =
2684 (struct megasas_instance *) shost->hostdata;
2685 int val = 0;
2686 unsigned long flags;
2687
2688 if (kstrtoint(buf, 0, &val) != 0)
2689 return -EINVAL;
2690
2691 spin_lock_irqsave(&instance->crashdump_lock, flags);
2692 instance->fw_crash_buffer_offset = val;
2693 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2694 return strlen(buf);
2695}
2696
2697static ssize_t
2698megasas_fw_crash_buffer_show(struct device *cdev,
2699 struct device_attribute *attr, char *buf)
2700{
2701 struct Scsi_Host *shost = class_to_shost(cdev);
2702 struct megasas_instance *instance =
2703 (struct megasas_instance *) shost->hostdata;
2704 u32 size;
2705 unsigned long buff_addr;
2706 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2707 unsigned long src_addr;
2708 unsigned long flags;
2709 u32 buff_offset;
2710
2711 spin_lock_irqsave(&instance->crashdump_lock, flags);
2712 buff_offset = instance->fw_crash_buffer_offset;
2713 if (!instance->crash_dump_buf &&
2714 !((instance->fw_crash_state == AVAILABLE) ||
2715 (instance->fw_crash_state == COPYING))) {
2716 dev_err(&instance->pdev->dev,
2717 "Firmware crash dump is not available\n");
2718 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2719 return -EINVAL;
2720 }
2721
2722 buff_addr = (unsigned long) buf;
2723
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002724 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302725 dev_err(&instance->pdev->dev,
2726 "Firmware crash dump offset is out of range\n");
2727 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2728 return 0;
2729 }
2730
2731 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2732 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2733
2734 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2735 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002736 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302737 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2738
2739 return size;
2740}
2741
2742static ssize_t
2743megasas_fw_crash_buffer_size_show(struct device *cdev,
2744 struct device_attribute *attr, char *buf)
2745{
2746 struct Scsi_Host *shost = class_to_shost(cdev);
2747 struct megasas_instance *instance =
2748 (struct megasas_instance *) shost->hostdata;
2749
2750 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2751 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2752}
2753
2754static ssize_t
2755megasas_fw_crash_state_store(struct device *cdev,
2756 struct device_attribute *attr, const char *buf, size_t count)
2757{
2758 struct Scsi_Host *shost = class_to_shost(cdev);
2759 struct megasas_instance *instance =
2760 (struct megasas_instance *) shost->hostdata;
2761 int val = 0;
2762 unsigned long flags;
2763
2764 if (kstrtoint(buf, 0, &val) != 0)
2765 return -EINVAL;
2766
2767 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2768 dev_err(&instance->pdev->dev, "application updates invalid "
2769 "firmware crash state\n");
2770 return -EINVAL;
2771 }
2772
2773 instance->fw_crash_state = val;
2774
2775 if ((val == COPIED) || (val == COPY_ERROR)) {
2776 spin_lock_irqsave(&instance->crashdump_lock, flags);
2777 megasas_free_host_crash_buffer(instance);
2778 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2779 if (val == COPY_ERROR)
2780 dev_info(&instance->pdev->dev, "application failed to "
2781 "copy Firmware crash dump\n");
2782 else
2783 dev_info(&instance->pdev->dev, "Firmware crash dump "
2784 "copied successfully\n");
2785 }
2786 return strlen(buf);
2787}
2788
2789static ssize_t
2790megasas_fw_crash_state_show(struct device *cdev,
2791 struct device_attribute *attr, char *buf)
2792{
2793 struct Scsi_Host *shost = class_to_shost(cdev);
2794 struct megasas_instance *instance =
2795 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002796
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302797 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2798}
2799
2800static ssize_t
2801megasas_page_size_show(struct device *cdev,
2802 struct device_attribute *attr, char *buf)
2803{
2804 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2805}
2806
2807static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2808 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2809static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2810 megasas_fw_crash_buffer_size_show, NULL);
2811static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2812 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2813static DEVICE_ATTR(page_size, S_IRUGO,
2814 megasas_page_size_show, NULL);
2815
2816struct device_attribute *megaraid_host_attrs[] = {
2817 &dev_attr_fw_crash_buffer_size,
2818 &dev_attr_fw_crash_buffer,
2819 &dev_attr_fw_crash_state,
2820 &dev_attr_page_size,
2821 NULL,
2822};
2823
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002824/*
2825 * Scsi host template for megaraid_sas driver
2826 */
2827static struct scsi_host_template megasas_template = {
2828
2829 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05302830 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002831 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002832 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002833 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002834 .queuecommand = megasas_queue_command,
2835 .eh_device_reset_handler = megasas_reset_device,
2836 .eh_bus_reset_handler = megasas_reset_bus_host,
2837 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002838 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302839 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002840 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002841 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002842 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002843 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002844};
2845
2846/**
2847 * megasas_complete_int_cmd - Completes an internal command
2848 * @instance: Adapter soft state
2849 * @cmd: Command to be completed
2850 *
2851 * The megasas_issue_blocked_cmd() function waits for a command to complete
2852 * after it issues a command. This function wakes up that waiting routine by
2853 * calling wake_up() on the wait queue.
2854 */
2855static void
2856megasas_complete_int_cmd(struct megasas_instance *instance,
2857 struct megasas_cmd *cmd)
2858{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302859 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002860 wake_up(&instance->int_cmd_wait_q);
2861}
2862
2863/**
2864 * megasas_complete_abort - Completes aborting a command
2865 * @instance: Adapter soft state
2866 * @cmd: Cmd that was issued to abort another cmd
2867 *
adam radford0d490162010-12-14 19:17:17 -08002868 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2869 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002870 * wakes up all functions waiting on the same wait queue.
2871 */
2872static void
2873megasas_complete_abort(struct megasas_instance *instance,
2874 struct megasas_cmd *cmd)
2875{
2876 if (cmd->sync_cmd) {
2877 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302878 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002879 wake_up(&instance->abort_cmd_wait_q);
2880 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002881}
2882
2883/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002884 * megasas_complete_cmd - Completes a command
2885 * @instance: Adapter soft state
2886 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002887 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002888 * SCSI mid-layer instead of the value returned
2889 * by the FW. This should be used if caller wants
2890 * an alternate status (as in the case of aborted
2891 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002892 */
adam radford9c915a82010-12-21 13:34:31 -08002893void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002894megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2895 u8 alt_status)
2896{
2897 int exception = 0;
2898 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002899 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002900 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302901 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002902
bo yang39a98552010-09-22 22:36:29 -04002903 /* flag for the retry reset */
2904 cmd->retry_for_fw_reset = 0;
2905
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002906 if (cmd->scmd)
2907 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002908
2909 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002910 case MFI_CMD_INVALID:
2911 /* Some older 1068 controller FW may keep a pended
2912 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2913 when booting the kdump kernel. Ignore this command to
2914 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002915 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
2916 "completed\n");
2917 dev_warn(&instance->pdev->dev, "If you have a controller "
2918 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07002919 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002920 case MFI_CMD_PD_SCSI_IO:
2921 case MFI_CMD_LD_SCSI_IO:
2922
2923 /*
2924 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2925 * issued either through an IO path or an IOCTL path. If it
2926 * was via IOCTL, we will send it to internal completion.
2927 */
2928 if (cmd->sync_cmd) {
2929 cmd->sync_cmd = 0;
2930 megasas_complete_int_cmd(instance, cmd);
2931 break;
2932 }
2933
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002934 case MFI_CMD_LD_READ:
2935 case MFI_CMD_LD_WRITE:
2936
2937 if (alt_status) {
2938 cmd->scmd->result = alt_status << 16;
2939 exception = 1;
2940 }
2941
2942 if (exception) {
2943
Sumant Patroe4a082c2006-05-30 12:03:37 -07002944 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002945
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002946 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002947 cmd->scmd->scsi_done(cmd->scmd);
2948 megasas_return_cmd(instance, cmd);
2949
2950 break;
2951 }
2952
2953 switch (hdr->cmd_status) {
2954
2955 case MFI_STAT_OK:
2956 cmd->scmd->result = DID_OK << 16;
2957 break;
2958
2959 case MFI_STAT_SCSI_IO_FAILED:
2960 case MFI_STAT_LD_INIT_IN_PROGRESS:
2961 cmd->scmd->result =
2962 (DID_ERROR << 16) | hdr->scsi_status;
2963 break;
2964
2965 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2966
2967 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2968
2969 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2970 memset(cmd->scmd->sense_buffer, 0,
2971 SCSI_SENSE_BUFFERSIZE);
2972 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2973 hdr->sense_len);
2974
2975 cmd->scmd->result |= DRIVER_SENSE << 24;
2976 }
2977
2978 break;
2979
2980 case MFI_STAT_LD_OFFLINE:
2981 case MFI_STAT_DEVICE_NOT_FOUND:
2982 cmd->scmd->result = DID_BAD_TARGET << 16;
2983 break;
2984
2985 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002986 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002987 hdr->cmd_status);
2988 cmd->scmd->result = DID_ERROR << 16;
2989 break;
2990 }
2991
Sumant Patroe4a082c2006-05-30 12:03:37 -07002992 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002993
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002994 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002995 cmd->scmd->scsi_done(cmd->scmd);
2996 megasas_return_cmd(instance, cmd);
2997
2998 break;
2999
3000 case MFI_CMD_SMP:
3001 case MFI_CMD_STP:
3002 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303003 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003004 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303005 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3006 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303007 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003008 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303009 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003010 if (cmd->frame->hdr.cmd_status != 0) {
3011 if (cmd->frame->hdr.cmd_status !=
3012 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003013 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003014 cmd->frame->hdr.cmd_status);
3015 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303016 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003017 spin_unlock_irqrestore(
3018 instance->host->host_lock,
3019 flags);
3020 break;
3021 }
3022 } else
3023 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303024 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303025
3026 /*
3027 * Set fast path IO to ZERO.
3028 * Validate Map will set proper value.
3029 * Meanwhile all IOs will go as LD IO.
3030 */
3031 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003032 fusion->fast_path_io = 1;
3033 else
3034 fusion->fast_path_io = 0;
3035 megasas_sync_map_info(instance);
3036 spin_unlock_irqrestore(instance->host->host_lock,
3037 flags);
3038 break;
3039 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303040 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3041 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003042 spin_lock_irqsave(&poll_aen_lock, flags);
3043 megasas_poll_wait_aen = 0;
3044 spin_unlock_irqrestore(&poll_aen_lock, flags);
3045 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003046
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303047 /* FW has an updated PD sequence */
3048 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3049 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3050
3051 spin_lock_irqsave(instance->host->host_lock, flags);
3052 status = cmd->frame->hdr.cmd_status;
3053 instance->jbod_seq_cmd = NULL;
3054 megasas_return_cmd(instance, cmd);
3055
3056 if (status == MFI_STAT_OK) {
3057 instance->pd_seq_map_id++;
3058 /* Re-register a pd sync seq num cmd */
3059 if (megasas_sync_pd_seq_num(instance, true))
3060 instance->use_seqnum_jbod_fp = false;
3061 } else
3062 instance->use_seqnum_jbod_fp = false;
3063
3064 spin_unlock_irqrestore(instance->host->host_lock, flags);
3065 break;
3066 }
3067
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003068 /*
3069 * See if got an event notification
3070 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303071 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003072 megasas_service_aen(instance, cmd);
3073 else
3074 megasas_complete_int_cmd(instance, cmd);
3075
3076 break;
3077
3078 case MFI_CMD_ABORT:
3079 /*
3080 * Cmd issued to abort another cmd returned
3081 */
3082 megasas_complete_abort(instance, cmd);
3083 break;
3084
3085 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003086 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003087 hdr->cmd);
3088 break;
3089 }
3090}
3091
3092/**
bo yang39a98552010-09-22 22:36:29 -04003093 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003094 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003095 * @instance: Adapter soft state
3096 */
3097static inline void
3098megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3099{
3100 struct megasas_cmd *cmd;
3101 struct list_head clist_local;
3102 union megasas_evt_class_locale class_locale;
3103 unsigned long flags;
3104 u32 seq_num;
3105
3106 INIT_LIST_HEAD(&clist_local);
3107 spin_lock_irqsave(&instance->hba_lock, flags);
3108 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3109 spin_unlock_irqrestore(&instance->hba_lock, flags);
3110
3111 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003112 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003113 struct megasas_cmd, list);
3114 list_del_init(&cmd->list);
3115
3116 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003117 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3118 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003119 cmd, cmd->scmd, cmd->sync_cmd);
3120
3121 cmd->retry_for_fw_reset++;
3122
3123 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003124 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003125 "was tried multiple times during reset."
3126 "Shutting down the HBA\n",
3127 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303128 instance->instancet->disable_intr(instance);
3129 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003130 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003131 return;
3132 }
3133 }
3134
3135 if (cmd->sync_cmd == 1) {
3136 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003137 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003138 "cmd attached to internal command!\n");
3139 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003140 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003141 "on the internal reset queue,"
3142 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303143 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003144 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003145 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003146 0, instance->reg_set);
3147 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003148 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003149 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003150 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003151
3152 atomic_inc(&instance->fw_outstanding);
3153 instance->instancet->fire_cmd(instance,
3154 cmd->frame_phys_addr,
3155 cmd->frame_count-1, instance->reg_set);
3156 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003157 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003158 "internal reset defer list while re-issue!!\n",
3159 cmd);
3160 }
3161 }
3162
3163 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003164 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003165 megasas_return_cmd(instance, instance->aen_cmd);
3166
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003167 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003168 }
3169
3170 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003171 * Initiate AEN (Asynchronous Event Notification)
3172 */
bo yang39a98552010-09-22 22:36:29 -04003173 seq_num = instance->last_seq_num;
3174 class_locale.members.reserved = 0;
3175 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3176 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3177
3178 megasas_register_aen(instance, seq_num, class_locale.word);
3179}
3180
3181/**
3182 * Move the internal reset pending commands to a deferred queue.
3183 *
3184 * We move the commands pending at internal reset time to a
3185 * pending queue. This queue would be flushed after successful
3186 * completion of the internal reset sequence. if the internal reset
3187 * did not complete in time, the kernel reset handler would flush
3188 * these commands.
3189 **/
3190static void
3191megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3192{
3193 struct megasas_cmd *cmd;
3194 int i;
3195 u32 max_cmd = instance->max_fw_cmds;
3196 u32 defer_index;
3197 unsigned long flags;
3198
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003199 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303200 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003201 for (i = 0; i < max_cmd; i++) {
3202 cmd = instance->cmd_list[i];
3203 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003204 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003205 "on the defer queue as internal\n",
3206 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3207
3208 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003209 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003210 " moving this cmd:%p, %d %p, it was"
3211 "discovered on some list?\n",
3212 cmd, cmd->sync_cmd, cmd->scmd);
3213
3214 list_del_init(&cmd->list);
3215 }
3216 defer_index++;
3217 list_add_tail(&cmd->list,
3218 &instance->internal_reset_pending_q);
3219 }
3220 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303221 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003222}
3223
3224
3225static void
3226process_fw_state_change_wq(struct work_struct *work)
3227{
3228 struct megasas_instance *instance =
3229 container_of(work, struct megasas_instance, work_init);
3230 u32 wait;
3231 unsigned long flags;
3232
3233 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003234 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
bo yang39a98552010-09-22 22:36:29 -04003235 instance->adprecovery);
3236 return ;
3237 }
3238
3239 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003240 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003241 "state, restarting it...\n");
3242
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303243 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003244 atomic_set(&instance->fw_outstanding, 0);
3245
3246 atomic_set(&instance->fw_reset_no_pci_access, 1);
3247 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003248 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003249
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003250 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003251 "initiating next stage...\n");
3252
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003253 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003254 "state 2 starting...\n");
3255
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003256 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003257 for (wait = 0; wait < 30; wait++) {
3258 msleep(1000);
3259 }
3260
adam radford058a8fa2011-10-08 18:14:27 -07003261 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003262 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003263
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303264 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003265 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003266 return ;
3267 }
3268
3269 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3270 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3271 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3272 ) {
3273 *instance->consumer = *instance->producer;
3274 } else {
3275 *instance->consumer = 0;
3276 *instance->producer = 0;
3277 }
3278
3279 megasas_issue_init_mfi(instance);
3280
3281 spin_lock_irqsave(&instance->hba_lock, flags);
3282 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3283 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303284 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003285
3286 megasas_issue_pending_cmds_again(instance);
3287 instance->issuepend_done = 1;
3288 }
bo yang39a98552010-09-22 22:36:29 -04003289}
3290
3291/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003292 * megasas_deplete_reply_queue - Processes all completed commands
3293 * @instance: Adapter soft state
3294 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003295 * SCSI mid-layer instead of the status
3296 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003297 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003298 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003299static int
bo yang39a98552010-09-22 22:36:29 -04003300megasas_deplete_reply_queue(struct megasas_instance *instance,
3301 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003302{
bo yang39a98552010-09-22 22:36:29 -04003303 u32 mfiStatus;
3304 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003305
bo yang39a98552010-09-22 22:36:29 -04003306 if ((mfiStatus = instance->instancet->check_reset(instance,
3307 instance->reg_set)) == 1) {
3308 return IRQ_HANDLED;
3309 }
3310
3311 if ((mfiStatus = instance->instancet->clear_intr(
3312 instance->reg_set)
3313 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003314 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003315 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003316 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003317 }
3318
3319 instance->mfiStatus = mfiStatus;
3320
3321 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3322 fw_state = instance->instancet->read_fw_status_reg(
3323 instance->reg_set) & MFI_STATE_MASK;
3324
3325 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003326 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003327 fw_state);
3328 }
3329
3330 if ((fw_state == MFI_STATE_FAULT) &&
3331 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003332 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003333
3334 if ((instance->pdev->device ==
3335 PCI_DEVICE_ID_LSI_SAS1064R) ||
3336 (instance->pdev->device ==
3337 PCI_DEVICE_ID_DELL_PERC5) ||
3338 (instance->pdev->device ==
3339 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3340
3341 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303342 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003343 }
3344
3345
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303346 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003347 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3348 instance->issuepend_done = 0;
3349
3350 atomic_set(&instance->fw_outstanding, 0);
3351 megasas_internal_reset_defer_cmds(instance);
3352
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003353 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
bo yang39a98552010-09-22 22:36:29 -04003354 fw_state, instance->adprecovery);
3355
3356 schedule_work(&instance->work_init);
3357 return IRQ_HANDLED;
3358
3359 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003360 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003361 fw_state, instance->disableOnlineCtrlReset);
3362 }
3363 }
3364
Sumant Patro5d018ad2006-10-03 13:13:18 -07003365 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003366 return IRQ_HANDLED;
3367}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003368/**
3369 * megasas_isr - isr entry point
3370 */
David Howells7d12e782006-10-05 14:55:46 +01003371static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003372{
adam radfordc8e858f2011-10-08 18:15:13 -07003373 struct megasas_irq_context *irq_context = devp;
3374 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003375 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003376 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003377
adam radfordc8e858f2011-10-08 18:15:13 -07003378 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003379 return IRQ_HANDLED;
3380
bo yang39a98552010-09-22 22:36:29 -04003381 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003382 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003383 spin_unlock_irqrestore(&instance->hba_lock, flags);
3384
3385 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003386}
3387
3388/**
3389 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003390 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003391 *
3392 * During the initialization, FW passes can potentially be in any one of
3393 * several possible states. If the FW in operational, waiting-for-handshake
3394 * states, driver must take steps to bring it to ready state. Otherwise, it
3395 * has to wait for the ready state.
3396 */
adam radford9c915a82010-12-21 13:34:31 -08003397int
adam radford058a8fa2011-10-08 18:14:27 -07003398megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003399{
3400 int i;
3401 u8 max_wait;
3402 u32 fw_state;
3403 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003404 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003405
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003406 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3407 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003408
Sumant Patroe3bbff92006-10-03 12:28:49 -07003409 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003410 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003411 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003412
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003413 while (fw_state != MFI_STATE_READY) {
3414
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003415 switch (fw_state) {
3416
3417 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003418 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003419 if (ocr) {
3420 max_wait = MEGASAS_RESET_WAIT_TIME;
3421 cur_state = MFI_STATE_FAULT;
3422 break;
3423 } else
3424 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003425
3426 case MFI_STATE_WAIT_HANDSHAKE:
3427 /*
3428 * Set the CLR bit in inbound doorbell
3429 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003430 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003431 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3432 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003433 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303434 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003435 writel(
3436 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003437 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303438 else
Yang, Bo87911122009-10-06 14:31:54 -06003439 writel(
3440 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3441 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003442
Yang, Bo7218df62009-10-06 14:52:20 -06003443 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003444 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3445 break;
3446
Sumant Patroe3bbff92006-10-03 12:28:49 -07003447 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003448 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003449 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3450 (instance->pdev->device ==
3451 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303452 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003453 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003454 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303455 else
Yang, Bo87911122009-10-06 14:31:54 -06003456 writel(MFI_INIT_HOTPLUG,
3457 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003458
Yang, Bo7218df62009-10-06 14:52:20 -06003459 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003460 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3461 break;
3462
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003463 case MFI_STATE_OPERATIONAL:
3464 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003465 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003466 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303467 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003468 if ((instance->pdev->device ==
3469 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3470 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003471 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303472 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003473 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003474 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303475
3476 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003477 for (i = 0; i < (10 * 1000); i += 20) {
3478 if (readl(
3479 &instance->
3480 reg_set->
3481 doorbell) & 1)
3482 msleep(20);
3483 else
3484 break;
3485 }
3486 }
Yang, Bo87911122009-10-06 14:31:54 -06003487 } else
3488 writel(MFI_RESET_FLAGS,
3489 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003490
Yang, Bo7218df62009-10-06 14:52:20 -06003491 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003492 cur_state = MFI_STATE_OPERATIONAL;
3493 break;
3494
3495 case MFI_STATE_UNDEFINED:
3496 /*
3497 * This state should not last for more than 2 seconds
3498 */
Yang, Bo7218df62009-10-06 14:52:20 -06003499 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003500 cur_state = MFI_STATE_UNDEFINED;
3501 break;
3502
3503 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003504 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003505 cur_state = MFI_STATE_BB_INIT;
3506 break;
3507
3508 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003509 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003510 cur_state = MFI_STATE_FW_INIT;
3511 break;
3512
3513 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003514 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003515 cur_state = MFI_STATE_FW_INIT_2;
3516 break;
3517
3518 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003519 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003520 cur_state = MFI_STATE_DEVICE_SCAN;
3521 break;
3522
3523 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003524 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003525 cur_state = MFI_STATE_FLUSH_CACHE;
3526 break;
3527
3528 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003529 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003530 fw_state);
3531 return -ENODEV;
3532 }
3533
3534 /*
3535 * The cur_state should not last for more than max_wait secs
3536 */
3537 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003538 curr_abs_state = instance->instancet->
3539 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003540
Yang, Bo7218df62009-10-06 14:52:20 -06003541 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003542 msleep(1);
3543 } else
3544 break;
3545 }
3546
3547 /*
3548 * Return error if fw_state hasn't changed after max_wait
3549 */
Yang, Bo7218df62009-10-06 14:52:20 -06003550 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003551 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003552 "in %d secs\n", fw_state, max_wait);
3553 return -ENODEV;
3554 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003555
3556 abs_state = curr_abs_state;
3557 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003558 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003559 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003560
3561 return 0;
3562}
3563
3564/**
3565 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3566 * @instance: Adapter soft state
3567 */
3568static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3569{
3570 int i;
adam radford9c915a82010-12-21 13:34:31 -08003571 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003572 struct megasas_cmd *cmd;
3573
3574 if (!instance->frame_dma_pool)
3575 return;
3576
3577 /*
3578 * Return all frames to pool
3579 */
3580 for (i = 0; i < max_cmd; i++) {
3581
3582 cmd = instance->cmd_list[i];
3583
3584 if (cmd->frame)
3585 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3586 cmd->frame_phys_addr);
3587
3588 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003589 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003590 cmd->sense_phys_addr);
3591 }
3592
3593 /*
3594 * Now destroy the pool itself
3595 */
3596 pci_pool_destroy(instance->frame_dma_pool);
3597 pci_pool_destroy(instance->sense_dma_pool);
3598
3599 instance->frame_dma_pool = NULL;
3600 instance->sense_dma_pool = NULL;
3601}
3602
3603/**
3604 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3605 * @instance: Adapter soft state
3606 *
3607 * Each command packet has an embedded DMA memory buffer that is used for
3608 * filling MFI frame and the SG list that immediately follows the frame. This
3609 * function creates those DMA memory buffers for each command packet by using
3610 * PCI pool facility.
3611 */
3612static int megasas_create_frame_pool(struct megasas_instance *instance)
3613{
3614 int i;
3615 u32 max_cmd;
3616 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003617 u32 total_sz;
3618 u32 frame_count;
3619 struct megasas_cmd *cmd;
3620
adam radford9c915a82010-12-21 13:34:31 -08003621 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003622
3623 /*
3624 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3625 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3626 */
3627 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3628 sizeof(struct megasas_sge32);
3629
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003630 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003631 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003632
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003633 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303634 * For MFI controllers.
3635 * max_num_sge = 60
3636 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3637 * Total 960 byte (15 MFI frame of 64 byte)
3638 *
3639 * Fusion adapter require only 3 extra frame.
3640 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3641 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3642 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003643 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303644 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003645 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3646 /*
3647 * Use DMA pool facility provided by PCI layer
3648 */
3649 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303650 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003651
3652 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003653 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003654 return -ENOMEM;
3655 }
3656
3657 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3658 instance->pdev, 128, 4, 0);
3659
3660 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003661 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003662
3663 pci_pool_destroy(instance->frame_dma_pool);
3664 instance->frame_dma_pool = NULL;
3665
3666 return -ENOMEM;
3667 }
3668
3669 /*
3670 * Allocate and attach a frame to each of the commands in cmd_list.
3671 * By making cmd->index as the context instead of the &cmd, we can
3672 * always use 32bit context regardless of the architecture
3673 */
3674 for (i = 0; i < max_cmd; i++) {
3675
3676 cmd = instance->cmd_list[i];
3677
3678 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3679 GFP_KERNEL, &cmd->frame_phys_addr);
3680
3681 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3682 GFP_KERNEL, &cmd->sense_phys_addr);
3683
3684 /*
3685 * megasas_teardown_frame_pool() takes care of freeing
3686 * whatever has been allocated
3687 */
3688 if (!cmd->frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003689 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003690 megasas_teardown_frame_pool(instance);
3691 return -ENOMEM;
3692 }
3693
Yang, Bo707e09b2010-10-12 07:20:27 -06003694 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303695 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003696 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303697 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003698 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003699 }
3700
3701 return 0;
3702}
3703
3704/**
3705 * megasas_free_cmds - Free all the cmds in the free cmd pool
3706 * @instance: Adapter soft state
3707 */
adam radford9c915a82010-12-21 13:34:31 -08003708void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003709{
3710 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003711
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003712 /* First free the MFI frame pool */
3713 megasas_teardown_frame_pool(instance);
3714
3715 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003716 for (i = 0; i < instance->max_mfi_cmds; i++)
3717
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003718 kfree(instance->cmd_list[i]);
3719
3720 /* Free the cmd_list buffer itself */
3721 kfree(instance->cmd_list);
3722 instance->cmd_list = NULL;
3723
3724 INIT_LIST_HEAD(&instance->cmd_pool);
3725}
3726
3727/**
3728 * megasas_alloc_cmds - Allocates the command packets
3729 * @instance: Adapter soft state
3730 *
3731 * Each command that is issued to the FW, whether IO commands from the OS or
3732 * internal commands like IOCTLs, are wrapped in local data structure called
3733 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3734 * the FW.
3735 *
3736 * Each frame has a 32-bit field called context (tag). This context is used
3737 * to get back the megasas_cmd from the frame when a frame gets completed in
3738 * the ISR. Typically the address of the megasas_cmd itself would be used as
3739 * the context. But we wanted to keep the differences between 32 and 64 bit
3740 * systems to the mininum. We always use 32 bit integers for the context. In
3741 * this driver, the 32 bit values are the indices into an array cmd_list.
3742 * This array is used only to look up the megasas_cmd given the context. The
3743 * free commands themselves are maintained in a linked list called cmd_pool.
3744 */
adam radford9c915a82010-12-21 13:34:31 -08003745int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003746{
3747 int i;
3748 int j;
3749 u32 max_cmd;
3750 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303751 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003752
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303753 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003754 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003755
3756 /*
3757 * instance->cmd_list is an array of struct megasas_cmd pointers.
3758 * Allocate the dynamic array first and then allocate individual
3759 * commands.
3760 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003761 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003762
3763 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003764 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003765 return -ENOMEM;
3766 }
3767
adam radford9c915a82010-12-21 13:34:31 -08003768 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003769
3770 for (i = 0; i < max_cmd; i++) {
3771 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3772 GFP_KERNEL);
3773
3774 if (!instance->cmd_list[i]) {
3775
3776 for (j = 0; j < i; j++)
3777 kfree(instance->cmd_list[j]);
3778
3779 kfree(instance->cmd_list);
3780 instance->cmd_list = NULL;
3781
3782 return -ENOMEM;
3783 }
3784 }
3785
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003786 for (i = 0; i < max_cmd; i++) {
3787 cmd = instance->cmd_list[i];
3788 memset(cmd, 0, sizeof(struct megasas_cmd));
3789 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003790 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003791 cmd->instance = instance;
3792
3793 list_add_tail(&cmd->list, &instance->cmd_pool);
3794 }
3795
3796 /*
3797 * Create a frame pool and assign one frame to each cmd
3798 */
3799 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003800 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003801 megasas_free_cmds(instance);
3802 }
3803
3804 return 0;
3805}
3806
Yang, Bo81e403c2009-10-06 14:27:54 -06003807/*
3808 * megasas_get_pd_list_info - Returns FW's pd_list structure
3809 * @instance: Adapter soft state
3810 * @pd_list: pd_list structure
3811 *
3812 * Issues an internal command (DCMD) to get the FW's controller PD
3813 * list structure. This information is mainly used to find out SYSTEM
3814 * supported by the FW.
3815 */
3816static int
3817megasas_get_pd_list(struct megasas_instance *instance)
3818{
3819 int ret = 0, pd_index = 0;
3820 struct megasas_cmd *cmd;
3821 struct megasas_dcmd_frame *dcmd;
3822 struct MR_PD_LIST *ci;
3823 struct MR_PD_ADDRESS *pd_addr;
3824 dma_addr_t ci_h = 0;
3825
3826 cmd = megasas_get_cmd(instance);
3827
3828 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003829 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06003830 return -ENOMEM;
3831 }
3832
3833 dcmd = &cmd->frame->dcmd;
3834
3835 ci = pci_alloc_consistent(instance->pdev,
3836 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3837
3838 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003839 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06003840 megasas_return_cmd(instance, cmd);
3841 return -ENOMEM;
3842 }
3843
3844 memset(ci, 0, sizeof(*ci));
3845 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3846
3847 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3848 dcmd->mbox.b[1] = 0;
3849 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303850 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06003851 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303852 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06003853 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003854 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303855 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3856 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3857 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3858 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06003859
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303860 if (instance->ctrl_context && !instance->mask_interrupts)
3861 ret = megasas_issue_blocked_cmd(instance, cmd,
3862 MEGASAS_BLOCKED_CMD_TIMEOUT);
3863 else
3864 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003865
3866 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003867 * the following function will get the instance PD LIST.
3868 */
Yang, Bo81e403c2009-10-06 14:27:54 -06003869
3870 pd_addr = ci->addr;
3871
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003872 if (ret == 0 &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303873 (le32_to_cpu(ci->count) <
Yang, Bo81e403c2009-10-06 14:27:54 -06003874 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3875
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303876 memset(instance->local_pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06003877 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3878
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303879 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Yang, Bo81e403c2009-10-06 14:27:54 -06003880
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303881 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303882 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303883 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Yang, Bo81e403c2009-10-06 14:27:54 -06003884 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303885 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Yang, Bo81e403c2009-10-06 14:27:54 -06003886 MR_PD_STATE_SYSTEM;
3887 pd_addr++;
3888 }
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303889 memcpy(instance->pd_list, instance->local_pd_list,
3890 sizeof(instance->pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06003891 }
3892
3893 pci_free_consistent(instance->pdev,
3894 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3895 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303896
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303897 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003898
3899 return ret;
3900}
3901
Yang, Bobdc6fb82009-12-06 08:30:19 -07003902/*
3903 * megasas_get_ld_list_info - Returns FW's ld_list structure
3904 * @instance: Adapter soft state
3905 * @ld_list: ld_list structure
3906 *
3907 * Issues an internal command (DCMD) to get the FW's controller PD
3908 * list structure. This information is mainly used to find out SYSTEM
3909 * supported by the FW.
3910 */
3911static int
3912megasas_get_ld_list(struct megasas_instance *instance)
3913{
3914 int ret = 0, ld_index = 0, ids = 0;
3915 struct megasas_cmd *cmd;
3916 struct megasas_dcmd_frame *dcmd;
3917 struct MR_LD_LIST *ci;
3918 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303919 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003920
3921 cmd = megasas_get_cmd(instance);
3922
3923 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003924 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07003925 return -ENOMEM;
3926 }
3927
3928 dcmd = &cmd->frame->dcmd;
3929
3930 ci = pci_alloc_consistent(instance->pdev,
3931 sizeof(struct MR_LD_LIST),
3932 &ci_h);
3933
3934 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003935 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07003936 megasas_return_cmd(instance, cmd);
3937 return -ENOMEM;
3938 }
3939
3940 memset(ci, 0, sizeof(*ci));
3941 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3942
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303943 if (instance->supportmax256vd)
3944 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003945 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303946 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003947 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303948 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003949 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303950 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3951 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3952 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3953 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07003954 dcmd->pad_0 = 0;
3955
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303956 if (instance->ctrl_context && !instance->mask_interrupts)
3957 ret = megasas_issue_blocked_cmd(instance, cmd,
3958 MEGASAS_BLOCKED_CMD_TIMEOUT);
3959 else
3960 ret = megasas_issue_polled(instance, cmd);
3961
Yang, Bobdc6fb82009-12-06 08:30:19 -07003962
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303963 ld_count = le32_to_cpu(ci->ldCount);
3964
Yang, Bobdc6fb82009-12-06 08:30:19 -07003965 /* the following function will get the instance PD LIST */
3966
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303967 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3968 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003969
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303970 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003971 if (ci->ldList[ld_index].state != 0) {
3972 ids = ci->ldList[ld_index].ref.targetId;
3973 instance->ld_ids[ids] =
3974 ci->ldList[ld_index].ref.targetId;
3975 }
3976 }
3977 }
3978
3979 pci_free_consistent(instance->pdev,
3980 sizeof(struct MR_LD_LIST),
3981 ci,
3982 ci_h);
3983
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303984 megasas_return_cmd(instance, cmd);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003985 return ret;
3986}
3987
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003988/**
adam radford21c9e162013-09-06 15:27:14 -07003989 * megasas_ld_list_query - Returns FW's ld_list structure
3990 * @instance: Adapter soft state
3991 * @ld_list: ld_list structure
3992 *
3993 * Issues an internal command (DCMD) to get the FW's controller PD
3994 * list structure. This information is mainly used to find out SYSTEM
3995 * supported by the FW.
3996 */
3997static int
3998megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3999{
4000 int ret = 0, ld_index = 0, ids = 0;
4001 struct megasas_cmd *cmd;
4002 struct megasas_dcmd_frame *dcmd;
4003 struct MR_LD_TARGETID_LIST *ci;
4004 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304005 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004006
4007 cmd = megasas_get_cmd(instance);
4008
4009 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004010 dev_warn(&instance->pdev->dev,
4011 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004012 return -ENOMEM;
4013 }
4014
4015 dcmd = &cmd->frame->dcmd;
4016
4017 ci = pci_alloc_consistent(instance->pdev,
4018 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4019
4020 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004021 dev_warn(&instance->pdev->dev,
4022 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004023 megasas_return_cmd(instance, cmd);
4024 return -ENOMEM;
4025 }
4026
4027 memset(ci, 0, sizeof(*ci));
4028 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4029
4030 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304031 if (instance->supportmax256vd)
4032 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004033
4034 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304035 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004036 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304037 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004038 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304039 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4040 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4041 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4042 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004043 dcmd->pad_0 = 0;
4044
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304045 if (instance->ctrl_context && !instance->mask_interrupts)
4046 ret = megasas_issue_blocked_cmd(instance, cmd,
4047 MEGASAS_BLOCKED_CMD_TIMEOUT);
4048 else
4049 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004050
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304051 tgtid_count = le32_to_cpu(ci->count);
4052
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304053 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
adam radford21c9e162013-09-06 15:27:14 -07004054 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304055 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004056 ids = ci->targetId[ld_index];
4057 instance->ld_ids[ids] = ci->targetId[ld_index];
4058 }
4059
4060 }
4061
4062 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4063 ci, ci_h);
4064
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304065 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004066
4067 return ret;
4068}
4069
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304070/*
4071 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4072 * instance : Controller's instance
4073*/
4074static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4075{
4076 struct fusion_context *fusion;
4077 u32 old_map_sz;
4078 u32 new_map_sz;
4079
4080 fusion = instance->ctrl_context;
4081 /* For MFI based controllers return dummy success */
4082 if (!fusion)
4083 return;
4084
4085 instance->supportmax256vd =
4086 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4087 /* Below is additional check to address future FW enhancement */
4088 if (instance->ctrl_info->max_lds > 64)
4089 instance->supportmax256vd = 1;
4090
4091 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4092 * MEGASAS_MAX_DEV_PER_CHANNEL;
4093 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4094 * MEGASAS_MAX_DEV_PER_CHANNEL;
4095 if (instance->supportmax256vd) {
4096 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4097 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4098 } else {
4099 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4100 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4101 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304102
4103 dev_info(&instance->pdev->dev,
4104 "firmware type\t: %s\n",
4105 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4106 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304107
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004108 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304109 (sizeof(struct MR_LD_SPAN_MAP) *
4110 (instance->fw_supported_vd_count - 1));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004111 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4112 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304113 (sizeof(struct MR_LD_SPAN_MAP) *
4114 (instance->drv_supported_vd_count - 1));
4115
4116 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4117
4118
4119 if (instance->supportmax256vd)
4120 fusion->current_map_sz = new_map_sz;
4121 else
4122 fusion->current_map_sz = old_map_sz;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304123}
4124
adam radford21c9e162013-09-06 15:27:14 -07004125/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004126 * megasas_get_controller_info - Returns FW's controller structure
4127 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004128 *
4129 * Issues an internal command (DCMD) to get the FW's controller structure.
4130 * This information is mainly used to find out the maximum IO transfer per
4131 * command supported by the FW.
4132 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304133int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304134megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004135{
4136 int ret = 0;
4137 struct megasas_cmd *cmd;
4138 struct megasas_dcmd_frame *dcmd;
4139 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304140 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004141 dma_addr_t ci_h = 0;
4142
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304143 ctrl_info = instance->ctrl_info;
4144
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004145 cmd = megasas_get_cmd(instance);
4146
4147 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004148 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004149 return -ENOMEM;
4150 }
4151
4152 dcmd = &cmd->frame->dcmd;
4153
4154 ci = pci_alloc_consistent(instance->pdev,
4155 sizeof(struct megasas_ctrl_info), &ci_h);
4156
4157 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004158 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004159 megasas_return_cmd(instance, cmd);
4160 return -ENOMEM;
4161 }
4162
4163 memset(ci, 0, sizeof(*ci));
4164 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4165
4166 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304167 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004168 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304169 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004170 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004171 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304172 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4173 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4174 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4175 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304176 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004177
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304178 if (instance->ctrl_context && !instance->mask_interrupts)
4179 ret = megasas_issue_blocked_cmd(instance, cmd,
4180 MEGASAS_BLOCKED_CMD_TIMEOUT);
4181 else
4182 ret = megasas_issue_polled(instance, cmd);
4183
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304184 if (!ret) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004185 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304186 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4187 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4188 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4189 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304190 instance->use_seqnum_jbod_fp =
4191 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304192 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4193 dev_info(&instance->pdev->dev,
4194 "controller type\t: %s(%dMB)\n",
4195 instance->is_imr ? "iMR" : "MR",
4196 le16_to_cpu(ctrl_info->memory_size));
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304197 instance->disableOnlineCtrlReset =
4198 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4199 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4200 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304201 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004202
4203 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4204 ci, ci_h);
4205
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304206 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004207 return ret;
4208}
4209
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304210/*
4211 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4212 * to firmware
4213 *
4214 * @instance: Adapter soft state
4215 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4216 MR_CRASH_BUF_TURN_OFF = 0
4217 MR_CRASH_BUF_TURN_ON = 1
4218 * @return 0 on success non-zero on failure.
4219 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4220 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4221 * that driver supports crash dump feature. This DCMD will be sent only if
4222 * crash dump feature is supported by the FW.
4223 *
4224 */
4225int megasas_set_crash_dump_params(struct megasas_instance *instance,
4226 u8 crash_buf_state)
4227{
4228 int ret = 0;
4229 struct megasas_cmd *cmd;
4230 struct megasas_dcmd_frame *dcmd;
4231
4232 cmd = megasas_get_cmd(instance);
4233
4234 if (!cmd) {
4235 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4236 return -ENOMEM;
4237 }
4238
4239
4240 dcmd = &cmd->frame->dcmd;
4241
4242 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4243 dcmd->mbox.b[0] = crash_buf_state;
4244 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304245 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304246 dcmd->sge_count = 1;
4247 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4248 dcmd->timeout = 0;
4249 dcmd->pad_0 = 0;
4250 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4251 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4252 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4253 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4254
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304255 if (instance->ctrl_context && !instance->mask_interrupts)
4256 ret = megasas_issue_blocked_cmd(instance, cmd,
4257 MEGASAS_BLOCKED_CMD_TIMEOUT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304258 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304259 ret = megasas_issue_polled(instance, cmd);
4260
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304261 megasas_return_cmd(instance, cmd);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304262 return ret;
4263}
4264
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004265/**
bo yang31ea7082007-11-07 12:09:50 -05004266 * megasas_issue_init_mfi - Initializes the FW
4267 * @instance: Adapter soft state
4268 *
4269 * Issues the INIT MFI cmd
4270 */
4271static int
4272megasas_issue_init_mfi(struct megasas_instance *instance)
4273{
Christoph Hellwig9ab9ed32015-04-23 16:32:54 +05304274 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004275 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004276 struct megasas_init_frame *init_frame;
4277 struct megasas_init_queue_info *initq_info;
4278 dma_addr_t init_frame_h;
4279 dma_addr_t initq_info_h;
4280
4281 /*
4282 * Prepare a init frame. Note the init frame points to queue info
4283 * structure. Each frame has SGL allocated after first 64 bytes. For
4284 * this frame - since we don't need any SGL - we use SGL's space as
4285 * queue info structure
4286 *
4287 * We will not get a NULL command below. We just created the pool.
4288 */
4289 cmd = megasas_get_cmd(instance);
4290
4291 init_frame = (struct megasas_init_frame *)cmd->frame;
4292 initq_info = (struct megasas_init_queue_info *)
4293 ((unsigned long)init_frame + 64);
4294
4295 init_frame_h = cmd->frame_phys_addr;
4296 initq_info_h = init_frame_h + 64;
4297
4298 context = init_frame->context;
4299 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4300 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4301 init_frame->context = context;
4302
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304303 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4304 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004305
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304306 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4307 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004308
4309 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304310 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304311 init_frame->queue_info_new_phys_addr_lo =
4312 cpu_to_le32(lower_32_bits(initq_info_h));
4313 init_frame->queue_info_new_phys_addr_hi =
4314 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004315
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304316 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004317
4318 /*
4319 * disable the intr before firing the init frame to FW
4320 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304321 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004322
4323 /*
4324 * Issue the init frame in polled mode
4325 */
4326
4327 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004328 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004329 megasas_return_cmd(instance, cmd);
4330 goto fail_fw_init;
4331 }
4332
4333 megasas_return_cmd(instance, cmd);
4334
4335 return 0;
4336
4337fail_fw_init:
4338 return -EINVAL;
4339}
4340
adam radfordcd50ba82010-12-21 10:23:23 -08004341static u32
4342megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004343{
adam radfordcd50ba82010-12-21 10:23:23 -08004344 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004345 u32 context_sz;
4346 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004347
4348 reg_set = instance->reg_set;
4349
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004350 /*
4351 * Get various operational parameters from status register
4352 */
Sumant Patro1341c932006-01-25 12:02:40 -08004353 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004354 /*
4355 * Reduce the max supported cmds by 1. This is to ensure that the
4356 * reply_q_sz (1 more than the max cmd that driver may send)
4357 * does not exceed max cmds that the FW can support
4358 */
4359 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004360 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004361 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004362 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004363 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304364 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4365 * are reserved for IOCTL + driver's internal DCMDs.
4366 */
4367 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4368 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4369 instance->max_scsi_cmds = (instance->max_fw_cmds -
4370 MEGASAS_SKINNY_INT_CMDS);
4371 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4372 } else {
4373 instance->max_scsi_cmds = (instance->max_fw_cmds -
4374 MEGASAS_INT_CMDS);
4375 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4376 }
4377
4378 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004379 * Create a pool of commands
4380 */
4381 if (megasas_alloc_cmds(instance))
4382 goto fail_alloc_cmds;
4383
4384 /*
4385 * Allocate memory for reply queue. Length of reply queue should
4386 * be _one_ more than the maximum commands handled by the firmware.
4387 *
4388 * Note: When FW completes commands, it places corresponding contex
4389 * values in this circular reply queue. This circular queue is a fairly
4390 * typical producer-consumer queue. FW is the producer (of completed
4391 * commands) and the driver is the consumer.
4392 */
4393 context_sz = sizeof(u32);
4394 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4395
4396 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4397 reply_q_sz,
4398 &instance->reply_queue_h);
4399
4400 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004401 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004402 goto fail_reply_queue;
4403 }
4404
bo yang31ea7082007-11-07 12:09:50 -05004405 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004406 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004407
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304408 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304409 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4410 "Fail from %s %d\n", instance->unique_id,
4411 __func__, __LINE__);
4412 goto fail_fw_init;
4413 }
4414
bo yang39a98552010-09-22 22:36:29 -04004415 instance->fw_support_ieee = 0;
4416 instance->fw_support_ieee =
4417 (instance->instancet->read_fw_status_reg(reg_set) &
4418 0x04000000);
4419
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004420 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004421 instance->fw_support_ieee);
4422
4423 if (instance->fw_support_ieee)
4424 instance->flag_ieee = 1;
4425
adam radfordcd50ba82010-12-21 10:23:23 -08004426 return 0;
4427
4428fail_fw_init:
4429
4430 pci_free_consistent(instance->pdev, reply_q_sz,
4431 instance->reply_queue, instance->reply_queue_h);
4432fail_reply_queue:
4433 megasas_free_cmds(instance);
4434
4435fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004436 return 1;
4437}
4438
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304439/*
4440 * megasas_setup_irqs_msix - register legacy interrupts.
4441 * @instance: Adapter soft state
4442 *
4443 * Do not enable interrupt, only setup ISRs.
4444 *
4445 * Return 0 on success.
4446 */
4447static int
4448megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4449{
4450 struct pci_dev *pdev;
4451
4452 pdev = instance->pdev;
4453 instance->irq_context[0].instance = instance;
4454 instance->irq_context[0].MSIxIndex = 0;
4455 if (request_irq(pdev->irq, instance->instancet->service_isr,
4456 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4457 dev_err(&instance->pdev->dev,
4458 "Failed to register IRQ from %s %d\n",
4459 __func__, __LINE__);
4460 return -1;
4461 }
4462 return 0;
4463}
4464
4465/**
4466 * megasas_setup_irqs_msix - register MSI-x interrupts.
4467 * @instance: Adapter soft state
4468 * @is_probe: Driver probe check
4469 *
4470 * Do not enable interrupt, only setup ISRs.
4471 *
4472 * Return 0 on success.
4473 */
4474static int
4475megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4476{
4477 int i, j, cpu;
4478 struct pci_dev *pdev;
4479
4480 pdev = instance->pdev;
4481
4482 /* Try MSI-x */
4483 cpu = cpumask_first(cpu_online_mask);
4484 for (i = 0; i < instance->msix_vectors; i++) {
4485 instance->irq_context[i].instance = instance;
4486 instance->irq_context[i].MSIxIndex = i;
4487 if (request_irq(instance->msixentry[i].vector,
4488 instance->instancet->service_isr, 0, "megasas",
4489 &instance->irq_context[i])) {
4490 dev_err(&instance->pdev->dev,
4491 "Failed to register IRQ for vector %d.\n", i);
4492 for (j = 0; j < i; j++) {
4493 if (smp_affinity_enable)
4494 irq_set_affinity_hint(
4495 instance->msixentry[j].vector, NULL);
4496 free_irq(instance->msixentry[j].vector,
4497 &instance->irq_context[j]);
4498 }
4499 /* Retry irq register for IO_APIC*/
4500 instance->msix_vectors = 0;
4501 if (is_probe)
4502 return megasas_setup_irqs_ioapic(instance);
4503 else
4504 return -1;
4505 }
4506 if (smp_affinity_enable) {
4507 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4508 get_cpu_mask(cpu)))
4509 dev_err(&instance->pdev->dev,
4510 "Failed to set affinity hint"
4511 " for cpu %d\n", cpu);
4512 cpu = cpumask_next(cpu, cpu_online_mask);
4513 }
4514 }
4515 return 0;
4516}
4517
4518/*
4519 * megasas_destroy_irqs- unregister interrupts.
4520 * @instance: Adapter soft state
4521 * return: void
4522 */
4523static void
4524megasas_destroy_irqs(struct megasas_instance *instance) {
4525
4526 int i;
4527
4528 if (instance->msix_vectors)
4529 for (i = 0; i < instance->msix_vectors; i++) {
4530 if (smp_affinity_enable)
4531 irq_set_affinity_hint(
4532 instance->msixentry[i].vector, NULL);
4533 free_irq(instance->msixentry[i].vector,
4534 &instance->irq_context[i]);
4535 }
4536 else
4537 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4538}
4539
adam radfordcd50ba82010-12-21 10:23:23 -08004540/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304541 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4542 * @instance: Adapter soft state
4543 * @is_probe: Driver probe check
4544 *
4545 * Return 0 on success.
4546 */
4547void
4548megasas_setup_jbod_map(struct megasas_instance *instance)
4549{
4550 int i;
4551 struct fusion_context *fusion = instance->ctrl_context;
4552 u32 pd_seq_map_sz;
4553
4554 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4555 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4556
4557 if (reset_devices || !fusion ||
4558 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4559 dev_info(&instance->pdev->dev,
4560 "Jbod map is not supported %s %d\n",
4561 __func__, __LINE__);
4562 instance->use_seqnum_jbod_fp = false;
4563 return;
4564 }
4565
4566 if (fusion->pd_seq_sync[0])
4567 goto skip_alloc;
4568
4569 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4570 fusion->pd_seq_sync[i] = dma_alloc_coherent
4571 (&instance->pdev->dev, pd_seq_map_sz,
4572 &fusion->pd_seq_phys[i], GFP_KERNEL);
4573 if (!fusion->pd_seq_sync[i]) {
4574 dev_err(&instance->pdev->dev,
4575 "Failed to allocate memory from %s %d\n",
4576 __func__, __LINE__);
4577 if (i == 1) {
4578 dma_free_coherent(&instance->pdev->dev,
4579 pd_seq_map_sz, fusion->pd_seq_sync[0],
4580 fusion->pd_seq_phys[0]);
4581 fusion->pd_seq_sync[0] = NULL;
4582 }
4583 instance->use_seqnum_jbod_fp = false;
4584 return;
4585 }
4586 }
4587
4588skip_alloc:
4589 if (!megasas_sync_pd_seq_num(instance, false) &&
4590 !megasas_sync_pd_seq_num(instance, true))
4591 instance->use_seqnum_jbod_fp = true;
4592 else
4593 instance->use_seqnum_jbod_fp = false;
4594}
4595
4596/**
adam radfordcd50ba82010-12-21 10:23:23 -08004597 * megasas_init_fw - Initializes the FW
4598 * @instance: Adapter soft state
4599 *
4600 * This is the main function for initializing firmware
4601 */
4602
4603static int megasas_init_fw(struct megasas_instance *instance)
4604{
4605 u32 max_sectors_1;
4606 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304607 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04004608 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08004609 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304610 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08004611 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304612 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07004613 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05304614 struct fusion_context *fusion;
4615
4616 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08004617
4618 /* Find first memory bar */
4619 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4620 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
adam radfordcd50ba82010-12-21 10:23:23 -08004621 if (pci_request_selected_regions(instance->pdev, instance->bar,
4622 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004623 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08004624 return -EBUSY;
4625 }
4626
Ben Collins11f8a7b2013-09-13 12:46:44 -04004627 base_addr = pci_resource_start(instance->pdev, instance->bar);
4628 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08004629
4630 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004631 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08004632 goto fail_ioremap;
4633 }
4634
4635 reg_set = instance->reg_set;
4636
4637 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08004638 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07004639 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07004640 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304641 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05304642 case PCI_DEVICE_ID_LSI_INTRUDER:
4643 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05304644 case PCI_DEVICE_ID_LSI_CUTLASS_52:
4645 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08004646 instance->instancet = &megasas_instance_template_fusion;
4647 break;
adam radfordcd50ba82010-12-21 10:23:23 -08004648 case PCI_DEVICE_ID_LSI_SAS1078R:
4649 case PCI_DEVICE_ID_LSI_SAS1078DE:
4650 instance->instancet = &megasas_instance_template_ppc;
4651 break;
4652 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4653 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4654 instance->instancet = &megasas_instance_template_gen2;
4655 break;
4656 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4657 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4658 instance->instancet = &megasas_instance_template_skinny;
4659 break;
4660 case PCI_DEVICE_ID_LSI_SAS1064R:
4661 case PCI_DEVICE_ID_DELL_PERC5:
4662 default:
4663 instance->instancet = &megasas_instance_template_xscale;
4664 break;
4665 }
4666
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05304667 if (megasas_transition_to_ready(instance, 0)) {
4668 atomic_set(&instance->fw_reset_no_pci_access, 1);
4669 instance->instancet->adp_reset
4670 (instance, instance->reg_set);
4671 atomic_set(&instance->fw_reset_no_pci_access, 0);
4672 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004673 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05304674 __func__);
4675
4676 /*waitting for about 30 second before retry*/
4677 ssleep(30);
4678
4679 if (megasas_transition_to_ready(instance, 0))
4680 goto fail_ready_state;
4681 }
adam radfordcd50ba82010-12-21 10:23:23 -08004682
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304683 /*
4684 * MSI-X host index 0 is common for all adapter.
4685 * It is used for all MPT based Adapters.
4686 */
4687 instance->reply_post_host_index_addr[0] =
Christoph Hellwig8a232bb2015-04-23 16:32:39 +05304688 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304689 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4690
adam radford3f1abce2011-05-11 18:33:47 -07004691 /* Check if MSI-X is supported while in ready state */
4692 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4693 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07004694 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304695 scratch_pad_2 = readl
4696 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07004697 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05304698 if (fusion) {
4699 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
4700 instance->msix_vectors = (scratch_pad_2
4701 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4702 fw_msix_count = instance->msix_vectors;
4703 } else { /* Invader series supports more than 8 MSI-x vectors*/
4704 instance->msix_vectors = ((scratch_pad_2
4705 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4706 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4707 fw_msix_count = instance->msix_vectors;
4708 /* Save 1-15 reply post index address to local memory
4709 * Index 0 is already saved from reg offset
4710 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4711 */
4712 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4713 instance->reply_post_host_index_addr[loop] =
4714 (u32 __iomem *)
4715 ((u8 __iomem *)instance->reg_set +
4716 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4717 + (loop * 0x10));
4718 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304719 }
4720 if (msix_vectors)
4721 instance->msix_vectors = min(msix_vectors,
4722 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05304723 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07004724 instance->msix_vectors = 1;
4725 /* Don't bother allocating more MSI-X vectors than cpus */
4726 instance->msix_vectors = min(instance->msix_vectors,
4727 (unsigned int)num_online_cpus());
4728 for (i = 0; i < instance->msix_vectors; i++)
4729 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004730 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4731 1, instance->msix_vectors);
Jiang Liuc12de882014-11-03 20:44:20 +08004732 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004733 instance->msix_vectors = i;
4734 else
adam radfordc8e858f2011-10-08 18:15:13 -07004735 instance->msix_vectors = 0;
4736 }
adam radford3f1abce2011-05-11 18:33:47 -07004737
Tomas Henzl258c3af2015-06-02 16:09:46 +05304738 dev_info(&instance->pdev->dev,
4739 "firmware supports msix\t: (%d)", fw_msix_count);
4740 dev_info(&instance->pdev->dev,
4741 "current msix/online cpus\t: (%d/%d)\n",
4742 instance->msix_vectors, (unsigned int)num_online_cpus());
4743
4744 if (instance->msix_vectors ?
4745 megasas_setup_irqs_msix(instance, 1) :
4746 megasas_setup_irqs_ioapic(instance))
4747 goto fail_setup_irqs;
4748
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304749 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4750 GFP_KERNEL);
4751 if (instance->ctrl_info == NULL)
4752 goto fail_init_adapter;
4753
4754 /*
4755 * Below are default value for legacy Firmware.
4756 * non-fusion based controllers
4757 */
4758 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4759 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08004760 /* Get operational params, sge flags, send init cmd to controller */
4761 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08004762 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08004763
Tomas Henzl258c3af2015-06-02 16:09:46 +05304764 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4765 (unsigned long)instance);
4766
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304767 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08004768
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004769 dev_err(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08004770
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304771 megasas_setup_jbod_map(instance);
4772
bo yang39a98552010-09-22 22:36:29 -04004773 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004774 * the following function will get the PD LIST.
4775 */
4776 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06004777 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01004778 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004779 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304780 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01004781 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004782
Yang, Bobdc6fb82009-12-06 08:30:19 -07004783 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07004784 if (megasas_ld_list_query(instance,
4785 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4786 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004787
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004788 /*
4789 * Compute the max allowed sectors per IO: The controller info has two
4790 * limits on max sectors. Driver should use the minimum of these two.
4791 *
4792 * 1 << stripe_sz_ops.min = max sectors per strip
4793 *
4794 * Note that older firmwares ( < FW ver 30) didn't report information
4795 * to calculate max_sectors_1. So the number ended up as zero always.
4796 */
bo yang14faea92007-11-09 04:14:00 -05004797 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304798 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004799
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304800 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4801 le16_to_cpu(ctrl_info->max_strips_per_io);
4802 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004803
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004804 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304805
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304806 instance->mpio = ctrl_info->adapterOperations2.mpio;
4807 instance->UnevenSpanSupport =
4808 ctrl_info->adapterOperations2.supportUnevenSpans;
4809 if (instance->UnevenSpanSupport) {
4810 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304811 if (MR_ValidateMapInfo(instance))
4812 fusion->fast_path_io = 1;
4813 else
4814 fusion->fast_path_io = 0;
4815
4816 }
4817 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05304818 instance->requestorId = ctrl_info->iov.requestorId;
4819 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
4820 if (!ctrl_info->adapterOperations2.activePassive)
4821 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304822
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05304823 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
4824 instance->PlasmaFW111 ? "1.11" : "new");
4825
4826 if (instance->PlasmaFW111) {
4827 iovPtr = (struct IOV_111 *)
4828 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
4829 instance->requestorId = iovPtr->requestorId;
4830 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304831 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05304832 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
4833 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304834 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05304835
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304836 instance->crash_dump_fw_support =
4837 ctrl_info->adapterOperations3.supportCrashDump;
4838 instance->crash_dump_drv_support =
4839 (instance->crash_dump_fw_support &&
4840 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304841 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304842 megasas_set_crash_dump_params(instance,
4843 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07004844
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304845 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304846 if (instance->crash_dump_buf)
4847 pci_free_consistent(instance->pdev,
4848 CRASH_DMA_BUF_SIZE,
4849 instance->crash_dump_buf,
4850 instance->crash_dump_h);
4851 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05004852 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05304853
4854 instance->secure_jbod_support =
4855 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304856
4857 dev_info(&instance->pdev->dev,
4858 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4859 le16_to_cpu(ctrl_info->pci.vendor_id),
4860 le16_to_cpu(ctrl_info->pci.device_id),
4861 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4862 le16_to_cpu(ctrl_info->pci.sub_device_id));
4863 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
4864 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304865 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
4866 instance->crash_dump_drv_support ? "yes" : "no");
4867 dev_info(&instance->pdev->dev, "secure jbod : %s\n",
4868 instance->secure_jbod_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304869 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
4870 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304871
4872
bo yang14faea92007-11-09 04:14:00 -05004873 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05304874 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05004875 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4876 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004877
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05304878 /* Check for valid throttlequeuedepth module parameter */
4879 if (throttlequeuedepth &&
4880 throttlequeuedepth <= instance->max_scsi_cmds)
4881 instance->throttlequeuedepth = throttlequeuedepth;
4882 else
4883 instance->throttlequeuedepth =
4884 MEGASAS_THROTTLE_QUEUE_DEPTH;
4885
bo yangad84db22007-11-09 04:40:16 -05004886
adam radford229fe472014-03-10 02:51:56 -07004887 /* Launch SR-IOV heartbeat timer */
4888 if (instance->requestorId) {
4889 if (!megasas_sriov_start_heartbeat(instance, 1))
4890 megasas_start_timer(instance,
4891 &instance->sriov_heartbeat_timer,
4892 megasas_sriov_heartbeat_handler,
4893 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4894 else
4895 instance->skip_heartbeat_timer_del = 1;
4896 }
4897
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004898 return 0;
4899
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304900fail_get_pd_list:
4901 instance->instancet->disable_intr(instance);
adam radfordeb1b1232011-02-24 20:55:56 -08004902fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304903 megasas_destroy_irqs(instance);
4904fail_setup_irqs:
4905 if (instance->msix_vectors)
4906 pci_disable_msix(instance->pdev);
4907 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08004908fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304909 kfree(instance->ctrl_info);
4910 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004911 iounmap(instance->reg_set);
4912
4913 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08004914 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004915
4916 return -EINVAL;
4917}
4918
4919/**
4920 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01004921 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004922 */
4923static void megasas_release_mfi(struct megasas_instance *instance)
4924{
adam radford9c915a82010-12-21 13:34:31 -08004925 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004926
adam radford9c915a82010-12-21 13:34:31 -08004927 if (instance->reply_queue)
4928 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004929 instance->reply_queue, instance->reply_queue_h);
4930
4931 megasas_free_cmds(instance);
4932
4933 iounmap(instance->reg_set);
4934
adam radfordb6d5d882010-12-14 18:56:07 -08004935 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004936}
4937
4938/**
4939 * megasas_get_seq_num - Gets latest event sequence numbers
4940 * @instance: Adapter soft state
4941 * @eli: FW event log sequence numbers information
4942 *
4943 * FW maintains a log of all events in a non-volatile area. Upper layers would
4944 * usually find out the latest sequence number of the events, the seq number at
4945 * the boot etc. They would "read" all the events below the latest seq number
4946 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4947 * number), they would subsribe to AEN (asynchronous event notification) and
4948 * wait for the events to happen.
4949 */
4950static int
4951megasas_get_seq_num(struct megasas_instance *instance,
4952 struct megasas_evt_log_info *eli)
4953{
4954 struct megasas_cmd *cmd;
4955 struct megasas_dcmd_frame *dcmd;
4956 struct megasas_evt_log_info *el_info;
4957 dma_addr_t el_info_h = 0;
4958
4959 cmd = megasas_get_cmd(instance);
4960
4961 if (!cmd) {
4962 return -ENOMEM;
4963 }
4964
4965 dcmd = &cmd->frame->dcmd;
4966 el_info = pci_alloc_consistent(instance->pdev,
4967 sizeof(struct megasas_evt_log_info),
4968 &el_info_h);
4969
4970 if (!el_info) {
4971 megasas_return_cmd(instance, cmd);
4972 return -ENOMEM;
4973 }
4974
4975 memset(el_info, 0, sizeof(*el_info));
4976 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4977
4978 dcmd->cmd = MFI_CMD_DCMD;
4979 dcmd->cmd_status = 0x0;
4980 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304981 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004982 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004983 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304984 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4985 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4986 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4987 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004988
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304989 if (megasas_issue_blocked_cmd(instance, cmd, 30))
4990 dev_err(&instance->pdev->dev, "Command timedout"
4991 "from %s\n", __func__);
4992 else {
4993 /*
4994 * Copy the data back into callers buffer
4995 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05304996 eli->newest_seq_num = el_info->newest_seq_num;
4997 eli->oldest_seq_num = el_info->oldest_seq_num;
4998 eli->clear_seq_num = el_info->clear_seq_num;
4999 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5000 eli->boot_seq_num = el_info->boot_seq_num;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305001 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005002
5003 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5004 el_info, el_info_h);
5005
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305006 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005007
5008 return 0;
5009}
5010
5011/**
5012 * megasas_register_aen - Registers for asynchronous event notification
5013 * @instance: Adapter soft state
5014 * @seq_num: The starting sequence number
5015 * @class_locale: Class of the event
5016 *
5017 * This function subscribes for AEN for events beyond the @seq_num. It requests
5018 * to be notified if and only if the event is of type @class_locale
5019 */
5020static int
5021megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5022 u32 class_locale_word)
5023{
5024 int ret_val;
5025 struct megasas_cmd *cmd;
5026 struct megasas_dcmd_frame *dcmd;
5027 union megasas_evt_class_locale curr_aen;
5028 union megasas_evt_class_locale prev_aen;
5029
5030 /*
5031 * If there an AEN pending already (aen_cmd), check if the
5032 * class_locale of that pending AEN is inclusive of the new
5033 * AEN request we currently have. If it is, then we don't have
5034 * to do anything. In other words, whichever events the current
5035 * AEN request is subscribing to, have already been subscribed
5036 * to.
5037 *
5038 * If the old_cmd is _not_ inclusive, then we have to abort
5039 * that command, form a class_locale that is superset of both
5040 * old and current and re-issue to the FW
5041 */
5042
5043 curr_aen.word = class_locale_word;
5044
5045 if (instance->aen_cmd) {
5046
Christoph Hellwiga9555532015-04-23 16:34:24 +05305047 prev_aen.word =
5048 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005049
5050 /*
5051 * A class whose enum value is smaller is inclusive of all
5052 * higher values. If a PROGRESS (= -1) was previously
5053 * registered, then a new registration requests for higher
5054 * classes need not be sent to FW. They are automatically
5055 * included.
5056 *
5057 * Locale numbers don't have such hierarchy. They are bitmap
5058 * values
5059 */
5060 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305061 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005062 curr_aen.members.locale)) {
5063 /*
5064 * Previously issued event registration includes
5065 * current request. Nothing to do.
5066 */
5067 return 0;
5068 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305069 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005070
5071 if (prev_aen.members.class < curr_aen.members.class)
5072 curr_aen.members.class = prev_aen.members.class;
5073
5074 instance->aen_cmd->abort_aen = 1;
5075 ret_val = megasas_issue_blocked_abort_cmd(instance,
5076 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305077 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005078
5079 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005080 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005081 "previous AEN command\n");
5082 return ret_val;
5083 }
5084 }
5085 }
5086
5087 cmd = megasas_get_cmd(instance);
5088
5089 if (!cmd)
5090 return -ENOMEM;
5091
5092 dcmd = &cmd->frame->dcmd;
5093
5094 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5095
5096 /*
5097 * Prepare DCMD for aen registration
5098 */
5099 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5100
5101 dcmd->cmd = MFI_CMD_DCMD;
5102 dcmd->cmd_status = 0x0;
5103 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305104 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005105 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005106 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305107 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5108 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5109 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005110 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305111 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5112 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5113 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005114
Yang, Bof4c9a132009-10-06 14:43:28 -06005115 if (instance->aen_cmd != NULL) {
5116 megasas_return_cmd(instance, cmd);
5117 return 0;
5118 }
5119
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005120 /*
5121 * Store reference to the cmd used to register for AEN. When an
5122 * application wants us to register for AEN, we have to abort this
5123 * cmd and re-register with a new EVENT LOCALE supplied by that app
5124 */
5125 instance->aen_cmd = cmd;
5126
5127 /*
5128 * Issue the aen registration frame
5129 */
adam radford9c915a82010-12-21 13:34:31 -08005130 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005131
5132 return 0;
5133}
5134
5135/**
5136 * megasas_start_aen - Subscribes to AEN during driver load time
5137 * @instance: Adapter soft state
5138 */
5139static int megasas_start_aen(struct megasas_instance *instance)
5140{
5141 struct megasas_evt_log_info eli;
5142 union megasas_evt_class_locale class_locale;
5143
5144 /*
5145 * Get the latest sequence number from FW
5146 */
5147 memset(&eli, 0, sizeof(eli));
5148
5149 if (megasas_get_seq_num(instance, &eli))
5150 return -1;
5151
5152 /*
5153 * Register AEN with FW for latest sequence number plus 1
5154 */
5155 class_locale.members.reserved = 0;
5156 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5157 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5158
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305159 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305160 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305161 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005162}
5163
5164/**
5165 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5166 * @instance: Adapter soft state
5167 */
5168static int megasas_io_attach(struct megasas_instance *instance)
5169{
5170 struct Scsi_Host *host = instance->host;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005171 u32 error;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005172
5173 /*
5174 * Export parameters required by SCSI mid-layer
5175 */
5176 host->irq = instance->pdev->irq;
5177 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305178 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005179 host->this_id = instance->init_id;
5180 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005181
5182 if (instance->fw_support_ieee)
5183 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5184
Yang, Bo1fd10682010-10-12 07:18:50 -06005185 /*
5186 * Check if the module parameter value for max_sectors can be used
5187 */
5188 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5189 instance->max_sectors_per_req = max_sectors;
5190 else {
5191 if (max_sectors) {
5192 if (((instance->pdev->device ==
5193 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5194 (instance->pdev->device ==
5195 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5196 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5197 instance->max_sectors_per_req = max_sectors;
5198 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005199 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005200 "and <= %d (or < 1MB for GEN2 controller)\n",
5201 instance->max_sectors_per_req);
5202 }
5203 }
5204 }
5205
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005206 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005207 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005208 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5209 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5210 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005211 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005212
adam radford9c915a82010-12-21 13:34:31 -08005213 /* Fusion only supports host reset */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305214 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005215 host->hostt->eh_device_reset_handler = NULL;
5216 host->hostt->eh_bus_reset_handler = NULL;
5217 }
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305218 error = scsi_init_shared_tag_map(host, host->can_queue);
5219 if (error) {
5220 dev_err(&instance->pdev->dev,
5221 "Failed to shared tag from %s %d\n",
5222 __func__, __LINE__);
5223 return -ENODEV;
5224 }
adam radford9c915a82010-12-21 13:34:31 -08005225
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005226 /*
5227 * Notify the mid-layer about the new controller
5228 */
5229 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305230 dev_err(&instance->pdev->dev,
5231 "Failed to add host from %s %d\n",
5232 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005233 return -ENODEV;
5234 }
5235
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005236 return 0;
5237}
5238
bo yang31ea7082007-11-07 12:09:50 -05005239static int
5240megasas_set_dma_mask(struct pci_dev *pdev)
5241{
5242 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005243 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005244 */
5245 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005246 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005247
Yang Hongyang284901a2009-04-06 19:01:15 -07005248 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005249 goto fail_set_dma_mask;
5250 }
5251 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005252 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005253 goto fail_set_dma_mask;
5254 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305255 /*
5256 * Ensure that all data structures are allocated in 32-bit
5257 * memory.
5258 */
5259 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5260 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5261 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5262 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5263 dev_info(&pdev->dev, "set 32bit DMA mask"
5264 "and 32 bit consistent mask\n");
5265 else
5266 goto fail_set_dma_mask;
5267 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305268
bo yang31ea7082007-11-07 12:09:50 -05005269 return 0;
5270
5271fail_set_dma_mask:
5272 return 1;
5273}
5274
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005275/**
5276 * megasas_probe_one - PCI hotplug entry point
5277 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005278 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005279 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005280static int megasas_probe_one(struct pci_dev *pdev,
5281 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005282{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305283 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005284 struct Scsi_Host *host;
5285 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005286 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305287 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005288
5289 /* Reset MSI-X in the kdump kernel */
5290 if (reset_devices) {
5291 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5292 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005293 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005294 &control);
5295 if (control & PCI_MSIX_FLAGS_ENABLE) {
5296 dev_info(&pdev->dev, "resetting MSI-X\n");
5297 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005298 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005299 control &
5300 ~PCI_MSIX_FLAGS_ENABLE);
5301 }
5302 }
5303 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005304
5305 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005306 * PCI prepping: enable device set bus mastering and dma mask
5307 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005308 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005309
5310 if (rval) {
5311 return rval;
5312 }
5313
5314 pci_set_master(pdev);
5315
bo yang31ea7082007-11-07 12:09:50 -05005316 if (megasas_set_dma_mask(pdev))
5317 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005318
5319 host = scsi_host_alloc(&megasas_template,
5320 sizeof(struct megasas_instance));
5321
5322 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005323 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005324 goto fail_alloc_instance;
5325 }
5326
5327 instance = (struct megasas_instance *)host->hostdata;
5328 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005329 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08005330 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005331
adam radford9c915a82010-12-21 13:34:31 -08005332 switch (instance->pdev->device) {
5333 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005334 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005335 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305336 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305337 case PCI_DEVICE_ID_LSI_INTRUDER:
5338 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305339 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5340 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005341 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305342 instance->ctrl_context_pages =
5343 get_order(sizeof(struct fusion_context));
5344 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5345 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005346 if (!instance->ctrl_context) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005347 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005348 "memory for Fusion context info\n");
5349 goto fail_alloc_dma_buf;
5350 }
5351 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305352 memset(fusion, 0,
5353 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305354 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5355 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5356 fusion->adapter_type = THUNDERBOLT_SERIES;
5357 else
5358 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08005359 }
5360 break;
5361 default: /* For all other supported controllers */
5362
5363 instance->producer =
5364 pci_alloc_consistent(pdev, sizeof(u32),
5365 &instance->producer_h);
5366 instance->consumer =
5367 pci_alloc_consistent(pdev, sizeof(u32),
5368 &instance->consumer_h);
5369
5370 if (!instance->producer || !instance->consumer) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005371 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
adam radford9c915a82010-12-21 13:34:31 -08005372 "memory for producer, consumer\n");
5373 goto fail_alloc_dma_buf;
5374 }
5375
5376 *instance->producer = 0;
5377 *instance->consumer = 0;
5378 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005379 }
5380
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305381 instance->system_info_buf = pci_zalloc_consistent(pdev,
5382 sizeof(struct MR_DRV_SYSTEM_INFO),
5383 &instance->system_info_h);
5384
5385 if (!instance->system_info_buf)
5386 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5387
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305388 /* Crash dump feature related initialisation*/
5389 instance->drv_buf_index = 0;
5390 instance->drv_buf_alloc = 0;
5391 instance->crash_dump_fw_support = 0;
5392 instance->crash_dump_app_support = 0;
5393 instance->fw_crash_state = UNAVAILABLE;
5394 spin_lock_init(&instance->crashdump_lock);
5395 instance->crash_dump_buf = NULL;
5396
5397 if (!reset_devices)
5398 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5399 CRASH_DMA_BUF_SIZE,
5400 &instance->crash_dump_h);
5401 if (!instance->crash_dump_buf)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005402 dev_err(&pdev->dev, "Can't allocate Firmware "
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305403 "crash dump DMA buffer\n");
5404
Yang, Boc3518832009-10-06 14:18:02 -06005405 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005406 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005407 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005408 instance->issuepend_done = 1;
5409 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305410 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005411
5412 instance->evt_detail = pci_alloc_consistent(pdev,
5413 sizeof(struct
5414 megasas_evt_detail),
5415 &instance->evt_detail_h);
5416
5417 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005418 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005419 "event detail structure\n");
5420 goto fail_alloc_dma_buf;
5421 }
5422
5423 /*
5424 * Initialize locks and queues
5425 */
5426 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005427 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005428
Sumant Patroe4a082c2006-05-30 12:03:37 -07005429 atomic_set(&instance->fw_outstanding,0);
5430
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005431 init_waitqueue_head(&instance->int_cmd_wait_q);
5432 init_waitqueue_head(&instance->abort_cmd_wait_q);
5433
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305434 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005435 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005436 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005437
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005438 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08005439 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005440
5441 /*
5442 * Initialize PCI related and misc parameters
5443 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005444 instance->host = host;
5445 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5446 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305447 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005448
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305449
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005450 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305451 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005452 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005453
Sumant Patro658dced2006-10-03 13:09:14 -07005454 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005455 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005456 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005457 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005458 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305459 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005460
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305461 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005462 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305463 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5464 } else
adam radford9c915a82010-12-21 13:34:31 -08005465 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005466
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005467 /*
adam radford0a770662011-02-24 20:56:12 -08005468 * Initialize MFI Firmware
5469 */
5470 if (megasas_init_fw(instance))
5471 goto fail_init_mfi;
5472
adam radford229fe472014-03-10 02:51:56 -07005473 if (instance->requestorId) {
5474 if (instance->PlasmaFW111) {
5475 instance->vf_affiliation_111 =
5476 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5477 &instance->vf_affiliation_111_h);
5478 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005479 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005480 "memory for VF affiliation buffer\n");
5481 } else {
5482 instance->vf_affiliation =
5483 pci_alloc_consistent(pdev,
5484 (MAX_LOGICAL_DRIVES + 1) *
5485 sizeof(struct MR_LD_VF_AFFILIATION),
5486 &instance->vf_affiliation_h);
5487 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005488 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005489 "memory for VF affiliation buffer\n");
5490 }
5491 }
5492
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005493 /*
5494 * Store instance in PCI softstate
5495 */
5496 pci_set_drvdata(pdev, instance);
5497
5498 /*
5499 * Add this controller to megasas_mgmt_info structure so that it
5500 * can be exported to management applications
5501 */
5502 megasas_mgmt_info.count++;
5503 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5504 megasas_mgmt_info.max_index++;
5505
5506 /*
adam radford541f90b2011-05-11 18:34:29 -07005507 * Register with SCSI mid-layer
5508 */
5509 if (megasas_io_attach(instance))
5510 goto fail_io_attach;
5511
5512 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305513 /*
5514 * Trigger SCSI to scan our drives
5515 */
5516 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005517
5518 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005519 * Initiate AEN (Asynchronous Event Notification)
5520 */
5521 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005522 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005523 goto fail_start_aen;
5524 }
5525
Adam Radford9ea81f82014-07-09 15:17:57 -07005526 /* Get current SR-IOV LD/VF affiliation */
5527 if (instance->requestorId)
5528 megasas_get_ld_vf_affiliation(instance, 1);
5529
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005530 return 0;
5531
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005532fail_start_aen:
5533fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005534 megasas_mgmt_info.count--;
5535 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5536 megasas_mgmt_info.max_index--;
5537
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305538 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305539 megasas_destroy_irqs(instance);
5540
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305541 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08005542 megasas_release_fusion(instance);
5543 else
5544 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005545 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005546 pci_disable_msix(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305547fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005548fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005549 if (instance->evt_detail)
5550 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5551 instance->evt_detail,
5552 instance->evt_detail_h);
5553
adam radfordeb1b1232011-02-24 20:55:56 -08005554 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005555 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5556 instance->producer_h);
5557 if (instance->consumer)
5558 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5559 instance->consumer_h);
5560 scsi_host_put(host);
5561
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005562fail_alloc_instance:
5563fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005564 pci_disable_device(pdev);
5565
5566 return -ENODEV;
5567}
5568
5569/**
5570 * megasas_flush_cache - Requests FW to flush all its caches
5571 * @instance: Adapter soft state
5572 */
5573static void megasas_flush_cache(struct megasas_instance *instance)
5574{
5575 struct megasas_cmd *cmd;
5576 struct megasas_dcmd_frame *dcmd;
5577
bo yang39a98552010-09-22 22:36:29 -04005578 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5579 return;
5580
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005581 cmd = megasas_get_cmd(instance);
5582
5583 if (!cmd)
5584 return;
5585
5586 dcmd = &cmd->frame->dcmd;
5587
5588 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5589
5590 dcmd->cmd = MFI_CMD_DCMD;
5591 dcmd->cmd_status = 0x0;
5592 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305593 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005594 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005595 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005596 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305597 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005598 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5599
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305600 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5601 dev_err(&instance->pdev->dev, "Command timedout"
5602 " from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005603
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305604 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005605}
5606
5607/**
5608 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5609 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05005610 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005611 */
bo yang31ea7082007-11-07 12:09:50 -05005612static void megasas_shutdown_controller(struct megasas_instance *instance,
5613 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005614{
5615 struct megasas_cmd *cmd;
5616 struct megasas_dcmd_frame *dcmd;
5617
bo yang39a98552010-09-22 22:36:29 -04005618 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5619 return;
5620
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005621 cmd = megasas_get_cmd(instance);
5622
5623 if (!cmd)
5624 return;
5625
5626 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305627 megasas_issue_blocked_abort_cmd(instance,
sumit.saxena@avagotech.come0bd0872015-08-31 17:23:01 +05305628 instance->aen_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
adam radford9c915a82010-12-21 13:34:31 -08005629 if (instance->map_update_cmd)
5630 megasas_issue_blocked_abort_cmd(instance,
sumit.saxena@avagotech.come0bd0872015-08-31 17:23:01 +05305631 instance->map_update_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305632 if (instance->jbod_seq_cmd)
5633 megasas_issue_blocked_abort_cmd(instance,
5634 instance->jbod_seq_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5635
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005636 dcmd = &cmd->frame->dcmd;
5637
5638 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5639
5640 dcmd->cmd = MFI_CMD_DCMD;
5641 dcmd->cmd_status = 0x0;
5642 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305643 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005644 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005645 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005646 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305647 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005648
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305649 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5650 dev_err(&instance->pdev->dev, "Command timedout"
5651 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005652
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305653 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005654}
5655
Jiri Slaby33139b22008-05-01 17:56:02 +02005656#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005657/**
bo yangad84db22007-11-09 04:40:16 -05005658 * megasas_suspend - driver suspend entry point
5659 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05005660 * @state: PCI power state to suspend routine
5661 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005662static int
bo yang31ea7082007-11-07 12:09:50 -05005663megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5664{
5665 struct Scsi_Host *host;
5666 struct megasas_instance *instance;
5667
5668 instance = pci_get_drvdata(pdev);
5669 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06005670 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05005671
adam radford229fe472014-03-10 02:51:56 -07005672 /* Shutdown SR-IOV heartbeat timer */
5673 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5674 del_timer_sync(&instance->sriov_heartbeat_timer);
5675
bo yang31ea7082007-11-07 12:09:50 -05005676 megasas_flush_cache(instance);
5677 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005678
5679 /* cancel the delayed work if this work still in queue */
5680 if (instance->ev != NULL) {
5681 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005682 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005683 instance->ev = NULL;
5684 }
5685
bo yang31ea7082007-11-07 12:09:50 -05005686 tasklet_kill(&instance->isr_tasklet);
5687
5688 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305689 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005690
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305691 megasas_destroy_irqs(instance);
5692
adam radfordc8e858f2011-10-08 18:15:13 -07005693 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005694 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05005695
5696 pci_save_state(pdev);
5697 pci_disable_device(pdev);
5698
5699 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5700
5701 return 0;
5702}
5703
5704/**
5705 * megasas_resume- driver resume entry point
5706 * @pdev: PCI device structure
5707 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005708static int
bo yang31ea7082007-11-07 12:09:50 -05005709megasas_resume(struct pci_dev *pdev)
5710{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305711 int rval;
bo yang31ea7082007-11-07 12:09:50 -05005712 struct Scsi_Host *host;
5713 struct megasas_instance *instance;
5714
5715 instance = pci_get_drvdata(pdev);
5716 host = instance->host;
5717 pci_set_power_state(pdev, PCI_D0);
5718 pci_enable_wake(pdev, PCI_D0, 0);
5719 pci_restore_state(pdev);
5720
5721 /*
5722 * PCI prepping: enable device set bus mastering and dma mask
5723 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005724 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05005725
5726 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005727 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05005728 return rval;
5729 }
5730
5731 pci_set_master(pdev);
5732
5733 if (megasas_set_dma_mask(pdev))
5734 goto fail_set_dma_mask;
5735
5736 /*
5737 * Initialize MFI Firmware
5738 */
5739
bo yang31ea7082007-11-07 12:09:50 -05005740 atomic_set(&instance->fw_outstanding, 0);
5741
5742 /*
5743 * We expect the FW state to be READY
5744 */
adam radford058a8fa2011-10-08 18:14:27 -07005745 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05005746 goto fail_ready_state;
5747
adam radford3f1abce2011-05-11 18:33:47 -07005748 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02005749 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005750 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5751 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02005752 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07005753
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305754 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005755 megasas_reset_reply_desc(instance);
5756 if (megasas_ioc_init_fusion(instance)) {
5757 megasas_free_cmds(instance);
5758 megasas_free_cmds_fusion(instance);
5759 goto fail_init_mfi;
5760 }
5761 if (!megasas_get_map_info(instance))
5762 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305763 } else {
adam radford9c915a82010-12-21 13:34:31 -08005764 *instance->producer = 0;
5765 *instance->consumer = 0;
5766 if (megasas_issue_init_mfi(instance))
5767 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08005768 }
bo yang31ea7082007-11-07 12:09:50 -05005769
adam radford9c915a82010-12-21 13:34:31 -08005770 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5771 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05005772
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305773 if (instance->msix_vectors ?
5774 megasas_setup_irqs_msix(instance, 0) :
5775 megasas_setup_irqs_ioapic(instance))
5776 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05005777
adam radford229fe472014-03-10 02:51:56 -07005778 /* Re-launch SR-IOV heartbeat timer */
5779 if (instance->requestorId) {
5780 if (!megasas_sriov_start_heartbeat(instance, 0))
5781 megasas_start_timer(instance,
5782 &instance->sriov_heartbeat_timer,
5783 megasas_sriov_heartbeat_handler,
5784 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305785 else {
adam radford229fe472014-03-10 02:51:56 -07005786 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305787 goto fail_init_mfi;
5788 }
adam radford229fe472014-03-10 02:51:56 -07005789 }
5790
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305791 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305792 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06005793 instance->unload = 0;
5794
adam radford541f90b2011-05-11 18:34:29 -07005795 /*
5796 * Initiate AEN (Asynchronous Event Notification)
5797 */
5798 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005799 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07005800
bo yang31ea7082007-11-07 12:09:50 -05005801 return 0;
5802
bo yang31ea7082007-11-07 12:09:50 -05005803fail_init_mfi:
5804 if (instance->evt_detail)
5805 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5806 instance->evt_detail,
5807 instance->evt_detail_h);
5808
5809 if (instance->producer)
5810 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5811 instance->producer_h);
5812 if (instance->consumer)
5813 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5814 instance->consumer_h);
5815 scsi_host_put(host);
5816
5817fail_set_dma_mask:
5818fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02005819fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05005820
5821 pci_disable_device(pdev);
5822
5823 return -ENODEV;
5824}
Jiri Slaby33139b22008-05-01 17:56:02 +02005825#else
5826#define megasas_suspend NULL
5827#define megasas_resume NULL
5828#endif
bo yang31ea7082007-11-07 12:09:50 -05005829
5830/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005831 * megasas_detach_one - PCI hot"un"plug entry point
5832 * @pdev: PCI device structure
5833 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005834static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005835{
5836 int i;
5837 struct Scsi_Host *host;
5838 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08005839 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305840 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005841
5842 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06005843 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005844 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08005845 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005846
adam radford229fe472014-03-10 02:51:56 -07005847 /* Shutdown SR-IOV heartbeat timer */
5848 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5849 del_timer_sync(&instance->sriov_heartbeat_timer);
5850
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305851 if (instance->fw_crash_state != UNAVAILABLE)
5852 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005853 scsi_remove_host(instance->host);
5854 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05005855 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005856
5857 /* cancel the delayed work if this work still in queue*/
5858 if (instance->ev != NULL) {
5859 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005860 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005861 instance->ev = NULL;
5862 }
5863
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305864 /* cancel all wait events */
5865 wake_up_all(&instance->int_cmd_wait_q);
5866
Sumant Patro5d018ad2006-10-03 13:13:18 -07005867 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005868
5869 /*
5870 * Take the instance off the instance array. Note that we will not
5871 * decrement the max_index. We let this array be sparse array
5872 */
5873 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5874 if (megasas_mgmt_info.instance[i] == instance) {
5875 megasas_mgmt_info.count--;
5876 megasas_mgmt_info.instance[i] = NULL;
5877
5878 break;
5879 }
5880 }
5881
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305882 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005883
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305884 megasas_destroy_irqs(instance);
5885
adam radfordc8e858f2011-10-08 18:15:13 -07005886 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005887 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005888
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305889 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005890 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305891 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5892 (sizeof(struct MR_PD_CFG_SEQ) *
5893 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305894 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08005895 if (fusion->ld_map[i])
5896 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305897 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08005898 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305899 fusion->ld_map_phys[i]);
5900 if (fusion->ld_drv_map[i])
5901 free_pages((ulong)fusion->ld_drv_map[i],
5902 fusion->drv_map_pages);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305903 if (fusion->pd_seq_sync)
5904 dma_free_coherent(&instance->pdev->dev,
5905 pd_seq_map_sz,
5906 fusion->pd_seq_sync[i],
5907 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305908 }
5909 free_pages((ulong)instance->ctrl_context,
5910 instance->ctrl_context_pages);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305911 } else {
adam radford9c915a82010-12-21 13:34:31 -08005912 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08005913 pci_free_consistent(pdev, sizeof(u32),
5914 instance->producer,
5915 instance->producer_h);
5916 pci_free_consistent(pdev, sizeof(u32),
5917 instance->consumer,
5918 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08005919 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005920
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305921 kfree(instance->ctrl_info);
5922
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05305923 if (instance->evt_detail)
5924 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5925 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07005926
5927 if (instance->vf_affiliation)
5928 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5929 sizeof(struct MR_LD_VF_AFFILIATION),
5930 instance->vf_affiliation,
5931 instance->vf_affiliation_h);
5932
5933 if (instance->vf_affiliation_111)
5934 pci_free_consistent(pdev,
5935 sizeof(struct MR_LD_VF_AFFILIATION_111),
5936 instance->vf_affiliation_111,
5937 instance->vf_affiliation_111_h);
5938
5939 if (instance->hb_host_mem)
5940 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5941 instance->hb_host_mem,
5942 instance->hb_host_mem_h);
5943
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305944 if (instance->crash_dump_buf)
5945 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5946 instance->crash_dump_buf, instance->crash_dump_h);
5947
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305948 if (instance->system_info_buf)
5949 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
5950 instance->system_info_buf, instance->system_info_h);
5951
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005952 scsi_host_put(host);
5953
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005954 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005955}
5956
5957/**
5958 * megasas_shutdown - Shutdown entry point
5959 * @device: Generic device structure
5960 */
5961static void megasas_shutdown(struct pci_dev *pdev)
5962{
5963 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07005964
Yang, Bo0c79e682009-10-06 14:47:35 -06005965 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005966 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07005967 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305968 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305969 megasas_destroy_irqs(instance);
5970
adam radfordc8e858f2011-10-08 18:15:13 -07005971 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07005972 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005973}
5974
5975/**
5976 * megasas_mgmt_open - char node "open" entry point
5977 */
5978static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5979{
5980 /*
5981 * Allow only those users with admin rights
5982 */
5983 if (!capable(CAP_SYS_ADMIN))
5984 return -EACCES;
5985
5986 return 0;
5987}
5988
5989/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005990 * megasas_mgmt_fasync - Async notifier registration from applications
5991 *
5992 * This function adds the calling process to a driver global queue. When an
5993 * event occurs, SIGIO will be sent to all processes in this queue.
5994 */
5995static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5996{
5997 int rc;
5998
Arjan van de Ven0b950672006-01-11 13:16:10 +01005999 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006000
6001 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6002
Arjan van de Ven0b950672006-01-11 13:16:10 +01006003 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006004
6005 if (rc >= 0) {
6006 /* For sanity check when we get ioctl */
6007 filep->private_data = filep;
6008 return 0;
6009 }
6010
6011 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6012
6013 return rc;
6014}
6015
6016/**
Yang, Boc3518832009-10-06 14:18:02 -06006017 * megasas_mgmt_poll - char node "poll" entry point
6018 * */
6019static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6020{
6021 unsigned int mask;
6022 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006023
Yang, Boc3518832009-10-06 14:18:02 -06006024 poll_wait(file, &megasas_poll_wait, wait);
6025 spin_lock_irqsave(&poll_aen_lock, flags);
6026 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006027 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006028 else
6029 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306030 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006031 spin_unlock_irqrestore(&poll_aen_lock, flags);
6032 return mask;
6033}
6034
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306035/*
6036 * megasas_set_crash_dump_params_ioctl:
6037 * Send CRASH_DUMP_MODE DCMD to all controllers
6038 * @cmd: MFI command frame
6039 */
6040
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006041static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306042{
6043 struct megasas_instance *local_instance;
6044 int i, error = 0;
6045 int crash_support;
6046
6047 crash_support = cmd->frame->dcmd.mbox.w[0];
6048
6049 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6050 local_instance = megasas_mgmt_info.instance[i];
6051 if (local_instance && local_instance->crash_dump_drv_support) {
6052 if ((local_instance->adprecovery ==
6053 MEGASAS_HBA_OPERATIONAL) &&
6054 !megasas_set_crash_dump_params(local_instance,
6055 crash_support)) {
6056 local_instance->crash_dump_app_support =
6057 crash_support;
6058 dev_info(&local_instance->pdev->dev,
6059 "Application firmware crash "
6060 "dump mode set success\n");
6061 error = 0;
6062 } else {
6063 dev_info(&local_instance->pdev->dev,
6064 "Application firmware crash "
6065 "dump mode set failed\n");
6066 error = -1;
6067 }
6068 }
6069 }
6070 return error;
6071}
6072
Yang, Boc3518832009-10-06 14:18:02 -06006073/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006074 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6075 * @instance: Adapter soft state
6076 * @argp: User's ioctl packet
6077 */
6078static int
6079megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6080 struct megasas_iocpacket __user * user_ioc,
6081 struct megasas_iocpacket *ioc)
6082{
6083 struct megasas_sge32 *kern_sge32;
6084 struct megasas_cmd *cmd;
6085 void *kbuff_arr[MAX_IOCTL_SGE];
6086 dma_addr_t buf_handle = 0;
6087 int error = 0, i;
6088 void *sense = NULL;
6089 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006090 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006091
6092 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6093
6094 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006095 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006096 ioc->sge_count, MAX_IOCTL_SGE);
6097 return -EINVAL;
6098 }
6099
6100 cmd = megasas_get_cmd(instance);
6101 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006102 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006103 return -ENOMEM;
6104 }
6105
6106 /*
6107 * User's IOCTL packet has 2 frames (maximum). Copy those two
6108 * frames into our cmd's frames. cmd->frame's context will get
6109 * overwritten when we copy from user's frames. So set that value
6110 * alone separately
6111 */
6112 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306113 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006114 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306115 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6116 MFI_FRAME_SGL64 |
6117 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006118
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306119 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6120 error = megasas_set_crash_dump_params_ioctl(cmd);
6121 megasas_return_cmd(instance, cmd);
6122 return error;
6123 }
6124
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006125 /*
6126 * The management interface between applications and the fw uses
6127 * MFI frames. E.g, RAID configuration changes, LD property changes
6128 * etc are accomplishes through different kinds of MFI frames. The
6129 * driver needs to care only about substituting user buffers with
6130 * kernel buffers in SGLs. The location of SGL is embedded in the
6131 * struct iocpacket itself.
6132 */
6133 kern_sge32 = (struct megasas_sge32 *)
6134 ((unsigned long)cmd->frame + ioc->sgl_off);
6135
6136 /*
6137 * For each user buffer, create a mirror buffer and copy in
6138 */
6139 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006140 if (!ioc->sgl[i].iov_len)
6141 continue;
6142
Sumant Patro9f35fa82007-02-14 12:55:45 -08006143 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006144 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006145 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006146 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006147 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6148 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006149 error = -ENOMEM;
6150 goto out;
6151 }
6152
6153 /*
6154 * We don't change the dma_coherent_mask, so
6155 * pci_alloc_consistent only returns 32bit addresses
6156 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306157 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6158 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006159
6160 /*
6161 * We created a kernel buffer corresponding to the
6162 * user buffer. Now copy in from the user buffer
6163 */
6164 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6165 (u32) (ioc->sgl[i].iov_len))) {
6166 error = -EFAULT;
6167 goto out;
6168 }
6169 }
6170
6171 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006172 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6173 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006174 if (!sense) {
6175 error = -ENOMEM;
6176 goto out;
6177 }
6178
6179 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006180 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306181 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006182 }
6183
6184 /*
6185 * Set the sync_cmd flag so that the ISR knows not to complete this
6186 * cmd to the SCSI mid-layer
6187 */
6188 cmd->sync_cmd = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306189 megasas_issue_blocked_cmd(instance, cmd, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006190 cmd->sync_cmd = 0;
6191
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306192 if (instance->unload == 1) {
6193 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6194 "don't submit data to application\n");
6195 goto out;
6196 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006197 /*
6198 * copy out the kernel buffers to user buffers
6199 */
6200 for (i = 0; i < ioc->sge_count; i++) {
6201 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6202 ioc->sgl[i].iov_len)) {
6203 error = -EFAULT;
6204 goto out;
6205 }
6206 }
6207
6208 /*
6209 * copy out the sense
6210 */
6211 if (ioc->sense_len) {
6212 /*
bo yangb70a41e2008-03-18 03:13:06 -04006213 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006214 * sense buffer address
6215 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006216 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6217 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006218
bo yangb70a41e2008-03-18 03:13:06 -04006219 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6220 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006221 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05006222 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006223 error = -EFAULT;
6224 goto out;
6225 }
6226 }
6227
6228 /*
6229 * copy the status codes returned by the fw
6230 */
6231 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6232 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006233 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006234 error = -EFAULT;
6235 }
6236
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006237out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006238 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006239 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006240 sense, sense_handle);
6241 }
6242
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006243 for (i = 0; i < ioc->sge_count; i++) {
6244 if (kbuff_arr[i])
6245 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306246 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006247 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306248 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306249 kbuff_arr[i] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006250 }
6251
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306252 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006253 return error;
6254}
6255
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006256static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6257{
6258 struct megasas_iocpacket __user *user_ioc =
6259 (struct megasas_iocpacket __user *)arg;
6260 struct megasas_iocpacket *ioc;
6261 struct megasas_instance *instance;
6262 int error;
bo yang39a98552010-09-22 22:36:29 -04006263 int i;
6264 unsigned long flags;
6265 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006266
6267 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6268 if (!ioc)
6269 return -ENOMEM;
6270
6271 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6272 error = -EFAULT;
6273 goto out_kfree_ioc;
6274 }
6275
6276 instance = megasas_lookup_instance(ioc->host_no);
6277 if (!instance) {
6278 error = -ENODEV;
6279 goto out_kfree_ioc;
6280 }
6281
adam radford229fe472014-03-10 02:51:56 -07006282 /* Adjust ioctl wait time for VF mode */
6283 if (instance->requestorId)
6284 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6285
6286 /* Block ioctls in VF mode */
6287 if (instance->requestorId && !allow_vf_ioctls) {
6288 error = -ENODEV;
6289 goto out_kfree_ioc;
6290 }
6291
bo yang39a98552010-09-22 22:36:29 -04006292 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006293 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006294 error = -ENODEV;
6295 goto out_kfree_ioc;
6296 }
6297
6298 if (instance->unload == 1) {
6299 error = -ENODEV;
6300 goto out_kfree_ioc;
6301 }
6302
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006303 if (down_interruptible(&instance->ioctl_sem)) {
6304 error = -ERESTARTSYS;
6305 goto out_kfree_ioc;
6306 }
bo yang39a98552010-09-22 22:36:29 -04006307
6308 for (i = 0; i < wait_time; i++) {
6309
6310 spin_lock_irqsave(&instance->hba_lock, flags);
6311 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6312 spin_unlock_irqrestore(&instance->hba_lock, flags);
6313 break;
6314 }
6315 spin_unlock_irqrestore(&instance->hba_lock, flags);
6316
6317 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006318 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04006319 "for controller reset to finish\n");
6320 }
6321
6322 msleep(1000);
6323 }
6324
6325 spin_lock_irqsave(&instance->hba_lock, flags);
6326 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6327 spin_unlock_irqrestore(&instance->hba_lock, flags);
6328
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006329 dev_err(&instance->pdev->dev, "timed out while"
bo yang39a98552010-09-22 22:36:29 -04006330 "waiting for HBA to recover\n");
6331 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006332 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006333 }
6334 spin_unlock_irqrestore(&instance->hba_lock, flags);
6335
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006336 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006337out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006338 up(&instance->ioctl_sem);
6339
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006340out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006341 kfree(ioc);
6342 return error;
6343}
6344
6345static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6346{
6347 struct megasas_instance *instance;
6348 struct megasas_aen aen;
6349 int error;
bo yang39a98552010-09-22 22:36:29 -04006350 int i;
6351 unsigned long flags;
6352 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006353
6354 if (file->private_data != file) {
6355 printk(KERN_DEBUG "megasas: fasync_helper was not "
6356 "called first\n");
6357 return -EINVAL;
6358 }
6359
6360 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6361 return -EFAULT;
6362
6363 instance = megasas_lookup_instance(aen.host_no);
6364
6365 if (!instance)
6366 return -ENODEV;
6367
bo yang39a98552010-09-22 22:36:29 -04006368 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6369 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006370 }
6371
6372 if (instance->unload == 1) {
6373 return -ENODEV;
6374 }
6375
bo yang39a98552010-09-22 22:36:29 -04006376 for (i = 0; i < wait_time; i++) {
6377
6378 spin_lock_irqsave(&instance->hba_lock, flags);
6379 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6380 spin_unlock_irqrestore(&instance->hba_lock,
6381 flags);
6382 break;
6383 }
6384
6385 spin_unlock_irqrestore(&instance->hba_lock, flags);
6386
6387 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006388 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04006389 "controller reset to finish\n");
6390 }
6391
6392 msleep(1000);
6393 }
6394
6395 spin_lock_irqsave(&instance->hba_lock, flags);
6396 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6397 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006398 dev_err(&instance->pdev->dev, "timed out while waiting"
6399 "for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04006400 return -ENODEV;
6401 }
6402 spin_unlock_irqrestore(&instance->hba_lock, flags);
6403
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006404 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006405 error = megasas_register_aen(instance, aen.seq_num,
6406 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006407 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006408 return error;
6409}
6410
6411/**
6412 * megasas_mgmt_ioctl - char node ioctl entry point
6413 */
6414static long
6415megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6416{
6417 switch (cmd) {
6418 case MEGASAS_IOC_FIRMWARE:
6419 return megasas_mgmt_ioctl_fw(file, arg);
6420
6421 case MEGASAS_IOC_GET_AEN:
6422 return megasas_mgmt_ioctl_aen(file, arg);
6423 }
6424
6425 return -ENOTTY;
6426}
6427
6428#ifdef CONFIG_COMPAT
6429static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6430{
6431 struct compat_megasas_iocpacket __user *cioc =
6432 (struct compat_megasas_iocpacket __user *)arg;
6433 struct megasas_iocpacket __user *ioc =
6434 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6435 int i;
6436 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006437 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006438
Jeff Garzik83aabc12006-10-04 06:34:03 -04006439 if (clear_user(ioc, sizeof(*ioc)))
6440 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006441
6442 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6443 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6444 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6445 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6446 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6447 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6448 return -EFAULT;
6449
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006450 /*
6451 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6452 * sense_len is not null, so prepare the 64bit value under
6453 * the same condition.
6454 */
6455 if (ioc->sense_len) {
6456 void __user **sense_ioc_ptr =
6457 (void __user **)(ioc->frame.raw + ioc->sense_off);
6458 compat_uptr_t *sense_cioc_ptr =
6459 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6460 if (get_user(ptr, sense_cioc_ptr) ||
6461 put_user(compat_ptr(ptr), sense_ioc_ptr))
6462 return -EFAULT;
6463 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006464
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006465 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006466 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6467 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6468 copy_in_user(&ioc->sgl[i].iov_len,
6469 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6470 return -EFAULT;
6471 }
6472
6473 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6474
6475 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6476 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6477 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6478 return -EFAULT;
6479 }
6480 return error;
6481}
6482
6483static long
6484megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6485 unsigned long arg)
6486{
6487 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006488 case MEGASAS_IOC_FIRMWARE32:
6489 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006490 case MEGASAS_IOC_GET_AEN:
6491 return megasas_mgmt_ioctl_aen(file, arg);
6492 }
6493
6494 return -ENOTTY;
6495}
6496#endif
6497
6498/*
6499 * File operations structure for management interface
6500 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006501static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006502 .owner = THIS_MODULE,
6503 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006504 .fasync = megasas_mgmt_fasync,
6505 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006506 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006507#ifdef CONFIG_COMPAT
6508 .compat_ioctl = megasas_mgmt_compat_ioctl,
6509#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006510 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006511};
6512
6513/*
6514 * PCI hotplug support registration structure
6515 */
6516static struct pci_driver megasas_pci_driver = {
6517
6518 .name = "megaraid_sas",
6519 .id_table = megasas_pci_table,
6520 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006521 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006522 .suspend = megasas_suspend,
6523 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006524 .shutdown = megasas_shutdown,
6525};
6526
6527/*
6528 * Sysfs driver attributes
6529 */
6530static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6531{
6532 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6533 MEGASAS_VERSION);
6534}
6535
6536static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6537
6538static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306539megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6540{
6541 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6542 MEGASAS_RELDATE);
6543}
6544
6545static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6546
6547static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06006548megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6549{
6550 return sprintf(buf, "%u\n", support_poll_for_event);
6551}
6552
6553static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6554 megasas_sysfs_show_support_poll_for_event, NULL);
6555
Yang, Bo837f5fe2010-10-11 06:59:20 -06006556 static ssize_t
6557megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6558{
6559 return sprintf(buf, "%u\n", support_device_change);
6560}
6561
6562static DRIVER_ATTR(support_device_change, S_IRUGO,
6563 megasas_sysfs_show_support_device_change, NULL);
6564
Yang, Bo72c4fd32009-10-06 14:20:59 -06006565static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07006566megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6567{
bo yangad84db22007-11-09 04:40:16 -05006568 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07006569}
6570
6571static ssize_t
6572megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6573{
6574 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006575
6576 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07006577 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6578 retval = -EINVAL;
6579 }
6580 return retval;
6581}
6582
Joe Malicki66dca9b2008-08-14 17:14:48 -04006583static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05006584 megasas_sysfs_set_dbg_lvl);
6585
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006586static void
6587megasas_aen_polling(struct work_struct *work)
6588{
6589 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006590 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006591 struct megasas_instance *instance = ev->instance;
6592 union megasas_evt_class_locale class_locale;
6593 struct Scsi_Host *host;
6594 struct scsi_device *sdev1;
6595 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006596 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006597 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07006598 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006599 int error;
6600
6601 if (!instance) {
6602 printk(KERN_ERR "invalid instance!\n");
6603 kfree(ev);
6604 return;
6605 }
adam radford229fe472014-03-10 02:51:56 -07006606
6607 /* Adjust event workqueue thread wait time for VF mode */
6608 if (instance->requestorId)
6609 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6610
6611 /* Don't run the event workqueue thread if OCR is running */
6612 for (i = 0; i < wait_time; i++) {
6613 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6614 break;
6615 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006616 dev_notice(&instance->pdev->dev, "%s waiting for "
adam radford229fe472014-03-10 02:51:56 -07006617 "controller reset to finish for scsi%d\n",
6618 __func__, instance->host->host_no);
6619 }
6620 msleep(1000);
6621 }
6622
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006623 instance->ev = NULL;
6624 host = instance->host;
6625 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05306626 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006627
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306628 switch (le32_to_cpu(instance->evt_detail->code)) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006629 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07006630 if (megasas_get_pd_list(instance) == 0) {
6631 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6632 for (j = 0;
6633 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6634 j++) {
6635
6636 pd_index =
6637 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6638
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006639 sdev1 = scsi_device_lookup(host, i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07006640
6641 if (instance->pd_list[pd_index].driveState
6642 == MR_PD_STATE_SYSTEM) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006643 if (!sdev1)
Yang, Boc9786842009-12-06 08:39:25 -07006644 scsi_add_device(host, i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07006645
6646 if (sdev1)
6647 scsi_device_put(sdev1);
6648 }
6649 }
6650 }
6651 }
6652 doscan = 0;
6653 break;
6654
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006655 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07006656 if (megasas_get_pd_list(instance) == 0) {
Yang, Boc9786842009-12-06 08:39:25 -07006657 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6658 for (j = 0;
6659 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6660 j++) {
6661
6662 pd_index =
6663 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6664
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006665 sdev1 = scsi_device_lookup(host, i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07006666
6667 if (instance->pd_list[pd_index].driveState
6668 == MR_PD_STATE_SYSTEM) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006669 if (sdev1)
Yang, Boc9786842009-12-06 08:39:25 -07006670 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006671 } else {
6672 if (sdev1) {
6673 scsi_remove_device(sdev1);
6674 scsi_device_put(sdev1);
6675 }
6676 }
6677 }
6678 }
6679 }
6680 doscan = 0;
6681 break;
6682
6683 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08006684 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07006685 case MR_EVT_LD_DELETED:
adam radford229fe472014-03-10 02:51:56 -07006686 if (!instance->requestorId ||
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306687 megasas_get_ld_vf_affiliation(instance, 0)) {
adam radford229fe472014-03-10 02:51:56 -07006688 if (megasas_ld_list_query(instance,
6689 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6690 megasas_get_ld_list(instance);
6691 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6692 for (j = 0;
6693 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6694 j++) {
Yang, Boc9786842009-12-06 08:39:25 -07006695
adam radford229fe472014-03-10 02:51:56 -07006696 ld_index =
6697 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006698
adam radford229fe472014-03-10 02:51:56 -07006699 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07006700
adam radford229fe472014-03-10 02:51:56 -07006701 if (instance->ld_ids[ld_index]
6702 != 0xff) {
6703 if (sdev1)
6704 scsi_device_put(sdev1);
6705 } else {
6706 if (sdev1) {
6707 scsi_remove_device(sdev1);
6708 scsi_device_put(sdev1);
6709 }
Yang, Boc9786842009-12-06 08:39:25 -07006710 }
6711 }
adam radford229fe472014-03-10 02:51:56 -07006712 }
6713 doscan = 0;
6714 }
6715 break;
6716 case MR_EVT_LD_CREATED:
6717 if (!instance->requestorId ||
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306718 megasas_get_ld_vf_affiliation(instance, 0)) {
adam radford229fe472014-03-10 02:51:56 -07006719 if (megasas_ld_list_query(instance,
6720 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6721 megasas_get_ld_list(instance);
6722 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6723 for (j = 0;
6724 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6725 j++) {
6726 ld_index =
6727 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6728
6729 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6730
6731 if (instance->ld_ids[ld_index]
6732 != 0xff) {
6733 if (!sdev1)
6734 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6735 }
6736 if (sdev1)
6737 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006738 }
6739 }
adam radford229fe472014-03-10 02:51:56 -07006740 doscan = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006741 }
Yang, Boc9786842009-12-06 08:39:25 -07006742 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006743 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07006744 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08006745 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006746 doscan = 1;
6747 break;
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05306748 case MR_EVT_CTRL_PROP_CHANGED:
6749 megasas_get_ctrl_info(instance);
6750 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006751 default:
6752 doscan = 0;
6753 break;
6754 }
6755 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006756 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006757 kfree(ev);
6758 return;
6759 }
6760
6761 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006762 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
adam radford229fe472014-03-10 02:51:56 -07006763 instance->host->host_no);
Hannes Reinecke58968fc2014-01-16 11:25:36 +01006764 if (megasas_get_pd_list(instance) == 0) {
6765 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6766 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6767 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6768 sdev1 = scsi_device_lookup(host, i, j, 0);
6769 if (instance->pd_list[pd_index].driveState ==
6770 MR_PD_STATE_SYSTEM) {
6771 if (!sdev1) {
6772 scsi_add_device(host, i, j, 0);
6773 }
6774 if (sdev1)
6775 scsi_device_put(sdev1);
6776 } else {
6777 if (sdev1) {
6778 scsi_remove_device(sdev1);
6779 scsi_device_put(sdev1);
6780 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006781 }
6782 }
6783 }
6784 }
Yang, Boc9786842009-12-06 08:39:25 -07006785
adam radford229fe472014-03-10 02:51:56 -07006786 if (!instance->requestorId ||
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306787 megasas_get_ld_vf_affiliation(instance, 0)) {
adam radford229fe472014-03-10 02:51:56 -07006788 if (megasas_ld_list_query(instance,
6789 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6790 megasas_get_ld_list(instance);
6791 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6792 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6793 j++) {
6794 ld_index =
6795 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006796
adam radford229fe472014-03-10 02:51:56 -07006797 sdev1 = scsi_device_lookup(host,
6798 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6799 if (instance->ld_ids[ld_index]
6800 != 0xff) {
6801 if (!sdev1)
6802 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6803 else
6804 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006805 } else {
adam radford229fe472014-03-10 02:51:56 -07006806 if (sdev1) {
6807 scsi_remove_device(sdev1);
6808 scsi_device_put(sdev1);
6809 }
Yang, Boc9786842009-12-06 08:39:25 -07006810 }
6811 }
6812 }
6813 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006814 }
6815
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006816 if (instance->aen_cmd != NULL) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006817 kfree(ev);
6818 return ;
6819 }
6820
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306821 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006822
6823 /* Register AEN with FW for latest sequence number plus 1 */
6824 class_locale.members.reserved = 0;
6825 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6826 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6827 mutex_lock(&instance->aen_mutex);
6828 error = megasas_register_aen(instance, seq_num,
6829 class_locale.word);
6830 mutex_unlock(&instance->aen_mutex);
6831
6832 if (error)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006833 dev_err(&instance->pdev->dev, "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006834
6835 kfree(ev);
6836}
6837
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006838/**
6839 * megasas_init - Driver load entry point
6840 */
6841static int __init megasas_init(void)
6842{
6843 int rval;
6844
6845 /*
6846 * Announce driver version and other information
6847 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05306848 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006849
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07006850 spin_lock_init(&poll_aen_lock);
6851
Yang, Bo72c4fd32009-10-06 14:20:59 -06006852 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06006853 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006854
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006855 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6856
6857 /*
6858 * Register character device node
6859 */
6860 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6861
6862 if (rval < 0) {
6863 printk(KERN_DEBUG "megasas: failed to open device node\n");
6864 return rval;
6865 }
6866
6867 megasas_mgmt_majorno = rval;
6868
6869 /*
6870 * Register ourselves as PCI hotplug module
6871 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00006872 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006873
6874 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09006875 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04006876 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006877 }
6878
Jeff Garzik83aabc12006-10-04 06:34:03 -04006879 rval = driver_create_file(&megasas_pci_driver.driver,
6880 &driver_attr_version);
6881 if (rval)
6882 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006883
6884 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306885 &driver_attr_release_date);
6886 if (rval)
6887 goto err_dcf_rel_date;
6888
6889 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06006890 &driver_attr_support_poll_for_event);
6891 if (rval)
6892 goto err_dcf_support_poll_for_event;
6893
Jeff Garzik83aabc12006-10-04 06:34:03 -04006894 rval = driver_create_file(&megasas_pci_driver.driver,
6895 &driver_attr_dbg_lvl);
6896 if (rval)
6897 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05006898 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006899 &driver_attr_support_device_change);
6900 if (rval)
6901 goto err_dcf_support_device_change;
6902
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006903 return rval;
bo yangad84db22007-11-09 04:40:16 -05006904
Yang, Bo837f5fe2010-10-11 06:59:20 -06006905err_dcf_support_device_change:
6906 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05006907 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006908err_dcf_dbg_lvl:
6909 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06006910 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06006911err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306912 driver_remove_file(&megasas_pci_driver.driver,
6913 &driver_attr_release_date);
6914err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04006915 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6916err_dcf_attr_ver:
6917 pci_unregister_driver(&megasas_pci_driver);
6918err_pcidrv:
6919 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08006920 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006921}
6922
6923/**
6924 * megasas_exit - Driver unload entry point
6925 */
6926static void __exit megasas_exit(void)
6927{
Sumant Patro658dced2006-10-03 13:09:14 -07006928 driver_remove_file(&megasas_pci_driver.driver,
6929 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006930 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006931 &driver_attr_support_poll_for_event);
6932 driver_remove_file(&megasas_pci_driver.driver,
6933 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306934 driver_remove_file(&megasas_pci_driver.driver,
6935 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006936 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006937
6938 pci_unregister_driver(&megasas_pci_driver);
6939 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6940}
6941
6942module_init(megasas_init);
6943module_exit(megasas_exit);