blob: a022c39153cfcf42ae296231e84acd109cd47881 [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 */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200138 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400139};
140
141MODULE_DEVICE_TABLE(pci, megasas_pci_table);
142
143static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700144struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400145static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100146static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400147
Yang, Boc3518832009-10-06 14:18:02 -0600148static int megasas_poll_wait_aen;
149static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600150static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800151u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600152static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700153
Yang, Boc3518832009-10-06 14:18:02 -0600154/* define lock for aen poll */
155spinlock_t poll_aen_lock;
156
adam radford9c915a82010-12-21 13:34:31 -0800157void
bo yang7343eb62007-11-09 04:35:44 -0500158megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
159 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800160static u32
161megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
162static int
163megasas_adp_reset_gen2(struct megasas_instance *instance,
164 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800165static irqreturn_t megasas_isr(int irq, void *devp);
166static u32
167megasas_init_adapter_mfi(struct megasas_instance *instance);
168u32
169megasas_build_and_issue_cmd(struct megasas_instance *instance,
170 struct scsi_cmnd *scmd);
171static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800172void
173megasas_release_fusion(struct megasas_instance *instance);
174int
175megasas_ioc_init_fusion(struct megasas_instance *instance);
176void
177megasas_free_cmds_fusion(struct megasas_instance *instance);
178u8
179megasas_get_map_info(struct megasas_instance *instance);
180int
181megasas_sync_map_info(struct megasas_instance *instance);
182int
adam radford229fe472014-03-10 02:51:56 -0700183wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
184 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800185void megasas_reset_reply_desc(struct megasas_instance *instance);
adam radford229fe472014-03-10 02:51:56 -0700186int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
adam radford9c915a82010-12-21 13:34:31 -0800187void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700188static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
189 int initial);
190int megasas_check_mpio_paths(struct megasas_instance *instance,
191 struct scsi_cmnd *scmd);
adam radfordcd50ba82010-12-21 10:23:23 -0800192
193void
194megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
195{
196 instance->instancet->fire_cmd(instance,
197 cmd->frame_phys_addr, 0, instance->reg_set);
198}
bo yang7343eb62007-11-09 04:35:44 -0500199
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400200/**
201 * megasas_get_cmd - Get a command from the free pool
202 * @instance: Adapter soft state
203 *
204 * Returns a free command from the pool
205 */
adam radford9c915a82010-12-21 13:34:31 -0800206struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400207 *instance)
208{
209 unsigned long flags;
210 struct megasas_cmd *cmd = NULL;
211
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530212 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400213
214 if (!list_empty(&instance->cmd_pool)) {
215 cmd = list_entry((&instance->cmd_pool)->next,
216 struct megasas_cmd, list);
217 list_del_init(&cmd->list);
218 } else {
219 printk(KERN_ERR "megasas: Command pool empty!\n");
220 }
221
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530222 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400223 return cmd;
224}
225
226/**
227 * megasas_return_cmd - Return a cmd to free command pool
228 * @instance: Adapter soft state
229 * @cmd: Command packet to be returned to free command pool
230 */
adam radford9c915a82010-12-21 13:34:31 -0800231inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400232megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
233{
234 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530235 u32 blk_tags;
236 struct megasas_cmd_fusion *cmd_fusion;
237 struct fusion_context *fusion = instance->ctrl_context;
238
239 /* This flag is used only for fusion adapter.
240 * Wait for Interrupt for Polled mode DCMD
241 */
242 if (cmd->flags & DRV_DCMD_POLLED_MODE)
243 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400244
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530245 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400246
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530247 if (fusion) {
248 blk_tags = instance->max_scsi_cmds + cmd->index;
249 cmd_fusion = fusion->cmd_list[blk_tags];
250 megasas_return_cmd_fusion(instance, cmd_fusion);
251 }
252 cmd->scmd = NULL;
253 cmd->frame_count = 0;
254 cmd->flags = 0;
255 if (!fusion && reset_devices)
256 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
257 list_add(&cmd->list, (&instance->cmd_pool)->next);
258
259 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
260
261}
Sumant Patro1341c932006-01-25 12:02:40 -0800262
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400263/**
adam radford0d490162010-12-14 19:17:17 -0800264* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800265* (deviceid : 1064R, PERC5) controllers
266*/
267
268/**
269 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400270 * @regs: MFI register set
271 */
272static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530273megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400274{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530275 struct megasas_register_set __iomem *regs;
276 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400277 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400278
279 /* Dummy readl to force pci flush */
280 readl(&regs->outbound_intr_mask);
281}
282
283/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700284 * megasas_disable_intr_xscale -Disables interrupt
285 * @regs: MFI register set
286 */
287static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530288megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700289{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530290 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700291 u32 mask = 0x1f;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530292 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700293 writel(mask, &regs->outbound_intr_mask);
294 /* Dummy readl to force pci flush */
295 readl(&regs->outbound_intr_mask);
296}
297
298/**
Sumant Patro1341c932006-01-25 12:02:40 -0800299 * megasas_read_fw_status_reg_xscale - returns the current FW status value
300 * @regs: MFI register set
301 */
302static u32
303megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
304{
305 return readl(&(regs)->outbound_msg_0);
306}
307/**
308 * megasas_clear_interrupt_xscale - Check & clear interrupt
309 * @regs: MFI register set
310 */
adam radford0d490162010-12-14 19:17:17 -0800311static int
Sumant Patro1341c932006-01-25 12:02:40 -0800312megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
313{
314 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400315 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800316 /*
317 * Check if it is our interrupt
318 */
319 status = readl(&regs->outbound_intr_status);
320
bo yang39a98552010-09-22 22:36:29 -0400321 if (status & MFI_OB_INTR_STATUS_MASK)
322 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
323 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
324 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800325
326 /*
327 * Clear the interrupt by writing back the same value
328 */
bo yang39a98552010-09-22 22:36:29 -0400329 if (mfiStatus)
330 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800331
Yang, Bo06f579d2008-08-10 12:42:37 -0700332 /* Dummy readl to force pci flush */
333 readl(&regs->outbound_intr_status);
334
bo yang39a98552010-09-22 22:36:29 -0400335 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800336}
337
338/**
339 * megasas_fire_cmd_xscale - Sends command to the FW
340 * @frame_phys_addr : Physical address of cmd
341 * @frame_count : Number of frames for the command
342 * @regs : MFI register set
343 */
adam radford0d490162010-12-14 19:17:17 -0800344static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600345megasas_fire_cmd_xscale(struct megasas_instance *instance,
346 dma_addr_t frame_phys_addr,
347 u32 frame_count,
348 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800349{
bo yang39a98552010-09-22 22:36:29 -0400350 unsigned long flags;
351 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800352 writel((frame_phys_addr >> 3)|(frame_count),
353 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400354 spin_unlock_irqrestore(&instance->hba_lock, flags);
355}
356
357/**
358 * megasas_adp_reset_xscale - For controller reset
359 * @regs: MFI register set
360 */
361static int
362megasas_adp_reset_xscale(struct megasas_instance *instance,
363 struct megasas_register_set __iomem *regs)
364{
365 u32 i;
366 u32 pcidata;
367 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
368
369 for (i = 0; i < 3; i++)
370 msleep(1000); /* sleep for 3 secs */
371 pcidata = 0;
372 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
373 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
374 if (pcidata & 0x2) {
375 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
376 pcidata &= ~0x2;
377 pci_write_config_dword(instance->pdev,
378 MFI_1068_PCSR_OFFSET, pcidata);
379
380 for (i = 0; i < 2; i++)
381 msleep(1000); /* need to wait 2 secs again */
382
383 pcidata = 0;
384 pci_read_config_dword(instance->pdev,
385 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
386 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
387 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
388 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
389 pcidata = 0;
390 pci_write_config_dword(instance->pdev,
391 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
392 }
393 }
394 return 0;
395}
396
397/**
398 * megasas_check_reset_xscale - For controller reset check
399 * @regs: MFI register set
400 */
401static int
402megasas_check_reset_xscale(struct megasas_instance *instance,
403 struct megasas_register_set __iomem *regs)
404{
bo yang39a98552010-09-22 22:36:29 -0400405
406 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530407 (le32_to_cpu(*instance->consumer) ==
408 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400409 return 1;
bo yang39a98552010-09-22 22:36:29 -0400410 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800411}
412
413static struct megasas_instance_template megasas_instance_template_xscale = {
414
415 .fire_cmd = megasas_fire_cmd_xscale,
416 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700417 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800418 .clear_intr = megasas_clear_intr_xscale,
419 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400420 .adp_reset = megasas_adp_reset_xscale,
421 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800422 .service_isr = megasas_isr,
423 .tasklet = megasas_complete_cmd_dpc,
424 .init_adapter = megasas_init_adapter_mfi,
425 .build_and_issue_cmd = megasas_build_and_issue_cmd,
426 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800427};
428
429/**
adam radford0d490162010-12-14 19:17:17 -0800430* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800431* to xscale (deviceid : 1064R, PERC5) controllers
432*/
433
434/**
adam radford0d490162010-12-14 19:17:17 -0800435* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800436* controllers
437*/
438
439/**
440 * megasas_enable_intr_ppc - Enables interrupts
441 * @regs: MFI register set
442 */
443static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530444megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800445{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530446 struct megasas_register_set __iomem *regs;
447 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800448 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800449
bo yang39a98552010-09-22 22:36:29 -0400450 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800451
452 /* Dummy readl to force pci flush */
453 readl(&regs->outbound_intr_mask);
454}
455
456/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700457 * megasas_disable_intr_ppc - Disable interrupt
458 * @regs: MFI register set
459 */
460static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530461megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700462{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530463 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700464 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530465 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700466 writel(mask, &regs->outbound_intr_mask);
467 /* Dummy readl to force pci flush */
468 readl(&regs->outbound_intr_mask);
469}
470
471/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800472 * megasas_read_fw_status_reg_ppc - returns the current FW status value
473 * @regs: MFI register set
474 */
475static u32
476megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
477{
478 return readl(&(regs)->outbound_scratch_pad);
479}
480
481/**
482 * megasas_clear_interrupt_ppc - Check & clear interrupt
483 * @regs: MFI register set
484 */
adam radford0d490162010-12-14 19:17:17 -0800485static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800486megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
487{
adam radford3cc6851f92011-05-11 18:34:52 -0700488 u32 status, mfiStatus = 0;
489
Sumant Patrof9876f02006-02-03 15:34:35 -0800490 /*
491 * Check if it is our interrupt
492 */
493 status = readl(&regs->outbound_intr_status);
494
adam radford3cc6851f92011-05-11 18:34:52 -0700495 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
496 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
497
498 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
499 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800500
501 /*
502 * Clear the interrupt by writing back the same value
503 */
504 writel(status, &regs->outbound_doorbell_clear);
505
Yang, Bo06f579d2008-08-10 12:42:37 -0700506 /* Dummy readl to force pci flush */
507 readl(&regs->outbound_doorbell_clear);
508
adam radford3cc6851f92011-05-11 18:34:52 -0700509 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800510}
adam radford3cc6851f92011-05-11 18:34:52 -0700511
Sumant Patrof9876f02006-02-03 15:34:35 -0800512/**
513 * megasas_fire_cmd_ppc - Sends command to the FW
514 * @frame_phys_addr : Physical address of cmd
515 * @frame_count : Number of frames for the command
516 * @regs : MFI register set
517 */
adam radford0d490162010-12-14 19:17:17 -0800518static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600519megasas_fire_cmd_ppc(struct megasas_instance *instance,
520 dma_addr_t frame_phys_addr,
521 u32 frame_count,
522 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800523{
bo yang39a98552010-09-22 22:36:29 -0400524 unsigned long flags;
525 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800526 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800527 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400528 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800529}
530
bo yang39a98552010-09-22 22:36:29 -0400531/**
bo yang39a98552010-09-22 22:36:29 -0400532 * megasas_check_reset_ppc - For controller reset check
533 * @regs: MFI register set
534 */
535static int
536megasas_check_reset_ppc(struct megasas_instance *instance,
537 struct megasas_register_set __iomem *regs)
538{
adam radford3cc6851f92011-05-11 18:34:52 -0700539 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
540 return 1;
541
bo yang39a98552010-09-22 22:36:29 -0400542 return 0;
543}
adam radford3cc6851f92011-05-11 18:34:52 -0700544
Sumant Patrof9876f02006-02-03 15:34:35 -0800545static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800546
Sumant Patrof9876f02006-02-03 15:34:35 -0800547 .fire_cmd = megasas_fire_cmd_ppc,
548 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700549 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800550 .clear_intr = megasas_clear_intr_ppc,
551 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700552 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400553 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800554 .service_isr = megasas_isr,
555 .tasklet = megasas_complete_cmd_dpc,
556 .init_adapter = megasas_init_adapter_mfi,
557 .build_and_issue_cmd = megasas_build_and_issue_cmd,
558 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800559};
560
561/**
Yang, Bo87911122009-10-06 14:31:54 -0600562 * megasas_enable_intr_skinny - Enables interrupts
563 * @regs: MFI register set
564 */
565static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530566megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600567{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530568 struct megasas_register_set __iomem *regs;
569 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600570 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
571
572 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
573
574 /* Dummy readl to force pci flush */
575 readl(&regs->outbound_intr_mask);
576}
577
578/**
579 * megasas_disable_intr_skinny - Disables interrupt
580 * @regs: MFI register set
581 */
582static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530583megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600584{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530585 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600586 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530587 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600588 writel(mask, &regs->outbound_intr_mask);
589 /* Dummy readl to force pci flush */
590 readl(&regs->outbound_intr_mask);
591}
592
593/**
594 * megasas_read_fw_status_reg_skinny - returns the current FW status value
595 * @regs: MFI register set
596 */
597static u32
598megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
599{
600 return readl(&(regs)->outbound_scratch_pad);
601}
602
603/**
604 * megasas_clear_interrupt_skinny - Check & clear interrupt
605 * @regs: MFI register set
606 */
607static int
608megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
609{
610 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800611 u32 mfiStatus = 0;
612
Yang, Bo87911122009-10-06 14:31:54 -0600613 /*
614 * Check if it is our interrupt
615 */
616 status = readl(&regs->outbound_intr_status);
617
618 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400619 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600620 }
621
622 /*
adam radfordebf054b2011-02-24 20:57:15 -0800623 * Check if it is our interrupt
624 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600625 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800626 MFI_STATE_FAULT) {
627 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
628 } else
629 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
630
631 /*
Yang, Bo87911122009-10-06 14:31:54 -0600632 * Clear the interrupt by writing back the same value
633 */
634 writel(status, &regs->outbound_intr_status);
635
636 /*
637 * dummy read to flush PCI
638 */
639 readl(&regs->outbound_intr_status);
640
adam radfordebf054b2011-02-24 20:57:15 -0800641 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600642}
643
644/**
645 * megasas_fire_cmd_skinny - Sends command to the FW
646 * @frame_phys_addr : Physical address of cmd
647 * @frame_count : Number of frames for the command
648 * @regs : MFI register set
649 */
650static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600651megasas_fire_cmd_skinny(struct megasas_instance *instance,
652 dma_addr_t frame_phys_addr,
653 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600654 struct megasas_register_set __iomem *regs)
655{
Yang, Bo0c79e682009-10-06 14:47:35 -0600656 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400657 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530658 writel(upper_32_bits(frame_phys_addr),
659 &(regs)->inbound_high_queue_port);
660 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
661 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400662 spin_unlock_irqrestore(&instance->hba_lock, flags);
663}
664
665/**
bo yang39a98552010-09-22 22:36:29 -0400666 * megasas_check_reset_skinny - For controller reset check
667 * @regs: MFI register set
668 */
669static int
670megasas_check_reset_skinny(struct megasas_instance *instance,
671 struct megasas_register_set __iomem *regs)
672{
adam radford3cc6851f92011-05-11 18:34:52 -0700673 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
674 return 1;
675
bo yang39a98552010-09-22 22:36:29 -0400676 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600677}
678
679static struct megasas_instance_template megasas_instance_template_skinny = {
680
681 .fire_cmd = megasas_fire_cmd_skinny,
682 .enable_intr = megasas_enable_intr_skinny,
683 .disable_intr = megasas_disable_intr_skinny,
684 .clear_intr = megasas_clear_intr_skinny,
685 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800686 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400687 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800688 .service_isr = megasas_isr,
689 .tasklet = megasas_complete_cmd_dpc,
690 .init_adapter = megasas_init_adapter_mfi,
691 .build_and_issue_cmd = megasas_build_and_issue_cmd,
692 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600693};
694
695
696/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700697* The following functions are defined for gen2 (deviceid : 0x78 0x79)
698* controllers
699*/
700
701/**
702 * megasas_enable_intr_gen2 - Enables interrupts
703 * @regs: MFI register set
704 */
705static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530706megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700707{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530708 struct megasas_register_set __iomem *regs;
709 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700710 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
711
712 /* write ~0x00000005 (4 & 1) to the intr mask*/
713 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
714
715 /* Dummy readl to force pci flush */
716 readl(&regs->outbound_intr_mask);
717}
718
719/**
720 * megasas_disable_intr_gen2 - Disables interrupt
721 * @regs: MFI register set
722 */
723static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530724megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700725{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530726 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700727 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530728 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700729 writel(mask, &regs->outbound_intr_mask);
730 /* Dummy readl to force pci flush */
731 readl(&regs->outbound_intr_mask);
732}
733
734/**
735 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
736 * @regs: MFI register set
737 */
738static u32
739megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
740{
741 return readl(&(regs)->outbound_scratch_pad);
742}
743
744/**
745 * megasas_clear_interrupt_gen2 - Check & clear interrupt
746 * @regs: MFI register set
747 */
748static int
749megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
750{
751 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400752 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700753 /*
754 * Check if it is our interrupt
755 */
756 status = readl(&regs->outbound_intr_status);
757
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530758 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400759 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
760 }
761 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
762 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
763 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700764
765 /*
766 * Clear the interrupt by writing back the same value
767 */
bo yang39a98552010-09-22 22:36:29 -0400768 if (mfiStatus)
769 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700770
771 /* Dummy readl to force pci flush */
772 readl(&regs->outbound_intr_status);
773
bo yang39a98552010-09-22 22:36:29 -0400774 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700775}
776/**
777 * megasas_fire_cmd_gen2 - Sends command to the FW
778 * @frame_phys_addr : Physical address of cmd
779 * @frame_count : Number of frames for the command
780 * @regs : MFI register set
781 */
782static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600783megasas_fire_cmd_gen2(struct megasas_instance *instance,
784 dma_addr_t frame_phys_addr,
785 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700786 struct megasas_register_set __iomem *regs)
787{
bo yang39a98552010-09-22 22:36:29 -0400788 unsigned long flags;
789 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700790 writel((frame_phys_addr | (frame_count<<1))|1,
791 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400792 spin_unlock_irqrestore(&instance->hba_lock, flags);
793}
794
795/**
796 * megasas_adp_reset_gen2 - For controller reset
797 * @regs: MFI register set
798 */
799static int
800megasas_adp_reset_gen2(struct megasas_instance *instance,
801 struct megasas_register_set __iomem *reg_set)
802{
803 u32 retry = 0 ;
804 u32 HostDiag;
Christoph Hellwig8a232bb2015-04-23 16:32:39 +0530805 u32 __iomem *seq_offset = &reg_set->seq_offset;
806 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400807
adam radfordebf054b2011-02-24 20:57:15 -0800808 if (instance->instancet == &megasas_instance_template_skinny) {
809 seq_offset = &reg_set->fusion_seq_offset;
810 hostdiag_offset = &reg_set->fusion_host_diag;
811 }
812
813 writel(0, seq_offset);
814 writel(4, seq_offset);
815 writel(0xb, seq_offset);
816 writel(2, seq_offset);
817 writel(7, seq_offset);
818 writel(0xd, seq_offset);
819
bo yang39a98552010-09-22 22:36:29 -0400820 msleep(1000);
821
adam radfordebf054b2011-02-24 20:57:15 -0800822 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400823
824 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
825 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800826 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400827 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
828 retry, HostDiag);
829
830 if (retry++ >= 100)
831 return 1;
832
833 }
834
835 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
836
adam radfordebf054b2011-02-24 20:57:15 -0800837 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400838
839 ssleep(10);
840
adam radfordebf054b2011-02-24 20:57:15 -0800841 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400842 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
843 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800844 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400845 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
846 retry, HostDiag);
847
848 if (retry++ >= 1000)
849 return 1;
850
851 }
852 return 0;
853}
854
855/**
856 * megasas_check_reset_gen2 - For controller reset check
857 * @regs: MFI register set
858 */
859static int
860megasas_check_reset_gen2(struct megasas_instance *instance,
861 struct megasas_register_set __iomem *regs)
862{
Yang, Bo707e09b2010-10-12 07:20:27 -0600863 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
864 return 1;
865 }
866
bo yang39a98552010-09-22 22:36:29 -0400867 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700868}
869
870static struct megasas_instance_template megasas_instance_template_gen2 = {
871
872 .fire_cmd = megasas_fire_cmd_gen2,
873 .enable_intr = megasas_enable_intr_gen2,
874 .disable_intr = megasas_disable_intr_gen2,
875 .clear_intr = megasas_clear_intr_gen2,
876 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400877 .adp_reset = megasas_adp_reset_gen2,
878 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800879 .service_isr = megasas_isr,
880 .tasklet = megasas_complete_cmd_dpc,
881 .init_adapter = megasas_init_adapter_mfi,
882 .build_and_issue_cmd = megasas_build_and_issue_cmd,
883 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700884};
885
886/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800887* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400888* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800889*/
890
adam radford9c915a82010-12-21 13:34:31 -0800891/*
892 * Template added for TB (Fusion)
893 */
894extern struct megasas_instance_template megasas_instance_template_fusion;
895
Sumant Patrof9876f02006-02-03 15:34:35 -0800896/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400897 * megasas_issue_polled - Issues a polling command
898 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800899 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400900 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530901 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400902 */
adam radford9c915a82010-12-21 13:34:31 -0800903int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400904megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
905{
adam radford229fe472014-03-10 02:51:56 -0700906 int seconds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400907
908 struct megasas_header *frame_hdr = &cmd->frame->hdr;
909
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530910 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
911 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400912
913 /*
914 * Issue the frame using inbound queue port
915 */
adam radford9c915a82010-12-21 13:34:31 -0800916 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400917
918 /*
919 * Wait for cmd_status to change
920 */
adam radford229fe472014-03-10 02:51:56 -0700921 if (instance->requestorId)
922 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
923 else
924 seconds = MFI_POLL_TIMEOUT_SECS;
925 return wait_and_poll(instance, cmd, seconds);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400926}
927
928/**
929 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
930 * @instance: Adapter soft state
931 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530932 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400933 *
934 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700935 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400936 * Used to issue ioctl commands.
937 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530938int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400939megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530940 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400941{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530942 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530943 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400944
adam radford9c915a82010-12-21 13:34:31 -0800945 instance->instancet->issue_dcmd(instance, cmd);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530946 if (timeout) {
947 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530948 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530949 if (!ret)
950 return 1;
951 } else
952 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530953 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400954
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530955 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
956 0 : 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400957}
958
959/**
960 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
961 * @instance: Adapter soft state
962 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530963 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400964 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530965 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400966 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700967 * cmd and waits for return status.
968 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400969 */
970static int
971megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530972 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400973{
974 struct megasas_cmd *cmd;
975 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530976 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400977
978 cmd = megasas_get_cmd(instance);
979
980 if (!cmd)
981 return -1;
982
983 abort_fr = &cmd->frame->abort;
984
985 /*
986 * Prepare and issue the abort frame
987 */
988 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530989 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530990 abort_fr->flags = cpu_to_le16(0);
991 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
992 abort_fr->abort_mfi_phys_addr_lo =
993 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
994 abort_fr->abort_mfi_phys_addr_hi =
995 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996
997 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530998 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400999
adam radford9c915a82010-12-21 13:34:31 -08001000 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001001
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301002 if (timeout) {
1003 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301004 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301005 if (!ret) {
1006 dev_err(&instance->pdev->dev, "Command timedout"
1007 "from %s\n", __func__);
1008 return 1;
1009 }
1010 } else
1011 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301012 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301013
bo yang39a98552010-09-22 22:36:29 -04001014 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001015
1016 megasas_return_cmd(instance, cmd);
1017 return 0;
1018}
1019
1020/**
1021 * megasas_make_sgl32 - Prepares 32-bit SGL
1022 * @instance: Adapter soft state
1023 * @scp: SCSI command from the mid-layer
1024 * @mfi_sgl: SGL to be filled in
1025 *
1026 * If successful, this function returns the number of SG elements. Otherwise,
1027 * it returnes -1.
1028 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001029static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001030megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1031 union megasas_sgl *mfi_sgl)
1032{
1033 int i;
1034 int sge_count;
1035 struct scatterlist *os_sgl;
1036
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001037 sge_count = scsi_dma_map(scp);
1038 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001039
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001040 if (sge_count) {
1041 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301042 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1043 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001044 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001045 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001046 return sge_count;
1047}
1048
1049/**
1050 * megasas_make_sgl64 - Prepares 64-bit SGL
1051 * @instance: Adapter soft state
1052 * @scp: SCSI command from the mid-layer
1053 * @mfi_sgl: SGL to be filled in
1054 *
1055 * If successful, this function returns the number of SG elements. Otherwise,
1056 * it returnes -1.
1057 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001058static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001059megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1060 union megasas_sgl *mfi_sgl)
1061{
1062 int i;
1063 int sge_count;
1064 struct scatterlist *os_sgl;
1065
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001066 sge_count = scsi_dma_map(scp);
1067 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001068
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001069 if (sge_count) {
1070 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301071 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1072 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001073 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001074 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001075 return sge_count;
1076}
1077
Yang, Bof4c9a132009-10-06 14:43:28 -06001078/**
1079 * megasas_make_sgl_skinny - Prepares IEEE SGL
1080 * @instance: Adapter soft state
1081 * @scp: SCSI command from the mid-layer
1082 * @mfi_sgl: SGL to be filled in
1083 *
1084 * If successful, this function returns the number of SG elements. Otherwise,
1085 * it returnes -1.
1086 */
1087static int
1088megasas_make_sgl_skinny(struct megasas_instance *instance,
1089 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1090{
1091 int i;
1092 int sge_count;
1093 struct scatterlist *os_sgl;
1094
1095 sge_count = scsi_dma_map(scp);
1096
1097 if (sge_count) {
1098 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301099 mfi_sgl->sge_skinny[i].length =
1100 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001101 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301102 cpu_to_le64(sg_dma_address(os_sgl));
1103 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001104 }
1105 }
1106 return sge_count;
1107}
1108
Sumant Patrob1df99d2006-10-03 12:40:47 -07001109 /**
1110 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001111 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001112 * @sge_count : number of sg elements
1113 *
1114 * Returns the number of frames required for numnber of sge's (sge_count)
1115 */
1116
Yang, Bof4c9a132009-10-06 14:43:28 -06001117static u32 megasas_get_frame_count(struct megasas_instance *instance,
1118 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001119{
1120 int num_cnt;
1121 int sge_bytes;
1122 u32 sge_sz;
1123 u32 frame_count=0;
1124
1125 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1126 sizeof(struct megasas_sge32);
1127
Yang, Bof4c9a132009-10-06 14:43:28 -06001128 if (instance->flag_ieee) {
1129 sge_sz = sizeof(struct megasas_sge_skinny);
1130 }
1131
Sumant Patrob1df99d2006-10-03 12:40:47 -07001132 /*
bo yangd532dbe2008-03-17 03:36:43 -04001133 * Main frame can contain 2 SGEs for 64-bit SGLs and
1134 * 3 SGEs for 32-bit SGLs for ldio &
1135 * 1 SGEs for 64-bit SGLs and
1136 * 2 SGEs for 32-bit SGLs for pthru frame
1137 */
1138 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001139 if (instance->flag_ieee == 1) {
1140 num_cnt = sge_count - 1;
1141 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001142 num_cnt = sge_count - 1;
1143 else
1144 num_cnt = sge_count - 2;
1145 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001146 if (instance->flag_ieee == 1) {
1147 num_cnt = sge_count - 1;
1148 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001149 num_cnt = sge_count - 2;
1150 else
1151 num_cnt = sge_count - 3;
1152 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001153
1154 if(num_cnt>0){
1155 sge_bytes = sge_sz * num_cnt;
1156
1157 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1158 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1159 }
1160 /* Main frame */
1161 frame_count +=1;
1162
1163 if (frame_count > 7)
1164 frame_count = 8;
1165 return frame_count;
1166}
1167
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001168/**
1169 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1170 * @instance: Adapter soft state
1171 * @scp: SCSI command
1172 * @cmd: Command to be prepared in
1173 *
1174 * This function prepares CDB commands. These are typcially pass-through
1175 * commands to the devices.
1176 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001177static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001178megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1179 struct megasas_cmd *cmd)
1180{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001181 u32 is_logical;
1182 u32 device_id;
1183 u16 flags = 0;
1184 struct megasas_pthru_frame *pthru;
1185
1186 is_logical = MEGASAS_IS_LOGICAL(scp);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301187 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001188 pthru = (struct megasas_pthru_frame *)cmd->frame;
1189
1190 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1191 flags = MFI_FRAME_DIR_WRITE;
1192 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1193 flags = MFI_FRAME_DIR_READ;
1194 else if (scp->sc_data_direction == PCI_DMA_NONE)
1195 flags = MFI_FRAME_DIR_NONE;
1196
Yang, Bof4c9a132009-10-06 14:43:28 -06001197 if (instance->flag_ieee == 1) {
1198 flags |= MFI_FRAME_IEEE;
1199 }
1200
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001201 /*
1202 * Prepare the DCDB frame
1203 */
1204 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1205 pthru->cmd_status = 0x0;
1206 pthru->scsi_status = 0x0;
1207 pthru->target_id = device_id;
1208 pthru->lun = scp->device->lun;
1209 pthru->cdb_len = scp->cmd_len;
1210 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001211 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301212 pthru->flags = cpu_to_le16(flags);
1213 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001214
1215 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1216
1217 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001218 * If the command is for the tape device, set the
1219 * pthru timeout to the os layer timeout value.
1220 */
1221 if (scp->device->type == TYPE_TAPE) {
1222 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301223 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001224 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301225 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001226 }
1227
1228 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001229 * Construct SGL
1230 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001231 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301232 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001233 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1234 &pthru->sgl);
1235 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301236 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001237 pthru->sge_count = megasas_make_sgl64(instance, scp,
1238 &pthru->sgl);
1239 } else
1240 pthru->sge_count = megasas_make_sgl32(instance, scp,
1241 &pthru->sgl);
1242
Yang, Bobdc6fb82009-12-06 08:30:19 -07001243 if (pthru->sge_count > instance->max_num_sge) {
1244 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1245 pthru->sge_count);
1246 return 0;
1247 }
1248
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001249 /*
1250 * Sense info specific
1251 */
1252 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301253 pthru->sense_buf_phys_addr_hi =
1254 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1255 pthru->sense_buf_phys_addr_lo =
1256 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001257
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001258 /*
1259 * Compute the total number of frames this command consumes. FW uses
1260 * this number to pull sufficient number of frames from host memory.
1261 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001262 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001263 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001264
1265 return cmd->frame_count;
1266}
1267
1268/**
1269 * megasas_build_ldio - Prepares IOs to logical devices
1270 * @instance: Adapter soft state
1271 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001272 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001273 *
1274 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1275 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001276static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001277megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1278 struct megasas_cmd *cmd)
1279{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001280 u32 device_id;
1281 u8 sc = scp->cmnd[0];
1282 u16 flags = 0;
1283 struct megasas_io_frame *ldio;
1284
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301285 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001286 ldio = (struct megasas_io_frame *)cmd->frame;
1287
1288 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1289 flags = MFI_FRAME_DIR_WRITE;
1290 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1291 flags = MFI_FRAME_DIR_READ;
1292
Yang, Bof4c9a132009-10-06 14:43:28 -06001293 if (instance->flag_ieee == 1) {
1294 flags |= MFI_FRAME_IEEE;
1295 }
1296
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001297 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001298 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001299 */
1300 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1301 ldio->cmd_status = 0x0;
1302 ldio->scsi_status = 0x0;
1303 ldio->target_id = device_id;
1304 ldio->timeout = 0;
1305 ldio->reserved_0 = 0;
1306 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301307 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001308 ldio->start_lba_hi = 0;
1309 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1310
1311 /*
1312 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1313 */
1314 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301315 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1316 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1317 ((u32) scp->cmnd[2] << 8) |
1318 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001319
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301320 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001321 }
1322
1323 /*
1324 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1325 */
1326 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301327 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1328 ((u32) scp->cmnd[7] << 8));
1329 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1330 ((u32) scp->cmnd[3] << 16) |
1331 ((u32) scp->cmnd[4] << 8) |
1332 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001333 }
1334
1335 /*
1336 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1337 */
1338 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301339 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1340 ((u32) scp->cmnd[7] << 16) |
1341 ((u32) scp->cmnd[8] << 8) |
1342 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001343
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301344 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1345 ((u32) scp->cmnd[3] << 16) |
1346 ((u32) scp->cmnd[4] << 8) |
1347 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001348 }
1349
1350 /*
1351 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1352 */
1353 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301354 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1355 ((u32) scp->cmnd[11] << 16) |
1356 ((u32) scp->cmnd[12] << 8) |
1357 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001358
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301359 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1360 ((u32) scp->cmnd[7] << 16) |
1361 ((u32) scp->cmnd[8] << 8) |
1362 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001363
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301364 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1365 ((u32) scp->cmnd[3] << 16) |
1366 ((u32) scp->cmnd[4] << 8) |
1367 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001368
1369 }
1370
1371 /*
1372 * Construct SGL
1373 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001374 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301375 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001376 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1377 &ldio->sgl);
1378 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301379 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001380 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1381 } else
1382 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1383
Yang, Bobdc6fb82009-12-06 08:30:19 -07001384 if (ldio->sge_count > instance->max_num_sge) {
1385 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1386 ldio->sge_count);
1387 return 0;
1388 }
1389
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001390 /*
1391 * Sense info specific
1392 */
1393 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1394 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301395 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001396
Sumant Patrob1df99d2006-10-03 12:40:47 -07001397 /*
1398 * Compute the total number of frames this command consumes. FW uses
1399 * this number to pull sufficient number of frames from host memory.
1400 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001401 cmd->frame_count = megasas_get_frame_count(instance,
1402 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001403
1404 return cmd->frame_count;
1405}
1406
1407/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301408 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1409 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001410 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001411 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001412 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301413inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001414{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301415 int ret;
1416
Sumant Patrocb59aa62006-01-25 11:53:25 -08001417 switch (cmd->cmnd[0]) {
1418 case READ_10:
1419 case WRITE_10:
1420 case READ_12:
1421 case WRITE_12:
1422 case READ_6:
1423 case WRITE_6:
1424 case READ_16:
1425 case WRITE_16:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301426 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1427 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1428 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001429 default:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301430 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1431 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001432 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301433 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001434}
1435
Sumant Patro658dced2006-10-03 13:09:14 -07001436 /**
1437 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1438 * in FW
1439 * @instance: Adapter soft state
1440 */
1441static inline void
1442megasas_dump_pending_frames(struct megasas_instance *instance)
1443{
1444 struct megasas_cmd *cmd;
1445 int i,n;
1446 union megasas_sgl *mfi_sgl;
1447 struct megasas_io_frame *ldio;
1448 struct megasas_pthru_frame *pthru;
1449 u32 sgcount;
1450 u32 max_cmd = instance->max_fw_cmds;
1451
1452 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1453 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1454 if (IS_DMA64)
1455 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1456 else
1457 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1458
1459 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1460 for (i = 0; i < max_cmd; i++) {
1461 cmd = instance->cmd_list[i];
1462 if(!cmd->scmd)
1463 continue;
1464 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301465 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001466 ldio = (struct megasas_io_frame *)cmd->frame;
1467 mfi_sgl = &ldio->sgl;
1468 sgcount = ldio->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301469 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1470 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1471 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1472 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1473 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001474 }
1475 else {
1476 pthru = (struct megasas_pthru_frame *) cmd->frame;
1477 mfi_sgl = &pthru->sgl;
1478 sgcount = pthru->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301479 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1480 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1481 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1482 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1483 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001484 }
1485 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1486 for (n = 0; n < sgcount; n++){
1487 if (IS_DMA64)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301488 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
1489 le32_to_cpu(mfi_sgl->sge64[n].length),
1490 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001491 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301492 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",
1493 le32_to_cpu(mfi_sgl->sge32[n].length),
1494 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001495 }
1496 }
1497 printk(KERN_ERR "\n");
1498 } /*for max_cmd*/
1499 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1500 for (i = 0; i < max_cmd; i++) {
1501
1502 cmd = instance->cmd_list[i];
1503
1504 if(cmd->sync_cmd == 1){
1505 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1506 }
1507 }
1508 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1509}
1510
adam radfordcd50ba82010-12-21 10:23:23 -08001511u32
1512megasas_build_and_issue_cmd(struct megasas_instance *instance,
1513 struct scsi_cmnd *scmd)
1514{
1515 struct megasas_cmd *cmd;
1516 u32 frame_count;
1517
1518 cmd = megasas_get_cmd(instance);
1519 if (!cmd)
1520 return SCSI_MLQUEUE_HOST_BUSY;
1521
1522 /*
1523 * Logical drive command
1524 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301525 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001526 frame_count = megasas_build_ldio(instance, scmd, cmd);
1527 else
1528 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1529
1530 if (!frame_count)
1531 goto out_return_cmd;
1532
1533 cmd->scmd = scmd;
1534 scmd->SCp.ptr = (char *)cmd;
1535
1536 /*
1537 * Issue the command to the FW
1538 */
1539 atomic_inc(&instance->fw_outstanding);
1540
1541 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1542 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001543
1544 return 0;
1545out_return_cmd:
1546 megasas_return_cmd(instance, cmd);
1547 return 1;
1548}
1549
1550
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001551/**
1552 * megasas_queue_command - Queue entry point
1553 * @scmd: SCSI command to be queued
1554 * @done: Callback entry point
1555 */
1556static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301557megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001558{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001559 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001560 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001561
1562 instance = (struct megasas_instance *)
1563 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001564
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301565 if (instance->unload == 1) {
1566 scmd->result = DID_NO_CONNECT << 16;
1567 scmd->scsi_done(scmd);
1568 return 0;
1569 }
1570
bo yang39a98552010-09-22 22:36:29 -04001571 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001572 return SCSI_MLQUEUE_HOST_BUSY;
1573
bo yang39a98552010-09-22 22:36:29 -04001574 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301575
adam radford229fe472014-03-10 02:51:56 -07001576 /* Check for an mpio path and adjust behavior */
1577 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1578 if (megasas_check_mpio_paths(instance, scmd) ==
1579 (DID_RESET << 16)) {
1580 spin_unlock_irqrestore(&instance->hba_lock, flags);
1581 return SCSI_MLQUEUE_HOST_BUSY;
1582 } else {
1583 spin_unlock_irqrestore(&instance->hba_lock, flags);
1584 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301585 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001586 return 0;
1587 }
1588 }
1589
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301590 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1591 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford229fe472014-03-10 02:51:56 -07001592 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301593 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301594 return 0;
1595 }
1596
bo yang39a98552010-09-22 22:36:29 -04001597 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1598 spin_unlock_irqrestore(&instance->hba_lock, flags);
1599 return SCSI_MLQUEUE_HOST_BUSY;
1600 }
1601
1602 spin_unlock_irqrestore(&instance->hba_lock, flags);
1603
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001604 scmd->result = 0;
1605
Sumant Patrocb59aa62006-01-25 11:53:25 -08001606 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301607 (scmd->device->id >= instance->fw_supported_vd_count ||
1608 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001609 scmd->result = DID_BAD_TARGET << 16;
1610 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001611 }
1612
Sumant Patro02b01e02007-02-14 13:00:55 -08001613 switch (scmd->cmnd[0]) {
1614 case SYNCHRONIZE_CACHE:
1615 /*
1616 * FW takes care of flush cache on its own
1617 * No need to send it down
1618 */
1619 scmd->result = DID_OK << 16;
1620 goto out_done;
1621 default:
1622 break;
1623 }
1624
adam radfordcd50ba82010-12-21 10:23:23 -08001625 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1626 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001627 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001628 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001629
1630 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001631
Sumant Patrocb59aa62006-01-25 11:53:25 -08001632 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301633 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001634 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001635}
1636
Yang, Bo044833b2009-10-06 14:33:06 -06001637static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1638{
1639 int i;
1640
1641 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1642
1643 if ((megasas_mgmt_info.instance[i]) &&
1644 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1645 return megasas_mgmt_info.instance[i];
1646 }
1647
1648 return NULL;
1649}
1650
Christoph Hellwig147aab62006-02-17 12:13:48 +01001651static int megasas_slave_configure(struct scsi_device *sdev)
1652{
Yang, Bo044833b2009-10-06 14:33:06 -06001653 /*
1654 * The RAID firmware may require extended timeouts.
1655 */
1656 blk_queue_rq_timeout(sdev->request_queue,
1657 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301658
Yang, Bo044833b2009-10-06 14:33:06 -06001659 return 0;
1660}
1661
1662static int megasas_slave_alloc(struct scsi_device *sdev)
1663{
1664 u16 pd_index = 0;
1665 struct megasas_instance *instance ;
1666 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301667 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001668 /*
1669 * Open the OS scan to the SYSTEM PD
1670 */
1671 pd_index =
1672 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1673 sdev->id;
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301674 if (instance->pd_list[pd_index].driveState ==
1675 MR_PD_STATE_SYSTEM) {
Yang, Bo044833b2009-10-06 14:33:06 -06001676 return 0;
1677 }
1678 return -ENXIO;
1679 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001680 return 0;
1681}
1682
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301683/*
1684* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1685* kill adapter
1686* @instance: Adapter soft state
1687*
1688*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301689static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301690{
1691 int i;
1692 struct megasas_cmd *cmd_mfi;
1693 struct megasas_cmd_fusion *cmd_fusion;
1694 struct fusion_context *fusion = instance->ctrl_context;
1695
1696 /* Find all outstanding ioctls */
1697 if (fusion) {
1698 for (i = 0; i < instance->max_fw_cmds; i++) {
1699 cmd_fusion = fusion->cmd_list[i];
1700 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1701 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1702 if (cmd_mfi->sync_cmd &&
1703 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1704 megasas_complete_cmd(instance,
1705 cmd_mfi, DID_OK);
1706 }
1707 }
1708 } else {
1709 for (i = 0; i < instance->max_fw_cmds; i++) {
1710 cmd_mfi = instance->cmd_list[i];
1711 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1712 MFI_CMD_ABORT)
1713 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1714 }
1715 }
1716}
1717
1718
adam radford9c915a82010-12-21 13:34:31 -08001719void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001720{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301721 /* Set critical error to block I/O & ioctls in case caller didn't */
1722 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1723 /* Wait 1 second to ensure IO or ioctls in build have posted */
1724 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001725 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301726 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1727 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1728 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
1729 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1730 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1731 writel(MFI_STOP_ADP,
1732 &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001733 /* Flush */
1734 readl(&instance->reg_set->doorbell);
1735 if (instance->mpio && instance->requestorId)
1736 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001737 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301738 writel(MFI_STOP_ADP,
1739 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001740 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301741 /* Complete outstanding ioctls when adapter is killed */
1742 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001743}
1744
1745 /**
1746 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1747 * restored to max value
1748 * @instance: Adapter soft state
1749 *
1750 */
1751void
1752megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1753{
1754 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301755
adam radford9c915a82010-12-21 13:34:31 -08001756 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001757 && time_after(jiffies, instance->last_time + 5 * HZ)
1758 && atomic_read(&instance->fw_outstanding) <
1759 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001760
1761 spin_lock_irqsave(instance->host->host_lock, flags);
1762 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001763
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301764 instance->host->can_queue = instance->max_scsi_cmds;
adam radford9c915a82010-12-21 13:34:31 -08001765 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001766 }
1767}
1768
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001769/**
bo yang7343eb62007-11-09 04:35:44 -05001770 * megasas_complete_cmd_dpc - Returns FW's controller structure
1771 * @instance_addr: Address of adapter soft state
1772 *
1773 * Tasklet to complete cmds
1774 */
1775static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1776{
1777 u32 producer;
1778 u32 consumer;
1779 u32 context;
1780 struct megasas_cmd *cmd;
1781 struct megasas_instance *instance =
1782 (struct megasas_instance *)instance_addr;
1783 unsigned long flags;
1784
1785 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001786 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001787 return;
1788
1789 spin_lock_irqsave(&instance->completion_lock, flags);
1790
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301791 producer = le32_to_cpu(*instance->producer);
1792 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001793
1794 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301795 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001796 if (context >= instance->max_fw_cmds) {
1797 printk(KERN_ERR "Unexpected context value %x\n",
1798 context);
1799 BUG();
1800 }
bo yang7343eb62007-11-09 04:35:44 -05001801
1802 cmd = instance->cmd_list[context];
1803
1804 megasas_complete_cmd(instance, cmd, DID_OK);
1805
1806 consumer++;
1807 if (consumer == (instance->max_fw_cmds + 1)) {
1808 consumer = 0;
1809 }
1810 }
1811
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301812 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05001813
1814 spin_unlock_irqrestore(&instance->completion_lock, flags);
1815
1816 /*
1817 * Check if we can restore can_queue
1818 */
adam radford9c915a82010-12-21 13:34:31 -08001819 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001820}
1821
adam radford229fe472014-03-10 02:51:56 -07001822/**
1823 * megasas_start_timer - Initializes a timer object
1824 * @instance: Adapter soft state
1825 * @timer: timer object to be initialized
1826 * @fn: timer function
1827 * @interval: time interval between timer function call
1828 *
1829 */
1830void megasas_start_timer(struct megasas_instance *instance,
1831 struct timer_list *timer,
1832 void *fn, unsigned long interval)
1833{
1834 init_timer(timer);
1835 timer->expires = jiffies + interval;
1836 timer->data = (unsigned long)instance;
1837 timer->function = fn;
1838 add_timer(timer);
1839}
1840
Yang, Bo707e09b2010-10-12 07:20:27 -06001841static void
1842megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1843
1844static void
1845process_fw_state_change_wq(struct work_struct *work);
1846
1847void megasas_do_ocr(struct megasas_instance *instance)
1848{
1849 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1850 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1851 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301852 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06001853 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301854 instance->instancet->disable_intr(instance);
Yang, Bo707e09b2010-10-12 07:20:27 -06001855 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1856 instance->issuepend_done = 0;
1857
1858 atomic_set(&instance->fw_outstanding, 0);
1859 megasas_internal_reset_defer_cmds(instance);
1860 process_fw_state_change_wq(&instance->work_init);
1861}
1862
Adam Radford4cbfea82014-07-09 15:17:56 -07001863static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1864 int initial)
adam radford229fe472014-03-10 02:51:56 -07001865{
1866 struct megasas_cmd *cmd;
1867 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07001868 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07001869 dma_addr_t new_affiliation_111_h;
1870 int ld, retval = 0;
1871 u8 thisVf;
1872
1873 cmd = megasas_get_cmd(instance);
1874
1875 if (!cmd) {
Adam Radford4cbfea82014-07-09 15:17:56 -07001876 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation_111:"
1877 "Failed to get cmd for scsi%d.\n",
adam radford229fe472014-03-10 02:51:56 -07001878 instance->host->host_no);
1879 return -ENOMEM;
1880 }
1881
1882 dcmd = &cmd->frame->dcmd;
1883
Adam Radford4cbfea82014-07-09 15:17:56 -07001884 if (!instance->vf_affiliation_111) {
adam radford229fe472014-03-10 02:51:56 -07001885 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1886 "affiliation for scsi%d.\n", instance->host->host_no);
1887 megasas_return_cmd(instance, cmd);
1888 return -ENOMEM;
1889 }
1890
1891 if (initial)
adam radford229fe472014-03-10 02:51:56 -07001892 memset(instance->vf_affiliation_111, 0,
1893 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001894 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07001895 new_affiliation_111 =
1896 pci_alloc_consistent(instance->pdev,
1897 sizeof(struct MR_LD_VF_AFFILIATION_111),
1898 &new_affiliation_111_h);
1899 if (!new_affiliation_111) {
adam radford229fe472014-03-10 02:51:56 -07001900 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
1901 "memory for new affiliation for scsi%d.\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07001902 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07001903 megasas_return_cmd(instance, cmd);
1904 return -ENOMEM;
1905 }
Adam Radford4cbfea82014-07-09 15:17:56 -07001906 memset(new_affiliation_111, 0,
1907 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001908 }
1909
1910 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1911
1912 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301913 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07001914 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05301915 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07001916 dcmd->timeout = 0;
1917 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05301918 dcmd->data_xfer_len =
1919 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
1920 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07001921
Adam Radford4cbfea82014-07-09 15:17:56 -07001922 if (initial)
1923 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05301924 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07001925 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05301926 dcmd->sgl.sge32[0].phys_addr =
1927 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07001928
Christoph Hellwig2213a462015-04-23 16:33:54 +05301929 dcmd->sgl.sge32[0].length = cpu_to_le32(
1930 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001931
1932 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
1933 "scsi%d\n", instance->host->host_no);
1934
1935 megasas_issue_blocked_cmd(instance, cmd, 0);
1936
1937 if (dcmd->cmd_status) {
1938 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
1939 " failed with status 0x%x for scsi%d.\n",
1940 dcmd->cmd_status, instance->host->host_no);
1941 retval = 1; /* Do a scan if we couldn't get affiliation */
1942 goto out;
1943 }
1944
1945 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07001946 thisVf = new_affiliation_111->thisVf;
1947 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
1948 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
1949 new_affiliation_111->map[ld].policy[thisVf]) {
1950 printk(KERN_WARNING "megasas: SR-IOV: "
1951 "Got new LD/VF affiliation "
adam radford229fe472014-03-10 02:51:56 -07001952 "for scsi%d.\n",
adam radford229fe472014-03-10 02:51:56 -07001953 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07001954 memcpy(instance->vf_affiliation_111,
1955 new_affiliation_111,
1956 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001957 retval = 1;
1958 goto out;
1959 }
Adam Radford4cbfea82014-07-09 15:17:56 -07001960 }
1961out:
1962 if (new_affiliation_111) {
1963 pci_free_consistent(instance->pdev,
1964 sizeof(struct MR_LD_VF_AFFILIATION_111),
1965 new_affiliation_111,
1966 new_affiliation_111_h);
1967 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301968
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301969 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07001970
1971 return retval;
1972}
1973
1974static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
1975 int initial)
1976{
1977 struct megasas_cmd *cmd;
1978 struct megasas_dcmd_frame *dcmd;
1979 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
1980 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
1981 dma_addr_t new_affiliation_h;
1982 int i, j, retval = 0, found = 0, doscan = 0;
1983 u8 thisVf;
1984
1985 cmd = megasas_get_cmd(instance);
1986
1987 if (!cmd) {
1988 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation12: "
1989 "Failed to get cmd for scsi%d.\n",
1990 instance->host->host_no);
1991 return -ENOMEM;
1992 }
1993
1994 dcmd = &cmd->frame->dcmd;
1995
1996 if (!instance->vf_affiliation) {
1997 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1998 "affiliation for scsi%d.\n", instance->host->host_no);
1999 megasas_return_cmd(instance, cmd);
2000 return -ENOMEM;
2001 }
2002
2003 if (initial)
2004 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2005 sizeof(struct MR_LD_VF_AFFILIATION));
2006 else {
2007 new_affiliation =
2008 pci_alloc_consistent(instance->pdev,
2009 (MAX_LOGICAL_DRIVES + 1) *
2010 sizeof(struct MR_LD_VF_AFFILIATION),
2011 &new_affiliation_h);
2012 if (!new_affiliation) {
2013 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
2014 "memory for new affiliation for scsi%d.\n",
2015 instance->host->host_no);
2016 megasas_return_cmd(instance, cmd);
2017 return -ENOMEM;
2018 }
2019 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2020 sizeof(struct MR_LD_VF_AFFILIATION));
2021 }
2022
2023 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2024
2025 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302026 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002027 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302028 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002029 dcmd->timeout = 0;
2030 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302031 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2032 sizeof(struct MR_LD_VF_AFFILIATION));
2033 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002034
2035 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302036 dcmd->sgl.sge32[0].phys_addr =
2037 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002038 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302039 dcmd->sgl.sge32[0].phys_addr =
2040 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002041
Christoph Hellwig2213a462015-04-23 16:33:54 +05302042 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2043 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002044
2045 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
2046 "scsi%d\n", instance->host->host_no);
2047
2048 megasas_issue_blocked_cmd(instance, cmd, 0);
2049
2050 if (dcmd->cmd_status) {
2051 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
2052 " failed with status 0x%x for scsi%d.\n",
2053 dcmd->cmd_status, instance->host->host_no);
2054 retval = 1; /* Do a scan if we couldn't get affiliation */
2055 goto out;
2056 }
2057
2058 if (!initial) {
2059 if (!new_affiliation->ldCount) {
2060 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2061 "affiliation for passive path for scsi%d.\n",
2062 instance->host->host_no);
2063 retval = 1;
2064 goto out;
2065 }
2066 newmap = new_affiliation->map;
2067 savedmap = instance->vf_affiliation->map;
2068 thisVf = new_affiliation->thisVf;
2069 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2070 found = 0;
2071 for (j = 0; j < instance->vf_affiliation->ldCount;
2072 j++) {
2073 if (newmap->ref.targetId ==
2074 savedmap->ref.targetId) {
2075 found = 1;
2076 if (newmap->policy[thisVf] !=
2077 savedmap->policy[thisVf]) {
2078 doscan = 1;
2079 goto out;
2080 }
adam radford229fe472014-03-10 02:51:56 -07002081 }
2082 savedmap = (struct MR_LD_VF_MAP *)
2083 ((unsigned char *)savedmap +
2084 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002085 }
2086 if (!found && newmap->policy[thisVf] !=
2087 MR_LD_ACCESS_HIDDEN) {
2088 doscan = 1;
2089 goto out;
2090 }
2091 newmap = (struct MR_LD_VF_MAP *)
2092 ((unsigned char *)newmap + newmap->size);
2093 }
2094
2095 newmap = new_affiliation->map;
2096 savedmap = instance->vf_affiliation->map;
2097
2098 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2099 found = 0;
2100 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2101 if (savedmap->ref.targetId ==
2102 newmap->ref.targetId) {
2103 found = 1;
2104 if (savedmap->policy[thisVf] !=
2105 newmap->policy[thisVf]) {
2106 doscan = 1;
2107 goto out;
2108 }
2109 }
adam radford229fe472014-03-10 02:51:56 -07002110 newmap = (struct MR_LD_VF_MAP *)
2111 ((unsigned char *)newmap +
2112 newmap->size);
2113 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002114 if (!found && savedmap->policy[thisVf] !=
2115 MR_LD_ACCESS_HIDDEN) {
2116 doscan = 1;
2117 goto out;
2118 }
2119 savedmap = (struct MR_LD_VF_MAP *)
2120 ((unsigned char *)savedmap +
2121 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002122 }
2123 }
2124out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002125 if (doscan) {
2126 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2127 "affiliation for scsi%d.\n", instance->host->host_no);
2128 memcpy(instance->vf_affiliation, new_affiliation,
2129 new_affiliation->size);
2130 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002131 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002132
2133 if (new_affiliation)
2134 pci_free_consistent(instance->pdev,
2135 (MAX_LOGICAL_DRIVES + 1) *
2136 sizeof(struct MR_LD_VF_AFFILIATION),
2137 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302138 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002139
2140 return retval;
2141}
2142
Adam Radford4cbfea82014-07-09 15:17:56 -07002143/* This function will get the current SR-IOV LD/VF affiliation */
2144static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2145 int initial)
2146{
2147 int retval;
2148
2149 if (instance->PlasmaFW111)
2150 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2151 else
2152 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2153 return retval;
2154}
2155
adam radford229fe472014-03-10 02:51:56 -07002156/* This function will tell FW to start the SR-IOV heartbeat */
2157int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2158 int initial)
2159{
2160 struct megasas_cmd *cmd;
2161 struct megasas_dcmd_frame *dcmd;
2162 int retval = 0;
2163
2164 cmd = megasas_get_cmd(instance);
2165
2166 if (!cmd) {
2167 printk(KERN_DEBUG "megasas: megasas_sriov_start_heartbeat: "
2168 "Failed to get cmd for scsi%d.\n",
2169 instance->host->host_no);
2170 return -ENOMEM;
2171 }
2172
2173 dcmd = &cmd->frame->dcmd;
2174
2175 if (initial) {
2176 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002177 pci_zalloc_consistent(instance->pdev,
2178 sizeof(struct MR_CTRL_HB_HOST_MEM),
2179 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002180 if (!instance->hb_host_mem) {
2181 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate"
2182 " memory for heartbeat host memory for "
2183 "scsi%d.\n", instance->host->host_no);
2184 retval = -ENOMEM;
2185 goto out;
2186 }
adam radford229fe472014-03-10 02:51:56 -07002187 }
2188
2189 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2190
Christoph Hellwig2213a462015-04-23 16:33:54 +05302191 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002192 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302193 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002194 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302195 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002196 dcmd->timeout = 0;
2197 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302198 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2199 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2200 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2201 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002202
2203 printk(KERN_WARNING "megasas: SR-IOV: Starting heartbeat for scsi%d\n",
2204 instance->host->host_no);
2205
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302206 if (instance->ctrl_context && !instance->mask_interrupts)
2207 retval = megasas_issue_blocked_cmd(instance, cmd,
2208 MEGASAS_ROUTINE_WAIT_TIME_VF);
2209 else
2210 retval = megasas_issue_polled(instance, cmd);
2211
2212 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302213 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2214 "_MEM_ALLOC DCMD %s for scsi%d\n",
2215 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2216 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002217 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002218 }
2219
2220out:
2221 megasas_return_cmd(instance, cmd);
2222
2223 return retval;
2224}
2225
2226/* Handler for SR-IOV heartbeat */
2227void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2228{
2229 struct megasas_instance *instance =
2230 (struct megasas_instance *)instance_addr;
2231
2232 if (instance->hb_host_mem->HB.fwCounter !=
2233 instance->hb_host_mem->HB.driverCounter) {
2234 instance->hb_host_mem->HB.driverCounter =
2235 instance->hb_host_mem->HB.fwCounter;
2236 mod_timer(&instance->sriov_heartbeat_timer,
2237 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2238 } else {
2239 printk(KERN_WARNING "megasas: SR-IOV: Heartbeat never "
2240 "completed for scsi%d\n", instance->host->host_no);
2241 schedule_work(&instance->work_init);
2242 }
2243}
2244
bo yang7343eb62007-11-09 04:35:44 -05002245/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002246 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2247 * @instance: Adapter soft state
2248 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002249 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002250 * complete all its outstanding commands. Returns error if one or more IOs
2251 * are pending after this time period. It also marks the controller dead.
2252 */
2253static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2254{
2255 int i;
bo yang39a98552010-09-22 22:36:29 -04002256 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002257 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002258 u8 adprecovery;
2259 unsigned long flags;
2260 struct list_head clist_local;
2261 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002262 u32 fw_state;
2263 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04002264
2265 spin_lock_irqsave(&instance->hba_lock, flags);
2266 adprecovery = instance->adprecovery;
2267 spin_unlock_irqrestore(&instance->hba_lock, flags);
2268
2269 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2270
2271 INIT_LIST_HEAD(&clist_local);
2272 spin_lock_irqsave(&instance->hba_lock, flags);
2273 list_splice_init(&instance->internal_reset_pending_q,
2274 &clist_local);
2275 spin_unlock_irqrestore(&instance->hba_lock, flags);
2276
2277 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
2278 for (i = 0; i < wait_time; i++) {
2279 msleep(1000);
2280 spin_lock_irqsave(&instance->hba_lock, flags);
2281 adprecovery = instance->adprecovery;
2282 spin_unlock_irqrestore(&instance->hba_lock, flags);
2283 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2284 break;
2285 }
2286
2287 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2288 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
2289 spin_lock_irqsave(&instance->hba_lock, flags);
2290 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2291 spin_unlock_irqrestore(&instance->hba_lock, flags);
2292 return FAILED;
2293 }
2294
2295 reset_index = 0;
2296 while (!list_empty(&clist_local)) {
2297 reset_cmd = list_entry((&clist_local)->next,
2298 struct megasas_cmd, list);
2299 list_del_init(&reset_cmd->list);
2300 if (reset_cmd->scmd) {
2301 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002302 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002303 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002304 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002305
2306 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2307 megasas_return_cmd(instance, reset_cmd);
2308 } else if (reset_cmd->sync_cmd) {
2309 printk(KERN_NOTICE "megasas:%p synch cmds"
2310 "reset queue\n",
2311 reset_cmd);
2312
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302313 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002314 instance->instancet->fire_cmd(instance,
2315 reset_cmd->frame_phys_addr,
2316 0, instance->reg_set);
2317 } else {
2318 printk(KERN_NOTICE "megasas: %p unexpected"
2319 "cmds lst\n",
2320 reset_cmd);
2321 }
2322 reset_index++;
2323 }
2324
2325 return SUCCESS;
2326 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002327
adam radfordc007b8b2012-07-17 18:20:24 -07002328 for (i = 0; i < resetwaittime; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002329
Sumant Patroe4a082c2006-05-30 12:03:37 -07002330 int outstanding = atomic_read(&instance->fw_outstanding);
2331
2332 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002333 break;
2334
2335 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2336 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002337 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002338 /*
2339 * Call cmd completion routine. Cmd to be
2340 * be completed directly without depending on isr.
2341 */
2342 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002343 }
2344
2345 msleep(1000);
2346 }
2347
Yang, Bo707e09b2010-10-12 07:20:27 -06002348 i = 0;
2349 kill_adapter_flag = 0;
2350 do {
2351 fw_state = instance->instancet->read_fw_status_reg(
2352 instance->reg_set) & MFI_STATE_MASK;
2353 if ((fw_state == MFI_STATE_FAULT) &&
2354 (instance->disableOnlineCtrlReset == 0)) {
2355 if (i == 3) {
2356 kill_adapter_flag = 2;
2357 break;
2358 }
2359 megasas_do_ocr(instance);
2360 kill_adapter_flag = 1;
2361
2362 /* wait for 1 secs to let FW finish the pending cmds */
2363 msleep(1000);
2364 }
2365 i++;
2366 } while (i <= 3);
2367
2368 if (atomic_read(&instance->fw_outstanding) &&
2369 !kill_adapter_flag) {
2370 if (instance->disableOnlineCtrlReset == 0) {
2371
2372 megasas_do_ocr(instance);
2373
2374 /* wait for 5 secs to let FW finish the pending cmds */
2375 for (i = 0; i < wait_time; i++) {
2376 int outstanding =
2377 atomic_read(&instance->fw_outstanding);
2378 if (!outstanding)
2379 return SUCCESS;
2380 msleep(1000);
2381 }
2382 }
2383 }
2384
2385 if (atomic_read(&instance->fw_outstanding) ||
2386 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04002387 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002388 /*
2389 * Send signal to FW to stop processing any pending cmds.
2390 * The controller will be taken offline by the OS now.
2391 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002392 if ((instance->pdev->device ==
2393 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2394 (instance->pdev->device ==
2395 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2396 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08002397 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002398 } else {
2399 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07002400 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002401 }
Sumant Patro658dced2006-10-03 13:09:14 -07002402 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04002403 spin_lock_irqsave(&instance->hba_lock, flags);
2404 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2405 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002406 return FAILED;
2407 }
2408
bo yang39a98552010-09-22 22:36:29 -04002409 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
2410
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002411 return SUCCESS;
2412}
2413
2414/**
2415 * megasas_generic_reset - Generic reset routine
2416 * @scmd: Mid-layer SCSI command
2417 *
2418 * This routine implements a generic reset handler for device, bus and host
2419 * reset requests. Device, bus and host specific reset handlers can use this
2420 * function after they do their specific tasks.
2421 */
2422static int megasas_generic_reset(struct scsi_cmnd *scmd)
2423{
2424 int ret_val;
2425 struct megasas_instance *instance;
2426
2427 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2428
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002429 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2430 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002431
bo yang39a98552010-09-22 22:36:29 -04002432 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002433 printk(KERN_ERR "megasas: cannot recover from previous reset "
2434 "failures\n");
2435 return FAILED;
2436 }
2437
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002438 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002439 if (ret_val == SUCCESS)
2440 printk(KERN_NOTICE "megasas: reset successful \n");
2441 else
2442 printk(KERN_ERR "megasas: failed to do reset\n");
2443
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002444 return ret_val;
2445}
2446
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002447/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002448 * megasas_reset_timer - quiesce the adapter if required
2449 * @scmd: scsi cmnd
2450 *
2451 * Sets the FW busy flag and reduces the host->can_queue if the
2452 * cmd has not been completed within the timeout period.
2453 */
2454static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002455blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002456{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002457 struct megasas_instance *instance;
2458 unsigned long flags;
2459
2460 if (time_after(jiffies, scmd->jiffies_at_alloc +
2461 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002462 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002463 }
2464
adam radfordf575c5d2011-10-13 16:01:12 -07002465 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002466 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2467 /* FW is busy, throttle IO */
2468 spin_lock_irqsave(instance->host->host_lock, flags);
2469
adam radfordc5daa6a2012-07-17 18:20:03 -07002470 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002471 instance->last_time = jiffies;
2472 instance->flag |= MEGASAS_FW_BUSY;
2473
2474 spin_unlock_irqrestore(instance->host->host_lock, flags);
2475 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002476 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002477}
2478
2479/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002480 * megasas_reset_device - Device reset handler entry point
2481 */
2482static int megasas_reset_device(struct scsi_cmnd *scmd)
2483{
2484 int ret;
2485
2486 /*
2487 * First wait for all commands to complete
2488 */
2489 ret = megasas_generic_reset(scmd);
2490
2491 return ret;
2492}
2493
2494/**
2495 * megasas_reset_bus_host - Bus & host reset handler entry point
2496 */
2497static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2498{
2499 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002500 struct megasas_instance *instance;
2501 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002502
2503 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002504 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002505 */
adam radford36807e62011-10-08 18:15:06 -07002506 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07002507 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302508 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2509 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radford229fe472014-03-10 02:51:56 -07002510 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002511 else
2512 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002513
2514 return ret;
2515}
2516
2517/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002518 * megasas_bios_param - Returns disk geometry for a disk
2519 * @sdev: device handle
2520 * @bdev: block device
2521 * @capacity: drive capacity
2522 * @geom: geometry parameters
2523 */
2524static int
2525megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2526 sector_t capacity, int geom[])
2527{
2528 int heads;
2529 int sectors;
2530 sector_t cylinders;
2531 unsigned long tmp;
2532 /* Default heads (64) & sectors (32) */
2533 heads = 64;
2534 sectors = 32;
2535
2536 tmp = heads * sectors;
2537 cylinders = capacity;
2538
2539 sector_div(cylinders, tmp);
2540
2541 /*
2542 * Handle extended translation size for logical drives > 1Gb
2543 */
2544
2545 if (capacity >= 0x200000) {
2546 heads = 255;
2547 sectors = 63;
2548 tmp = heads*sectors;
2549 cylinders = capacity;
2550 sector_div(cylinders, tmp);
2551 }
2552
2553 geom[0] = heads;
2554 geom[1] = sectors;
2555 geom[2] = cylinders;
2556
2557 return 0;
2558}
2559
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002560static void megasas_aen_polling(struct work_struct *work);
2561
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002562/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002563 * megasas_service_aen - Processes an event notification
2564 * @instance: Adapter soft state
2565 * @cmd: AEN command completed by the ISR
2566 *
2567 * For AEN, driver sends a command down to FW that is held by the FW till an
2568 * event occurs. When an event of interest occurs, FW completes the command
2569 * that it was previously holding.
2570 *
2571 * This routines sends SIGIO signal to processes that have registered with the
2572 * driver for AEN.
2573 */
2574static void
2575megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2576{
Yang, Boc3518832009-10-06 14:18:02 -06002577 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002578 /*
2579 * Don't signal app if it is just an aborted previously registered aen
2580 */
Yang, Boc3518832009-10-06 14:18:02 -06002581 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2582 spin_lock_irqsave(&poll_aen_lock, flags);
2583 megasas_poll_wait_aen = 1;
2584 spin_unlock_irqrestore(&poll_aen_lock, flags);
2585 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002586 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002587 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002588 else
2589 cmd->abort_aen = 0;
2590
2591 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302592
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302593 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002594
bo yang39a98552010-09-22 22:36:29 -04002595 if ((instance->unload == 0) &&
2596 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002597 struct megasas_aen_event *ev;
2598 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2599 if (!ev) {
2600 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2601 } else {
2602 ev->instance = instance;
2603 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002604 INIT_DELAYED_WORK(&ev->hotplug_work,
2605 megasas_aen_polling);
2606 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002607 }
2608 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002609}
2610
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302611static ssize_t
2612megasas_fw_crash_buffer_store(struct device *cdev,
2613 struct device_attribute *attr, const char *buf, size_t count)
2614{
2615 struct Scsi_Host *shost = class_to_shost(cdev);
2616 struct megasas_instance *instance =
2617 (struct megasas_instance *) shost->hostdata;
2618 int val = 0;
2619 unsigned long flags;
2620
2621 if (kstrtoint(buf, 0, &val) != 0)
2622 return -EINVAL;
2623
2624 spin_lock_irqsave(&instance->crashdump_lock, flags);
2625 instance->fw_crash_buffer_offset = val;
2626 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2627 return strlen(buf);
2628}
2629
2630static ssize_t
2631megasas_fw_crash_buffer_show(struct device *cdev,
2632 struct device_attribute *attr, char *buf)
2633{
2634 struct Scsi_Host *shost = class_to_shost(cdev);
2635 struct megasas_instance *instance =
2636 (struct megasas_instance *) shost->hostdata;
2637 u32 size;
2638 unsigned long buff_addr;
2639 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2640 unsigned long src_addr;
2641 unsigned long flags;
2642 u32 buff_offset;
2643
2644 spin_lock_irqsave(&instance->crashdump_lock, flags);
2645 buff_offset = instance->fw_crash_buffer_offset;
2646 if (!instance->crash_dump_buf &&
2647 !((instance->fw_crash_state == AVAILABLE) ||
2648 (instance->fw_crash_state == COPYING))) {
2649 dev_err(&instance->pdev->dev,
2650 "Firmware crash dump is not available\n");
2651 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2652 return -EINVAL;
2653 }
2654
2655 buff_addr = (unsigned long) buf;
2656
2657 if (buff_offset >
2658 (instance->fw_crash_buffer_size * dmachunk)) {
2659 dev_err(&instance->pdev->dev,
2660 "Firmware crash dump offset is out of range\n");
2661 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2662 return 0;
2663 }
2664
2665 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2666 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2667
2668 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2669 (buff_offset % dmachunk);
2670 memcpy(buf, (void *)src_addr, size);
2671 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2672
2673 return size;
2674}
2675
2676static ssize_t
2677megasas_fw_crash_buffer_size_show(struct device *cdev,
2678 struct device_attribute *attr, char *buf)
2679{
2680 struct Scsi_Host *shost = class_to_shost(cdev);
2681 struct megasas_instance *instance =
2682 (struct megasas_instance *) shost->hostdata;
2683
2684 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2685 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2686}
2687
2688static ssize_t
2689megasas_fw_crash_state_store(struct device *cdev,
2690 struct device_attribute *attr, const char *buf, size_t count)
2691{
2692 struct Scsi_Host *shost = class_to_shost(cdev);
2693 struct megasas_instance *instance =
2694 (struct megasas_instance *) shost->hostdata;
2695 int val = 0;
2696 unsigned long flags;
2697
2698 if (kstrtoint(buf, 0, &val) != 0)
2699 return -EINVAL;
2700
2701 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2702 dev_err(&instance->pdev->dev, "application updates invalid "
2703 "firmware crash state\n");
2704 return -EINVAL;
2705 }
2706
2707 instance->fw_crash_state = val;
2708
2709 if ((val == COPIED) || (val == COPY_ERROR)) {
2710 spin_lock_irqsave(&instance->crashdump_lock, flags);
2711 megasas_free_host_crash_buffer(instance);
2712 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2713 if (val == COPY_ERROR)
2714 dev_info(&instance->pdev->dev, "application failed to "
2715 "copy Firmware crash dump\n");
2716 else
2717 dev_info(&instance->pdev->dev, "Firmware crash dump "
2718 "copied successfully\n");
2719 }
2720 return strlen(buf);
2721}
2722
2723static ssize_t
2724megasas_fw_crash_state_show(struct device *cdev,
2725 struct device_attribute *attr, char *buf)
2726{
2727 struct Scsi_Host *shost = class_to_shost(cdev);
2728 struct megasas_instance *instance =
2729 (struct megasas_instance *) shost->hostdata;
2730 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2731}
2732
2733static ssize_t
2734megasas_page_size_show(struct device *cdev,
2735 struct device_attribute *attr, char *buf)
2736{
2737 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2738}
2739
2740static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2741 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2742static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2743 megasas_fw_crash_buffer_size_show, NULL);
2744static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2745 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2746static DEVICE_ATTR(page_size, S_IRUGO,
2747 megasas_page_size_show, NULL);
2748
2749struct device_attribute *megaraid_host_attrs[] = {
2750 &dev_attr_fw_crash_buffer_size,
2751 &dev_attr_fw_crash_buffer,
2752 &dev_attr_fw_crash_state,
2753 &dev_attr_page_size,
2754 NULL,
2755};
2756
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002757/*
2758 * Scsi host template for megaraid_sas driver
2759 */
2760static struct scsi_host_template megasas_template = {
2761
2762 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05302763 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002764 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002765 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002766 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002767 .queuecommand = megasas_queue_command,
2768 .eh_device_reset_handler = megasas_reset_device,
2769 .eh_bus_reset_handler = megasas_reset_bus_host,
2770 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002771 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302772 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002773 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002774 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002775 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002776 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002777};
2778
2779/**
2780 * megasas_complete_int_cmd - Completes an internal command
2781 * @instance: Adapter soft state
2782 * @cmd: Command to be completed
2783 *
2784 * The megasas_issue_blocked_cmd() function waits for a command to complete
2785 * after it issues a command. This function wakes up that waiting routine by
2786 * calling wake_up() on the wait queue.
2787 */
2788static void
2789megasas_complete_int_cmd(struct megasas_instance *instance,
2790 struct megasas_cmd *cmd)
2791{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302792 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002793 wake_up(&instance->int_cmd_wait_q);
2794}
2795
2796/**
2797 * megasas_complete_abort - Completes aborting a command
2798 * @instance: Adapter soft state
2799 * @cmd: Cmd that was issued to abort another cmd
2800 *
adam radford0d490162010-12-14 19:17:17 -08002801 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2802 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002803 * wakes up all functions waiting on the same wait queue.
2804 */
2805static void
2806megasas_complete_abort(struct megasas_instance *instance,
2807 struct megasas_cmd *cmd)
2808{
2809 if (cmd->sync_cmd) {
2810 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302811 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002812 wake_up(&instance->abort_cmd_wait_q);
2813 }
2814
2815 return;
2816}
2817
2818/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002819 * megasas_complete_cmd - Completes a command
2820 * @instance: Adapter soft state
2821 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002822 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002823 * SCSI mid-layer instead of the value returned
2824 * by the FW. This should be used if caller wants
2825 * an alternate status (as in the case of aborted
2826 * commands)
2827 */
adam radford9c915a82010-12-21 13:34:31 -08002828void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002829megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2830 u8 alt_status)
2831{
2832 int exception = 0;
2833 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002834 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002835 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302836 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002837
bo yang39a98552010-09-22 22:36:29 -04002838 /* flag for the retry reset */
2839 cmd->retry_for_fw_reset = 0;
2840
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002841 if (cmd->scmd)
2842 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002843
2844 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002845 case MFI_CMD_INVALID:
2846 /* Some older 1068 controller FW may keep a pended
2847 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2848 when booting the kdump kernel. Ignore this command to
2849 prevent a kernel panic on shutdown of the kdump kernel. */
2850 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2851 "completed.\n");
2852 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2853 "other than PERC5, please upgrade your firmware.\n");
2854 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002855 case MFI_CMD_PD_SCSI_IO:
2856 case MFI_CMD_LD_SCSI_IO:
2857
2858 /*
2859 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2860 * issued either through an IO path or an IOCTL path. If it
2861 * was via IOCTL, we will send it to internal completion.
2862 */
2863 if (cmd->sync_cmd) {
2864 cmd->sync_cmd = 0;
2865 megasas_complete_int_cmd(instance, cmd);
2866 break;
2867 }
2868
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002869 case MFI_CMD_LD_READ:
2870 case MFI_CMD_LD_WRITE:
2871
2872 if (alt_status) {
2873 cmd->scmd->result = alt_status << 16;
2874 exception = 1;
2875 }
2876
2877 if (exception) {
2878
Sumant Patroe4a082c2006-05-30 12:03:37 -07002879 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002880
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002881 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002882 cmd->scmd->scsi_done(cmd->scmd);
2883 megasas_return_cmd(instance, cmd);
2884
2885 break;
2886 }
2887
2888 switch (hdr->cmd_status) {
2889
2890 case MFI_STAT_OK:
2891 cmd->scmd->result = DID_OK << 16;
2892 break;
2893
2894 case MFI_STAT_SCSI_IO_FAILED:
2895 case MFI_STAT_LD_INIT_IN_PROGRESS:
2896 cmd->scmd->result =
2897 (DID_ERROR << 16) | hdr->scsi_status;
2898 break;
2899
2900 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2901
2902 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2903
2904 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2905 memset(cmd->scmd->sense_buffer, 0,
2906 SCSI_SENSE_BUFFERSIZE);
2907 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2908 hdr->sense_len);
2909
2910 cmd->scmd->result |= DRIVER_SENSE << 24;
2911 }
2912
2913 break;
2914
2915 case MFI_STAT_LD_OFFLINE:
2916 case MFI_STAT_DEVICE_NOT_FOUND:
2917 cmd->scmd->result = DID_BAD_TARGET << 16;
2918 break;
2919
2920 default:
2921 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2922 hdr->cmd_status);
2923 cmd->scmd->result = DID_ERROR << 16;
2924 break;
2925 }
2926
Sumant Patroe4a082c2006-05-30 12:03:37 -07002927 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002928
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002929 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002930 cmd->scmd->scsi_done(cmd->scmd);
2931 megasas_return_cmd(instance, cmd);
2932
2933 break;
2934
2935 case MFI_CMD_SMP:
2936 case MFI_CMD_STP:
2937 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302938 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08002939 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302940 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2941 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302942 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08002943 spin_lock_irqsave(instance->host->host_lock, flags);
2944 if (cmd->frame->hdr.cmd_status != 0) {
2945 if (cmd->frame->hdr.cmd_status !=
2946 MFI_STAT_NOT_FOUND)
2947 printk(KERN_WARNING "megasas: map sync"
2948 "failed, status = 0x%x.\n",
2949 cmd->frame->hdr.cmd_status);
2950 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302951 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08002952 spin_unlock_irqrestore(
2953 instance->host->host_lock,
2954 flags);
2955 break;
2956 }
2957 } else
2958 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302959 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302960
2961 /*
2962 * Set fast path IO to ZERO.
2963 * Validate Map will set proper value.
2964 * Meanwhile all IOs will go as LD IO.
2965 */
2966 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08002967 fusion->fast_path_io = 1;
2968 else
2969 fusion->fast_path_io = 0;
2970 megasas_sync_map_info(instance);
2971 spin_unlock_irqrestore(instance->host->host_lock,
2972 flags);
2973 break;
2974 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302975 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2976 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06002977 spin_lock_irqsave(&poll_aen_lock, flags);
2978 megasas_poll_wait_aen = 0;
2979 spin_unlock_irqrestore(&poll_aen_lock, flags);
2980 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002981
2982 /*
2983 * See if got an event notification
2984 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302985 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002986 megasas_service_aen(instance, cmd);
2987 else
2988 megasas_complete_int_cmd(instance, cmd);
2989
2990 break;
2991
2992 case MFI_CMD_ABORT:
2993 /*
2994 * Cmd issued to abort another cmd returned
2995 */
2996 megasas_complete_abort(instance, cmd);
2997 break;
2998
2999 default:
3000 printk("megasas: Unknown command completed! [0x%X]\n",
3001 hdr->cmd);
3002 break;
3003 }
3004}
3005
3006/**
bo yang39a98552010-09-22 22:36:29 -04003007 * megasas_issue_pending_cmds_again - issue all pending cmds
3008 * in FW again because of the fw reset
3009 * @instance: Adapter soft state
3010 */
3011static inline void
3012megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3013{
3014 struct megasas_cmd *cmd;
3015 struct list_head clist_local;
3016 union megasas_evt_class_locale class_locale;
3017 unsigned long flags;
3018 u32 seq_num;
3019
3020 INIT_LIST_HEAD(&clist_local);
3021 spin_lock_irqsave(&instance->hba_lock, flags);
3022 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3023 spin_unlock_irqrestore(&instance->hba_lock, flags);
3024
3025 while (!list_empty(&clist_local)) {
3026 cmd = list_entry((&clist_local)->next,
3027 struct megasas_cmd, list);
3028 list_del_init(&cmd->list);
3029
3030 if (cmd->sync_cmd || cmd->scmd) {
3031 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
3032 "detected to be pending while HBA reset.\n",
3033 cmd, cmd->scmd, cmd->sync_cmd);
3034
3035 cmd->retry_for_fw_reset++;
3036
3037 if (cmd->retry_for_fw_reset == 3) {
3038 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
3039 "was tried multiple times during reset."
3040 "Shutting down the HBA\n",
3041 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303042 instance->instancet->disable_intr(instance);
3043 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003044 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003045 return;
3046 }
3047 }
3048
3049 if (cmd->sync_cmd == 1) {
3050 if (cmd->scmd) {
3051 printk(KERN_NOTICE "megaraid_sas: unexpected"
3052 "cmd attached to internal command!\n");
3053 }
3054 printk(KERN_NOTICE "megasas: %p synchronous cmd"
3055 "on the internal reset queue,"
3056 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303057 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003058 instance->instancet->fire_cmd(instance,
3059 cmd->frame_phys_addr ,
3060 0, instance->reg_set);
3061 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003062 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003063 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003064 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003065
3066 atomic_inc(&instance->fw_outstanding);
3067 instance->instancet->fire_cmd(instance,
3068 cmd->frame_phys_addr,
3069 cmd->frame_count-1, instance->reg_set);
3070 } else {
3071 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
3072 "internal reset defer list while re-issue!!\n",
3073 cmd);
3074 }
3075 }
3076
3077 if (instance->aen_cmd) {
3078 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
3079 megasas_return_cmd(instance, instance->aen_cmd);
3080
3081 instance->aen_cmd = NULL;
3082 }
3083
3084 /*
3085 * Initiate AEN (Asynchronous Event Notification)
3086 */
3087 seq_num = instance->last_seq_num;
3088 class_locale.members.reserved = 0;
3089 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3090 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3091
3092 megasas_register_aen(instance, seq_num, class_locale.word);
3093}
3094
3095/**
3096 * Move the internal reset pending commands to a deferred queue.
3097 *
3098 * We move the commands pending at internal reset time to a
3099 * pending queue. This queue would be flushed after successful
3100 * completion of the internal reset sequence. if the internal reset
3101 * did not complete in time, the kernel reset handler would flush
3102 * these commands.
3103 **/
3104static void
3105megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3106{
3107 struct megasas_cmd *cmd;
3108 int i;
3109 u32 max_cmd = instance->max_fw_cmds;
3110 u32 defer_index;
3111 unsigned long flags;
3112
3113 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303114 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003115 for (i = 0; i < max_cmd; i++) {
3116 cmd = instance->cmd_list[i];
3117 if (cmd->sync_cmd == 1 || cmd->scmd) {
3118 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
3119 "on the defer queue as internal\n",
3120 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3121
3122 if (!list_empty(&cmd->list)) {
3123 printk(KERN_NOTICE "megaraid_sas: ERROR while"
3124 " moving this cmd:%p, %d %p, it was"
3125 "discovered on some list?\n",
3126 cmd, cmd->sync_cmd, cmd->scmd);
3127
3128 list_del_init(&cmd->list);
3129 }
3130 defer_index++;
3131 list_add_tail(&cmd->list,
3132 &instance->internal_reset_pending_q);
3133 }
3134 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303135 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003136}
3137
3138
3139static void
3140process_fw_state_change_wq(struct work_struct *work)
3141{
3142 struct megasas_instance *instance =
3143 container_of(work, struct megasas_instance, work_init);
3144 u32 wait;
3145 unsigned long flags;
3146
3147 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3148 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
3149 instance->adprecovery);
3150 return ;
3151 }
3152
3153 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3154 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
3155 "state, restarting it...\n");
3156
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303157 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003158 atomic_set(&instance->fw_outstanding, 0);
3159
3160 atomic_set(&instance->fw_reset_no_pci_access, 1);
3161 instance->instancet->adp_reset(instance, instance->reg_set);
3162 atomic_set(&instance->fw_reset_no_pci_access, 0 );
3163
3164 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
3165 "initiating next stage...\n");
3166
3167 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
3168 "state 2 starting...\n");
3169
3170 /*waitting for about 20 second before start the second init*/
3171 for (wait = 0; wait < 30; wait++) {
3172 msleep(1000);
3173 }
3174
adam radford058a8fa2011-10-08 18:14:27 -07003175 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04003176 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
3177
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303178 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003179 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003180 return ;
3181 }
3182
3183 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3184 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3185 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3186 ) {
3187 *instance->consumer = *instance->producer;
3188 } else {
3189 *instance->consumer = 0;
3190 *instance->producer = 0;
3191 }
3192
3193 megasas_issue_init_mfi(instance);
3194
3195 spin_lock_irqsave(&instance->hba_lock, flags);
3196 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3197 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303198 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003199
3200 megasas_issue_pending_cmds_again(instance);
3201 instance->issuepend_done = 1;
3202 }
3203 return ;
3204}
3205
3206/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003207 * megasas_deplete_reply_queue - Processes all completed commands
3208 * @instance: Adapter soft state
3209 * @alt_status: Alternate status to be returned to
3210 * SCSI mid-layer instead of the status
3211 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003212 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003213 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003214static int
bo yang39a98552010-09-22 22:36:29 -04003215megasas_deplete_reply_queue(struct megasas_instance *instance,
3216 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003217{
bo yang39a98552010-09-22 22:36:29 -04003218 u32 mfiStatus;
3219 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003220
bo yang39a98552010-09-22 22:36:29 -04003221 if ((mfiStatus = instance->instancet->check_reset(instance,
3222 instance->reg_set)) == 1) {
3223 return IRQ_HANDLED;
3224 }
3225
3226 if ((mfiStatus = instance->instancet->clear_intr(
3227 instance->reg_set)
3228 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003229 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003230 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003231 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003232 }
3233
3234 instance->mfiStatus = mfiStatus;
3235
3236 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3237 fw_state = instance->instancet->read_fw_status_reg(
3238 instance->reg_set) & MFI_STATE_MASK;
3239
3240 if (fw_state != MFI_STATE_FAULT) {
3241 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
3242 fw_state);
3243 }
3244
3245 if ((fw_state == MFI_STATE_FAULT) &&
3246 (instance->disableOnlineCtrlReset == 0)) {
3247 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
3248
3249 if ((instance->pdev->device ==
3250 PCI_DEVICE_ID_LSI_SAS1064R) ||
3251 (instance->pdev->device ==
3252 PCI_DEVICE_ID_DELL_PERC5) ||
3253 (instance->pdev->device ==
3254 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3255
3256 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303257 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003258 }
3259
3260
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303261 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003262 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3263 instance->issuepend_done = 0;
3264
3265 atomic_set(&instance->fw_outstanding, 0);
3266 megasas_internal_reset_defer_cmds(instance);
3267
3268 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
3269 fw_state, instance->adprecovery);
3270
3271 schedule_work(&instance->work_init);
3272 return IRQ_HANDLED;
3273
3274 } else {
3275 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
3276 fw_state, instance->disableOnlineCtrlReset);
3277 }
3278 }
3279
Sumant Patro5d018ad2006-10-03 13:13:18 -07003280 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003281 return IRQ_HANDLED;
3282}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003283/**
3284 * megasas_isr - isr entry point
3285 */
David Howells7d12e782006-10-05 14:55:46 +01003286static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003287{
adam radfordc8e858f2011-10-08 18:15:13 -07003288 struct megasas_irq_context *irq_context = devp;
3289 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003290 unsigned long flags;
3291 irqreturn_t rc;
3292
adam radfordc8e858f2011-10-08 18:15:13 -07003293 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003294 return IRQ_HANDLED;
3295
bo yang39a98552010-09-22 22:36:29 -04003296 spin_lock_irqsave(&instance->hba_lock, flags);
3297 rc = megasas_deplete_reply_queue(instance, DID_OK);
3298 spin_unlock_irqrestore(&instance->hba_lock, flags);
3299
3300 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003301}
3302
3303/**
3304 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003305 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003306 *
3307 * During the initialization, FW passes can potentially be in any one of
3308 * several possible states. If the FW in operational, waiting-for-handshake
3309 * states, driver must take steps to bring it to ready state. Otherwise, it
3310 * has to wait for the ready state.
3311 */
adam radford9c915a82010-12-21 13:34:31 -08003312int
adam radford058a8fa2011-10-08 18:14:27 -07003313megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003314{
3315 int i;
3316 u8 max_wait;
3317 u32 fw_state;
3318 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003319 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003320
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003321 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3322 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003323
Sumant Patroe3bbff92006-10-03 12:28:49 -07003324 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08003325 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
3326 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003327
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003328 while (fw_state != MFI_STATE_READY) {
3329
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003330 switch (fw_state) {
3331
3332 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003333 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003334 if (ocr) {
3335 max_wait = MEGASAS_RESET_WAIT_TIME;
3336 cur_state = MFI_STATE_FAULT;
3337 break;
3338 } else
3339 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003340
3341 case MFI_STATE_WAIT_HANDSHAKE:
3342 /*
3343 * Set the CLR bit in inbound doorbell
3344 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003345 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003346 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3347 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003348 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3349 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303350 PCI_DEVICE_ID_LSI_FUSION) ||
adam radford36807e62011-10-08 18:15:06 -07003351 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003352 PCI_DEVICE_ID_LSI_PLASMA) ||
3353 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303354 PCI_DEVICE_ID_LSI_INVADER) ||
3355 (instance->pdev->device ==
3356 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003357 writel(
3358 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003359 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06003360 } else {
3361 writel(
3362 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3363 &instance->reg_set->inbound_doorbell);
3364 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003365
Yang, Bo7218df62009-10-06 14:52:20 -06003366 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003367 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3368 break;
3369
Sumant Patroe3bbff92006-10-03 12:28:49 -07003370 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003371 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003372 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3373 (instance->pdev->device ==
3374 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3375 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07003376 PCI_DEVICE_ID_LSI_FUSION) ||
3377 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003378 PCI_DEVICE_ID_LSI_PLASMA) ||
3379 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303380 PCI_DEVICE_ID_LSI_INVADER) ||
3381 (instance->pdev->device ==
3382 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003383 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003384 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06003385 } else
3386 writel(MFI_INIT_HOTPLUG,
3387 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003388
Yang, Bo7218df62009-10-06 14:52:20 -06003389 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003390 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3391 break;
3392
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003393 case MFI_STATE_OPERATIONAL:
3394 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003395 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003396 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303397 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003398 if ((instance->pdev->device ==
3399 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3400 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003401 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3402 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07003403 == PCI_DEVICE_ID_LSI_FUSION) ||
3404 (instance->pdev->device
adam radford229fe472014-03-10 02:51:56 -07003405 == PCI_DEVICE_ID_LSI_PLASMA) ||
3406 (instance->pdev->device
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303407 == PCI_DEVICE_ID_LSI_INVADER) ||
3408 (instance->pdev->device
3409 == PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003410 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003411 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07003412 if ((instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303413 PCI_DEVICE_ID_LSI_FUSION) ||
3414 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003415 PCI_DEVICE_ID_LSI_PLASMA) ||
3416 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303417 PCI_DEVICE_ID_LSI_INVADER) ||
3418 (instance->pdev->device ==
3419 PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08003420 for (i = 0; i < (10 * 1000); i += 20) {
3421 if (readl(
3422 &instance->
3423 reg_set->
3424 doorbell) & 1)
3425 msleep(20);
3426 else
3427 break;
3428 }
3429 }
Yang, Bo87911122009-10-06 14:31:54 -06003430 } else
3431 writel(MFI_RESET_FLAGS,
3432 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003433
Yang, Bo7218df62009-10-06 14:52:20 -06003434 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003435 cur_state = MFI_STATE_OPERATIONAL;
3436 break;
3437
3438 case MFI_STATE_UNDEFINED:
3439 /*
3440 * This state should not last for more than 2 seconds
3441 */
Yang, Bo7218df62009-10-06 14:52:20 -06003442 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003443 cur_state = MFI_STATE_UNDEFINED;
3444 break;
3445
3446 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003447 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003448 cur_state = MFI_STATE_BB_INIT;
3449 break;
3450
3451 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003452 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003453 cur_state = MFI_STATE_FW_INIT;
3454 break;
3455
3456 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003457 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003458 cur_state = MFI_STATE_FW_INIT_2;
3459 break;
3460
3461 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003462 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003463 cur_state = MFI_STATE_DEVICE_SCAN;
3464 break;
3465
3466 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003467 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003468 cur_state = MFI_STATE_FLUSH_CACHE;
3469 break;
3470
3471 default:
3472 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
3473 fw_state);
3474 return -ENODEV;
3475 }
3476
3477 /*
3478 * The cur_state should not last for more than max_wait secs
3479 */
3480 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003481 curr_abs_state = instance->instancet->
3482 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003483
Yang, Bo7218df62009-10-06 14:52:20 -06003484 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003485 msleep(1);
3486 } else
3487 break;
3488 }
3489
3490 /*
3491 * Return error if fw_state hasn't changed after max_wait
3492 */
Yang, Bo7218df62009-10-06 14:52:20 -06003493 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003494 printk(KERN_DEBUG "FW state [%d] hasn't changed "
3495 "in %d secs\n", fw_state, max_wait);
3496 return -ENODEV;
3497 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003498
3499 abs_state = curr_abs_state;
3500 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003501 }
adam radford0d490162010-12-14 19:17:17 -08003502 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003503
3504 return 0;
3505}
3506
3507/**
3508 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3509 * @instance: Adapter soft state
3510 */
3511static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3512{
3513 int i;
adam radford9c915a82010-12-21 13:34:31 -08003514 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003515 struct megasas_cmd *cmd;
3516
3517 if (!instance->frame_dma_pool)
3518 return;
3519
3520 /*
3521 * Return all frames to pool
3522 */
3523 for (i = 0; i < max_cmd; i++) {
3524
3525 cmd = instance->cmd_list[i];
3526
3527 if (cmd->frame)
3528 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3529 cmd->frame_phys_addr);
3530
3531 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003532 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003533 cmd->sense_phys_addr);
3534 }
3535
3536 /*
3537 * Now destroy the pool itself
3538 */
3539 pci_pool_destroy(instance->frame_dma_pool);
3540 pci_pool_destroy(instance->sense_dma_pool);
3541
3542 instance->frame_dma_pool = NULL;
3543 instance->sense_dma_pool = NULL;
3544}
3545
3546/**
3547 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3548 * @instance: Adapter soft state
3549 *
3550 * Each command packet has an embedded DMA memory buffer that is used for
3551 * filling MFI frame and the SG list that immediately follows the frame. This
3552 * function creates those DMA memory buffers for each command packet by using
3553 * PCI pool facility.
3554 */
3555static int megasas_create_frame_pool(struct megasas_instance *instance)
3556{
3557 int i;
3558 u32 max_cmd;
3559 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003560 u32 total_sz;
3561 u32 frame_count;
3562 struct megasas_cmd *cmd;
3563
adam radford9c915a82010-12-21 13:34:31 -08003564 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565
3566 /*
3567 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3568 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3569 */
3570 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3571 sizeof(struct megasas_sge32);
3572
Yang, Bof4c9a132009-10-06 14:43:28 -06003573 if (instance->flag_ieee) {
3574 sge_sz = sizeof(struct megasas_sge_skinny);
3575 }
3576
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003577 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303578 * For MFI controllers.
3579 * max_num_sge = 60
3580 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3581 * Total 960 byte (15 MFI frame of 64 byte)
3582 *
3583 * Fusion adapter require only 3 extra frame.
3584 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3585 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3586 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003587 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303588 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003589 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3590 /*
3591 * Use DMA pool facility provided by PCI layer
3592 */
3593 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303594 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003595
3596 if (!instance->frame_dma_pool) {
3597 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
3598 return -ENOMEM;
3599 }
3600
3601 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3602 instance->pdev, 128, 4, 0);
3603
3604 if (!instance->sense_dma_pool) {
3605 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
3606
3607 pci_pool_destroy(instance->frame_dma_pool);
3608 instance->frame_dma_pool = NULL;
3609
3610 return -ENOMEM;
3611 }
3612
3613 /*
3614 * Allocate and attach a frame to each of the commands in cmd_list.
3615 * By making cmd->index as the context instead of the &cmd, we can
3616 * always use 32bit context regardless of the architecture
3617 */
3618 for (i = 0; i < max_cmd; i++) {
3619
3620 cmd = instance->cmd_list[i];
3621
3622 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3623 GFP_KERNEL, &cmd->frame_phys_addr);
3624
3625 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3626 GFP_KERNEL, &cmd->sense_phys_addr);
3627
3628 /*
3629 * megasas_teardown_frame_pool() takes care of freeing
3630 * whatever has been allocated
3631 */
3632 if (!cmd->frame || !cmd->sense) {
3633 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
3634 megasas_teardown_frame_pool(instance);
3635 return -ENOMEM;
3636 }
3637
Yang, Bo707e09b2010-10-12 07:20:27 -06003638 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303639 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003640 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07003641 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
adam radford229fe472014-03-10 02:51:56 -07003642 (instance->pdev->device != PCI_DEVICE_ID_LSI_PLASMA) &&
adam radforde5f93a32011-10-08 18:15:19 -07003643 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303644 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
adam radforde5f93a32011-10-08 18:15:19 -07003645 (reset_devices))
3646 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003647 }
3648
3649 return 0;
3650}
3651
3652/**
3653 * megasas_free_cmds - Free all the cmds in the free cmd pool
3654 * @instance: Adapter soft state
3655 */
adam radford9c915a82010-12-21 13:34:31 -08003656void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003657{
3658 int i;
3659 /* First free the MFI frame pool */
3660 megasas_teardown_frame_pool(instance);
3661
3662 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003663 for (i = 0; i < instance->max_mfi_cmds; i++)
3664
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003665 kfree(instance->cmd_list[i]);
3666
3667 /* Free the cmd_list buffer itself */
3668 kfree(instance->cmd_list);
3669 instance->cmd_list = NULL;
3670
3671 INIT_LIST_HEAD(&instance->cmd_pool);
3672}
3673
3674/**
3675 * megasas_alloc_cmds - Allocates the command packets
3676 * @instance: Adapter soft state
3677 *
3678 * Each command that is issued to the FW, whether IO commands from the OS or
3679 * internal commands like IOCTLs, are wrapped in local data structure called
3680 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3681 * the FW.
3682 *
3683 * Each frame has a 32-bit field called context (tag). This context is used
3684 * to get back the megasas_cmd from the frame when a frame gets completed in
3685 * the ISR. Typically the address of the megasas_cmd itself would be used as
3686 * the context. But we wanted to keep the differences between 32 and 64 bit
3687 * systems to the mininum. We always use 32 bit integers for the context. In
3688 * this driver, the 32 bit values are the indices into an array cmd_list.
3689 * This array is used only to look up the megasas_cmd given the context. The
3690 * free commands themselves are maintained in a linked list called cmd_pool.
3691 */
adam radford9c915a82010-12-21 13:34:31 -08003692int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003693{
3694 int i;
3695 int j;
3696 u32 max_cmd;
3697 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303698 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003699
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303700 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003701 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003702
3703 /*
3704 * instance->cmd_list is an array of struct megasas_cmd pointers.
3705 * Allocate the dynamic array first and then allocate individual
3706 * commands.
3707 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003708 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003709
3710 if (!instance->cmd_list) {
3711 printk(KERN_DEBUG "megasas: out of memory\n");
3712 return -ENOMEM;
3713 }
3714
adam radford9c915a82010-12-21 13:34:31 -08003715 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003716
3717 for (i = 0; i < max_cmd; i++) {
3718 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3719 GFP_KERNEL);
3720
3721 if (!instance->cmd_list[i]) {
3722
3723 for (j = 0; j < i; j++)
3724 kfree(instance->cmd_list[j]);
3725
3726 kfree(instance->cmd_list);
3727 instance->cmd_list = NULL;
3728
3729 return -ENOMEM;
3730 }
3731 }
3732
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003733 for (i = 0; i < max_cmd; i++) {
3734 cmd = instance->cmd_list[i];
3735 memset(cmd, 0, sizeof(struct megasas_cmd));
3736 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003737 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003738 cmd->instance = instance;
3739
3740 list_add_tail(&cmd->list, &instance->cmd_pool);
3741 }
3742
3743 /*
3744 * Create a frame pool and assign one frame to each cmd
3745 */
3746 if (megasas_create_frame_pool(instance)) {
3747 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3748 megasas_free_cmds(instance);
3749 }
3750
3751 return 0;
3752}
3753
Yang, Bo81e403c2009-10-06 14:27:54 -06003754/*
3755 * megasas_get_pd_list_info - Returns FW's pd_list structure
3756 * @instance: Adapter soft state
3757 * @pd_list: pd_list structure
3758 *
3759 * Issues an internal command (DCMD) to get the FW's controller PD
3760 * list structure. This information is mainly used to find out SYSTEM
3761 * supported by the FW.
3762 */
3763static int
3764megasas_get_pd_list(struct megasas_instance *instance)
3765{
3766 int ret = 0, pd_index = 0;
3767 struct megasas_cmd *cmd;
3768 struct megasas_dcmd_frame *dcmd;
3769 struct MR_PD_LIST *ci;
3770 struct MR_PD_ADDRESS *pd_addr;
3771 dma_addr_t ci_h = 0;
3772
3773 cmd = megasas_get_cmd(instance);
3774
3775 if (!cmd) {
3776 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3777 return -ENOMEM;
3778 }
3779
3780 dcmd = &cmd->frame->dcmd;
3781
3782 ci = pci_alloc_consistent(instance->pdev,
3783 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3784
3785 if (!ci) {
3786 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3787 megasas_return_cmd(instance, cmd);
3788 return -ENOMEM;
3789 }
3790
3791 memset(ci, 0, sizeof(*ci));
3792 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3793
3794 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3795 dcmd->mbox.b[1] = 0;
3796 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303797 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06003798 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303799 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06003800 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003801 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303802 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3803 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3804 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3805 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06003806
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303807 if (instance->ctrl_context && !instance->mask_interrupts)
3808 ret = megasas_issue_blocked_cmd(instance, cmd,
3809 MEGASAS_BLOCKED_CMD_TIMEOUT);
3810 else
3811 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003812
3813 /*
3814 * the following function will get the instance PD LIST.
3815 */
3816
3817 pd_addr = ci->addr;
3818
3819 if ( ret == 0 &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303820 (le32_to_cpu(ci->count) <
Yang, Bo81e403c2009-10-06 14:27:54 -06003821 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3822
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303823 memset(instance->local_pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06003824 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3825
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303826 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Yang, Bo81e403c2009-10-06 14:27:54 -06003827
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303828 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303829 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303830 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Yang, Bo81e403c2009-10-06 14:27:54 -06003831 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303832 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Yang, Bo81e403c2009-10-06 14:27:54 -06003833 MR_PD_STATE_SYSTEM;
3834 pd_addr++;
3835 }
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303836 memcpy(instance->pd_list, instance->local_pd_list,
3837 sizeof(instance->pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06003838 }
3839
3840 pci_free_consistent(instance->pdev,
3841 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3842 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303843
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303844 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003845
3846 return ret;
3847}
3848
Yang, Bobdc6fb82009-12-06 08:30:19 -07003849/*
3850 * megasas_get_ld_list_info - Returns FW's ld_list structure
3851 * @instance: Adapter soft state
3852 * @ld_list: ld_list structure
3853 *
3854 * Issues an internal command (DCMD) to get the FW's controller PD
3855 * list structure. This information is mainly used to find out SYSTEM
3856 * supported by the FW.
3857 */
3858static int
3859megasas_get_ld_list(struct megasas_instance *instance)
3860{
3861 int ret = 0, ld_index = 0, ids = 0;
3862 struct megasas_cmd *cmd;
3863 struct megasas_dcmd_frame *dcmd;
3864 struct MR_LD_LIST *ci;
3865 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303866 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003867
3868 cmd = megasas_get_cmd(instance);
3869
3870 if (!cmd) {
3871 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3872 return -ENOMEM;
3873 }
3874
3875 dcmd = &cmd->frame->dcmd;
3876
3877 ci = pci_alloc_consistent(instance->pdev,
3878 sizeof(struct MR_LD_LIST),
3879 &ci_h);
3880
3881 if (!ci) {
3882 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3883 megasas_return_cmd(instance, cmd);
3884 return -ENOMEM;
3885 }
3886
3887 memset(ci, 0, sizeof(*ci));
3888 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3889
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303890 if (instance->supportmax256vd)
3891 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003892 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303893 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003894 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303895 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003896 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303897 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3898 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3899 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3900 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07003901 dcmd->pad_0 = 0;
3902
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303903 if (instance->ctrl_context && !instance->mask_interrupts)
3904 ret = megasas_issue_blocked_cmd(instance, cmd,
3905 MEGASAS_BLOCKED_CMD_TIMEOUT);
3906 else
3907 ret = megasas_issue_polled(instance, cmd);
3908
Yang, Bobdc6fb82009-12-06 08:30:19 -07003909
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303910 ld_count = le32_to_cpu(ci->ldCount);
3911
Yang, Bobdc6fb82009-12-06 08:30:19 -07003912 /* the following function will get the instance PD LIST */
3913
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303914 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3915 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003916
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303917 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003918 if (ci->ldList[ld_index].state != 0) {
3919 ids = ci->ldList[ld_index].ref.targetId;
3920 instance->ld_ids[ids] =
3921 ci->ldList[ld_index].ref.targetId;
3922 }
3923 }
3924 }
3925
3926 pci_free_consistent(instance->pdev,
3927 sizeof(struct MR_LD_LIST),
3928 ci,
3929 ci_h);
3930
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303931 megasas_return_cmd(instance, cmd);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003932 return ret;
3933}
3934
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003935/**
adam radford21c9e162013-09-06 15:27:14 -07003936 * megasas_ld_list_query - Returns FW's ld_list structure
3937 * @instance: Adapter soft state
3938 * @ld_list: ld_list structure
3939 *
3940 * Issues an internal command (DCMD) to get the FW's controller PD
3941 * list structure. This information is mainly used to find out SYSTEM
3942 * supported by the FW.
3943 */
3944static int
3945megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3946{
3947 int ret = 0, ld_index = 0, ids = 0;
3948 struct megasas_cmd *cmd;
3949 struct megasas_dcmd_frame *dcmd;
3950 struct MR_LD_TARGETID_LIST *ci;
3951 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303952 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07003953
3954 cmd = megasas_get_cmd(instance);
3955
3956 if (!cmd) {
3957 printk(KERN_WARNING
3958 "megasas:(megasas_ld_list_query): Failed to get cmd\n");
3959 return -ENOMEM;
3960 }
3961
3962 dcmd = &cmd->frame->dcmd;
3963
3964 ci = pci_alloc_consistent(instance->pdev,
3965 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
3966
3967 if (!ci) {
3968 printk(KERN_WARNING
3969 "megasas: Failed to alloc mem for ld_list_query\n");
3970 megasas_return_cmd(instance, cmd);
3971 return -ENOMEM;
3972 }
3973
3974 memset(ci, 0, sizeof(*ci));
3975 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3976
3977 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303978 if (instance->supportmax256vd)
3979 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07003980
3981 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303982 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07003983 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303984 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07003985 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303986 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3987 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
3988 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3989 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07003990 dcmd->pad_0 = 0;
3991
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303992 if (instance->ctrl_context && !instance->mask_interrupts)
3993 ret = megasas_issue_blocked_cmd(instance, cmd,
3994 MEGASAS_BLOCKED_CMD_TIMEOUT);
3995 else
3996 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07003997
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303998 tgtid_count = le32_to_cpu(ci->count);
3999
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304000 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
adam radford21c9e162013-09-06 15:27:14 -07004001 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304002 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004003 ids = ci->targetId[ld_index];
4004 instance->ld_ids[ids] = ci->targetId[ld_index];
4005 }
4006
4007 }
4008
4009 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4010 ci, ci_h);
4011
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304012 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004013
4014 return ret;
4015}
4016
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304017/*
4018 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4019 * instance : Controller's instance
4020*/
4021static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4022{
4023 struct fusion_context *fusion;
4024 u32 old_map_sz;
4025 u32 new_map_sz;
4026
4027 fusion = instance->ctrl_context;
4028 /* For MFI based controllers return dummy success */
4029 if (!fusion)
4030 return;
4031
4032 instance->supportmax256vd =
4033 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4034 /* Below is additional check to address future FW enhancement */
4035 if (instance->ctrl_info->max_lds > 64)
4036 instance->supportmax256vd = 1;
4037
4038 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4039 * MEGASAS_MAX_DEV_PER_CHANNEL;
4040 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4041 * MEGASAS_MAX_DEV_PER_CHANNEL;
4042 if (instance->supportmax256vd) {
4043 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4044 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4045 } else {
4046 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4047 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4048 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304049
4050 dev_info(&instance->pdev->dev,
4051 "firmware type\t: %s\n",
4052 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4053 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304054
4055 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4056 (sizeof(struct MR_LD_SPAN_MAP) *
4057 (instance->fw_supported_vd_count - 1));
4058 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4059 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4060 (sizeof(struct MR_LD_SPAN_MAP) *
4061 (instance->drv_supported_vd_count - 1));
4062
4063 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4064
4065
4066 if (instance->supportmax256vd)
4067 fusion->current_map_sz = new_map_sz;
4068 else
4069 fusion->current_map_sz = old_map_sz;
4070
4071}
4072
adam radford21c9e162013-09-06 15:27:14 -07004073/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004074 * megasas_get_controller_info - Returns FW's controller structure
4075 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004076 *
4077 * Issues an internal command (DCMD) to get the FW's controller structure.
4078 * This information is mainly used to find out the maximum IO transfer per
4079 * command supported by the FW.
4080 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304081int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304082megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004083{
4084 int ret = 0;
4085 struct megasas_cmd *cmd;
4086 struct megasas_dcmd_frame *dcmd;
4087 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304088 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004089 dma_addr_t ci_h = 0;
4090
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304091 ctrl_info = instance->ctrl_info;
4092
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004093 cmd = megasas_get_cmd(instance);
4094
4095 if (!cmd) {
4096 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
4097 return -ENOMEM;
4098 }
4099
4100 dcmd = &cmd->frame->dcmd;
4101
4102 ci = pci_alloc_consistent(instance->pdev,
4103 sizeof(struct megasas_ctrl_info), &ci_h);
4104
4105 if (!ci) {
4106 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
4107 megasas_return_cmd(instance, cmd);
4108 return -ENOMEM;
4109 }
4110
4111 memset(ci, 0, sizeof(*ci));
4112 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4113
4114 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304115 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004116 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304117 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004118 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004119 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304120 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4121 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4122 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4123 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304124 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004125
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304126 if (instance->ctrl_context && !instance->mask_interrupts)
4127 ret = megasas_issue_blocked_cmd(instance, cmd,
4128 MEGASAS_BLOCKED_CMD_TIMEOUT);
4129 else
4130 ret = megasas_issue_polled(instance, cmd);
4131
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304132 if (!ret) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004133 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304134 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4135 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4136 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4137 megasas_update_ext_vd_details(instance);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304138 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4139 dev_info(&instance->pdev->dev,
4140 "controller type\t: %s(%dMB)\n",
4141 instance->is_imr ? "iMR" : "MR",
4142 le16_to_cpu(ctrl_info->memory_size));
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304143 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004144
4145 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4146 ci, ci_h);
4147
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304148 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004149 return ret;
4150}
4151
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304152/*
4153 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4154 * to firmware
4155 *
4156 * @instance: Adapter soft state
4157 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4158 MR_CRASH_BUF_TURN_OFF = 0
4159 MR_CRASH_BUF_TURN_ON = 1
4160 * @return 0 on success non-zero on failure.
4161 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4162 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4163 * that driver supports crash dump feature. This DCMD will be sent only if
4164 * crash dump feature is supported by the FW.
4165 *
4166 */
4167int megasas_set_crash_dump_params(struct megasas_instance *instance,
4168 u8 crash_buf_state)
4169{
4170 int ret = 0;
4171 struct megasas_cmd *cmd;
4172 struct megasas_dcmd_frame *dcmd;
4173
4174 cmd = megasas_get_cmd(instance);
4175
4176 if (!cmd) {
4177 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4178 return -ENOMEM;
4179 }
4180
4181
4182 dcmd = &cmd->frame->dcmd;
4183
4184 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4185 dcmd->mbox.b[0] = crash_buf_state;
4186 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304187 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304188 dcmd->sge_count = 1;
4189 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4190 dcmd->timeout = 0;
4191 dcmd->pad_0 = 0;
4192 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4193 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4194 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4195 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4196
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304197 if (instance->ctrl_context && !instance->mask_interrupts)
4198 ret = megasas_issue_blocked_cmd(instance, cmd,
4199 MEGASAS_BLOCKED_CMD_TIMEOUT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304200 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304201 ret = megasas_issue_polled(instance, cmd);
4202
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304203 megasas_return_cmd(instance, cmd);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304204 return ret;
4205}
4206
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004207/**
bo yang31ea7082007-11-07 12:09:50 -05004208 * megasas_issue_init_mfi - Initializes the FW
4209 * @instance: Adapter soft state
4210 *
4211 * Issues the INIT MFI cmd
4212 */
4213static int
4214megasas_issue_init_mfi(struct megasas_instance *instance)
4215{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304216 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004217
4218 struct megasas_cmd *cmd;
4219
4220 struct megasas_init_frame *init_frame;
4221 struct megasas_init_queue_info *initq_info;
4222 dma_addr_t init_frame_h;
4223 dma_addr_t initq_info_h;
4224
4225 /*
4226 * Prepare a init frame. Note the init frame points to queue info
4227 * structure. Each frame has SGL allocated after first 64 bytes. For
4228 * this frame - since we don't need any SGL - we use SGL's space as
4229 * queue info structure
4230 *
4231 * We will not get a NULL command below. We just created the pool.
4232 */
4233 cmd = megasas_get_cmd(instance);
4234
4235 init_frame = (struct megasas_init_frame *)cmd->frame;
4236 initq_info = (struct megasas_init_queue_info *)
4237 ((unsigned long)init_frame + 64);
4238
4239 init_frame_h = cmd->frame_phys_addr;
4240 initq_info_h = init_frame_h + 64;
4241
4242 context = init_frame->context;
4243 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4244 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4245 init_frame->context = context;
4246
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304247 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4248 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004249
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304250 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4251 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004252
4253 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304254 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304255 init_frame->queue_info_new_phys_addr_lo =
4256 cpu_to_le32(lower_32_bits(initq_info_h));
4257 init_frame->queue_info_new_phys_addr_hi =
4258 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004259
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304260 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004261
4262 /*
4263 * disable the intr before firing the init frame to FW
4264 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304265 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004266
4267 /*
4268 * Issue the init frame in polled mode
4269 */
4270
4271 if (megasas_issue_polled(instance, cmd)) {
4272 printk(KERN_ERR "megasas: Failed to init firmware\n");
4273 megasas_return_cmd(instance, cmd);
4274 goto fail_fw_init;
4275 }
4276
4277 megasas_return_cmd(instance, cmd);
4278
4279 return 0;
4280
4281fail_fw_init:
4282 return -EINVAL;
4283}
4284
adam radfordcd50ba82010-12-21 10:23:23 -08004285static u32
4286megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004287{
adam radfordcd50ba82010-12-21 10:23:23 -08004288 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004289 u32 context_sz;
4290 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004291
4292 reg_set = instance->reg_set;
4293
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004294 /*
4295 * Get various operational parameters from status register
4296 */
Sumant Patro1341c932006-01-25 12:02:40 -08004297 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004298 /*
4299 * Reduce the max supported cmds by 1. This is to ensure that the
4300 * reply_q_sz (1 more than the max cmd that driver may send)
4301 * does not exceed max cmds that the FW can support
4302 */
4303 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004304 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004305 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004306 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004307 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304308 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4309 * are reserved for IOCTL + driver's internal DCMDs.
4310 */
4311 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4312 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4313 instance->max_scsi_cmds = (instance->max_fw_cmds -
4314 MEGASAS_SKINNY_INT_CMDS);
4315 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4316 } else {
4317 instance->max_scsi_cmds = (instance->max_fw_cmds -
4318 MEGASAS_INT_CMDS);
4319 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4320 }
4321
4322 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004323 * Create a pool of commands
4324 */
4325 if (megasas_alloc_cmds(instance))
4326 goto fail_alloc_cmds;
4327
4328 /*
4329 * Allocate memory for reply queue. Length of reply queue should
4330 * be _one_ more than the maximum commands handled by the firmware.
4331 *
4332 * Note: When FW completes commands, it places corresponding contex
4333 * values in this circular reply queue. This circular queue is a fairly
4334 * typical producer-consumer queue. FW is the producer (of completed
4335 * commands) and the driver is the consumer.
4336 */
4337 context_sz = sizeof(u32);
4338 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4339
4340 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4341 reply_q_sz,
4342 &instance->reply_queue_h);
4343
4344 if (!instance->reply_queue) {
4345 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
4346 goto fail_reply_queue;
4347 }
4348
bo yang31ea7082007-11-07 12:09:50 -05004349 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004350 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004351
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304352 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304353 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4354 "Fail from %s %d\n", instance->unique_id,
4355 __func__, __LINE__);
4356 goto fail_fw_init;
4357 }
4358
bo yang39a98552010-09-22 22:36:29 -04004359 instance->fw_support_ieee = 0;
4360 instance->fw_support_ieee =
4361 (instance->instancet->read_fw_status_reg(reg_set) &
4362 0x04000000);
4363
4364 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
4365 instance->fw_support_ieee);
4366
4367 if (instance->fw_support_ieee)
4368 instance->flag_ieee = 1;
4369
adam radfordcd50ba82010-12-21 10:23:23 -08004370 return 0;
4371
4372fail_fw_init:
4373
4374 pci_free_consistent(instance->pdev, reply_q_sz,
4375 instance->reply_queue, instance->reply_queue_h);
4376fail_reply_queue:
4377 megasas_free_cmds(instance);
4378
4379fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004380 return 1;
4381}
4382
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304383/*
4384 * megasas_setup_irqs_msix - register legacy interrupts.
4385 * @instance: Adapter soft state
4386 *
4387 * Do not enable interrupt, only setup ISRs.
4388 *
4389 * Return 0 on success.
4390 */
4391static int
4392megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4393{
4394 struct pci_dev *pdev;
4395
4396 pdev = instance->pdev;
4397 instance->irq_context[0].instance = instance;
4398 instance->irq_context[0].MSIxIndex = 0;
4399 if (request_irq(pdev->irq, instance->instancet->service_isr,
4400 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4401 dev_err(&instance->pdev->dev,
4402 "Failed to register IRQ from %s %d\n",
4403 __func__, __LINE__);
4404 return -1;
4405 }
4406 return 0;
4407}
4408
4409/**
4410 * megasas_setup_irqs_msix - register MSI-x interrupts.
4411 * @instance: Adapter soft state
4412 * @is_probe: Driver probe check
4413 *
4414 * Do not enable interrupt, only setup ISRs.
4415 *
4416 * Return 0 on success.
4417 */
4418static int
4419megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4420{
4421 int i, j, cpu;
4422 struct pci_dev *pdev;
4423
4424 pdev = instance->pdev;
4425
4426 /* Try MSI-x */
4427 cpu = cpumask_first(cpu_online_mask);
4428 for (i = 0; i < instance->msix_vectors; i++) {
4429 instance->irq_context[i].instance = instance;
4430 instance->irq_context[i].MSIxIndex = i;
4431 if (request_irq(instance->msixentry[i].vector,
4432 instance->instancet->service_isr, 0, "megasas",
4433 &instance->irq_context[i])) {
4434 dev_err(&instance->pdev->dev,
4435 "Failed to register IRQ for vector %d.\n", i);
4436 for (j = 0; j < i; j++) {
4437 if (smp_affinity_enable)
4438 irq_set_affinity_hint(
4439 instance->msixentry[j].vector, NULL);
4440 free_irq(instance->msixentry[j].vector,
4441 &instance->irq_context[j]);
4442 }
4443 /* Retry irq register for IO_APIC*/
4444 instance->msix_vectors = 0;
4445 if (is_probe)
4446 return megasas_setup_irqs_ioapic(instance);
4447 else
4448 return -1;
4449 }
4450 if (smp_affinity_enable) {
4451 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4452 get_cpu_mask(cpu)))
4453 dev_err(&instance->pdev->dev,
4454 "Failed to set affinity hint"
4455 " for cpu %d\n", cpu);
4456 cpu = cpumask_next(cpu, cpu_online_mask);
4457 }
4458 }
4459 return 0;
4460}
4461
4462/*
4463 * megasas_destroy_irqs- unregister interrupts.
4464 * @instance: Adapter soft state
4465 * return: void
4466 */
4467static void
4468megasas_destroy_irqs(struct megasas_instance *instance) {
4469
4470 int i;
4471
4472 if (instance->msix_vectors)
4473 for (i = 0; i < instance->msix_vectors; i++) {
4474 if (smp_affinity_enable)
4475 irq_set_affinity_hint(
4476 instance->msixentry[i].vector, NULL);
4477 free_irq(instance->msixentry[i].vector,
4478 &instance->irq_context[i]);
4479 }
4480 else
4481 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4482}
4483
adam radfordcd50ba82010-12-21 10:23:23 -08004484/**
4485 * megasas_init_fw - Initializes the FW
4486 * @instance: Adapter soft state
4487 *
4488 * This is the main function for initializing firmware
4489 */
4490
4491static int megasas_init_fw(struct megasas_instance *instance)
4492{
4493 u32 max_sectors_1;
4494 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304495 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04004496 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08004497 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304498 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08004499 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304500 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07004501 struct IOV_111 *iovPtr;
adam radfordcd50ba82010-12-21 10:23:23 -08004502
4503 /* Find first memory bar */
4504 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4505 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
adam radfordcd50ba82010-12-21 10:23:23 -08004506 if (pci_request_selected_regions(instance->pdev, instance->bar,
4507 "megasas: LSI")) {
4508 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
4509 return -EBUSY;
4510 }
4511
Ben Collins11f8a7b2013-09-13 12:46:44 -04004512 base_addr = pci_resource_start(instance->pdev, instance->bar);
4513 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08004514
4515 if (!instance->reg_set) {
4516 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
4517 goto fail_ioremap;
4518 }
4519
4520 reg_set = instance->reg_set;
4521
4522 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08004523 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07004524 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07004525 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304526 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08004527 instance->instancet = &megasas_instance_template_fusion;
4528 break;
adam radfordcd50ba82010-12-21 10:23:23 -08004529 case PCI_DEVICE_ID_LSI_SAS1078R:
4530 case PCI_DEVICE_ID_LSI_SAS1078DE:
4531 instance->instancet = &megasas_instance_template_ppc;
4532 break;
4533 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4534 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4535 instance->instancet = &megasas_instance_template_gen2;
4536 break;
4537 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4538 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4539 instance->instancet = &megasas_instance_template_skinny;
4540 break;
4541 case PCI_DEVICE_ID_LSI_SAS1064R:
4542 case PCI_DEVICE_ID_DELL_PERC5:
4543 default:
4544 instance->instancet = &megasas_instance_template_xscale;
4545 break;
4546 }
4547
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05304548 if (megasas_transition_to_ready(instance, 0)) {
4549 atomic_set(&instance->fw_reset_no_pci_access, 1);
4550 instance->instancet->adp_reset
4551 (instance, instance->reg_set);
4552 atomic_set(&instance->fw_reset_no_pci_access, 0);
4553 dev_info(&instance->pdev->dev,
4554 "megasas: FW restarted successfully from %s!\n",
4555 __func__);
4556
4557 /*waitting for about 30 second before retry*/
4558 ssleep(30);
4559
4560 if (megasas_transition_to_ready(instance, 0))
4561 goto fail_ready_state;
4562 }
adam radfordcd50ba82010-12-21 10:23:23 -08004563
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304564 /*
4565 * MSI-X host index 0 is common for all adapter.
4566 * It is used for all MPT based Adapters.
4567 */
4568 instance->reply_post_host_index_addr[0] =
Christoph Hellwig8a232bb2015-04-23 16:32:39 +05304569 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304570 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4571
adam radford3f1abce2011-05-11 18:33:47 -07004572 /* Check if MSI-X is supported while in ready state */
4573 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4574 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07004575 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304576 scratch_pad_2 = readl
4577 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07004578 /* Check max MSI-X vectors */
adam radford229fe472014-03-10 02:51:56 -07004579 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4580 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA)) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304581 instance->msix_vectors = (scratch_pad_2
4582 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4583 fw_msix_count = instance->msix_vectors;
adam radford079eadd2012-10-01 19:26:59 -07004584 if (msix_vectors)
4585 instance->msix_vectors =
4586 min(msix_vectors,
4587 instance->msix_vectors);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304588 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
4589 || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
4590 /* Invader/Fury supports more than 8 MSI-X */
4591 instance->msix_vectors = ((scratch_pad_2
4592 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4593 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4594 fw_msix_count = instance->msix_vectors;
4595 /* Save 1-15 reply post index address to local memory
4596 * Index 0 is already saved from reg offset
4597 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4598 */
4599 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4600 instance->reply_post_host_index_addr[loop] =
Christoph Hellwig8a232bb2015-04-23 16:32:39 +05304601 (u32 __iomem *)
4602 ((u8 __iomem *)instance->reg_set +
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304603 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4604 + (loop * 0x10));
4605 }
4606 if (msix_vectors)
4607 instance->msix_vectors = min(msix_vectors,
4608 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07004609 } else
4610 instance->msix_vectors = 1;
4611 /* Don't bother allocating more MSI-X vectors than cpus */
4612 instance->msix_vectors = min(instance->msix_vectors,
4613 (unsigned int)num_online_cpus());
4614 for (i = 0; i < instance->msix_vectors; i++)
4615 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004616 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4617 1, instance->msix_vectors);
Jiang Liuc12de882014-11-03 20:44:20 +08004618 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004619 instance->msix_vectors = i;
4620 else
adam radfordc8e858f2011-10-08 18:15:13 -07004621 instance->msix_vectors = 0;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304622
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304623 dev_info(&instance->pdev->dev,
4624 "firmware supports msix\t: (%d)", fw_msix_count);
4625 dev_info(&instance->pdev->dev,
4626 "current msix/online cpus\t: (%d/%d)\n",
4627 instance->msix_vectors, (unsigned int)num_online_cpus());
4628
4629 if (instance->msix_vectors ?
4630 megasas_setup_irqs_msix(instance, 1) :
4631 megasas_setup_irqs_ioapic(instance))
4632 goto fail_setup_irqs;
adam radfordc8e858f2011-10-08 18:15:13 -07004633 }
adam radford3f1abce2011-05-11 18:33:47 -07004634
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304635 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4636 GFP_KERNEL);
4637 if (instance->ctrl_info == NULL)
4638 goto fail_init_adapter;
4639
4640 /*
4641 * Below are default value for legacy Firmware.
4642 * non-fusion based controllers
4643 */
4644 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4645 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08004646 /* Get operational params, sge flags, send init cmd to controller */
4647 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08004648 goto fail_init_adapter;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304649 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08004650
4651 printk(KERN_ERR "megasas: INIT adapter done\n");
4652
bo yang39a98552010-09-22 22:36:29 -04004653 /** for passthrough
4654 * the following function will get the PD LIST.
4655 */
4656
Yang, Bo81e403c2009-10-06 14:27:54 -06004657 memset(instance->pd_list, 0 ,
4658 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01004659 if (megasas_get_pd_list(instance) < 0) {
4660 printk(KERN_ERR "megasas: failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304661 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01004662 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004663
Yang, Bobdc6fb82009-12-06 08:30:19 -07004664 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07004665 if (megasas_ld_list_query(instance,
4666 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4667 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004668
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004669 /*
4670 * Compute the max allowed sectors per IO: The controller info has two
4671 * limits on max sectors. Driver should use the minimum of these two.
4672 *
4673 * 1 << stripe_sz_ops.min = max sectors per strip
4674 *
4675 * Note that older firmwares ( < FW ver 30) didn't report information
4676 * to calculate max_sectors_1. So the number ended up as zero always.
4677 */
bo yang14faea92007-11-09 04:14:00 -05004678 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304679 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004680
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304681 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4682 le16_to_cpu(ctrl_info->max_strips_per_io);
4683 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004684
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304685 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304686
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304687 instance->disableOnlineCtrlReset =
4688 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304689 instance->mpio = ctrl_info->adapterOperations2.mpio;
4690 instance->UnevenSpanSupport =
4691 ctrl_info->adapterOperations2.supportUnevenSpans;
4692 if (instance->UnevenSpanSupport) {
4693 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304694 if (MR_ValidateMapInfo(instance))
4695 fusion->fast_path_io = 1;
4696 else
4697 fusion->fast_path_io = 0;
4698
4699 }
4700 if (ctrl_info->host_interface.SRIOV) {
4701 if (!ctrl_info->adapterOperations2.activePassive)
4702 instance->PlasmaFW111 = 1;
4703
4704 if (!instance->PlasmaFW111)
4705 instance->requestorId =
4706 ctrl_info->iov.requestorId;
4707 else {
4708 iovPtr = (struct IOV_111 *)((unsigned char *)ctrl_info + IOV_111_OFFSET);
4709 instance->requestorId = iovPtr->requestorId;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304710 }
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304711 dev_warn(&instance->pdev->dev, "I am VF "
4712 "requestorId %d\n", instance->requestorId);
4713 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05304714
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304715 instance->crash_dump_fw_support =
4716 ctrl_info->adapterOperations3.supportCrashDump;
4717 instance->crash_dump_drv_support =
4718 (instance->crash_dump_fw_support &&
4719 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304720 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304721 megasas_set_crash_dump_params(instance,
4722 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07004723
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304724 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304725 if (instance->crash_dump_buf)
4726 pci_free_consistent(instance->pdev,
4727 CRASH_DMA_BUF_SIZE,
4728 instance->crash_dump_buf,
4729 instance->crash_dump_h);
4730 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05004731 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05304732
4733 instance->secure_jbod_support =
4734 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304735
4736 dev_info(&instance->pdev->dev,
4737 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4738 le16_to_cpu(ctrl_info->pci.vendor_id),
4739 le16_to_cpu(ctrl_info->pci.device_id),
4740 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4741 le16_to_cpu(ctrl_info->pci.sub_device_id));
4742 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
4743 instance->UnevenSpanSupport ? "yes" : "no");
4744 dev_info(&instance->pdev->dev, "disable ocr : %s\n",
4745 instance->disableOnlineCtrlReset ? "yes" : "no");
4746 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
4747 instance->crash_dump_drv_support ? "yes" : "no");
4748 dev_info(&instance->pdev->dev, "secure jbod : %s\n",
4749 instance->secure_jbod_support ? "yes" : "no");
4750
4751
bo yang14faea92007-11-09 04:14:00 -05004752 instance->max_sectors_per_req = instance->max_num_sge *
4753 PAGE_SIZE / 512;
4754 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4755 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004756
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05304757 /* Check for valid throttlequeuedepth module parameter */
4758 if (throttlequeuedepth &&
4759 throttlequeuedepth <= instance->max_scsi_cmds)
4760 instance->throttlequeuedepth = throttlequeuedepth;
4761 else
4762 instance->throttlequeuedepth =
4763 MEGASAS_THROTTLE_QUEUE_DEPTH;
4764
Sumant Patro5d018ad2006-10-03 13:13:18 -07004765 /*
4766 * Setup tasklet for cmd completion
4767 */
4768
adam radfordf86c54242011-02-24 20:57:00 -08004769 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05004770 (unsigned long)instance);
4771
adam radford229fe472014-03-10 02:51:56 -07004772 /* Launch SR-IOV heartbeat timer */
4773 if (instance->requestorId) {
4774 if (!megasas_sriov_start_heartbeat(instance, 1))
4775 megasas_start_timer(instance,
4776 &instance->sriov_heartbeat_timer,
4777 megasas_sriov_heartbeat_handler,
4778 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4779 else
4780 instance->skip_heartbeat_timer_del = 1;
4781 }
4782
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004783 return 0;
4784
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304785fail_get_pd_list:
4786 instance->instancet->disable_intr(instance);
adam radfordeb1b1232011-02-24 20:55:56 -08004787fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304788 megasas_destroy_irqs(instance);
4789fail_setup_irqs:
4790 if (instance->msix_vectors)
4791 pci_disable_msix(instance->pdev);
4792 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08004793fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304794 kfree(instance->ctrl_info);
4795 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004796 iounmap(instance->reg_set);
4797
4798 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08004799 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004800
4801 return -EINVAL;
4802}
4803
4804/**
4805 * megasas_release_mfi - Reverses the FW initialization
4806 * @intance: Adapter soft state
4807 */
4808static void megasas_release_mfi(struct megasas_instance *instance)
4809{
adam radford9c915a82010-12-21 13:34:31 -08004810 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004811
adam radford9c915a82010-12-21 13:34:31 -08004812 if (instance->reply_queue)
4813 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004814 instance->reply_queue, instance->reply_queue_h);
4815
4816 megasas_free_cmds(instance);
4817
4818 iounmap(instance->reg_set);
4819
adam radfordb6d5d882010-12-14 18:56:07 -08004820 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004821}
4822
4823/**
4824 * megasas_get_seq_num - Gets latest event sequence numbers
4825 * @instance: Adapter soft state
4826 * @eli: FW event log sequence numbers information
4827 *
4828 * FW maintains a log of all events in a non-volatile area. Upper layers would
4829 * usually find out the latest sequence number of the events, the seq number at
4830 * the boot etc. They would "read" all the events below the latest seq number
4831 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4832 * number), they would subsribe to AEN (asynchronous event notification) and
4833 * wait for the events to happen.
4834 */
4835static int
4836megasas_get_seq_num(struct megasas_instance *instance,
4837 struct megasas_evt_log_info *eli)
4838{
4839 struct megasas_cmd *cmd;
4840 struct megasas_dcmd_frame *dcmd;
4841 struct megasas_evt_log_info *el_info;
4842 dma_addr_t el_info_h = 0;
4843
4844 cmd = megasas_get_cmd(instance);
4845
4846 if (!cmd) {
4847 return -ENOMEM;
4848 }
4849
4850 dcmd = &cmd->frame->dcmd;
4851 el_info = pci_alloc_consistent(instance->pdev,
4852 sizeof(struct megasas_evt_log_info),
4853 &el_info_h);
4854
4855 if (!el_info) {
4856 megasas_return_cmd(instance, cmd);
4857 return -ENOMEM;
4858 }
4859
4860 memset(el_info, 0, sizeof(*el_info));
4861 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4862
4863 dcmd->cmd = MFI_CMD_DCMD;
4864 dcmd->cmd_status = 0x0;
4865 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304866 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004867 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004868 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304869 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4870 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4871 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4872 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004873
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304874 if (megasas_issue_blocked_cmd(instance, cmd, 30))
4875 dev_err(&instance->pdev->dev, "Command timedout"
4876 "from %s\n", __func__);
4877 else {
4878 /*
4879 * Copy the data back into callers buffer
4880 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05304881 eli->newest_seq_num = el_info->newest_seq_num;
4882 eli->oldest_seq_num = el_info->oldest_seq_num;
4883 eli->clear_seq_num = el_info->clear_seq_num;
4884 eli->shutdown_seq_num = el_info->shutdown_seq_num;
4885 eli->boot_seq_num = el_info->boot_seq_num;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304886 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004887
4888 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
4889 el_info, el_info_h);
4890
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304891 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004892
4893 return 0;
4894}
4895
4896/**
4897 * megasas_register_aen - Registers for asynchronous event notification
4898 * @instance: Adapter soft state
4899 * @seq_num: The starting sequence number
4900 * @class_locale: Class of the event
4901 *
4902 * This function subscribes for AEN for events beyond the @seq_num. It requests
4903 * to be notified if and only if the event is of type @class_locale
4904 */
4905static int
4906megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
4907 u32 class_locale_word)
4908{
4909 int ret_val;
4910 struct megasas_cmd *cmd;
4911 struct megasas_dcmd_frame *dcmd;
4912 union megasas_evt_class_locale curr_aen;
4913 union megasas_evt_class_locale prev_aen;
4914
4915 /*
4916 * If there an AEN pending already (aen_cmd), check if the
4917 * class_locale of that pending AEN is inclusive of the new
4918 * AEN request we currently have. If it is, then we don't have
4919 * to do anything. In other words, whichever events the current
4920 * AEN request is subscribing to, have already been subscribed
4921 * to.
4922 *
4923 * If the old_cmd is _not_ inclusive, then we have to abort
4924 * that command, form a class_locale that is superset of both
4925 * old and current and re-issue to the FW
4926 */
4927
4928 curr_aen.word = class_locale_word;
4929
4930 if (instance->aen_cmd) {
4931
Christoph Hellwiga9555532015-04-23 16:34:24 +05304932 prev_aen.word =
4933 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004934
4935 /*
4936 * A class whose enum value is smaller is inclusive of all
4937 * higher values. If a PROGRESS (= -1) was previously
4938 * registered, then a new registration requests for higher
4939 * classes need not be sent to FW. They are automatically
4940 * included.
4941 *
4942 * Locale numbers don't have such hierarchy. They are bitmap
4943 * values
4944 */
4945 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304946 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004947 curr_aen.members.locale)) {
4948 /*
4949 * Previously issued event registration includes
4950 * current request. Nothing to do.
4951 */
4952 return 0;
4953 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304954 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004955
4956 if (prev_aen.members.class < curr_aen.members.class)
4957 curr_aen.members.class = prev_aen.members.class;
4958
4959 instance->aen_cmd->abort_aen = 1;
4960 ret_val = megasas_issue_blocked_abort_cmd(instance,
4961 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304962 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004963
4964 if (ret_val) {
4965 printk(KERN_DEBUG "megasas: Failed to abort "
4966 "previous AEN command\n");
4967 return ret_val;
4968 }
4969 }
4970 }
4971
4972 cmd = megasas_get_cmd(instance);
4973
4974 if (!cmd)
4975 return -ENOMEM;
4976
4977 dcmd = &cmd->frame->dcmd;
4978
4979 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4980
4981 /*
4982 * Prepare DCMD for aen registration
4983 */
4984 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4985
4986 dcmd->cmd = MFI_CMD_DCMD;
4987 dcmd->cmd_status = 0x0;
4988 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304989 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004990 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004991 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304992 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4993 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4994 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04004995 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304996 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4997 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4998 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004999
Yang, Bof4c9a132009-10-06 14:43:28 -06005000 if (instance->aen_cmd != NULL) {
5001 megasas_return_cmd(instance, cmd);
5002 return 0;
5003 }
5004
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005005 /*
5006 * Store reference to the cmd used to register for AEN. When an
5007 * application wants us to register for AEN, we have to abort this
5008 * cmd and re-register with a new EVENT LOCALE supplied by that app
5009 */
5010 instance->aen_cmd = cmd;
5011
5012 /*
5013 * Issue the aen registration frame
5014 */
adam radford9c915a82010-12-21 13:34:31 -08005015 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005016
5017 return 0;
5018}
5019
5020/**
5021 * megasas_start_aen - Subscribes to AEN during driver load time
5022 * @instance: Adapter soft state
5023 */
5024static int megasas_start_aen(struct megasas_instance *instance)
5025{
5026 struct megasas_evt_log_info eli;
5027 union megasas_evt_class_locale class_locale;
5028
5029 /*
5030 * Get the latest sequence number from FW
5031 */
5032 memset(&eli, 0, sizeof(eli));
5033
5034 if (megasas_get_seq_num(instance, &eli))
5035 return -1;
5036
5037 /*
5038 * Register AEN with FW for latest sequence number plus 1
5039 */
5040 class_locale.members.reserved = 0;
5041 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5042 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5043
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305044 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305045 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305046 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005047}
5048
5049/**
5050 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5051 * @instance: Adapter soft state
5052 */
5053static int megasas_io_attach(struct megasas_instance *instance)
5054{
5055 struct Scsi_Host *host = instance->host;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305056 u32 error;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005057
5058 /*
5059 * Export parameters required by SCSI mid-layer
5060 */
5061 host->irq = instance->pdev->irq;
5062 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305063 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005064 host->this_id = instance->init_id;
5065 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005066
5067 if (instance->fw_support_ieee)
5068 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5069
Yang, Bo1fd10682010-10-12 07:18:50 -06005070 /*
5071 * Check if the module parameter value for max_sectors can be used
5072 */
5073 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5074 instance->max_sectors_per_req = max_sectors;
5075 else {
5076 if (max_sectors) {
5077 if (((instance->pdev->device ==
5078 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5079 (instance->pdev->device ==
5080 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5081 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5082 instance->max_sectors_per_req = max_sectors;
5083 } else {
5084 printk(KERN_INFO "megasas: max_sectors should be > 0"
5085 "and <= %d (or < 1MB for GEN2 controller)\n",
5086 instance->max_sectors_per_req);
5087 }
5088 }
5089 }
5090
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005091 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005092 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005093 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5094 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5095 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005096 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005097
adam radford9c915a82010-12-21 13:34:31 -08005098 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07005099 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005100 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305101 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5102 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08005103 host->hostt->eh_device_reset_handler = NULL;
5104 host->hostt->eh_bus_reset_handler = NULL;
5105 }
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305106 error = scsi_init_shared_tag_map(host, host->can_queue);
5107 if (error) {
5108 dev_err(&instance->pdev->dev,
5109 "Failed to shared tag from %s %d\n",
5110 __func__, __LINE__);
5111 return -ENODEV;
5112 }
adam radford9c915a82010-12-21 13:34:31 -08005113
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005114 /*
5115 * Notify the mid-layer about the new controller
5116 */
5117 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305118 dev_err(&instance->pdev->dev,
5119 "Failed to add host from %s %d\n",
5120 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005121 return -ENODEV;
5122 }
5123
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005124 return 0;
5125}
5126
bo yang31ea7082007-11-07 12:09:50 -05005127static int
5128megasas_set_dma_mask(struct pci_dev *pdev)
5129{
5130 /*
5131 * All our contollers are capable of performing 64-bit DMA
5132 */
5133 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005134 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005135
Yang Hongyang284901a2009-04-06 19:01:15 -07005136 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005137 goto fail_set_dma_mask;
5138 }
5139 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005140 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005141 goto fail_set_dma_mask;
5142 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305143 /*
5144 * Ensure that all data structures are allocated in 32-bit
5145 * memory.
5146 */
5147 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5148 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5149 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5150 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5151 dev_info(&pdev->dev, "set 32bit DMA mask"
5152 "and 32 bit consistent mask\n");
5153 else
5154 goto fail_set_dma_mask;
5155 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305156
bo yang31ea7082007-11-07 12:09:50 -05005157 return 0;
5158
5159fail_set_dma_mask:
5160 return 1;
5161}
5162
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005163/**
5164 * megasas_probe_one - PCI hotplug entry point
5165 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005166 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005167 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005168static int megasas_probe_one(struct pci_dev *pdev,
5169 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005170{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305171 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005172 struct Scsi_Host *host;
5173 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005174 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305175 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005176
5177 /* Reset MSI-X in the kdump kernel */
5178 if (reset_devices) {
5179 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5180 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005181 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005182 &control);
5183 if (control & PCI_MSIX_FLAGS_ENABLE) {
5184 dev_info(&pdev->dev, "resetting MSI-X\n");
5185 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005186 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005187 control &
5188 ~PCI_MSIX_FLAGS_ENABLE);
5189 }
5190 }
5191 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005192
5193 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005194 * PCI prepping: enable device set bus mastering and dma mask
5195 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005196 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005197
5198 if (rval) {
5199 return rval;
5200 }
5201
5202 pci_set_master(pdev);
5203
bo yang31ea7082007-11-07 12:09:50 -05005204 if (megasas_set_dma_mask(pdev))
5205 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005206
5207 host = scsi_host_alloc(&megasas_template,
5208 sizeof(struct megasas_instance));
5209
5210 if (!host) {
5211 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
5212 goto fail_alloc_instance;
5213 }
5214
5215 instance = (struct megasas_instance *)host->hostdata;
5216 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04005217 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08005218 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005219
adam radford9c915a82010-12-21 13:34:31 -08005220 switch (instance->pdev->device) {
5221 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005222 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005223 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305224 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005225 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305226 instance->ctrl_context_pages =
5227 get_order(sizeof(struct fusion_context));
5228 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5229 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005230 if (!instance->ctrl_context) {
5231 printk(KERN_DEBUG "megasas: Failed to allocate "
5232 "memory for Fusion context info\n");
5233 goto fail_alloc_dma_buf;
5234 }
5235 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305236 memset(fusion, 0,
5237 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
adam radford9c915a82010-12-21 13:34:31 -08005238 }
5239 break;
5240 default: /* For all other supported controllers */
5241
5242 instance->producer =
5243 pci_alloc_consistent(pdev, sizeof(u32),
5244 &instance->producer_h);
5245 instance->consumer =
5246 pci_alloc_consistent(pdev, sizeof(u32),
5247 &instance->consumer_h);
5248
5249 if (!instance->producer || !instance->consumer) {
5250 printk(KERN_DEBUG "megasas: Failed to allocate"
5251 "memory for producer, consumer\n");
5252 goto fail_alloc_dma_buf;
5253 }
5254
5255 *instance->producer = 0;
5256 *instance->consumer = 0;
5257 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005258 }
5259
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305260 instance->system_info_buf = pci_zalloc_consistent(pdev,
5261 sizeof(struct MR_DRV_SYSTEM_INFO),
5262 &instance->system_info_h);
5263
5264 if (!instance->system_info_buf)
5265 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5266
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305267 /* Crash dump feature related initialisation*/
5268 instance->drv_buf_index = 0;
5269 instance->drv_buf_alloc = 0;
5270 instance->crash_dump_fw_support = 0;
5271 instance->crash_dump_app_support = 0;
5272 instance->fw_crash_state = UNAVAILABLE;
5273 spin_lock_init(&instance->crashdump_lock);
5274 instance->crash_dump_buf = NULL;
5275
5276 if (!reset_devices)
5277 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5278 CRASH_DMA_BUF_SIZE,
5279 &instance->crash_dump_h);
5280 if (!instance->crash_dump_buf)
5281 dev_err(&instance->pdev->dev, "Can't allocate Firmware "
5282 "crash dump DMA buffer\n");
5283
Yang, Boc3518832009-10-06 14:18:02 -06005284 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005285 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005286 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005287 instance->issuepend_done = 1;
5288 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305289 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005290
5291 instance->evt_detail = pci_alloc_consistent(pdev,
5292 sizeof(struct
5293 megasas_evt_detail),
5294 &instance->evt_detail_h);
5295
5296 if (!instance->evt_detail) {
5297 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
5298 "event detail structure\n");
5299 goto fail_alloc_dma_buf;
5300 }
5301
5302 /*
5303 * Initialize locks and queues
5304 */
5305 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005306 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005307
Sumant Patroe4a082c2006-05-30 12:03:37 -07005308 atomic_set(&instance->fw_outstanding,0);
5309
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005310 init_waitqueue_head(&instance->int_cmd_wait_q);
5311 init_waitqueue_head(&instance->abort_cmd_wait_q);
5312
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305313 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005314 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005315 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005316
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005317 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08005318 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005319
5320 /*
5321 * Initialize PCI related and misc parameters
5322 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005323 instance->host = host;
5324 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5325 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305326 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005327
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305328
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005329 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305330 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005331 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005332
Sumant Patro658dced2006-10-03 13:09:14 -07005333 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005334 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005335 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005336 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005337 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305338 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005339
adam radford36807e62011-10-08 18:15:06 -07005340 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005341 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305342 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305343 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08005344 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305345 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5346 } else
adam radford9c915a82010-12-21 13:34:31 -08005347 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005348
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005349 /*
adam radford0a770662011-02-24 20:56:12 -08005350 * Initialize MFI Firmware
5351 */
5352 if (megasas_init_fw(instance))
5353 goto fail_init_mfi;
5354
adam radford229fe472014-03-10 02:51:56 -07005355 if (instance->requestorId) {
5356 if (instance->PlasmaFW111) {
5357 instance->vf_affiliation_111 =
5358 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5359 &instance->vf_affiliation_111_h);
5360 if (!instance->vf_affiliation_111)
5361 printk(KERN_WARNING "megasas: Can't allocate "
5362 "memory for VF affiliation buffer\n");
5363 } else {
5364 instance->vf_affiliation =
5365 pci_alloc_consistent(pdev,
5366 (MAX_LOGICAL_DRIVES + 1) *
5367 sizeof(struct MR_LD_VF_AFFILIATION),
5368 &instance->vf_affiliation_h);
5369 if (!instance->vf_affiliation)
5370 printk(KERN_WARNING "megasas: Can't allocate "
5371 "memory for VF affiliation buffer\n");
5372 }
5373 }
5374
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005375 /*
5376 * Store instance in PCI softstate
5377 */
5378 pci_set_drvdata(pdev, instance);
5379
5380 /*
5381 * Add this controller to megasas_mgmt_info structure so that it
5382 * can be exported to management applications
5383 */
5384 megasas_mgmt_info.count++;
5385 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5386 megasas_mgmt_info.max_index++;
5387
5388 /*
adam radford541f90b2011-05-11 18:34:29 -07005389 * Register with SCSI mid-layer
5390 */
5391 if (megasas_io_attach(instance))
5392 goto fail_io_attach;
5393
5394 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305395 /*
5396 * Trigger SCSI to scan our drives
5397 */
5398 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005399
5400 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005401 * Initiate AEN (Asynchronous Event Notification)
5402 */
5403 if (megasas_start_aen(instance)) {
5404 printk(KERN_DEBUG "megasas: start aen failed\n");
5405 goto fail_start_aen;
5406 }
5407
Adam Radford9ea81f82014-07-09 15:17:57 -07005408 /* Get current SR-IOV LD/VF affiliation */
5409 if (instance->requestorId)
5410 megasas_get_ld_vf_affiliation(instance, 1);
5411
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005412 return 0;
5413
5414 fail_start_aen:
5415 fail_io_attach:
5416 megasas_mgmt_info.count--;
5417 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5418 megasas_mgmt_info.max_index--;
5419
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305420 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305421 megasas_destroy_irqs(instance);
5422
adam radford36807e62011-10-08 18:15:06 -07005423 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005424 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305425 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5426 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radfordeb1b1232011-02-24 20:55:56 -08005427 megasas_release_fusion(instance);
5428 else
5429 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005430 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005431 pci_disable_msix(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305432fail_init_mfi:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005433 fail_alloc_dma_buf:
5434 if (instance->evt_detail)
5435 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5436 instance->evt_detail,
5437 instance->evt_detail_h);
5438
adam radfordeb1b1232011-02-24 20:55:56 -08005439 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005440 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5441 instance->producer_h);
5442 if (instance->consumer)
5443 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5444 instance->consumer_h);
5445 scsi_host_put(host);
5446
5447 fail_alloc_instance:
5448 fail_set_dma_mask:
5449 pci_disable_device(pdev);
5450
5451 return -ENODEV;
5452}
5453
5454/**
5455 * megasas_flush_cache - Requests FW to flush all its caches
5456 * @instance: Adapter soft state
5457 */
5458static void megasas_flush_cache(struct megasas_instance *instance)
5459{
5460 struct megasas_cmd *cmd;
5461 struct megasas_dcmd_frame *dcmd;
5462
bo yang39a98552010-09-22 22:36:29 -04005463 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5464 return;
5465
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005466 cmd = megasas_get_cmd(instance);
5467
5468 if (!cmd)
5469 return;
5470
5471 dcmd = &cmd->frame->dcmd;
5472
5473 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5474
5475 dcmd->cmd = MFI_CMD_DCMD;
5476 dcmd->cmd_status = 0x0;
5477 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305478 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005479 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005480 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005481 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305482 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005483 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5484
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305485 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5486 dev_err(&instance->pdev->dev, "Command timedout"
5487 " from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005488
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305489 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005490
5491 return;
5492}
5493
5494/**
5495 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5496 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05005497 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005498 */
bo yang31ea7082007-11-07 12:09:50 -05005499static void megasas_shutdown_controller(struct megasas_instance *instance,
5500 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005501{
5502 struct megasas_cmd *cmd;
5503 struct megasas_dcmd_frame *dcmd;
5504
bo yang39a98552010-09-22 22:36:29 -04005505 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5506 return;
5507
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005508 cmd = megasas_get_cmd(instance);
5509
5510 if (!cmd)
5511 return;
5512
5513 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305514 megasas_issue_blocked_abort_cmd(instance,
5515 instance->aen_cmd, 30);
adam radford9c915a82010-12-21 13:34:31 -08005516 if (instance->map_update_cmd)
5517 megasas_issue_blocked_abort_cmd(instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305518 instance->map_update_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005519 dcmd = &cmd->frame->dcmd;
5520
5521 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5522
5523 dcmd->cmd = MFI_CMD_DCMD;
5524 dcmd->cmd_status = 0x0;
5525 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305526 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005527 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005528 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005529 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305530 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005531
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305532 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5533 dev_err(&instance->pdev->dev, "Command timedout"
5534 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005535
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305536 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005537
5538 return;
5539}
5540
Jiri Slaby33139b22008-05-01 17:56:02 +02005541#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005542/**
bo yangad84db22007-11-09 04:40:16 -05005543 * megasas_suspend - driver suspend entry point
5544 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05005545 * @state: PCI power state to suspend routine
5546 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005547static int
bo yang31ea7082007-11-07 12:09:50 -05005548megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5549{
5550 struct Scsi_Host *host;
5551 struct megasas_instance *instance;
5552
5553 instance = pci_get_drvdata(pdev);
5554 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06005555 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05005556
adam radford229fe472014-03-10 02:51:56 -07005557 /* Shutdown SR-IOV heartbeat timer */
5558 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5559 del_timer_sync(&instance->sriov_heartbeat_timer);
5560
bo yang31ea7082007-11-07 12:09:50 -05005561 megasas_flush_cache(instance);
5562 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005563
5564 /* cancel the delayed work if this work still in queue */
5565 if (instance->ev != NULL) {
5566 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005567 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005568 instance->ev = NULL;
5569 }
5570
bo yang31ea7082007-11-07 12:09:50 -05005571 tasklet_kill(&instance->isr_tasklet);
5572
5573 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305574 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005575
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305576 megasas_destroy_irqs(instance);
5577
adam radfordc8e858f2011-10-08 18:15:13 -07005578 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005579 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05005580
5581 pci_save_state(pdev);
5582 pci_disable_device(pdev);
5583
5584 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5585
5586 return 0;
5587}
5588
5589/**
5590 * megasas_resume- driver resume entry point
5591 * @pdev: PCI device structure
5592 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005593static int
bo yang31ea7082007-11-07 12:09:50 -05005594megasas_resume(struct pci_dev *pdev)
5595{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305596 int rval;
bo yang31ea7082007-11-07 12:09:50 -05005597 struct Scsi_Host *host;
5598 struct megasas_instance *instance;
5599
5600 instance = pci_get_drvdata(pdev);
5601 host = instance->host;
5602 pci_set_power_state(pdev, PCI_D0);
5603 pci_enable_wake(pdev, PCI_D0, 0);
5604 pci_restore_state(pdev);
5605
5606 /*
5607 * PCI prepping: enable device set bus mastering and dma mask
5608 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005609 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05005610
5611 if (rval) {
5612 printk(KERN_ERR "megasas: Enable device failed\n");
5613 return rval;
5614 }
5615
5616 pci_set_master(pdev);
5617
5618 if (megasas_set_dma_mask(pdev))
5619 goto fail_set_dma_mask;
5620
5621 /*
5622 * Initialize MFI Firmware
5623 */
5624
bo yang31ea7082007-11-07 12:09:50 -05005625 atomic_set(&instance->fw_outstanding, 0);
5626
5627 /*
5628 * We expect the FW state to be READY
5629 */
adam radford058a8fa2011-10-08 18:14:27 -07005630 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05005631 goto fail_ready_state;
5632
adam radford3f1abce2011-05-11 18:33:47 -07005633 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02005634 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005635 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5636 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02005637 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07005638
adam radford9c915a82010-12-21 13:34:31 -08005639 switch (instance->pdev->device) {
5640 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005641 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005642 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305643 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005644 {
5645 megasas_reset_reply_desc(instance);
5646 if (megasas_ioc_init_fusion(instance)) {
5647 megasas_free_cmds(instance);
5648 megasas_free_cmds_fusion(instance);
5649 goto fail_init_mfi;
5650 }
5651 if (!megasas_get_map_info(instance))
5652 megasas_sync_map_info(instance);
5653 }
5654 break;
5655 default:
5656 *instance->producer = 0;
5657 *instance->consumer = 0;
5658 if (megasas_issue_init_mfi(instance))
5659 goto fail_init_mfi;
5660 break;
5661 }
bo yang31ea7082007-11-07 12:09:50 -05005662
adam radford9c915a82010-12-21 13:34:31 -08005663 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5664 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05005665
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305666 if (instance->msix_vectors ?
5667 megasas_setup_irqs_msix(instance, 0) :
5668 megasas_setup_irqs_ioapic(instance))
5669 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05005670
adam radford229fe472014-03-10 02:51:56 -07005671 /* Re-launch SR-IOV heartbeat timer */
5672 if (instance->requestorId) {
5673 if (!megasas_sriov_start_heartbeat(instance, 0))
5674 megasas_start_timer(instance,
5675 &instance->sriov_heartbeat_timer,
5676 megasas_sriov_heartbeat_handler,
5677 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305678 else {
adam radford229fe472014-03-10 02:51:56 -07005679 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305680 goto fail_init_mfi;
5681 }
adam radford229fe472014-03-10 02:51:56 -07005682 }
5683
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305684 instance->instancet->enable_intr(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06005685 instance->unload = 0;
5686
adam radford541f90b2011-05-11 18:34:29 -07005687 /*
5688 * Initiate AEN (Asynchronous Event Notification)
5689 */
5690 if (megasas_start_aen(instance))
5691 printk(KERN_ERR "megasas: Start AEN failed\n");
5692
bo yang31ea7082007-11-07 12:09:50 -05005693 return 0;
5694
bo yang31ea7082007-11-07 12:09:50 -05005695fail_init_mfi:
5696 if (instance->evt_detail)
5697 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5698 instance->evt_detail,
5699 instance->evt_detail_h);
5700
5701 if (instance->producer)
5702 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5703 instance->producer_h);
5704 if (instance->consumer)
5705 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5706 instance->consumer_h);
5707 scsi_host_put(host);
5708
5709fail_set_dma_mask:
5710fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02005711fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05005712
5713 pci_disable_device(pdev);
5714
5715 return -ENODEV;
5716}
Jiri Slaby33139b22008-05-01 17:56:02 +02005717#else
5718#define megasas_suspend NULL
5719#define megasas_resume NULL
5720#endif
bo yang31ea7082007-11-07 12:09:50 -05005721
5722/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005723 * megasas_detach_one - PCI hot"un"plug entry point
5724 * @pdev: PCI device structure
5725 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005726static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005727{
5728 int i;
5729 struct Scsi_Host *host;
5730 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08005731 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005732
5733 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06005734 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005735 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08005736 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005737
adam radford229fe472014-03-10 02:51:56 -07005738 /* Shutdown SR-IOV heartbeat timer */
5739 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5740 del_timer_sync(&instance->sriov_heartbeat_timer);
5741
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305742 if (instance->fw_crash_state != UNAVAILABLE)
5743 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005744 scsi_remove_host(instance->host);
5745 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05005746 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005747
5748 /* cancel the delayed work if this work still in queue*/
5749 if (instance->ev != NULL) {
5750 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005751 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005752 instance->ev = NULL;
5753 }
5754
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305755 /* cancel all wait events */
5756 wake_up_all(&instance->int_cmd_wait_q);
5757
Sumant Patro5d018ad2006-10-03 13:13:18 -07005758 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005759
5760 /*
5761 * Take the instance off the instance array. Note that we will not
5762 * decrement the max_index. We let this array be sparse array
5763 */
5764 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5765 if (megasas_mgmt_info.instance[i] == instance) {
5766 megasas_mgmt_info.count--;
5767 megasas_mgmt_info.instance[i] = NULL;
5768
5769 break;
5770 }
5771 }
5772
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305773 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005774
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305775 megasas_destroy_irqs(instance);
5776
adam radfordc8e858f2011-10-08 18:15:13 -07005777 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005778 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005779
adam radford9c915a82010-12-21 13:34:31 -08005780 switch (instance->pdev->device) {
5781 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005782 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005783 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305784 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005785 megasas_release_fusion(instance);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305786 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08005787 if (fusion->ld_map[i])
5788 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305789 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08005790 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305791 fusion->ld_map_phys[i]);
5792 if (fusion->ld_drv_map[i])
5793 free_pages((ulong)fusion->ld_drv_map[i],
5794 fusion->drv_map_pages);
5795 }
5796 free_pages((ulong)instance->ctrl_context,
5797 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005798 break;
5799 default:
5800 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08005801 pci_free_consistent(pdev, sizeof(u32),
5802 instance->producer,
5803 instance->producer_h);
5804 pci_free_consistent(pdev, sizeof(u32),
5805 instance->consumer,
5806 instance->consumer_h);
5807 break;
5808 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005809
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305810 kfree(instance->ctrl_info);
5811
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05305812 if (instance->evt_detail)
5813 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5814 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07005815
5816 if (instance->vf_affiliation)
5817 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5818 sizeof(struct MR_LD_VF_AFFILIATION),
5819 instance->vf_affiliation,
5820 instance->vf_affiliation_h);
5821
5822 if (instance->vf_affiliation_111)
5823 pci_free_consistent(pdev,
5824 sizeof(struct MR_LD_VF_AFFILIATION_111),
5825 instance->vf_affiliation_111,
5826 instance->vf_affiliation_111_h);
5827
5828 if (instance->hb_host_mem)
5829 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5830 instance->hb_host_mem,
5831 instance->hb_host_mem_h);
5832
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305833 if (instance->crash_dump_buf)
5834 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5835 instance->crash_dump_buf, instance->crash_dump_h);
5836
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05305837 if (instance->system_info_buf)
5838 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
5839 instance->system_info_buf, instance->system_info_h);
5840
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005841 scsi_host_put(host);
5842
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005843 pci_disable_device(pdev);
5844
5845 return;
5846}
5847
5848/**
5849 * megasas_shutdown - Shutdown entry point
5850 * @device: Generic device structure
5851 */
5852static void megasas_shutdown(struct pci_dev *pdev)
5853{
5854 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07005855
Yang, Bo0c79e682009-10-06 14:47:35 -06005856 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005857 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07005858 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305859 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305860 megasas_destroy_irqs(instance);
5861
adam radfordc8e858f2011-10-08 18:15:13 -07005862 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07005863 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005864}
5865
5866/**
5867 * megasas_mgmt_open - char node "open" entry point
5868 */
5869static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5870{
5871 /*
5872 * Allow only those users with admin rights
5873 */
5874 if (!capable(CAP_SYS_ADMIN))
5875 return -EACCES;
5876
5877 return 0;
5878}
5879
5880/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005881 * megasas_mgmt_fasync - Async notifier registration from applications
5882 *
5883 * This function adds the calling process to a driver global queue. When an
5884 * event occurs, SIGIO will be sent to all processes in this queue.
5885 */
5886static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5887{
5888 int rc;
5889
Arjan van de Ven0b950672006-01-11 13:16:10 +01005890 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005891
5892 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
5893
Arjan van de Ven0b950672006-01-11 13:16:10 +01005894 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005895
5896 if (rc >= 0) {
5897 /* For sanity check when we get ioctl */
5898 filep->private_data = filep;
5899 return 0;
5900 }
5901
5902 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
5903
5904 return rc;
5905}
5906
5907/**
Yang, Boc3518832009-10-06 14:18:02 -06005908 * megasas_mgmt_poll - char node "poll" entry point
5909 * */
5910static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
5911{
5912 unsigned int mask;
5913 unsigned long flags;
5914 poll_wait(file, &megasas_poll_wait, wait);
5915 spin_lock_irqsave(&poll_aen_lock, flags);
5916 if (megasas_poll_wait_aen)
5917 mask = (POLLIN | POLLRDNORM);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305918
Yang, Boc3518832009-10-06 14:18:02 -06005919 else
5920 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305921 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06005922 spin_unlock_irqrestore(&poll_aen_lock, flags);
5923 return mask;
5924}
5925
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305926/*
5927 * megasas_set_crash_dump_params_ioctl:
5928 * Send CRASH_DUMP_MODE DCMD to all controllers
5929 * @cmd: MFI command frame
5930 */
5931
5932static int megasas_set_crash_dump_params_ioctl(
5933 struct megasas_cmd *cmd)
5934{
5935 struct megasas_instance *local_instance;
5936 int i, error = 0;
5937 int crash_support;
5938
5939 crash_support = cmd->frame->dcmd.mbox.w[0];
5940
5941 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5942 local_instance = megasas_mgmt_info.instance[i];
5943 if (local_instance && local_instance->crash_dump_drv_support) {
5944 if ((local_instance->adprecovery ==
5945 MEGASAS_HBA_OPERATIONAL) &&
5946 !megasas_set_crash_dump_params(local_instance,
5947 crash_support)) {
5948 local_instance->crash_dump_app_support =
5949 crash_support;
5950 dev_info(&local_instance->pdev->dev,
5951 "Application firmware crash "
5952 "dump mode set success\n");
5953 error = 0;
5954 } else {
5955 dev_info(&local_instance->pdev->dev,
5956 "Application firmware crash "
5957 "dump mode set failed\n");
5958 error = -1;
5959 }
5960 }
5961 }
5962 return error;
5963}
5964
Yang, Boc3518832009-10-06 14:18:02 -06005965/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005966 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
5967 * @instance: Adapter soft state
5968 * @argp: User's ioctl packet
5969 */
5970static int
5971megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
5972 struct megasas_iocpacket __user * user_ioc,
5973 struct megasas_iocpacket *ioc)
5974{
5975 struct megasas_sge32 *kern_sge32;
5976 struct megasas_cmd *cmd;
5977 void *kbuff_arr[MAX_IOCTL_SGE];
5978 dma_addr_t buf_handle = 0;
5979 int error = 0, i;
5980 void *sense = NULL;
5981 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06005982 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005983
5984 memset(kbuff_arr, 0, sizeof(kbuff_arr));
5985
5986 if (ioc->sge_count > MAX_IOCTL_SGE) {
5987 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
5988 ioc->sge_count, MAX_IOCTL_SGE);
5989 return -EINVAL;
5990 }
5991
5992 cmd = megasas_get_cmd(instance);
5993 if (!cmd) {
5994 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
5995 return -ENOMEM;
5996 }
5997
5998 /*
5999 * User's IOCTL packet has 2 frames (maximum). Copy those two
6000 * frames into our cmd's frames. cmd->frame's context will get
6001 * overwritten when we copy from user's frames. So set that value
6002 * alone separately
6003 */
6004 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306005 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006006 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306007 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6008 MFI_FRAME_SGL64 |
6009 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006010
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306011 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6012 error = megasas_set_crash_dump_params_ioctl(cmd);
6013 megasas_return_cmd(instance, cmd);
6014 return error;
6015 }
6016
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006017 /*
6018 * The management interface between applications and the fw uses
6019 * MFI frames. E.g, RAID configuration changes, LD property changes
6020 * etc are accomplishes through different kinds of MFI frames. The
6021 * driver needs to care only about substituting user buffers with
6022 * kernel buffers in SGLs. The location of SGL is embedded in the
6023 * struct iocpacket itself.
6024 */
6025 kern_sge32 = (struct megasas_sge32 *)
6026 ((unsigned long)cmd->frame + ioc->sgl_off);
6027
6028 /*
6029 * For each user buffer, create a mirror buffer and copy in
6030 */
6031 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006032 if (!ioc->sgl[i].iov_len)
6033 continue;
6034
Sumant Patro9f35fa82007-02-14 12:55:45 -08006035 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006036 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006037 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006038 if (!kbuff_arr[i]) {
6039 printk(KERN_DEBUG "megasas: Failed to alloc "
6040 "kernel SGL buffer for IOCTL \n");
6041 error = -ENOMEM;
6042 goto out;
6043 }
6044
6045 /*
6046 * We don't change the dma_coherent_mask, so
6047 * pci_alloc_consistent only returns 32bit addresses
6048 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306049 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6050 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006051
6052 /*
6053 * We created a kernel buffer corresponding to the
6054 * user buffer. Now copy in from the user buffer
6055 */
6056 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6057 (u32) (ioc->sgl[i].iov_len))) {
6058 error = -EFAULT;
6059 goto out;
6060 }
6061 }
6062
6063 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006064 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6065 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006066 if (!sense) {
6067 error = -ENOMEM;
6068 goto out;
6069 }
6070
6071 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006072 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306073 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006074 }
6075
6076 /*
6077 * Set the sync_cmd flag so that the ISR knows not to complete this
6078 * cmd to the SCSI mid-layer
6079 */
6080 cmd->sync_cmd = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306081 megasas_issue_blocked_cmd(instance, cmd, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006082 cmd->sync_cmd = 0;
6083
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306084 if (instance->unload == 1) {
6085 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6086 "don't submit data to application\n");
6087 goto out;
6088 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006089 /*
6090 * copy out the kernel buffers to user buffers
6091 */
6092 for (i = 0; i < ioc->sge_count; i++) {
6093 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6094 ioc->sgl[i].iov_len)) {
6095 error = -EFAULT;
6096 goto out;
6097 }
6098 }
6099
6100 /*
6101 * copy out the sense
6102 */
6103 if (ioc->sense_len) {
6104 /*
bo yangb70a41e2008-03-18 03:13:06 -04006105 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006106 * sense buffer address
6107 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006108 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6109 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006110
bo yangb70a41e2008-03-18 03:13:06 -04006111 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6112 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05006113 printk(KERN_ERR "megasas: Failed to copy out to user "
6114 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006115 error = -EFAULT;
6116 goto out;
6117 }
6118 }
6119
6120 /*
6121 * copy the status codes returned by the fw
6122 */
6123 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6124 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6125 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
6126 error = -EFAULT;
6127 }
6128
6129 out:
6130 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006131 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006132 sense, sense_handle);
6133 }
6134
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006135 for (i = 0; i < ioc->sge_count; i++) {
6136 if (kbuff_arr[i])
6137 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306138 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006139 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306140 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306141 kbuff_arr[i] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006142 }
6143
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306144 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006145 return error;
6146}
6147
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006148static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6149{
6150 struct megasas_iocpacket __user *user_ioc =
6151 (struct megasas_iocpacket __user *)arg;
6152 struct megasas_iocpacket *ioc;
6153 struct megasas_instance *instance;
6154 int error;
bo yang39a98552010-09-22 22:36:29 -04006155 int i;
6156 unsigned long flags;
6157 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006158
6159 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6160 if (!ioc)
6161 return -ENOMEM;
6162
6163 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6164 error = -EFAULT;
6165 goto out_kfree_ioc;
6166 }
6167
6168 instance = megasas_lookup_instance(ioc->host_no);
6169 if (!instance) {
6170 error = -ENODEV;
6171 goto out_kfree_ioc;
6172 }
6173
adam radford229fe472014-03-10 02:51:56 -07006174 /* Adjust ioctl wait time for VF mode */
6175 if (instance->requestorId)
6176 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6177
6178 /* Block ioctls in VF mode */
6179 if (instance->requestorId && !allow_vf_ioctls) {
6180 error = -ENODEV;
6181 goto out_kfree_ioc;
6182 }
6183
bo yang39a98552010-09-22 22:36:29 -04006184 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6185 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006186 error = -ENODEV;
6187 goto out_kfree_ioc;
6188 }
6189
6190 if (instance->unload == 1) {
6191 error = -ENODEV;
6192 goto out_kfree_ioc;
6193 }
6194
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006195 if (down_interruptible(&instance->ioctl_sem)) {
6196 error = -ERESTARTSYS;
6197 goto out_kfree_ioc;
6198 }
bo yang39a98552010-09-22 22:36:29 -04006199
6200 for (i = 0; i < wait_time; i++) {
6201
6202 spin_lock_irqsave(&instance->hba_lock, flags);
6203 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6204 spin_unlock_irqrestore(&instance->hba_lock, flags);
6205 break;
6206 }
6207 spin_unlock_irqrestore(&instance->hba_lock, flags);
6208
6209 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6210 printk(KERN_NOTICE "megasas: waiting"
6211 "for controller reset to finish\n");
6212 }
6213
6214 msleep(1000);
6215 }
6216
6217 spin_lock_irqsave(&instance->hba_lock, flags);
6218 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6219 spin_unlock_irqrestore(&instance->hba_lock, flags);
6220
6221 printk(KERN_ERR "megaraid_sas: timed out while"
6222 "waiting for HBA to recover\n");
6223 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006224 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006225 }
6226 spin_unlock_irqrestore(&instance->hba_lock, flags);
6227
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006228 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Dan Carpenterc64e4832013-04-16 10:44:19 +03006229 out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006230 up(&instance->ioctl_sem);
6231
6232 out_kfree_ioc:
6233 kfree(ioc);
6234 return error;
6235}
6236
6237static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6238{
6239 struct megasas_instance *instance;
6240 struct megasas_aen aen;
6241 int error;
bo yang39a98552010-09-22 22:36:29 -04006242 int i;
6243 unsigned long flags;
6244 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006245
6246 if (file->private_data != file) {
6247 printk(KERN_DEBUG "megasas: fasync_helper was not "
6248 "called first\n");
6249 return -EINVAL;
6250 }
6251
6252 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6253 return -EFAULT;
6254
6255 instance = megasas_lookup_instance(aen.host_no);
6256
6257 if (!instance)
6258 return -ENODEV;
6259
bo yang39a98552010-09-22 22:36:29 -04006260 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6261 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006262 }
6263
6264 if (instance->unload == 1) {
6265 return -ENODEV;
6266 }
6267
bo yang39a98552010-09-22 22:36:29 -04006268 for (i = 0; i < wait_time; i++) {
6269
6270 spin_lock_irqsave(&instance->hba_lock, flags);
6271 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6272 spin_unlock_irqrestore(&instance->hba_lock,
6273 flags);
6274 break;
6275 }
6276
6277 spin_unlock_irqrestore(&instance->hba_lock, flags);
6278
6279 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6280 printk(KERN_NOTICE "megasas: waiting for"
6281 "controller reset to finish\n");
6282 }
6283
6284 msleep(1000);
6285 }
6286
6287 spin_lock_irqsave(&instance->hba_lock, flags);
6288 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6289 spin_unlock_irqrestore(&instance->hba_lock, flags);
6290 printk(KERN_ERR "megaraid_sas: timed out while waiting"
6291 "for HBA to recover.\n");
6292 return -ENODEV;
6293 }
6294 spin_unlock_irqrestore(&instance->hba_lock, flags);
6295
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006296 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006297 error = megasas_register_aen(instance, aen.seq_num,
6298 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006299 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006300 return error;
6301}
6302
6303/**
6304 * megasas_mgmt_ioctl - char node ioctl entry point
6305 */
6306static long
6307megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6308{
6309 switch (cmd) {
6310 case MEGASAS_IOC_FIRMWARE:
6311 return megasas_mgmt_ioctl_fw(file, arg);
6312
6313 case MEGASAS_IOC_GET_AEN:
6314 return megasas_mgmt_ioctl_aen(file, arg);
6315 }
6316
6317 return -ENOTTY;
6318}
6319
6320#ifdef CONFIG_COMPAT
6321static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6322{
6323 struct compat_megasas_iocpacket __user *cioc =
6324 (struct compat_megasas_iocpacket __user *)arg;
6325 struct megasas_iocpacket __user *ioc =
6326 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6327 int i;
6328 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006329 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006330
Jeff Garzik83aabc12006-10-04 06:34:03 -04006331 if (clear_user(ioc, sizeof(*ioc)))
6332 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006333
6334 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6335 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6336 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6337 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6338 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6339 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6340 return -EFAULT;
6341
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006342 /*
6343 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6344 * sense_len is not null, so prepare the 64bit value under
6345 * the same condition.
6346 */
6347 if (ioc->sense_len) {
6348 void __user **sense_ioc_ptr =
6349 (void __user **)(ioc->frame.raw + ioc->sense_off);
6350 compat_uptr_t *sense_cioc_ptr =
6351 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6352 if (get_user(ptr, sense_cioc_ptr) ||
6353 put_user(compat_ptr(ptr), sense_ioc_ptr))
6354 return -EFAULT;
6355 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006356
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006357 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006358 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6359 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6360 copy_in_user(&ioc->sgl[i].iov_len,
6361 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6362 return -EFAULT;
6363 }
6364
6365 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6366
6367 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6368 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6369 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6370 return -EFAULT;
6371 }
6372 return error;
6373}
6374
6375static long
6376megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6377 unsigned long arg)
6378{
6379 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006380 case MEGASAS_IOC_FIRMWARE32:
6381 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006382 case MEGASAS_IOC_GET_AEN:
6383 return megasas_mgmt_ioctl_aen(file, arg);
6384 }
6385
6386 return -ENOTTY;
6387}
6388#endif
6389
6390/*
6391 * File operations structure for management interface
6392 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006393static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006394 .owner = THIS_MODULE,
6395 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006396 .fasync = megasas_mgmt_fasync,
6397 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006398 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006399#ifdef CONFIG_COMPAT
6400 .compat_ioctl = megasas_mgmt_compat_ioctl,
6401#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006402 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006403};
6404
6405/*
6406 * PCI hotplug support registration structure
6407 */
6408static struct pci_driver megasas_pci_driver = {
6409
6410 .name = "megaraid_sas",
6411 .id_table = megasas_pci_table,
6412 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006413 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006414 .suspend = megasas_suspend,
6415 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006416 .shutdown = megasas_shutdown,
6417};
6418
6419/*
6420 * Sysfs driver attributes
6421 */
6422static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6423{
6424 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6425 MEGASAS_VERSION);
6426}
6427
6428static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6429
6430static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306431megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6432{
6433 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6434 MEGASAS_RELDATE);
6435}
6436
6437static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6438
6439static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06006440megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6441{
6442 return sprintf(buf, "%u\n", support_poll_for_event);
6443}
6444
6445static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6446 megasas_sysfs_show_support_poll_for_event, NULL);
6447
Yang, Bo837f5fe2010-10-11 06:59:20 -06006448 static ssize_t
6449megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6450{
6451 return sprintf(buf, "%u\n", support_device_change);
6452}
6453
6454static DRIVER_ATTR(support_device_change, S_IRUGO,
6455 megasas_sysfs_show_support_device_change, NULL);
6456
Yang, Bo72c4fd32009-10-06 14:20:59 -06006457static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07006458megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6459{
bo yangad84db22007-11-09 04:40:16 -05006460 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07006461}
6462
6463static ssize_t
6464megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6465{
6466 int retval = count;
6467 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
6468 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6469 retval = -EINVAL;
6470 }
6471 return retval;
6472}
6473
Joe Malicki66dca9b2008-08-14 17:14:48 -04006474static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05006475 megasas_sysfs_set_dbg_lvl);
6476
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006477static void
6478megasas_aen_polling(struct work_struct *work)
6479{
6480 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006481 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006482 struct megasas_instance *instance = ev->instance;
6483 union megasas_evt_class_locale class_locale;
6484 struct Scsi_Host *host;
6485 struct scsi_device *sdev1;
6486 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006487 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006488 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07006489 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006490 int error;
6491
6492 if (!instance) {
6493 printk(KERN_ERR "invalid instance!\n");
6494 kfree(ev);
6495 return;
6496 }
adam radford229fe472014-03-10 02:51:56 -07006497
6498 /* Adjust event workqueue thread wait time for VF mode */
6499 if (instance->requestorId)
6500 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6501
6502 /* Don't run the event workqueue thread if OCR is running */
6503 for (i = 0; i < wait_time; i++) {
6504 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6505 break;
6506 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6507 printk(KERN_NOTICE "megasas: %s waiting for "
6508 "controller reset to finish for scsi%d\n",
6509 __func__, instance->host->host_no);
6510 }
6511 msleep(1000);
6512 }
6513
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006514 instance->ev = NULL;
6515 host = instance->host;
6516 if (instance->evt_detail) {
6517
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306518 switch (le32_to_cpu(instance->evt_detail->code)) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006519 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07006520 if (megasas_get_pd_list(instance) == 0) {
6521 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6522 for (j = 0;
6523 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6524 j++) {
6525
6526 pd_index =
6527 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6528
6529 sdev1 =
6530 scsi_device_lookup(host, i, j, 0);
6531
6532 if (instance->pd_list[pd_index].driveState
6533 == MR_PD_STATE_SYSTEM) {
6534 if (!sdev1) {
6535 scsi_add_device(host, i, j, 0);
6536 }
6537
6538 if (sdev1)
6539 scsi_device_put(sdev1);
6540 }
6541 }
6542 }
6543 }
6544 doscan = 0;
6545 break;
6546
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006547 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07006548 if (megasas_get_pd_list(instance) == 0) {
Yang, Boc9786842009-12-06 08:39:25 -07006549 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6550 for (j = 0;
6551 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6552 j++) {
6553
6554 pd_index =
6555 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6556
6557 sdev1 =
6558 scsi_device_lookup(host, i, j, 0);
6559
6560 if (instance->pd_list[pd_index].driveState
6561 == MR_PD_STATE_SYSTEM) {
6562 if (sdev1) {
6563 scsi_device_put(sdev1);
6564 }
6565 } else {
6566 if (sdev1) {
6567 scsi_remove_device(sdev1);
6568 scsi_device_put(sdev1);
6569 }
6570 }
6571 }
6572 }
6573 }
6574 doscan = 0;
6575 break;
6576
6577 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08006578 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07006579 case MR_EVT_LD_DELETED:
adam radford229fe472014-03-10 02:51:56 -07006580 if (!instance->requestorId ||
6581 (instance->requestorId &&
6582 megasas_get_ld_vf_affiliation(instance, 0))) {
6583 if (megasas_ld_list_query(instance,
6584 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6585 megasas_get_ld_list(instance);
6586 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6587 for (j = 0;
6588 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6589 j++) {
Yang, Boc9786842009-12-06 08:39:25 -07006590
adam radford229fe472014-03-10 02:51:56 -07006591 ld_index =
6592 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006593
adam radford229fe472014-03-10 02:51:56 -07006594 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07006595
adam radford229fe472014-03-10 02:51:56 -07006596 if (instance->ld_ids[ld_index]
6597 != 0xff) {
6598 if (sdev1)
6599 scsi_device_put(sdev1);
6600 } else {
6601 if (sdev1) {
6602 scsi_remove_device(sdev1);
6603 scsi_device_put(sdev1);
6604 }
Yang, Boc9786842009-12-06 08:39:25 -07006605 }
6606 }
adam radford229fe472014-03-10 02:51:56 -07006607 }
6608 doscan = 0;
6609 }
6610 break;
6611 case MR_EVT_LD_CREATED:
6612 if (!instance->requestorId ||
6613 (instance->requestorId &&
6614 megasas_get_ld_vf_affiliation(instance, 0))) {
6615 if (megasas_ld_list_query(instance,
6616 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6617 megasas_get_ld_list(instance);
6618 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6619 for (j = 0;
6620 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6621 j++) {
6622 ld_index =
6623 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6624
6625 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6626
6627 if (instance->ld_ids[ld_index]
6628 != 0xff) {
6629 if (!sdev1)
6630 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6631 }
6632 if (sdev1)
6633 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006634 }
6635 }
adam radford229fe472014-03-10 02:51:56 -07006636 doscan = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006637 }
Yang, Boc9786842009-12-06 08:39:25 -07006638 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006639 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07006640 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08006641 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006642 doscan = 1;
6643 break;
6644 default:
6645 doscan = 0;
6646 break;
6647 }
6648 } else {
6649 printk(KERN_ERR "invalid evt_detail!\n");
6650 kfree(ev);
6651 return;
6652 }
6653
6654 if (doscan) {
adam radford229fe472014-03-10 02:51:56 -07006655 printk(KERN_INFO "megaraid_sas: scanning for scsi%d...\n",
6656 instance->host->host_no);
Hannes Reinecke58968fc2014-01-16 11:25:36 +01006657 if (megasas_get_pd_list(instance) == 0) {
6658 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6659 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6660 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6661 sdev1 = scsi_device_lookup(host, i, j, 0);
6662 if (instance->pd_list[pd_index].driveState ==
6663 MR_PD_STATE_SYSTEM) {
6664 if (!sdev1) {
6665 scsi_add_device(host, i, j, 0);
6666 }
6667 if (sdev1)
6668 scsi_device_put(sdev1);
6669 } else {
6670 if (sdev1) {
6671 scsi_remove_device(sdev1);
6672 scsi_device_put(sdev1);
6673 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006674 }
6675 }
6676 }
6677 }
Yang, Boc9786842009-12-06 08:39:25 -07006678
adam radford229fe472014-03-10 02:51:56 -07006679 if (!instance->requestorId ||
6680 (instance->requestorId &&
6681 megasas_get_ld_vf_affiliation(instance, 0))) {
6682 if (megasas_ld_list_query(instance,
6683 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6684 megasas_get_ld_list(instance);
6685 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6686 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6687 j++) {
6688 ld_index =
6689 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006690
adam radford229fe472014-03-10 02:51:56 -07006691 sdev1 = scsi_device_lookup(host,
6692 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6693 if (instance->ld_ids[ld_index]
6694 != 0xff) {
6695 if (!sdev1)
6696 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6697 else
6698 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006699 } else {
adam radford229fe472014-03-10 02:51:56 -07006700 if (sdev1) {
6701 scsi_remove_device(sdev1);
6702 scsi_device_put(sdev1);
6703 }
Yang, Boc9786842009-12-06 08:39:25 -07006704 }
6705 }
6706 }
6707 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006708 }
6709
6710 if ( instance->aen_cmd != NULL ) {
6711 kfree(ev);
6712 return ;
6713 }
6714
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306715 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006716
6717 /* Register AEN with FW for latest sequence number plus 1 */
6718 class_locale.members.reserved = 0;
6719 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6720 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6721 mutex_lock(&instance->aen_mutex);
6722 error = megasas_register_aen(instance, seq_num,
6723 class_locale.word);
6724 mutex_unlock(&instance->aen_mutex);
6725
6726 if (error)
6727 printk(KERN_ERR "register aen failed error %x\n", error);
6728
6729 kfree(ev);
6730}
6731
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006732/**
6733 * megasas_init - Driver load entry point
6734 */
6735static int __init megasas_init(void)
6736{
6737 int rval;
6738
6739 /*
6740 * Announce driver version and other information
6741 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05306742 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006743
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07006744 spin_lock_init(&poll_aen_lock);
6745
Yang, Bo72c4fd32009-10-06 14:20:59 -06006746 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06006747 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006748
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006749 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6750
6751 /*
6752 * Register character device node
6753 */
6754 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6755
6756 if (rval < 0) {
6757 printk(KERN_DEBUG "megasas: failed to open device node\n");
6758 return rval;
6759 }
6760
6761 megasas_mgmt_majorno = rval;
6762
6763 /*
6764 * Register ourselves as PCI hotplug module
6765 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00006766 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006767
6768 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09006769 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04006770 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006771 }
6772
Jeff Garzik83aabc12006-10-04 06:34:03 -04006773 rval = driver_create_file(&megasas_pci_driver.driver,
6774 &driver_attr_version);
6775 if (rval)
6776 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006777
6778 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306779 &driver_attr_release_date);
6780 if (rval)
6781 goto err_dcf_rel_date;
6782
6783 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06006784 &driver_attr_support_poll_for_event);
6785 if (rval)
6786 goto err_dcf_support_poll_for_event;
6787
Jeff Garzik83aabc12006-10-04 06:34:03 -04006788 rval = driver_create_file(&megasas_pci_driver.driver,
6789 &driver_attr_dbg_lvl);
6790 if (rval)
6791 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05006792 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006793 &driver_attr_support_device_change);
6794 if (rval)
6795 goto err_dcf_support_device_change;
6796
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006797 return rval;
bo yangad84db22007-11-09 04:40:16 -05006798
Yang, Bo837f5fe2010-10-11 06:59:20 -06006799err_dcf_support_device_change:
6800 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05006801 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006802err_dcf_dbg_lvl:
6803 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06006804 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06006805err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306806 driver_remove_file(&megasas_pci_driver.driver,
6807 &driver_attr_release_date);
6808err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04006809 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6810err_dcf_attr_ver:
6811 pci_unregister_driver(&megasas_pci_driver);
6812err_pcidrv:
6813 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08006814 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006815}
6816
6817/**
6818 * megasas_exit - Driver unload entry point
6819 */
6820static void __exit megasas_exit(void)
6821{
Sumant Patro658dced2006-10-03 13:09:14 -07006822 driver_remove_file(&megasas_pci_driver.driver,
6823 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006824 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006825 &driver_attr_support_poll_for_event);
6826 driver_remove_file(&megasas_pci_driver.driver,
6827 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05306828 driver_remove_file(&megasas_pci_driver.driver,
6829 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006830 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006831
6832 pci_unregister_driver(&megasas_pci_driver);
6833 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6834}
6835
6836module_init(megasas_init);
6837module_exit(megasas_exit);