blob: bf31dde554cecd63deaec2215cdcb941d73b6eac [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);
Sumant Patro3d6d1742006-12-29 08:13:54 -080097MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050098MODULE_DESCRIPTION("LSI 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);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530218 atomic_set(&cmd->mfi_mpt_pthr, MFI_MPT_DETACHED);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400219 } else {
220 printk(KERN_ERR "megasas: Command pool empty!\n");
221 }
222
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530223 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400224 return cmd;
225}
226
227/**
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530228 * __megasas_return_cmd - Return a cmd to free command pool
229 * @instance: Adapter soft state
230 * @cmd: Command packet to be returned to free command pool
231 */
232inline void
233__megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
234{
235 /*
236 * Don't go ahead and free the MFI frame, if corresponding
237 * MPT frame is not freed(valid for only fusion adapters).
238 * In case of MFI adapters, anyways for any allocated MFI
239 * frame will have cmd->mfi_mpt_mpthr set to MFI_MPT_DETACHED
240 */
241 if (atomic_read(&cmd->mfi_mpt_pthr) != MFI_MPT_DETACHED)
242 return;
243
244 cmd->scmd = NULL;
245 cmd->frame_count = 0;
246 cmd->is_wait_event = 0;
247 cmd->mpt_pthr_cmd_blocked = NULL;
248
249 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
250 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
251 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
252 (reset_devices))
253 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
254
255 atomic_set(&cmd->mfi_mpt_pthr, MFI_LIST_ADDED);
256 list_add(&cmd->list, (&instance->cmd_pool)->next);
257}
258
259/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400260 * megasas_return_cmd - Return a cmd to free command pool
261 * @instance: Adapter soft state
262 * @cmd: Command packet to be returned to free command pool
263 */
adam radford9c915a82010-12-21 13:34:31 -0800264inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400265megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
266{
267 unsigned long flags;
268
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530269 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
270 __megasas_return_cmd(instance, cmd);
271 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400272}
273
Sumant Patro1341c932006-01-25 12:02:40 -0800274
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400275/**
adam radford0d490162010-12-14 19:17:17 -0800276* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800277* (deviceid : 1064R, PERC5) controllers
278*/
279
280/**
281 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400282 * @regs: MFI register set
283 */
284static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530285megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400286{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530287 struct megasas_register_set __iomem *regs;
288 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400289 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400290
291 /* Dummy readl to force pci flush */
292 readl(&regs->outbound_intr_mask);
293}
294
295/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700296 * megasas_disable_intr_xscale -Disables interrupt
297 * @regs: MFI register set
298 */
299static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530300megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700301{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530302 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700303 u32 mask = 0x1f;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530304 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700305 writel(mask, &regs->outbound_intr_mask);
306 /* Dummy readl to force pci flush */
307 readl(&regs->outbound_intr_mask);
308}
309
310/**
Sumant Patro1341c932006-01-25 12:02:40 -0800311 * megasas_read_fw_status_reg_xscale - returns the current FW status value
312 * @regs: MFI register set
313 */
314static u32
315megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
316{
317 return readl(&(regs)->outbound_msg_0);
318}
319/**
320 * megasas_clear_interrupt_xscale - Check & clear interrupt
321 * @regs: MFI register set
322 */
adam radford0d490162010-12-14 19:17:17 -0800323static int
Sumant Patro1341c932006-01-25 12:02:40 -0800324megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
325{
326 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400327 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800328 /*
329 * Check if it is our interrupt
330 */
331 status = readl(&regs->outbound_intr_status);
332
bo yang39a98552010-09-22 22:36:29 -0400333 if (status & MFI_OB_INTR_STATUS_MASK)
334 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
335 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
336 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800337
338 /*
339 * Clear the interrupt by writing back the same value
340 */
bo yang39a98552010-09-22 22:36:29 -0400341 if (mfiStatus)
342 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800343
Yang, Bo06f579d2008-08-10 12:42:37 -0700344 /* Dummy readl to force pci flush */
345 readl(&regs->outbound_intr_status);
346
bo yang39a98552010-09-22 22:36:29 -0400347 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800348}
349
350/**
351 * megasas_fire_cmd_xscale - Sends command to the FW
352 * @frame_phys_addr : Physical address of cmd
353 * @frame_count : Number of frames for the command
354 * @regs : MFI register set
355 */
adam radford0d490162010-12-14 19:17:17 -0800356static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600357megasas_fire_cmd_xscale(struct megasas_instance *instance,
358 dma_addr_t frame_phys_addr,
359 u32 frame_count,
360 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800361{
bo yang39a98552010-09-22 22:36:29 -0400362 unsigned long flags;
363 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800364 writel((frame_phys_addr >> 3)|(frame_count),
365 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400366 spin_unlock_irqrestore(&instance->hba_lock, flags);
367}
368
369/**
370 * megasas_adp_reset_xscale - For controller reset
371 * @regs: MFI register set
372 */
373static int
374megasas_adp_reset_xscale(struct megasas_instance *instance,
375 struct megasas_register_set __iomem *regs)
376{
377 u32 i;
378 u32 pcidata;
379 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
380
381 for (i = 0; i < 3; i++)
382 msleep(1000); /* sleep for 3 secs */
383 pcidata = 0;
384 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
385 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
386 if (pcidata & 0x2) {
387 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
388 pcidata &= ~0x2;
389 pci_write_config_dword(instance->pdev,
390 MFI_1068_PCSR_OFFSET, pcidata);
391
392 for (i = 0; i < 2; i++)
393 msleep(1000); /* need to wait 2 secs again */
394
395 pcidata = 0;
396 pci_read_config_dword(instance->pdev,
397 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
398 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
399 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
400 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
401 pcidata = 0;
402 pci_write_config_dword(instance->pdev,
403 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
404 }
405 }
406 return 0;
407}
408
409/**
410 * megasas_check_reset_xscale - For controller reset check
411 * @regs: MFI register set
412 */
413static int
414megasas_check_reset_xscale(struct megasas_instance *instance,
415 struct megasas_register_set __iomem *regs)
416{
bo yang39a98552010-09-22 22:36:29 -0400417
418 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530419 (le32_to_cpu(*instance->consumer) ==
420 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400421 return 1;
bo yang39a98552010-09-22 22:36:29 -0400422 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800423}
424
425static struct megasas_instance_template megasas_instance_template_xscale = {
426
427 .fire_cmd = megasas_fire_cmd_xscale,
428 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700429 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800430 .clear_intr = megasas_clear_intr_xscale,
431 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400432 .adp_reset = megasas_adp_reset_xscale,
433 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800434 .service_isr = megasas_isr,
435 .tasklet = megasas_complete_cmd_dpc,
436 .init_adapter = megasas_init_adapter_mfi,
437 .build_and_issue_cmd = megasas_build_and_issue_cmd,
438 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800439};
440
441/**
adam radford0d490162010-12-14 19:17:17 -0800442* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800443* to xscale (deviceid : 1064R, PERC5) controllers
444*/
445
446/**
adam radford0d490162010-12-14 19:17:17 -0800447* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800448* controllers
449*/
450
451/**
452 * megasas_enable_intr_ppc - Enables interrupts
453 * @regs: MFI register set
454 */
455static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530456megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800457{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530458 struct megasas_register_set __iomem *regs;
459 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800460 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800461
bo yang39a98552010-09-22 22:36:29 -0400462 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800463
464 /* Dummy readl to force pci flush */
465 readl(&regs->outbound_intr_mask);
466}
467
468/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700469 * megasas_disable_intr_ppc - Disable interrupt
470 * @regs: MFI register set
471 */
472static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530473megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700474{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530475 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700476 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530477 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700478 writel(mask, &regs->outbound_intr_mask);
479 /* Dummy readl to force pci flush */
480 readl(&regs->outbound_intr_mask);
481}
482
483/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800484 * megasas_read_fw_status_reg_ppc - returns the current FW status value
485 * @regs: MFI register set
486 */
487static u32
488megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
489{
490 return readl(&(regs)->outbound_scratch_pad);
491}
492
493/**
494 * megasas_clear_interrupt_ppc - Check & clear interrupt
495 * @regs: MFI register set
496 */
adam radford0d490162010-12-14 19:17:17 -0800497static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800498megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
499{
adam radford3cc6851f92011-05-11 18:34:52 -0700500 u32 status, mfiStatus = 0;
501
Sumant Patrof9876f02006-02-03 15:34:35 -0800502 /*
503 * Check if it is our interrupt
504 */
505 status = readl(&regs->outbound_intr_status);
506
adam radford3cc6851f92011-05-11 18:34:52 -0700507 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
508 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
509
510 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
511 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800512
513 /*
514 * Clear the interrupt by writing back the same value
515 */
516 writel(status, &regs->outbound_doorbell_clear);
517
Yang, Bo06f579d2008-08-10 12:42:37 -0700518 /* Dummy readl to force pci flush */
519 readl(&regs->outbound_doorbell_clear);
520
adam radford3cc6851f92011-05-11 18:34:52 -0700521 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800522}
adam radford3cc6851f92011-05-11 18:34:52 -0700523
Sumant Patrof9876f02006-02-03 15:34:35 -0800524/**
525 * megasas_fire_cmd_ppc - Sends command to the FW
526 * @frame_phys_addr : Physical address of cmd
527 * @frame_count : Number of frames for the command
528 * @regs : MFI register set
529 */
adam radford0d490162010-12-14 19:17:17 -0800530static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600531megasas_fire_cmd_ppc(struct megasas_instance *instance,
532 dma_addr_t frame_phys_addr,
533 u32 frame_count,
534 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800535{
bo yang39a98552010-09-22 22:36:29 -0400536 unsigned long flags;
537 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800538 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800539 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400540 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800541}
542
bo yang39a98552010-09-22 22:36:29 -0400543/**
bo yang39a98552010-09-22 22:36:29 -0400544 * megasas_check_reset_ppc - For controller reset check
545 * @regs: MFI register set
546 */
547static int
548megasas_check_reset_ppc(struct megasas_instance *instance,
549 struct megasas_register_set __iomem *regs)
550{
adam radford3cc6851f92011-05-11 18:34:52 -0700551 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
552 return 1;
553
bo yang39a98552010-09-22 22:36:29 -0400554 return 0;
555}
adam radford3cc6851f92011-05-11 18:34:52 -0700556
Sumant Patrof9876f02006-02-03 15:34:35 -0800557static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800558
Sumant Patrof9876f02006-02-03 15:34:35 -0800559 .fire_cmd = megasas_fire_cmd_ppc,
560 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700561 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800562 .clear_intr = megasas_clear_intr_ppc,
563 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700564 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400565 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800566 .service_isr = megasas_isr,
567 .tasklet = megasas_complete_cmd_dpc,
568 .init_adapter = megasas_init_adapter_mfi,
569 .build_and_issue_cmd = megasas_build_and_issue_cmd,
570 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800571};
572
573/**
Yang, Bo87911122009-10-06 14:31:54 -0600574 * megasas_enable_intr_skinny - Enables interrupts
575 * @regs: MFI register set
576 */
577static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530578megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600579{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530580 struct megasas_register_set __iomem *regs;
581 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600582 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
583
584 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
585
586 /* Dummy readl to force pci flush */
587 readl(&regs->outbound_intr_mask);
588}
589
590/**
591 * megasas_disable_intr_skinny - Disables interrupt
592 * @regs: MFI register set
593 */
594static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530595megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600596{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530597 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600598 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530599 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600600 writel(mask, &regs->outbound_intr_mask);
601 /* Dummy readl to force pci flush */
602 readl(&regs->outbound_intr_mask);
603}
604
605/**
606 * megasas_read_fw_status_reg_skinny - returns the current FW status value
607 * @regs: MFI register set
608 */
609static u32
610megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
611{
612 return readl(&(regs)->outbound_scratch_pad);
613}
614
615/**
616 * megasas_clear_interrupt_skinny - Check & clear interrupt
617 * @regs: MFI register set
618 */
619static int
620megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
621{
622 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800623 u32 mfiStatus = 0;
624
Yang, Bo87911122009-10-06 14:31:54 -0600625 /*
626 * Check if it is our interrupt
627 */
628 status = readl(&regs->outbound_intr_status);
629
630 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400631 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600632 }
633
634 /*
adam radfordebf054b2011-02-24 20:57:15 -0800635 * Check if it is our interrupt
636 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600637 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800638 MFI_STATE_FAULT) {
639 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
640 } else
641 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
642
643 /*
Yang, Bo87911122009-10-06 14:31:54 -0600644 * Clear the interrupt by writing back the same value
645 */
646 writel(status, &regs->outbound_intr_status);
647
648 /*
649 * dummy read to flush PCI
650 */
651 readl(&regs->outbound_intr_status);
652
adam radfordebf054b2011-02-24 20:57:15 -0800653 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600654}
655
656/**
657 * megasas_fire_cmd_skinny - Sends command to the FW
658 * @frame_phys_addr : Physical address of cmd
659 * @frame_count : Number of frames for the command
660 * @regs : MFI register set
661 */
662static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600663megasas_fire_cmd_skinny(struct megasas_instance *instance,
664 dma_addr_t frame_phys_addr,
665 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600666 struct megasas_register_set __iomem *regs)
667{
Yang, Bo0c79e682009-10-06 14:47:35 -0600668 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400669 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530670 writel(upper_32_bits(frame_phys_addr),
671 &(regs)->inbound_high_queue_port);
672 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
673 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400674 spin_unlock_irqrestore(&instance->hba_lock, flags);
675}
676
677/**
bo yang39a98552010-09-22 22:36:29 -0400678 * megasas_check_reset_skinny - For controller reset check
679 * @regs: MFI register set
680 */
681static int
682megasas_check_reset_skinny(struct megasas_instance *instance,
683 struct megasas_register_set __iomem *regs)
684{
adam radford3cc6851f92011-05-11 18:34:52 -0700685 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
686 return 1;
687
bo yang39a98552010-09-22 22:36:29 -0400688 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600689}
690
691static struct megasas_instance_template megasas_instance_template_skinny = {
692
693 .fire_cmd = megasas_fire_cmd_skinny,
694 .enable_intr = megasas_enable_intr_skinny,
695 .disable_intr = megasas_disable_intr_skinny,
696 .clear_intr = megasas_clear_intr_skinny,
697 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800698 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400699 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800700 .service_isr = megasas_isr,
701 .tasklet = megasas_complete_cmd_dpc,
702 .init_adapter = megasas_init_adapter_mfi,
703 .build_and_issue_cmd = megasas_build_and_issue_cmd,
704 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600705};
706
707
708/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700709* The following functions are defined for gen2 (deviceid : 0x78 0x79)
710* controllers
711*/
712
713/**
714 * megasas_enable_intr_gen2 - Enables interrupts
715 * @regs: MFI register set
716 */
717static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530718megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700719{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530720 struct megasas_register_set __iomem *regs;
721 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700722 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
723
724 /* write ~0x00000005 (4 & 1) to the intr mask*/
725 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
726
727 /* Dummy readl to force pci flush */
728 readl(&regs->outbound_intr_mask);
729}
730
731/**
732 * megasas_disable_intr_gen2 - Disables interrupt
733 * @regs: MFI register set
734 */
735static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530736megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700737{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530738 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700739 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530740 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700741 writel(mask, &regs->outbound_intr_mask);
742 /* Dummy readl to force pci flush */
743 readl(&regs->outbound_intr_mask);
744}
745
746/**
747 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
748 * @regs: MFI register set
749 */
750static u32
751megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
752{
753 return readl(&(regs)->outbound_scratch_pad);
754}
755
756/**
757 * megasas_clear_interrupt_gen2 - Check & clear interrupt
758 * @regs: MFI register set
759 */
760static int
761megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
762{
763 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400764 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700765 /*
766 * Check if it is our interrupt
767 */
768 status = readl(&regs->outbound_intr_status);
769
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530770 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400771 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
772 }
773 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
774 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
775 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700776
777 /*
778 * Clear the interrupt by writing back the same value
779 */
bo yang39a98552010-09-22 22:36:29 -0400780 if (mfiStatus)
781 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700782
783 /* Dummy readl to force pci flush */
784 readl(&regs->outbound_intr_status);
785
bo yang39a98552010-09-22 22:36:29 -0400786 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700787}
788/**
789 * megasas_fire_cmd_gen2 - Sends command to the FW
790 * @frame_phys_addr : Physical address of cmd
791 * @frame_count : Number of frames for the command
792 * @regs : MFI register set
793 */
794static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600795megasas_fire_cmd_gen2(struct megasas_instance *instance,
796 dma_addr_t frame_phys_addr,
797 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700798 struct megasas_register_set __iomem *regs)
799{
bo yang39a98552010-09-22 22:36:29 -0400800 unsigned long flags;
801 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700802 writel((frame_phys_addr | (frame_count<<1))|1,
803 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400804 spin_unlock_irqrestore(&instance->hba_lock, flags);
805}
806
807/**
808 * megasas_adp_reset_gen2 - For controller reset
809 * @regs: MFI register set
810 */
811static int
812megasas_adp_reset_gen2(struct megasas_instance *instance,
813 struct megasas_register_set __iomem *reg_set)
814{
815 u32 retry = 0 ;
816 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800817 u32 *seq_offset = &reg_set->seq_offset;
818 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400819
adam radfordebf054b2011-02-24 20:57:15 -0800820 if (instance->instancet == &megasas_instance_template_skinny) {
821 seq_offset = &reg_set->fusion_seq_offset;
822 hostdiag_offset = &reg_set->fusion_host_diag;
823 }
824
825 writel(0, seq_offset);
826 writel(4, seq_offset);
827 writel(0xb, seq_offset);
828 writel(2, seq_offset);
829 writel(7, seq_offset);
830 writel(0xd, seq_offset);
831
bo yang39a98552010-09-22 22:36:29 -0400832 msleep(1000);
833
adam radfordebf054b2011-02-24 20:57:15 -0800834 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400835
836 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
837 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800838 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400839 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
840 retry, HostDiag);
841
842 if (retry++ >= 100)
843 return 1;
844
845 }
846
847 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
848
adam radfordebf054b2011-02-24 20:57:15 -0800849 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400850
851 ssleep(10);
852
adam radfordebf054b2011-02-24 20:57:15 -0800853 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400854 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
855 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800856 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400857 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
858 retry, HostDiag);
859
860 if (retry++ >= 1000)
861 return 1;
862
863 }
864 return 0;
865}
866
867/**
868 * megasas_check_reset_gen2 - For controller reset check
869 * @regs: MFI register set
870 */
871static int
872megasas_check_reset_gen2(struct megasas_instance *instance,
873 struct megasas_register_set __iomem *regs)
874{
Yang, Bo707e09b2010-10-12 07:20:27 -0600875 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
876 return 1;
877 }
878
bo yang39a98552010-09-22 22:36:29 -0400879 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700880}
881
882static struct megasas_instance_template megasas_instance_template_gen2 = {
883
884 .fire_cmd = megasas_fire_cmd_gen2,
885 .enable_intr = megasas_enable_intr_gen2,
886 .disable_intr = megasas_disable_intr_gen2,
887 .clear_intr = megasas_clear_intr_gen2,
888 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400889 .adp_reset = megasas_adp_reset_gen2,
890 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800891 .service_isr = megasas_isr,
892 .tasklet = megasas_complete_cmd_dpc,
893 .init_adapter = megasas_init_adapter_mfi,
894 .build_and_issue_cmd = megasas_build_and_issue_cmd,
895 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700896};
897
898/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800899* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400900* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800901*/
902
adam radford9c915a82010-12-21 13:34:31 -0800903/*
904 * Template added for TB (Fusion)
905 */
906extern struct megasas_instance_template megasas_instance_template_fusion;
907
Sumant Patrof9876f02006-02-03 15:34:35 -0800908/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400909 * megasas_issue_polled - Issues a polling command
910 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800911 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400912 *
913 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
914 */
adam radford9c915a82010-12-21 13:34:31 -0800915int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400916megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
917{
adam radford229fe472014-03-10 02:51:56 -0700918 int seconds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400919
920 struct megasas_header *frame_hdr = &cmd->frame->hdr;
921
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530922 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
923 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400924
925 /*
926 * Issue the frame using inbound queue port
927 */
adam radford9c915a82010-12-21 13:34:31 -0800928 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400929
930 /*
931 * Wait for cmd_status to change
932 */
adam radford229fe472014-03-10 02:51:56 -0700933 if (instance->requestorId)
934 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
935 else
936 seconds = MFI_POLL_TIMEOUT_SECS;
937 return wait_and_poll(instance, cmd, seconds);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400938}
939
940/**
941 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
942 * @instance: Adapter soft state
943 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530944 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400945 *
946 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700947 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400948 * Used to issue ioctl commands.
949 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530950int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400951megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530952 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400953{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530954 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400955 cmd->cmd_status = ENODATA;
956
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530957 cmd->is_wait_event = 1;
adam radford9c915a82010-12-21 13:34:31 -0800958 instance->instancet->issue_dcmd(instance, cmd);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530959 if (timeout) {
960 ret = wait_event_timeout(instance->int_cmd_wait_q,
961 cmd->cmd_status != ENODATA, timeout * HZ);
962 if (!ret)
963 return 1;
964 } else
965 wait_event(instance->int_cmd_wait_q,
966 cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400967
968 return 0;
969}
970
971/**
972 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
973 * @instance: Adapter soft state
974 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530975 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400976 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530977 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400978 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700979 * cmd and waits for return status.
980 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400981 */
982static int
983megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530984 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400985{
986 struct megasas_cmd *cmd;
987 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530988 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989
990 cmd = megasas_get_cmd(instance);
991
992 if (!cmd)
993 return -1;
994
995 abort_fr = &cmd->frame->abort;
996
997 /*
998 * Prepare and issue the abort frame
999 */
1000 abort_fr->cmd = MFI_CMD_ABORT;
1001 abort_fr->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301002 abort_fr->flags = cpu_to_le16(0);
1003 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1004 abort_fr->abort_mfi_phys_addr_lo =
1005 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1006 abort_fr->abort_mfi_phys_addr_hi =
1007 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001008
1009 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com170c2382014-11-17 15:24:23 +05301010 cmd->cmd_status = ENODATA;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001011
adam radford9c915a82010-12-21 13:34:31 -08001012 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001013
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301014 if (timeout) {
1015 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1016 cmd->cmd_status != ENODATA, timeout * HZ);
1017 if (!ret) {
1018 dev_err(&instance->pdev->dev, "Command timedout"
1019 "from %s\n", __func__);
1020 return 1;
1021 }
1022 } else
1023 wait_event(instance->abort_cmd_wait_q,
1024 cmd->cmd_status != ENODATA);
1025
bo yang39a98552010-09-22 22:36:29 -04001026 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001027
1028 megasas_return_cmd(instance, cmd);
1029 return 0;
1030}
1031
1032/**
1033 * megasas_make_sgl32 - Prepares 32-bit SGL
1034 * @instance: Adapter soft state
1035 * @scp: SCSI command from the mid-layer
1036 * @mfi_sgl: SGL to be filled in
1037 *
1038 * If successful, this function returns the number of SG elements. Otherwise,
1039 * it returnes -1.
1040 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001041static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001042megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1043 union megasas_sgl *mfi_sgl)
1044{
1045 int i;
1046 int sge_count;
1047 struct scatterlist *os_sgl;
1048
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001049 sge_count = scsi_dma_map(scp);
1050 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001051
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001052 if (sge_count) {
1053 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301054 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1055 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001056 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001057 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001058 return sge_count;
1059}
1060
1061/**
1062 * megasas_make_sgl64 - Prepares 64-bit SGL
1063 * @instance: Adapter soft state
1064 * @scp: SCSI command from the mid-layer
1065 * @mfi_sgl: SGL to be filled in
1066 *
1067 * If successful, this function returns the number of SG elements. Otherwise,
1068 * it returnes -1.
1069 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001070static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001071megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1072 union megasas_sgl *mfi_sgl)
1073{
1074 int i;
1075 int sge_count;
1076 struct scatterlist *os_sgl;
1077
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001078 sge_count = scsi_dma_map(scp);
1079 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001080
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001081 if (sge_count) {
1082 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301083 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1084 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001085 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001086 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001087 return sge_count;
1088}
1089
Yang, Bof4c9a132009-10-06 14:43:28 -06001090/**
1091 * megasas_make_sgl_skinny - Prepares IEEE SGL
1092 * @instance: Adapter soft state
1093 * @scp: SCSI command from the mid-layer
1094 * @mfi_sgl: SGL to be filled in
1095 *
1096 * If successful, this function returns the number of SG elements. Otherwise,
1097 * it returnes -1.
1098 */
1099static int
1100megasas_make_sgl_skinny(struct megasas_instance *instance,
1101 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1102{
1103 int i;
1104 int sge_count;
1105 struct scatterlist *os_sgl;
1106
1107 sge_count = scsi_dma_map(scp);
1108
1109 if (sge_count) {
1110 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301111 mfi_sgl->sge_skinny[i].length =
1112 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001113 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301114 cpu_to_le64(sg_dma_address(os_sgl));
1115 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001116 }
1117 }
1118 return sge_count;
1119}
1120
Sumant Patrob1df99d2006-10-03 12:40:47 -07001121 /**
1122 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001123 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001124 * @sge_count : number of sg elements
1125 *
1126 * Returns the number of frames required for numnber of sge's (sge_count)
1127 */
1128
Yang, Bof4c9a132009-10-06 14:43:28 -06001129static u32 megasas_get_frame_count(struct megasas_instance *instance,
1130 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001131{
1132 int num_cnt;
1133 int sge_bytes;
1134 u32 sge_sz;
1135 u32 frame_count=0;
1136
1137 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1138 sizeof(struct megasas_sge32);
1139
Yang, Bof4c9a132009-10-06 14:43:28 -06001140 if (instance->flag_ieee) {
1141 sge_sz = sizeof(struct megasas_sge_skinny);
1142 }
1143
Sumant Patrob1df99d2006-10-03 12:40:47 -07001144 /*
bo yangd532dbe2008-03-17 03:36:43 -04001145 * Main frame can contain 2 SGEs for 64-bit SGLs and
1146 * 3 SGEs for 32-bit SGLs for ldio &
1147 * 1 SGEs for 64-bit SGLs and
1148 * 2 SGEs for 32-bit SGLs for pthru frame
1149 */
1150 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001151 if (instance->flag_ieee == 1) {
1152 num_cnt = sge_count - 1;
1153 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001154 num_cnt = sge_count - 1;
1155 else
1156 num_cnt = sge_count - 2;
1157 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001158 if (instance->flag_ieee == 1) {
1159 num_cnt = sge_count - 1;
1160 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001161 num_cnt = sge_count - 2;
1162 else
1163 num_cnt = sge_count - 3;
1164 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001165
1166 if(num_cnt>0){
1167 sge_bytes = sge_sz * num_cnt;
1168
1169 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1170 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1171 }
1172 /* Main frame */
1173 frame_count +=1;
1174
1175 if (frame_count > 7)
1176 frame_count = 8;
1177 return frame_count;
1178}
1179
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001180/**
1181 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1182 * @instance: Adapter soft state
1183 * @scp: SCSI command
1184 * @cmd: Command to be prepared in
1185 *
1186 * This function prepares CDB commands. These are typcially pass-through
1187 * commands to the devices.
1188 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001189static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001190megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1191 struct megasas_cmd *cmd)
1192{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001193 u32 is_logical;
1194 u32 device_id;
1195 u16 flags = 0;
1196 struct megasas_pthru_frame *pthru;
1197
1198 is_logical = MEGASAS_IS_LOGICAL(scp);
1199 device_id = MEGASAS_DEV_INDEX(instance, scp);
1200 pthru = (struct megasas_pthru_frame *)cmd->frame;
1201
1202 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1203 flags = MFI_FRAME_DIR_WRITE;
1204 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1205 flags = MFI_FRAME_DIR_READ;
1206 else if (scp->sc_data_direction == PCI_DMA_NONE)
1207 flags = MFI_FRAME_DIR_NONE;
1208
Yang, Bof4c9a132009-10-06 14:43:28 -06001209 if (instance->flag_ieee == 1) {
1210 flags |= MFI_FRAME_IEEE;
1211 }
1212
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001213 /*
1214 * Prepare the DCDB frame
1215 */
1216 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1217 pthru->cmd_status = 0x0;
1218 pthru->scsi_status = 0x0;
1219 pthru->target_id = device_id;
1220 pthru->lun = scp->device->lun;
1221 pthru->cdb_len = scp->cmd_len;
1222 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001223 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301224 pthru->flags = cpu_to_le16(flags);
1225 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001226
1227 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1228
1229 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001230 * If the command is for the tape device, set the
1231 * pthru timeout to the os layer timeout value.
1232 */
1233 if (scp->device->type == TYPE_TAPE) {
1234 if ((scp->request->timeout / HZ) > 0xFFFF)
1235 pthru->timeout = 0xFFFF;
1236 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301237 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001238 }
1239
1240 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001241 * Construct SGL
1242 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001243 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301244 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001245 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1246 &pthru->sgl);
1247 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301248 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001249 pthru->sge_count = megasas_make_sgl64(instance, scp,
1250 &pthru->sgl);
1251 } else
1252 pthru->sge_count = megasas_make_sgl32(instance, scp,
1253 &pthru->sgl);
1254
Yang, Bobdc6fb82009-12-06 08:30:19 -07001255 if (pthru->sge_count > instance->max_num_sge) {
1256 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1257 pthru->sge_count);
1258 return 0;
1259 }
1260
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001261 /*
1262 * Sense info specific
1263 */
1264 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301265 pthru->sense_buf_phys_addr_hi =
1266 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1267 pthru->sense_buf_phys_addr_lo =
1268 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001269
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001270 /*
1271 * Compute the total number of frames this command consumes. FW uses
1272 * this number to pull sufficient number of frames from host memory.
1273 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001274 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001275 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001276
1277 return cmd->frame_count;
1278}
1279
1280/**
1281 * megasas_build_ldio - Prepares IOs to logical devices
1282 * @instance: Adapter soft state
1283 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001284 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001285 *
1286 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1287 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001288static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001289megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1290 struct megasas_cmd *cmd)
1291{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001292 u32 device_id;
1293 u8 sc = scp->cmnd[0];
1294 u16 flags = 0;
1295 struct megasas_io_frame *ldio;
1296
1297 device_id = MEGASAS_DEV_INDEX(instance, scp);
1298 ldio = (struct megasas_io_frame *)cmd->frame;
1299
1300 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1301 flags = MFI_FRAME_DIR_WRITE;
1302 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1303 flags = MFI_FRAME_DIR_READ;
1304
Yang, Bof4c9a132009-10-06 14:43:28 -06001305 if (instance->flag_ieee == 1) {
1306 flags |= MFI_FRAME_IEEE;
1307 }
1308
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001309 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001310 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001311 */
1312 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1313 ldio->cmd_status = 0x0;
1314 ldio->scsi_status = 0x0;
1315 ldio->target_id = device_id;
1316 ldio->timeout = 0;
1317 ldio->reserved_0 = 0;
1318 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301319 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001320 ldio->start_lba_hi = 0;
1321 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1322
1323 /*
1324 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1325 */
1326 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301327 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1328 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1329 ((u32) scp->cmnd[2] << 8) |
1330 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001331
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301332 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001333 }
1334
1335 /*
1336 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1337 */
1338 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301339 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1340 ((u32) scp->cmnd[7] << 8));
1341 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1342 ((u32) scp->cmnd[3] << 16) |
1343 ((u32) scp->cmnd[4] << 8) |
1344 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001345 }
1346
1347 /*
1348 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1349 */
1350 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301351 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1352 ((u32) scp->cmnd[7] << 16) |
1353 ((u32) scp->cmnd[8] << 8) |
1354 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001355
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301356 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1357 ((u32) scp->cmnd[3] << 16) |
1358 ((u32) scp->cmnd[4] << 8) |
1359 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001360 }
1361
1362 /*
1363 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1364 */
1365 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301366 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1367 ((u32) scp->cmnd[11] << 16) |
1368 ((u32) scp->cmnd[12] << 8) |
1369 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001370
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301371 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1372 ((u32) scp->cmnd[7] << 16) |
1373 ((u32) scp->cmnd[8] << 8) |
1374 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001375
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301376 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1377 ((u32) scp->cmnd[3] << 16) |
1378 ((u32) scp->cmnd[4] << 8) |
1379 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001380
1381 }
1382
1383 /*
1384 * Construct SGL
1385 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001386 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301387 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001388 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1389 &ldio->sgl);
1390 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301391 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001392 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1393 } else
1394 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1395
Yang, Bobdc6fb82009-12-06 08:30:19 -07001396 if (ldio->sge_count > instance->max_num_sge) {
1397 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1398 ldio->sge_count);
1399 return 0;
1400 }
1401
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001402 /*
1403 * Sense info specific
1404 */
1405 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1406 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301407 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001408
Sumant Patrob1df99d2006-10-03 12:40:47 -07001409 /*
1410 * Compute the total number of frames this command consumes. FW uses
1411 * this number to pull sufficient number of frames from host memory.
1412 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001413 cmd->frame_count = megasas_get_frame_count(instance,
1414 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001415
1416 return cmd->frame_count;
1417}
1418
1419/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301420 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1421 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001422 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001423 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001424 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301425inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001426{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301427 int ret;
1428
Sumant Patrocb59aa62006-01-25 11:53:25 -08001429 switch (cmd->cmnd[0]) {
1430 case READ_10:
1431 case WRITE_10:
1432 case READ_12:
1433 case WRITE_12:
1434 case READ_6:
1435 case WRITE_6:
1436 case READ_16:
1437 case WRITE_16:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301438 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1439 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1440 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001441 default:
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301442 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1443 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001444 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301445 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001446}
1447
Sumant Patro658dced2006-10-03 13:09:14 -07001448 /**
1449 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1450 * in FW
1451 * @instance: Adapter soft state
1452 */
1453static inline void
1454megasas_dump_pending_frames(struct megasas_instance *instance)
1455{
1456 struct megasas_cmd *cmd;
1457 int i,n;
1458 union megasas_sgl *mfi_sgl;
1459 struct megasas_io_frame *ldio;
1460 struct megasas_pthru_frame *pthru;
1461 u32 sgcount;
1462 u32 max_cmd = instance->max_fw_cmds;
1463
1464 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1465 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1466 if (IS_DMA64)
1467 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1468 else
1469 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1470
1471 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1472 for (i = 0; i < max_cmd; i++) {
1473 cmd = instance->cmd_list[i];
1474 if(!cmd->scmd)
1475 continue;
1476 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 +05301477 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001478 ldio = (struct megasas_io_frame *)cmd->frame;
1479 mfi_sgl = &ldio->sgl;
1480 sgcount = ldio->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301481 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1482 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1483 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1484 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1485 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001486 }
1487 else {
1488 pthru = (struct megasas_pthru_frame *) cmd->frame;
1489 mfi_sgl = &pthru->sgl;
1490 sgcount = pthru->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301491 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1492 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1493 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1494 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1495 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001496 }
1497 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1498 for (n = 0; n < sgcount; n++){
1499 if (IS_DMA64)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301500 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
1501 le32_to_cpu(mfi_sgl->sge64[n].length),
1502 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001503 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301504 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",
1505 le32_to_cpu(mfi_sgl->sge32[n].length),
1506 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001507 }
1508 }
1509 printk(KERN_ERR "\n");
1510 } /*for max_cmd*/
1511 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1512 for (i = 0; i < max_cmd; i++) {
1513
1514 cmd = instance->cmd_list[i];
1515
1516 if(cmd->sync_cmd == 1){
1517 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1518 }
1519 }
1520 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1521}
1522
adam radfordcd50ba82010-12-21 10:23:23 -08001523u32
1524megasas_build_and_issue_cmd(struct megasas_instance *instance,
1525 struct scsi_cmnd *scmd)
1526{
1527 struct megasas_cmd *cmd;
1528 u32 frame_count;
1529
1530 cmd = megasas_get_cmd(instance);
1531 if (!cmd)
1532 return SCSI_MLQUEUE_HOST_BUSY;
1533
1534 /*
1535 * Logical drive command
1536 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301537 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001538 frame_count = megasas_build_ldio(instance, scmd, cmd);
1539 else
1540 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1541
1542 if (!frame_count)
1543 goto out_return_cmd;
1544
1545 cmd->scmd = scmd;
1546 scmd->SCp.ptr = (char *)cmd;
1547
1548 /*
1549 * Issue the command to the FW
1550 */
1551 atomic_inc(&instance->fw_outstanding);
1552
1553 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1554 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001555
1556 return 0;
1557out_return_cmd:
1558 megasas_return_cmd(instance, cmd);
1559 return 1;
1560}
1561
1562
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001563/**
1564 * megasas_queue_command - Queue entry point
1565 * @scmd: SCSI command to be queued
1566 * @done: Callback entry point
1567 */
1568static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301569megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001570{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001571 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001572 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001573
1574 instance = (struct megasas_instance *)
1575 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001576
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301577 if (instance->unload == 1) {
1578 scmd->result = DID_NO_CONNECT << 16;
1579 scmd->scsi_done(scmd);
1580 return 0;
1581 }
1582
bo yang39a98552010-09-22 22:36:29 -04001583 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001584 return SCSI_MLQUEUE_HOST_BUSY;
1585
bo yang39a98552010-09-22 22:36:29 -04001586 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301587
adam radford229fe472014-03-10 02:51:56 -07001588 /* Check for an mpio path and adjust behavior */
1589 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1590 if (megasas_check_mpio_paths(instance, scmd) ==
1591 (DID_RESET << 16)) {
1592 spin_unlock_irqrestore(&instance->hba_lock, flags);
1593 return SCSI_MLQUEUE_HOST_BUSY;
1594 } else {
1595 spin_unlock_irqrestore(&instance->hba_lock, flags);
1596 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301597 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001598 return 0;
1599 }
1600 }
1601
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301602 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1603 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford229fe472014-03-10 02:51:56 -07001604 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301605 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301606 return 0;
1607 }
1608
bo yang39a98552010-09-22 22:36:29 -04001609 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1610 spin_unlock_irqrestore(&instance->hba_lock, flags);
1611 return SCSI_MLQUEUE_HOST_BUSY;
1612 }
1613
1614 spin_unlock_irqrestore(&instance->hba_lock, flags);
1615
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001616 scmd->result = 0;
1617
Sumant Patrocb59aa62006-01-25 11:53:25 -08001618 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301619 (scmd->device->id >= instance->fw_supported_vd_count ||
1620 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001621 scmd->result = DID_BAD_TARGET << 16;
1622 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001623 }
1624
Sumant Patro02b01e02007-02-14 13:00:55 -08001625 switch (scmd->cmnd[0]) {
1626 case SYNCHRONIZE_CACHE:
1627 /*
1628 * FW takes care of flush cache on its own
1629 * No need to send it down
1630 */
1631 scmd->result = DID_OK << 16;
1632 goto out_done;
1633 default:
1634 break;
1635 }
1636
adam radfordcd50ba82010-12-21 10:23:23 -08001637 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1638 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001639 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001640 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001641
1642 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001643
Sumant Patrocb59aa62006-01-25 11:53:25 -08001644 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301645 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001646 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001647}
1648
Yang, Bo044833b2009-10-06 14:33:06 -06001649static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1650{
1651 int i;
1652
1653 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1654
1655 if ((megasas_mgmt_info.instance[i]) &&
1656 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1657 return megasas_mgmt_info.instance[i];
1658 }
1659
1660 return NULL;
1661}
1662
Christoph Hellwig147aab62006-02-17 12:13:48 +01001663static int megasas_slave_configure(struct scsi_device *sdev)
1664{
Yang, Bo044833b2009-10-06 14:33:06 -06001665 /*
1666 * The RAID firmware may require extended timeouts.
1667 */
1668 blk_queue_rq_timeout(sdev->request_queue,
1669 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301670
Yang, Bo044833b2009-10-06 14:33:06 -06001671 return 0;
1672}
1673
1674static int megasas_slave_alloc(struct scsi_device *sdev)
1675{
1676 u16 pd_index = 0;
1677 struct megasas_instance *instance ;
1678 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301679 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001680 /*
1681 * Open the OS scan to the SYSTEM PD
1682 */
1683 pd_index =
1684 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1685 sdev->id;
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301686 if (instance->pd_list[pd_index].driveState ==
1687 MR_PD_STATE_SYSTEM) {
Yang, Bo044833b2009-10-06 14:33:06 -06001688 return 0;
1689 }
1690 return -ENXIO;
1691 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001692 return 0;
1693}
1694
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301695/*
1696* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1697* kill adapter
1698* @instance: Adapter soft state
1699*
1700*/
1701void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1702{
1703 int i;
1704 struct megasas_cmd *cmd_mfi;
1705 struct megasas_cmd_fusion *cmd_fusion;
1706 struct fusion_context *fusion = instance->ctrl_context;
1707
1708 /* Find all outstanding ioctls */
1709 if (fusion) {
1710 for (i = 0; i < instance->max_fw_cmds; i++) {
1711 cmd_fusion = fusion->cmd_list[i];
1712 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1713 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1714 if (cmd_mfi->sync_cmd &&
1715 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1716 megasas_complete_cmd(instance,
1717 cmd_mfi, DID_OK);
1718 }
1719 }
1720 } else {
1721 for (i = 0; i < instance->max_fw_cmds; i++) {
1722 cmd_mfi = instance->cmd_list[i];
1723 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1724 MFI_CMD_ABORT)
1725 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1726 }
1727 }
1728}
1729
1730
adam radford9c915a82010-12-21 13:34:31 -08001731void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001732{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301733 /* Set critical error to block I/O & ioctls in case caller didn't */
1734 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1735 /* Wait 1 second to ensure IO or ioctls in build have posted */
1736 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001737 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301738 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1739 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1740 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
1741 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1742 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1743 writel(MFI_STOP_ADP,
1744 &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001745 /* Flush */
1746 readl(&instance->reg_set->doorbell);
1747 if (instance->mpio && instance->requestorId)
1748 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001749 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301750 writel(MFI_STOP_ADP,
1751 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001752 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301753 /* Complete outstanding ioctls when adapter is killed */
1754 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001755}
1756
1757 /**
1758 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1759 * restored to max value
1760 * @instance: Adapter soft state
1761 *
1762 */
1763void
1764megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1765{
1766 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301767
adam radford9c915a82010-12-21 13:34:31 -08001768 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001769 && time_after(jiffies, instance->last_time + 5 * HZ)
1770 && atomic_read(&instance->fw_outstanding) <
1771 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001772
1773 spin_lock_irqsave(instance->host->host_lock, flags);
1774 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001775
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301776 instance->host->can_queue = instance->max_scsi_cmds;
adam radford9c915a82010-12-21 13:34:31 -08001777 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001778 }
1779}
1780
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001781/**
bo yang7343eb62007-11-09 04:35:44 -05001782 * megasas_complete_cmd_dpc - Returns FW's controller structure
1783 * @instance_addr: Address of adapter soft state
1784 *
1785 * Tasklet to complete cmds
1786 */
1787static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1788{
1789 u32 producer;
1790 u32 consumer;
1791 u32 context;
1792 struct megasas_cmd *cmd;
1793 struct megasas_instance *instance =
1794 (struct megasas_instance *)instance_addr;
1795 unsigned long flags;
1796
1797 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001798 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001799 return;
1800
1801 spin_lock_irqsave(&instance->completion_lock, flags);
1802
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301803 producer = le32_to_cpu(*instance->producer);
1804 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001805
1806 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301807 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001808 if (context >= instance->max_fw_cmds) {
1809 printk(KERN_ERR "Unexpected context value %x\n",
1810 context);
1811 BUG();
1812 }
bo yang7343eb62007-11-09 04:35:44 -05001813
1814 cmd = instance->cmd_list[context];
1815
1816 megasas_complete_cmd(instance, cmd, DID_OK);
1817
1818 consumer++;
1819 if (consumer == (instance->max_fw_cmds + 1)) {
1820 consumer = 0;
1821 }
1822 }
1823
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301824 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05001825
1826 spin_unlock_irqrestore(&instance->completion_lock, flags);
1827
1828 /*
1829 * Check if we can restore can_queue
1830 */
adam radford9c915a82010-12-21 13:34:31 -08001831 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001832}
1833
adam radford229fe472014-03-10 02:51:56 -07001834/**
1835 * megasas_start_timer - Initializes a timer object
1836 * @instance: Adapter soft state
1837 * @timer: timer object to be initialized
1838 * @fn: timer function
1839 * @interval: time interval between timer function call
1840 *
1841 */
1842void megasas_start_timer(struct megasas_instance *instance,
1843 struct timer_list *timer,
1844 void *fn, unsigned long interval)
1845{
1846 init_timer(timer);
1847 timer->expires = jiffies + interval;
1848 timer->data = (unsigned long)instance;
1849 timer->function = fn;
1850 add_timer(timer);
1851}
1852
Yang, Bo707e09b2010-10-12 07:20:27 -06001853static void
1854megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1855
1856static void
1857process_fw_state_change_wq(struct work_struct *work);
1858
1859void megasas_do_ocr(struct megasas_instance *instance)
1860{
1861 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1862 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1863 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301864 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06001865 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301866 instance->instancet->disable_intr(instance);
Yang, Bo707e09b2010-10-12 07:20:27 -06001867 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1868 instance->issuepend_done = 0;
1869
1870 atomic_set(&instance->fw_outstanding, 0);
1871 megasas_internal_reset_defer_cmds(instance);
1872 process_fw_state_change_wq(&instance->work_init);
1873}
1874
Adam Radford4cbfea82014-07-09 15:17:56 -07001875static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1876 int initial)
adam radford229fe472014-03-10 02:51:56 -07001877{
1878 struct megasas_cmd *cmd;
1879 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07001880 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07001881 dma_addr_t new_affiliation_111_h;
1882 int ld, retval = 0;
1883 u8 thisVf;
1884
1885 cmd = megasas_get_cmd(instance);
1886
1887 if (!cmd) {
Adam Radford4cbfea82014-07-09 15:17:56 -07001888 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation_111:"
1889 "Failed to get cmd for scsi%d.\n",
adam radford229fe472014-03-10 02:51:56 -07001890 instance->host->host_no);
1891 return -ENOMEM;
1892 }
1893
1894 dcmd = &cmd->frame->dcmd;
1895
Adam Radford4cbfea82014-07-09 15:17:56 -07001896 if (!instance->vf_affiliation_111) {
adam radford229fe472014-03-10 02:51:56 -07001897 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1898 "affiliation for scsi%d.\n", instance->host->host_no);
1899 megasas_return_cmd(instance, cmd);
1900 return -ENOMEM;
1901 }
1902
1903 if (initial)
adam radford229fe472014-03-10 02:51:56 -07001904 memset(instance->vf_affiliation_111, 0,
1905 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001906 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07001907 new_affiliation_111 =
1908 pci_alloc_consistent(instance->pdev,
1909 sizeof(struct MR_LD_VF_AFFILIATION_111),
1910 &new_affiliation_111_h);
1911 if (!new_affiliation_111) {
adam radford229fe472014-03-10 02:51:56 -07001912 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
1913 "memory for new affiliation for scsi%d.\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07001914 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07001915 megasas_return_cmd(instance, cmd);
1916 return -ENOMEM;
1917 }
Adam Radford4cbfea82014-07-09 15:17:56 -07001918 memset(new_affiliation_111, 0,
1919 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001920 }
1921
1922 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1923
1924 dcmd->cmd = MFI_CMD_DCMD;
1925 dcmd->cmd_status = 0xFF;
1926 dcmd->sge_count = 1;
1927 dcmd->flags = MFI_FRAME_DIR_BOTH;
1928 dcmd->timeout = 0;
1929 dcmd->pad_0 = 0;
Adam Radford4cbfea82014-07-09 15:17:56 -07001930 dcmd->data_xfer_len = sizeof(struct MR_LD_VF_AFFILIATION_111);
1931 dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111;
adam radford229fe472014-03-10 02:51:56 -07001932
Adam Radford4cbfea82014-07-09 15:17:56 -07001933 if (initial)
1934 dcmd->sgl.sge32[0].phys_addr =
1935 instance->vf_affiliation_111_h;
adam radford229fe472014-03-10 02:51:56 -07001936 else
Adam Radford4cbfea82014-07-09 15:17:56 -07001937 dcmd->sgl.sge32[0].phys_addr = new_affiliation_111_h;
1938
1939 dcmd->sgl.sge32[0].length =
1940 sizeof(struct MR_LD_VF_AFFILIATION_111);
adam radford229fe472014-03-10 02:51:56 -07001941
1942 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
1943 "scsi%d\n", instance->host->host_no);
1944
1945 megasas_issue_blocked_cmd(instance, cmd, 0);
1946
1947 if (dcmd->cmd_status) {
1948 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
1949 " failed with status 0x%x for scsi%d.\n",
1950 dcmd->cmd_status, instance->host->host_no);
1951 retval = 1; /* Do a scan if we couldn't get affiliation */
1952 goto out;
1953 }
1954
1955 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07001956 thisVf = new_affiliation_111->thisVf;
1957 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
1958 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
1959 new_affiliation_111->map[ld].policy[thisVf]) {
1960 printk(KERN_WARNING "megasas: SR-IOV: "
1961 "Got new LD/VF affiliation "
adam radford229fe472014-03-10 02:51:56 -07001962 "for scsi%d.\n",
adam radford229fe472014-03-10 02:51:56 -07001963 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07001964 memcpy(instance->vf_affiliation_111,
1965 new_affiliation_111,
1966 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001967 retval = 1;
1968 goto out;
1969 }
Adam Radford4cbfea82014-07-09 15:17:56 -07001970 }
1971out:
1972 if (new_affiliation_111) {
1973 pci_free_consistent(instance->pdev,
1974 sizeof(struct MR_LD_VF_AFFILIATION_111),
1975 new_affiliation_111,
1976 new_affiliation_111_h);
1977 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301978
1979 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
1980 megasas_return_mfi_mpt_pthr(instance, cmd,
1981 cmd->mpt_pthr_cmd_blocked);
1982 else
1983 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07001984
1985 return retval;
1986}
1987
1988static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
1989 int initial)
1990{
1991 struct megasas_cmd *cmd;
1992 struct megasas_dcmd_frame *dcmd;
1993 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
1994 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
1995 dma_addr_t new_affiliation_h;
1996 int i, j, retval = 0, found = 0, doscan = 0;
1997 u8 thisVf;
1998
1999 cmd = megasas_get_cmd(instance);
2000
2001 if (!cmd) {
2002 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation12: "
2003 "Failed to get cmd for scsi%d.\n",
2004 instance->host->host_no);
2005 return -ENOMEM;
2006 }
2007
2008 dcmd = &cmd->frame->dcmd;
2009
2010 if (!instance->vf_affiliation) {
2011 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
2012 "affiliation for scsi%d.\n", instance->host->host_no);
2013 megasas_return_cmd(instance, cmd);
2014 return -ENOMEM;
2015 }
2016
2017 if (initial)
2018 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2019 sizeof(struct MR_LD_VF_AFFILIATION));
2020 else {
2021 new_affiliation =
2022 pci_alloc_consistent(instance->pdev,
2023 (MAX_LOGICAL_DRIVES + 1) *
2024 sizeof(struct MR_LD_VF_AFFILIATION),
2025 &new_affiliation_h);
2026 if (!new_affiliation) {
2027 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
2028 "memory for new affiliation for scsi%d.\n",
2029 instance->host->host_no);
2030 megasas_return_cmd(instance, cmd);
2031 return -ENOMEM;
2032 }
2033 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2034 sizeof(struct MR_LD_VF_AFFILIATION));
2035 }
2036
2037 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2038
2039 dcmd->cmd = MFI_CMD_DCMD;
2040 dcmd->cmd_status = 0xFF;
2041 dcmd->sge_count = 1;
2042 dcmd->flags = MFI_FRAME_DIR_BOTH;
2043 dcmd->timeout = 0;
2044 dcmd->pad_0 = 0;
2045 dcmd->data_xfer_len = (MAX_LOGICAL_DRIVES + 1) *
2046 sizeof(struct MR_LD_VF_AFFILIATION);
2047 dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS;
2048
2049 if (initial)
2050 dcmd->sgl.sge32[0].phys_addr = instance->vf_affiliation_h;
2051 else
2052 dcmd->sgl.sge32[0].phys_addr = new_affiliation_h;
2053
2054 dcmd->sgl.sge32[0].length = (MAX_LOGICAL_DRIVES + 1) *
2055 sizeof(struct MR_LD_VF_AFFILIATION);
2056
2057 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
2058 "scsi%d\n", instance->host->host_no);
2059
2060 megasas_issue_blocked_cmd(instance, cmd, 0);
2061
2062 if (dcmd->cmd_status) {
2063 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
2064 " failed with status 0x%x for scsi%d.\n",
2065 dcmd->cmd_status, instance->host->host_no);
2066 retval = 1; /* Do a scan if we couldn't get affiliation */
2067 goto out;
2068 }
2069
2070 if (!initial) {
2071 if (!new_affiliation->ldCount) {
2072 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2073 "affiliation for passive path for scsi%d.\n",
2074 instance->host->host_no);
2075 retval = 1;
2076 goto out;
2077 }
2078 newmap = new_affiliation->map;
2079 savedmap = instance->vf_affiliation->map;
2080 thisVf = new_affiliation->thisVf;
2081 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2082 found = 0;
2083 for (j = 0; j < instance->vf_affiliation->ldCount;
2084 j++) {
2085 if (newmap->ref.targetId ==
2086 savedmap->ref.targetId) {
2087 found = 1;
2088 if (newmap->policy[thisVf] !=
2089 savedmap->policy[thisVf]) {
2090 doscan = 1;
2091 goto out;
2092 }
adam radford229fe472014-03-10 02:51:56 -07002093 }
2094 savedmap = (struct MR_LD_VF_MAP *)
2095 ((unsigned char *)savedmap +
2096 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002097 }
2098 if (!found && newmap->policy[thisVf] !=
2099 MR_LD_ACCESS_HIDDEN) {
2100 doscan = 1;
2101 goto out;
2102 }
2103 newmap = (struct MR_LD_VF_MAP *)
2104 ((unsigned char *)newmap + newmap->size);
2105 }
2106
2107 newmap = new_affiliation->map;
2108 savedmap = instance->vf_affiliation->map;
2109
2110 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2111 found = 0;
2112 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2113 if (savedmap->ref.targetId ==
2114 newmap->ref.targetId) {
2115 found = 1;
2116 if (savedmap->policy[thisVf] !=
2117 newmap->policy[thisVf]) {
2118 doscan = 1;
2119 goto out;
2120 }
2121 }
adam radford229fe472014-03-10 02:51:56 -07002122 newmap = (struct MR_LD_VF_MAP *)
2123 ((unsigned char *)newmap +
2124 newmap->size);
2125 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002126 if (!found && savedmap->policy[thisVf] !=
2127 MR_LD_ACCESS_HIDDEN) {
2128 doscan = 1;
2129 goto out;
2130 }
2131 savedmap = (struct MR_LD_VF_MAP *)
2132 ((unsigned char *)savedmap +
2133 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002134 }
2135 }
2136out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002137 if (doscan) {
2138 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2139 "affiliation for scsi%d.\n", instance->host->host_no);
2140 memcpy(instance->vf_affiliation, new_affiliation,
2141 new_affiliation->size);
2142 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002143 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002144
2145 if (new_affiliation)
2146 pci_free_consistent(instance->pdev,
2147 (MAX_LOGICAL_DRIVES + 1) *
2148 sizeof(struct MR_LD_VF_AFFILIATION),
2149 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302150 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2151 megasas_return_mfi_mpt_pthr(instance, cmd,
2152 cmd->mpt_pthr_cmd_blocked);
2153 else
2154 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002155
2156 return retval;
2157}
2158
Adam Radford4cbfea82014-07-09 15:17:56 -07002159/* This function will get the current SR-IOV LD/VF affiliation */
2160static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2161 int initial)
2162{
2163 int retval;
2164
2165 if (instance->PlasmaFW111)
2166 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2167 else
2168 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2169 return retval;
2170}
2171
adam radford229fe472014-03-10 02:51:56 -07002172/* This function will tell FW to start the SR-IOV heartbeat */
2173int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2174 int initial)
2175{
2176 struct megasas_cmd *cmd;
2177 struct megasas_dcmd_frame *dcmd;
2178 int retval = 0;
2179
2180 cmd = megasas_get_cmd(instance);
2181
2182 if (!cmd) {
2183 printk(KERN_DEBUG "megasas: megasas_sriov_start_heartbeat: "
2184 "Failed to get cmd for scsi%d.\n",
2185 instance->host->host_no);
2186 return -ENOMEM;
2187 }
2188
2189 dcmd = &cmd->frame->dcmd;
2190
2191 if (initial) {
2192 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002193 pci_zalloc_consistent(instance->pdev,
2194 sizeof(struct MR_CTRL_HB_HOST_MEM),
2195 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002196 if (!instance->hb_host_mem) {
2197 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate"
2198 " memory for heartbeat host memory for "
2199 "scsi%d.\n", instance->host->host_no);
2200 retval = -ENOMEM;
2201 goto out;
2202 }
adam radford229fe472014-03-10 02:51:56 -07002203 }
2204
2205 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2206
2207 dcmd->mbox.s[0] = sizeof(struct MR_CTRL_HB_HOST_MEM);
2208 dcmd->cmd = MFI_CMD_DCMD;
2209 dcmd->cmd_status = 0xFF;
2210 dcmd->sge_count = 1;
2211 dcmd->flags = MFI_FRAME_DIR_BOTH;
2212 dcmd->timeout = 0;
2213 dcmd->pad_0 = 0;
2214 dcmd->data_xfer_len = sizeof(struct MR_CTRL_HB_HOST_MEM);
2215 dcmd->opcode = MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC;
2216 dcmd->sgl.sge32[0].phys_addr = instance->hb_host_mem_h;
2217 dcmd->sgl.sge32[0].length = sizeof(struct MR_CTRL_HB_HOST_MEM);
2218
2219 printk(KERN_WARNING "megasas: SR-IOV: Starting heartbeat for scsi%d\n",
2220 instance->host->host_no);
2221
2222 if (!megasas_issue_polled(instance, cmd)) {
2223 retval = 0;
2224 } else {
2225 printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2226 "_MEM_ALLOC DCMD timed out for scsi%d\n",
2227 instance->host->host_no);
2228 retval = 1;
2229 goto out;
2230 }
2231
2232
2233 if (dcmd->cmd_status) {
2234 printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2235 "_MEM_ALLOC DCMD failed with status 0x%x for scsi%d\n",
2236 dcmd->cmd_status,
2237 instance->host->host_no);
2238 retval = 1;
2239 goto out;
2240 }
2241
2242out:
2243 megasas_return_cmd(instance, cmd);
2244
2245 return retval;
2246}
2247
2248/* Handler for SR-IOV heartbeat */
2249void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2250{
2251 struct megasas_instance *instance =
2252 (struct megasas_instance *)instance_addr;
2253
2254 if (instance->hb_host_mem->HB.fwCounter !=
2255 instance->hb_host_mem->HB.driverCounter) {
2256 instance->hb_host_mem->HB.driverCounter =
2257 instance->hb_host_mem->HB.fwCounter;
2258 mod_timer(&instance->sriov_heartbeat_timer,
2259 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2260 } else {
2261 printk(KERN_WARNING "megasas: SR-IOV: Heartbeat never "
2262 "completed for scsi%d\n", instance->host->host_no);
2263 schedule_work(&instance->work_init);
2264 }
2265}
2266
bo yang7343eb62007-11-09 04:35:44 -05002267/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002268 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2269 * @instance: Adapter soft state
2270 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002271 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002272 * complete all its outstanding commands. Returns error if one or more IOs
2273 * are pending after this time period. It also marks the controller dead.
2274 */
2275static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2276{
2277 int i;
bo yang39a98552010-09-22 22:36:29 -04002278 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002279 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002280 u8 adprecovery;
2281 unsigned long flags;
2282 struct list_head clist_local;
2283 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002284 u32 fw_state;
2285 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04002286
2287 spin_lock_irqsave(&instance->hba_lock, flags);
2288 adprecovery = instance->adprecovery;
2289 spin_unlock_irqrestore(&instance->hba_lock, flags);
2290
2291 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2292
2293 INIT_LIST_HEAD(&clist_local);
2294 spin_lock_irqsave(&instance->hba_lock, flags);
2295 list_splice_init(&instance->internal_reset_pending_q,
2296 &clist_local);
2297 spin_unlock_irqrestore(&instance->hba_lock, flags);
2298
2299 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
2300 for (i = 0; i < wait_time; i++) {
2301 msleep(1000);
2302 spin_lock_irqsave(&instance->hba_lock, flags);
2303 adprecovery = instance->adprecovery;
2304 spin_unlock_irqrestore(&instance->hba_lock, flags);
2305 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2306 break;
2307 }
2308
2309 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2310 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
2311 spin_lock_irqsave(&instance->hba_lock, flags);
2312 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2313 spin_unlock_irqrestore(&instance->hba_lock, flags);
2314 return FAILED;
2315 }
2316
2317 reset_index = 0;
2318 while (!list_empty(&clist_local)) {
2319 reset_cmd = list_entry((&clist_local)->next,
2320 struct megasas_cmd, list);
2321 list_del_init(&reset_cmd->list);
2322 if (reset_cmd->scmd) {
2323 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002324 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002325 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002326 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002327
2328 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2329 megasas_return_cmd(instance, reset_cmd);
2330 } else if (reset_cmd->sync_cmd) {
2331 printk(KERN_NOTICE "megasas:%p synch cmds"
2332 "reset queue\n",
2333 reset_cmd);
2334
2335 reset_cmd->cmd_status = ENODATA;
2336 instance->instancet->fire_cmd(instance,
2337 reset_cmd->frame_phys_addr,
2338 0, instance->reg_set);
2339 } else {
2340 printk(KERN_NOTICE "megasas: %p unexpected"
2341 "cmds lst\n",
2342 reset_cmd);
2343 }
2344 reset_index++;
2345 }
2346
2347 return SUCCESS;
2348 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002349
adam radfordc007b8b2012-07-17 18:20:24 -07002350 for (i = 0; i < resetwaittime; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002351
Sumant Patroe4a082c2006-05-30 12:03:37 -07002352 int outstanding = atomic_read(&instance->fw_outstanding);
2353
2354 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002355 break;
2356
2357 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2358 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002359 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002360 /*
2361 * Call cmd completion routine. Cmd to be
2362 * be completed directly without depending on isr.
2363 */
2364 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002365 }
2366
2367 msleep(1000);
2368 }
2369
Yang, Bo707e09b2010-10-12 07:20:27 -06002370 i = 0;
2371 kill_adapter_flag = 0;
2372 do {
2373 fw_state = instance->instancet->read_fw_status_reg(
2374 instance->reg_set) & MFI_STATE_MASK;
2375 if ((fw_state == MFI_STATE_FAULT) &&
2376 (instance->disableOnlineCtrlReset == 0)) {
2377 if (i == 3) {
2378 kill_adapter_flag = 2;
2379 break;
2380 }
2381 megasas_do_ocr(instance);
2382 kill_adapter_flag = 1;
2383
2384 /* wait for 1 secs to let FW finish the pending cmds */
2385 msleep(1000);
2386 }
2387 i++;
2388 } while (i <= 3);
2389
2390 if (atomic_read(&instance->fw_outstanding) &&
2391 !kill_adapter_flag) {
2392 if (instance->disableOnlineCtrlReset == 0) {
2393
2394 megasas_do_ocr(instance);
2395
2396 /* wait for 5 secs to let FW finish the pending cmds */
2397 for (i = 0; i < wait_time; i++) {
2398 int outstanding =
2399 atomic_read(&instance->fw_outstanding);
2400 if (!outstanding)
2401 return SUCCESS;
2402 msleep(1000);
2403 }
2404 }
2405 }
2406
2407 if (atomic_read(&instance->fw_outstanding) ||
2408 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04002409 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002410 /*
2411 * Send signal to FW to stop processing any pending cmds.
2412 * The controller will be taken offline by the OS now.
2413 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002414 if ((instance->pdev->device ==
2415 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2416 (instance->pdev->device ==
2417 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2418 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08002419 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002420 } else {
2421 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07002422 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002423 }
Sumant Patro658dced2006-10-03 13:09:14 -07002424 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04002425 spin_lock_irqsave(&instance->hba_lock, flags);
2426 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2427 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002428 return FAILED;
2429 }
2430
bo yang39a98552010-09-22 22:36:29 -04002431 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
2432
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002433 return SUCCESS;
2434}
2435
2436/**
2437 * megasas_generic_reset - Generic reset routine
2438 * @scmd: Mid-layer SCSI command
2439 *
2440 * This routine implements a generic reset handler for device, bus and host
2441 * reset requests. Device, bus and host specific reset handlers can use this
2442 * function after they do their specific tasks.
2443 */
2444static int megasas_generic_reset(struct scsi_cmnd *scmd)
2445{
2446 int ret_val;
2447 struct megasas_instance *instance;
2448
2449 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2450
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002451 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2452 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002453
bo yang39a98552010-09-22 22:36:29 -04002454 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002455 printk(KERN_ERR "megasas: cannot recover from previous reset "
2456 "failures\n");
2457 return FAILED;
2458 }
2459
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002460 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002461 if (ret_val == SUCCESS)
2462 printk(KERN_NOTICE "megasas: reset successful \n");
2463 else
2464 printk(KERN_ERR "megasas: failed to do reset\n");
2465
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002466 return ret_val;
2467}
2468
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002469/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002470 * megasas_reset_timer - quiesce the adapter if required
2471 * @scmd: scsi cmnd
2472 *
2473 * Sets the FW busy flag and reduces the host->can_queue if the
2474 * cmd has not been completed within the timeout period.
2475 */
2476static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002477blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002478{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002479 struct megasas_instance *instance;
2480 unsigned long flags;
2481
2482 if (time_after(jiffies, scmd->jiffies_at_alloc +
2483 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002484 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002485 }
2486
adam radfordf575c5d2011-10-13 16:01:12 -07002487 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002488 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2489 /* FW is busy, throttle IO */
2490 spin_lock_irqsave(instance->host->host_lock, flags);
2491
adam radfordc5daa6a2012-07-17 18:20:03 -07002492 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002493 instance->last_time = jiffies;
2494 instance->flag |= MEGASAS_FW_BUSY;
2495
2496 spin_unlock_irqrestore(instance->host->host_lock, flags);
2497 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002498 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002499}
2500
2501/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002502 * megasas_reset_device - Device reset handler entry point
2503 */
2504static int megasas_reset_device(struct scsi_cmnd *scmd)
2505{
2506 int ret;
2507
2508 /*
2509 * First wait for all commands to complete
2510 */
2511 ret = megasas_generic_reset(scmd);
2512
2513 return ret;
2514}
2515
2516/**
2517 * megasas_reset_bus_host - Bus & host reset handler entry point
2518 */
2519static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2520{
2521 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002522 struct megasas_instance *instance;
2523 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002524
2525 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002526 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002527 */
adam radford36807e62011-10-08 18:15:06 -07002528 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07002529 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302530 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2531 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radford229fe472014-03-10 02:51:56 -07002532 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002533 else
2534 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002535
2536 return ret;
2537}
2538
2539/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002540 * megasas_bios_param - Returns disk geometry for a disk
2541 * @sdev: device handle
2542 * @bdev: block device
2543 * @capacity: drive capacity
2544 * @geom: geometry parameters
2545 */
2546static int
2547megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2548 sector_t capacity, int geom[])
2549{
2550 int heads;
2551 int sectors;
2552 sector_t cylinders;
2553 unsigned long tmp;
2554 /* Default heads (64) & sectors (32) */
2555 heads = 64;
2556 sectors = 32;
2557
2558 tmp = heads * sectors;
2559 cylinders = capacity;
2560
2561 sector_div(cylinders, tmp);
2562
2563 /*
2564 * Handle extended translation size for logical drives > 1Gb
2565 */
2566
2567 if (capacity >= 0x200000) {
2568 heads = 255;
2569 sectors = 63;
2570 tmp = heads*sectors;
2571 cylinders = capacity;
2572 sector_div(cylinders, tmp);
2573 }
2574
2575 geom[0] = heads;
2576 geom[1] = sectors;
2577 geom[2] = cylinders;
2578
2579 return 0;
2580}
2581
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002582static void megasas_aen_polling(struct work_struct *work);
2583
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002584/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002585 * megasas_service_aen - Processes an event notification
2586 * @instance: Adapter soft state
2587 * @cmd: AEN command completed by the ISR
2588 *
2589 * For AEN, driver sends a command down to FW that is held by the FW till an
2590 * event occurs. When an event of interest occurs, FW completes the command
2591 * that it was previously holding.
2592 *
2593 * This routines sends SIGIO signal to processes that have registered with the
2594 * driver for AEN.
2595 */
2596static void
2597megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2598{
Yang, Boc3518832009-10-06 14:18:02 -06002599 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002600 /*
2601 * Don't signal app if it is just an aborted previously registered aen
2602 */
Yang, Boc3518832009-10-06 14:18:02 -06002603 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2604 spin_lock_irqsave(&poll_aen_lock, flags);
2605 megasas_poll_wait_aen = 1;
2606 spin_unlock_irqrestore(&poll_aen_lock, flags);
2607 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002608 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002609 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002610 else
2611 cmd->abort_aen = 0;
2612
2613 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302614
2615 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2616 megasas_return_mfi_mpt_pthr(instance, cmd,
2617 cmd->mpt_pthr_cmd_blocked);
2618 else
2619 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002620
bo yang39a98552010-09-22 22:36:29 -04002621 if ((instance->unload == 0) &&
2622 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002623 struct megasas_aen_event *ev;
2624 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2625 if (!ev) {
2626 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2627 } else {
2628 ev->instance = instance;
2629 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002630 INIT_DELAYED_WORK(&ev->hotplug_work,
2631 megasas_aen_polling);
2632 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002633 }
2634 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002635}
2636
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302637static ssize_t
2638megasas_fw_crash_buffer_store(struct device *cdev,
2639 struct device_attribute *attr, const char *buf, size_t count)
2640{
2641 struct Scsi_Host *shost = class_to_shost(cdev);
2642 struct megasas_instance *instance =
2643 (struct megasas_instance *) shost->hostdata;
2644 int val = 0;
2645 unsigned long flags;
2646
2647 if (kstrtoint(buf, 0, &val) != 0)
2648 return -EINVAL;
2649
2650 spin_lock_irqsave(&instance->crashdump_lock, flags);
2651 instance->fw_crash_buffer_offset = val;
2652 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2653 return strlen(buf);
2654}
2655
2656static ssize_t
2657megasas_fw_crash_buffer_show(struct device *cdev,
2658 struct device_attribute *attr, char *buf)
2659{
2660 struct Scsi_Host *shost = class_to_shost(cdev);
2661 struct megasas_instance *instance =
2662 (struct megasas_instance *) shost->hostdata;
2663 u32 size;
2664 unsigned long buff_addr;
2665 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2666 unsigned long src_addr;
2667 unsigned long flags;
2668 u32 buff_offset;
2669
2670 spin_lock_irqsave(&instance->crashdump_lock, flags);
2671 buff_offset = instance->fw_crash_buffer_offset;
2672 if (!instance->crash_dump_buf &&
2673 !((instance->fw_crash_state == AVAILABLE) ||
2674 (instance->fw_crash_state == COPYING))) {
2675 dev_err(&instance->pdev->dev,
2676 "Firmware crash dump is not available\n");
2677 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2678 return -EINVAL;
2679 }
2680
2681 buff_addr = (unsigned long) buf;
2682
2683 if (buff_offset >
2684 (instance->fw_crash_buffer_size * dmachunk)) {
2685 dev_err(&instance->pdev->dev,
2686 "Firmware crash dump offset is out of range\n");
2687 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2688 return 0;
2689 }
2690
2691 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2692 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2693
2694 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2695 (buff_offset % dmachunk);
2696 memcpy(buf, (void *)src_addr, size);
2697 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2698
2699 return size;
2700}
2701
2702static ssize_t
2703megasas_fw_crash_buffer_size_show(struct device *cdev,
2704 struct device_attribute *attr, char *buf)
2705{
2706 struct Scsi_Host *shost = class_to_shost(cdev);
2707 struct megasas_instance *instance =
2708 (struct megasas_instance *) shost->hostdata;
2709
2710 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2711 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2712}
2713
2714static ssize_t
2715megasas_fw_crash_state_store(struct device *cdev,
2716 struct device_attribute *attr, const char *buf, size_t count)
2717{
2718 struct Scsi_Host *shost = class_to_shost(cdev);
2719 struct megasas_instance *instance =
2720 (struct megasas_instance *) shost->hostdata;
2721 int val = 0;
2722 unsigned long flags;
2723
2724 if (kstrtoint(buf, 0, &val) != 0)
2725 return -EINVAL;
2726
2727 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2728 dev_err(&instance->pdev->dev, "application updates invalid "
2729 "firmware crash state\n");
2730 return -EINVAL;
2731 }
2732
2733 instance->fw_crash_state = val;
2734
2735 if ((val == COPIED) || (val == COPY_ERROR)) {
2736 spin_lock_irqsave(&instance->crashdump_lock, flags);
2737 megasas_free_host_crash_buffer(instance);
2738 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2739 if (val == COPY_ERROR)
2740 dev_info(&instance->pdev->dev, "application failed to "
2741 "copy Firmware crash dump\n");
2742 else
2743 dev_info(&instance->pdev->dev, "Firmware crash dump "
2744 "copied successfully\n");
2745 }
2746 return strlen(buf);
2747}
2748
2749static ssize_t
2750megasas_fw_crash_state_show(struct device *cdev,
2751 struct device_attribute *attr, char *buf)
2752{
2753 struct Scsi_Host *shost = class_to_shost(cdev);
2754 struct megasas_instance *instance =
2755 (struct megasas_instance *) shost->hostdata;
2756 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2757}
2758
2759static ssize_t
2760megasas_page_size_show(struct device *cdev,
2761 struct device_attribute *attr, char *buf)
2762{
2763 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2764}
2765
2766static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2767 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2768static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2769 megasas_fw_crash_buffer_size_show, NULL);
2770static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2771 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2772static DEVICE_ATTR(page_size, S_IRUGO,
2773 megasas_page_size_show, NULL);
2774
2775struct device_attribute *megaraid_host_attrs[] = {
2776 &dev_attr_fw_crash_buffer_size,
2777 &dev_attr_fw_crash_buffer,
2778 &dev_attr_fw_crash_state,
2779 &dev_attr_page_size,
2780 NULL,
2781};
2782
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002783/*
2784 * Scsi host template for megaraid_sas driver
2785 */
2786static struct scsi_host_template megasas_template = {
2787
2788 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002789 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002790 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002791 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002792 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002793 .queuecommand = megasas_queue_command,
2794 .eh_device_reset_handler = megasas_reset_device,
2795 .eh_bus_reset_handler = megasas_reset_bus_host,
2796 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002797 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302798 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002799 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002800 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002801 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002802 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002803};
2804
2805/**
2806 * megasas_complete_int_cmd - Completes an internal command
2807 * @instance: Adapter soft state
2808 * @cmd: Command to be completed
2809 *
2810 * The megasas_issue_blocked_cmd() function waits for a command to complete
2811 * after it issues a command. This function wakes up that waiting routine by
2812 * calling wake_up() on the wait queue.
2813 */
2814static void
2815megasas_complete_int_cmd(struct megasas_instance *instance,
2816 struct megasas_cmd *cmd)
2817{
2818 cmd->cmd_status = cmd->frame->io.cmd_status;
2819
2820 if (cmd->cmd_status == ENODATA) {
2821 cmd->cmd_status = 0;
2822 }
2823 wake_up(&instance->int_cmd_wait_q);
2824}
2825
2826/**
2827 * megasas_complete_abort - Completes aborting a command
2828 * @instance: Adapter soft state
2829 * @cmd: Cmd that was issued to abort another cmd
2830 *
adam radford0d490162010-12-14 19:17:17 -08002831 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2832 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002833 * wakes up all functions waiting on the same wait queue.
2834 */
2835static void
2836megasas_complete_abort(struct megasas_instance *instance,
2837 struct megasas_cmd *cmd)
2838{
2839 if (cmd->sync_cmd) {
2840 cmd->sync_cmd = 0;
2841 cmd->cmd_status = 0;
2842 wake_up(&instance->abort_cmd_wait_q);
2843 }
2844
2845 return;
2846}
2847
2848/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002849 * megasas_complete_cmd - Completes a command
2850 * @instance: Adapter soft state
2851 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002852 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002853 * SCSI mid-layer instead of the value returned
2854 * by the FW. This should be used if caller wants
2855 * an alternate status (as in the case of aborted
2856 * commands)
2857 */
adam radford9c915a82010-12-21 13:34:31 -08002858void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002859megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2860 u8 alt_status)
2861{
2862 int exception = 0;
2863 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002864 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002865 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302866 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002867
bo yang39a98552010-09-22 22:36:29 -04002868 /* flag for the retry reset */
2869 cmd->retry_for_fw_reset = 0;
2870
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002871 if (cmd->scmd)
2872 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002873
2874 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002875 case MFI_CMD_INVALID:
2876 /* Some older 1068 controller FW may keep a pended
2877 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2878 when booting the kdump kernel. Ignore this command to
2879 prevent a kernel panic on shutdown of the kdump kernel. */
2880 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2881 "completed.\n");
2882 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2883 "other than PERC5, please upgrade your firmware.\n");
2884 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002885 case MFI_CMD_PD_SCSI_IO:
2886 case MFI_CMD_LD_SCSI_IO:
2887
2888 /*
2889 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2890 * issued either through an IO path or an IOCTL path. If it
2891 * was via IOCTL, we will send it to internal completion.
2892 */
2893 if (cmd->sync_cmd) {
2894 cmd->sync_cmd = 0;
2895 megasas_complete_int_cmd(instance, cmd);
2896 break;
2897 }
2898
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002899 case MFI_CMD_LD_READ:
2900 case MFI_CMD_LD_WRITE:
2901
2902 if (alt_status) {
2903 cmd->scmd->result = alt_status << 16;
2904 exception = 1;
2905 }
2906
2907 if (exception) {
2908
Sumant Patroe4a082c2006-05-30 12:03:37 -07002909 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002910
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002911 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002912 cmd->scmd->scsi_done(cmd->scmd);
2913 megasas_return_cmd(instance, cmd);
2914
2915 break;
2916 }
2917
2918 switch (hdr->cmd_status) {
2919
2920 case MFI_STAT_OK:
2921 cmd->scmd->result = DID_OK << 16;
2922 break;
2923
2924 case MFI_STAT_SCSI_IO_FAILED:
2925 case MFI_STAT_LD_INIT_IN_PROGRESS:
2926 cmd->scmd->result =
2927 (DID_ERROR << 16) | hdr->scsi_status;
2928 break;
2929
2930 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2931
2932 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2933
2934 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2935 memset(cmd->scmd->sense_buffer, 0,
2936 SCSI_SENSE_BUFFERSIZE);
2937 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2938 hdr->sense_len);
2939
2940 cmd->scmd->result |= DRIVER_SENSE << 24;
2941 }
2942
2943 break;
2944
2945 case MFI_STAT_LD_OFFLINE:
2946 case MFI_STAT_DEVICE_NOT_FOUND:
2947 cmd->scmd->result = DID_BAD_TARGET << 16;
2948 break;
2949
2950 default:
2951 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2952 hdr->cmd_status);
2953 cmd->scmd->result = DID_ERROR << 16;
2954 break;
2955 }
2956
Sumant Patroe4a082c2006-05-30 12:03:37 -07002957 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002958
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002959 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002960 cmd->scmd->scsi_done(cmd->scmd);
2961 megasas_return_cmd(instance, cmd);
2962
2963 break;
2964
2965 case MFI_CMD_SMP:
2966 case MFI_CMD_STP:
2967 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302968 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08002969 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302970 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2971 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302972 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08002973 spin_lock_irqsave(instance->host->host_lock, flags);
2974 if (cmd->frame->hdr.cmd_status != 0) {
2975 if (cmd->frame->hdr.cmd_status !=
2976 MFI_STAT_NOT_FOUND)
2977 printk(KERN_WARNING "megasas: map sync"
2978 "failed, status = 0x%x.\n",
2979 cmd->frame->hdr.cmd_status);
2980 else {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302981 megasas_return_mfi_mpt_pthr(instance,
2982 cmd, cmd->mpt_pthr_cmd_blocked);
adam radford9c915a82010-12-21 13:34:31 -08002983 spin_unlock_irqrestore(
2984 instance->host->host_lock,
2985 flags);
2986 break;
2987 }
2988 } else
2989 instance->map_id++;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302990 megasas_return_mfi_mpt_pthr(instance, cmd,
2991 cmd->mpt_pthr_cmd_blocked);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302992
2993 /*
2994 * Set fast path IO to ZERO.
2995 * Validate Map will set proper value.
2996 * Meanwhile all IOs will go as LD IO.
2997 */
2998 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08002999 fusion->fast_path_io = 1;
3000 else
3001 fusion->fast_path_io = 0;
3002 megasas_sync_map_info(instance);
3003 spin_unlock_irqrestore(instance->host->host_lock,
3004 flags);
3005 break;
3006 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303007 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3008 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003009 spin_lock_irqsave(&poll_aen_lock, flags);
3010 megasas_poll_wait_aen = 0;
3011 spin_unlock_irqrestore(&poll_aen_lock, flags);
3012 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003013
3014 /*
3015 * See if got an event notification
3016 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303017 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003018 megasas_service_aen(instance, cmd);
3019 else
3020 megasas_complete_int_cmd(instance, cmd);
3021
3022 break;
3023
3024 case MFI_CMD_ABORT:
3025 /*
3026 * Cmd issued to abort another cmd returned
3027 */
3028 megasas_complete_abort(instance, cmd);
3029 break;
3030
3031 default:
3032 printk("megasas: Unknown command completed! [0x%X]\n",
3033 hdr->cmd);
3034 break;
3035 }
3036}
3037
3038/**
bo yang39a98552010-09-22 22:36:29 -04003039 * megasas_issue_pending_cmds_again - issue all pending cmds
3040 * in FW again because of the fw reset
3041 * @instance: Adapter soft state
3042 */
3043static inline void
3044megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3045{
3046 struct megasas_cmd *cmd;
3047 struct list_head clist_local;
3048 union megasas_evt_class_locale class_locale;
3049 unsigned long flags;
3050 u32 seq_num;
3051
3052 INIT_LIST_HEAD(&clist_local);
3053 spin_lock_irqsave(&instance->hba_lock, flags);
3054 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3055 spin_unlock_irqrestore(&instance->hba_lock, flags);
3056
3057 while (!list_empty(&clist_local)) {
3058 cmd = list_entry((&clist_local)->next,
3059 struct megasas_cmd, list);
3060 list_del_init(&cmd->list);
3061
3062 if (cmd->sync_cmd || cmd->scmd) {
3063 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
3064 "detected to be pending while HBA reset.\n",
3065 cmd, cmd->scmd, cmd->sync_cmd);
3066
3067 cmd->retry_for_fw_reset++;
3068
3069 if (cmd->retry_for_fw_reset == 3) {
3070 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
3071 "was tried multiple times during reset."
3072 "Shutting down the HBA\n",
3073 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303074 instance->instancet->disable_intr(instance);
3075 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003076 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003077 return;
3078 }
3079 }
3080
3081 if (cmd->sync_cmd == 1) {
3082 if (cmd->scmd) {
3083 printk(KERN_NOTICE "megaraid_sas: unexpected"
3084 "cmd attached to internal command!\n");
3085 }
3086 printk(KERN_NOTICE "megasas: %p synchronous cmd"
3087 "on the internal reset queue,"
3088 "issue it again.\n", cmd);
3089 cmd->cmd_status = ENODATA;
3090 instance->instancet->fire_cmd(instance,
3091 cmd->frame_phys_addr ,
3092 0, instance->reg_set);
3093 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003094 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003095 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003096 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003097
3098 atomic_inc(&instance->fw_outstanding);
3099 instance->instancet->fire_cmd(instance,
3100 cmd->frame_phys_addr,
3101 cmd->frame_count-1, instance->reg_set);
3102 } else {
3103 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
3104 "internal reset defer list while re-issue!!\n",
3105 cmd);
3106 }
3107 }
3108
3109 if (instance->aen_cmd) {
3110 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
3111 megasas_return_cmd(instance, instance->aen_cmd);
3112
3113 instance->aen_cmd = NULL;
3114 }
3115
3116 /*
3117 * Initiate AEN (Asynchronous Event Notification)
3118 */
3119 seq_num = instance->last_seq_num;
3120 class_locale.members.reserved = 0;
3121 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3122 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3123
3124 megasas_register_aen(instance, seq_num, class_locale.word);
3125}
3126
3127/**
3128 * Move the internal reset pending commands to a deferred queue.
3129 *
3130 * We move the commands pending at internal reset time to a
3131 * pending queue. This queue would be flushed after successful
3132 * completion of the internal reset sequence. if the internal reset
3133 * did not complete in time, the kernel reset handler would flush
3134 * these commands.
3135 **/
3136static void
3137megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3138{
3139 struct megasas_cmd *cmd;
3140 int i;
3141 u32 max_cmd = instance->max_fw_cmds;
3142 u32 defer_index;
3143 unsigned long flags;
3144
3145 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303146 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003147 for (i = 0; i < max_cmd; i++) {
3148 cmd = instance->cmd_list[i];
3149 if (cmd->sync_cmd == 1 || cmd->scmd) {
3150 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
3151 "on the defer queue as internal\n",
3152 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3153
3154 if (!list_empty(&cmd->list)) {
3155 printk(KERN_NOTICE "megaraid_sas: ERROR while"
3156 " moving this cmd:%p, %d %p, it was"
3157 "discovered on some list?\n",
3158 cmd, cmd->sync_cmd, cmd->scmd);
3159
3160 list_del_init(&cmd->list);
3161 }
3162 defer_index++;
3163 list_add_tail(&cmd->list,
3164 &instance->internal_reset_pending_q);
3165 }
3166 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303167 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003168}
3169
3170
3171static void
3172process_fw_state_change_wq(struct work_struct *work)
3173{
3174 struct megasas_instance *instance =
3175 container_of(work, struct megasas_instance, work_init);
3176 u32 wait;
3177 unsigned long flags;
3178
3179 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3180 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
3181 instance->adprecovery);
3182 return ;
3183 }
3184
3185 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3186 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
3187 "state, restarting it...\n");
3188
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303189 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003190 atomic_set(&instance->fw_outstanding, 0);
3191
3192 atomic_set(&instance->fw_reset_no_pci_access, 1);
3193 instance->instancet->adp_reset(instance, instance->reg_set);
3194 atomic_set(&instance->fw_reset_no_pci_access, 0 );
3195
3196 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
3197 "initiating next stage...\n");
3198
3199 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
3200 "state 2 starting...\n");
3201
3202 /*waitting for about 20 second before start the second init*/
3203 for (wait = 0; wait < 30; wait++) {
3204 msleep(1000);
3205 }
3206
adam radford058a8fa2011-10-08 18:14:27 -07003207 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04003208 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
3209
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303210 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003211 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003212 return ;
3213 }
3214
3215 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3216 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3217 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3218 ) {
3219 *instance->consumer = *instance->producer;
3220 } else {
3221 *instance->consumer = 0;
3222 *instance->producer = 0;
3223 }
3224
3225 megasas_issue_init_mfi(instance);
3226
3227 spin_lock_irqsave(&instance->hba_lock, flags);
3228 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3229 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303230 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003231
3232 megasas_issue_pending_cmds_again(instance);
3233 instance->issuepend_done = 1;
3234 }
3235 return ;
3236}
3237
3238/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003239 * megasas_deplete_reply_queue - Processes all completed commands
3240 * @instance: Adapter soft state
3241 * @alt_status: Alternate status to be returned to
3242 * SCSI mid-layer instead of the status
3243 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003244 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003245 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003246static int
bo yang39a98552010-09-22 22:36:29 -04003247megasas_deplete_reply_queue(struct megasas_instance *instance,
3248 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003249{
bo yang39a98552010-09-22 22:36:29 -04003250 u32 mfiStatus;
3251 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003252
bo yang39a98552010-09-22 22:36:29 -04003253 if ((mfiStatus = instance->instancet->check_reset(instance,
3254 instance->reg_set)) == 1) {
3255 return IRQ_HANDLED;
3256 }
3257
3258 if ((mfiStatus = instance->instancet->clear_intr(
3259 instance->reg_set)
3260 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003261 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003262 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003263 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003264 }
3265
3266 instance->mfiStatus = mfiStatus;
3267
3268 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3269 fw_state = instance->instancet->read_fw_status_reg(
3270 instance->reg_set) & MFI_STATE_MASK;
3271
3272 if (fw_state != MFI_STATE_FAULT) {
3273 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
3274 fw_state);
3275 }
3276
3277 if ((fw_state == MFI_STATE_FAULT) &&
3278 (instance->disableOnlineCtrlReset == 0)) {
3279 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
3280
3281 if ((instance->pdev->device ==
3282 PCI_DEVICE_ID_LSI_SAS1064R) ||
3283 (instance->pdev->device ==
3284 PCI_DEVICE_ID_DELL_PERC5) ||
3285 (instance->pdev->device ==
3286 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3287
3288 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303289 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003290 }
3291
3292
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303293 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003294 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3295 instance->issuepend_done = 0;
3296
3297 atomic_set(&instance->fw_outstanding, 0);
3298 megasas_internal_reset_defer_cmds(instance);
3299
3300 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
3301 fw_state, instance->adprecovery);
3302
3303 schedule_work(&instance->work_init);
3304 return IRQ_HANDLED;
3305
3306 } else {
3307 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
3308 fw_state, instance->disableOnlineCtrlReset);
3309 }
3310 }
3311
Sumant Patro5d018ad2006-10-03 13:13:18 -07003312 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003313 return IRQ_HANDLED;
3314}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003315/**
3316 * megasas_isr - isr entry point
3317 */
David Howells7d12e782006-10-05 14:55:46 +01003318static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003319{
adam radfordc8e858f2011-10-08 18:15:13 -07003320 struct megasas_irq_context *irq_context = devp;
3321 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003322 unsigned long flags;
3323 irqreturn_t rc;
3324
adam radfordc8e858f2011-10-08 18:15:13 -07003325 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003326 return IRQ_HANDLED;
3327
bo yang39a98552010-09-22 22:36:29 -04003328 spin_lock_irqsave(&instance->hba_lock, flags);
3329 rc = megasas_deplete_reply_queue(instance, DID_OK);
3330 spin_unlock_irqrestore(&instance->hba_lock, flags);
3331
3332 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003333}
3334
3335/**
3336 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003337 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003338 *
3339 * During the initialization, FW passes can potentially be in any one of
3340 * several possible states. If the FW in operational, waiting-for-handshake
3341 * states, driver must take steps to bring it to ready state. Otherwise, it
3342 * has to wait for the ready state.
3343 */
adam radford9c915a82010-12-21 13:34:31 -08003344int
adam radford058a8fa2011-10-08 18:14:27 -07003345megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003346{
3347 int i;
3348 u8 max_wait;
3349 u32 fw_state;
3350 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003351 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003352
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003353 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3354 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003355
Sumant Patroe3bbff92006-10-03 12:28:49 -07003356 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08003357 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
3358 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003359
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003360 while (fw_state != MFI_STATE_READY) {
3361
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003362 switch (fw_state) {
3363
3364 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003365 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003366 if (ocr) {
3367 max_wait = MEGASAS_RESET_WAIT_TIME;
3368 cur_state = MFI_STATE_FAULT;
3369 break;
3370 } else
3371 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003372
3373 case MFI_STATE_WAIT_HANDSHAKE:
3374 /*
3375 * Set the CLR bit in inbound doorbell
3376 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003377 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003378 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3379 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003380 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3381 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303382 PCI_DEVICE_ID_LSI_FUSION) ||
adam radford36807e62011-10-08 18:15:06 -07003383 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003384 PCI_DEVICE_ID_LSI_PLASMA) ||
3385 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303386 PCI_DEVICE_ID_LSI_INVADER) ||
3387 (instance->pdev->device ==
3388 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003389 writel(
3390 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003391 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06003392 } else {
3393 writel(
3394 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3395 &instance->reg_set->inbound_doorbell);
3396 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003397
Yang, Bo7218df62009-10-06 14:52:20 -06003398 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003399 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3400 break;
3401
Sumant Patroe3bbff92006-10-03 12:28:49 -07003402 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003403 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003404 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3405 (instance->pdev->device ==
3406 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3407 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07003408 PCI_DEVICE_ID_LSI_FUSION) ||
3409 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003410 PCI_DEVICE_ID_LSI_PLASMA) ||
3411 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303412 PCI_DEVICE_ID_LSI_INVADER) ||
3413 (instance->pdev->device ==
3414 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003415 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003416 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06003417 } else
3418 writel(MFI_INIT_HOTPLUG,
3419 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003420
Yang, Bo7218df62009-10-06 14:52:20 -06003421 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003422 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3423 break;
3424
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003425 case MFI_STATE_OPERATIONAL:
3426 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003427 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003428 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303429 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003430 if ((instance->pdev->device ==
3431 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3432 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003433 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3434 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07003435 == PCI_DEVICE_ID_LSI_FUSION) ||
3436 (instance->pdev->device
adam radford229fe472014-03-10 02:51:56 -07003437 == PCI_DEVICE_ID_LSI_PLASMA) ||
3438 (instance->pdev->device
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303439 == PCI_DEVICE_ID_LSI_INVADER) ||
3440 (instance->pdev->device
3441 == PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003442 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003443 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07003444 if ((instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303445 PCI_DEVICE_ID_LSI_FUSION) ||
3446 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003447 PCI_DEVICE_ID_LSI_PLASMA) ||
3448 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303449 PCI_DEVICE_ID_LSI_INVADER) ||
3450 (instance->pdev->device ==
3451 PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08003452 for (i = 0; i < (10 * 1000); i += 20) {
3453 if (readl(
3454 &instance->
3455 reg_set->
3456 doorbell) & 1)
3457 msleep(20);
3458 else
3459 break;
3460 }
3461 }
Yang, Bo87911122009-10-06 14:31:54 -06003462 } else
3463 writel(MFI_RESET_FLAGS,
3464 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003465
Yang, Bo7218df62009-10-06 14:52:20 -06003466 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003467 cur_state = MFI_STATE_OPERATIONAL;
3468 break;
3469
3470 case MFI_STATE_UNDEFINED:
3471 /*
3472 * This state should not last for more than 2 seconds
3473 */
Yang, Bo7218df62009-10-06 14:52:20 -06003474 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003475 cur_state = MFI_STATE_UNDEFINED;
3476 break;
3477
3478 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003479 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003480 cur_state = MFI_STATE_BB_INIT;
3481 break;
3482
3483 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003484 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003485 cur_state = MFI_STATE_FW_INIT;
3486 break;
3487
3488 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003489 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003490 cur_state = MFI_STATE_FW_INIT_2;
3491 break;
3492
3493 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003494 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003495 cur_state = MFI_STATE_DEVICE_SCAN;
3496 break;
3497
3498 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003499 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003500 cur_state = MFI_STATE_FLUSH_CACHE;
3501 break;
3502
3503 default:
3504 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
3505 fw_state);
3506 return -ENODEV;
3507 }
3508
3509 /*
3510 * The cur_state should not last for more than max_wait secs
3511 */
3512 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003513 curr_abs_state = instance->instancet->
3514 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003515
Yang, Bo7218df62009-10-06 14:52:20 -06003516 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003517 msleep(1);
3518 } else
3519 break;
3520 }
3521
3522 /*
3523 * Return error if fw_state hasn't changed after max_wait
3524 */
Yang, Bo7218df62009-10-06 14:52:20 -06003525 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003526 printk(KERN_DEBUG "FW state [%d] hasn't changed "
3527 "in %d secs\n", fw_state, max_wait);
3528 return -ENODEV;
3529 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003530
3531 abs_state = curr_abs_state;
3532 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003533 }
adam radford0d490162010-12-14 19:17:17 -08003534 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003535
3536 return 0;
3537}
3538
3539/**
3540 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3541 * @instance: Adapter soft state
3542 */
3543static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3544{
3545 int i;
adam radford9c915a82010-12-21 13:34:31 -08003546 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003547 struct megasas_cmd *cmd;
3548
3549 if (!instance->frame_dma_pool)
3550 return;
3551
3552 /*
3553 * Return all frames to pool
3554 */
3555 for (i = 0; i < max_cmd; i++) {
3556
3557 cmd = instance->cmd_list[i];
3558
3559 if (cmd->frame)
3560 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3561 cmd->frame_phys_addr);
3562
3563 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003564 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565 cmd->sense_phys_addr);
3566 }
3567
3568 /*
3569 * Now destroy the pool itself
3570 */
3571 pci_pool_destroy(instance->frame_dma_pool);
3572 pci_pool_destroy(instance->sense_dma_pool);
3573
3574 instance->frame_dma_pool = NULL;
3575 instance->sense_dma_pool = NULL;
3576}
3577
3578/**
3579 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3580 * @instance: Adapter soft state
3581 *
3582 * Each command packet has an embedded DMA memory buffer that is used for
3583 * filling MFI frame and the SG list that immediately follows the frame. This
3584 * function creates those DMA memory buffers for each command packet by using
3585 * PCI pool facility.
3586 */
3587static int megasas_create_frame_pool(struct megasas_instance *instance)
3588{
3589 int i;
3590 u32 max_cmd;
3591 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003592 u32 total_sz;
3593 u32 frame_count;
3594 struct megasas_cmd *cmd;
3595
adam radford9c915a82010-12-21 13:34:31 -08003596 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003597
3598 /*
3599 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3600 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3601 */
3602 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3603 sizeof(struct megasas_sge32);
3604
Yang, Bof4c9a132009-10-06 14:43:28 -06003605 if (instance->flag_ieee) {
3606 sge_sz = sizeof(struct megasas_sge_skinny);
3607 }
3608
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003609 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303610 * For MFI controllers.
3611 * max_num_sge = 60
3612 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3613 * Total 960 byte (15 MFI frame of 64 byte)
3614 *
3615 * Fusion adapter require only 3 extra frame.
3616 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3617 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3618 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003619 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303620 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003621 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3622 /*
3623 * Use DMA pool facility provided by PCI layer
3624 */
3625 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303626 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003627
3628 if (!instance->frame_dma_pool) {
3629 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
3630 return -ENOMEM;
3631 }
3632
3633 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3634 instance->pdev, 128, 4, 0);
3635
3636 if (!instance->sense_dma_pool) {
3637 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
3638
3639 pci_pool_destroy(instance->frame_dma_pool);
3640 instance->frame_dma_pool = NULL;
3641
3642 return -ENOMEM;
3643 }
3644
3645 /*
3646 * Allocate and attach a frame to each of the commands in cmd_list.
3647 * By making cmd->index as the context instead of the &cmd, we can
3648 * always use 32bit context regardless of the architecture
3649 */
3650 for (i = 0; i < max_cmd; i++) {
3651
3652 cmd = instance->cmd_list[i];
3653
3654 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3655 GFP_KERNEL, &cmd->frame_phys_addr);
3656
3657 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3658 GFP_KERNEL, &cmd->sense_phys_addr);
3659
3660 /*
3661 * megasas_teardown_frame_pool() takes care of freeing
3662 * whatever has been allocated
3663 */
3664 if (!cmd->frame || !cmd->sense) {
3665 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
3666 megasas_teardown_frame_pool(instance);
3667 return -ENOMEM;
3668 }
3669
Yang, Bo707e09b2010-10-12 07:20:27 -06003670 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303671 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003672 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07003673 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
adam radford229fe472014-03-10 02:51:56 -07003674 (instance->pdev->device != PCI_DEVICE_ID_LSI_PLASMA) &&
adam radforde5f93a32011-10-08 18:15:19 -07003675 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303676 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
adam radforde5f93a32011-10-08 18:15:19 -07003677 (reset_devices))
3678 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003679 }
3680
3681 return 0;
3682}
3683
3684/**
3685 * megasas_free_cmds - Free all the cmds in the free cmd pool
3686 * @instance: Adapter soft state
3687 */
adam radford9c915a82010-12-21 13:34:31 -08003688void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003689{
3690 int i;
3691 /* First free the MFI frame pool */
3692 megasas_teardown_frame_pool(instance);
3693
3694 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003695 for (i = 0; i < instance->max_mfi_cmds; i++)
3696
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003697 kfree(instance->cmd_list[i]);
3698
3699 /* Free the cmd_list buffer itself */
3700 kfree(instance->cmd_list);
3701 instance->cmd_list = NULL;
3702
3703 INIT_LIST_HEAD(&instance->cmd_pool);
3704}
3705
3706/**
3707 * megasas_alloc_cmds - Allocates the command packets
3708 * @instance: Adapter soft state
3709 *
3710 * Each command that is issued to the FW, whether IO commands from the OS or
3711 * internal commands like IOCTLs, are wrapped in local data structure called
3712 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3713 * the FW.
3714 *
3715 * Each frame has a 32-bit field called context (tag). This context is used
3716 * to get back the megasas_cmd from the frame when a frame gets completed in
3717 * the ISR. Typically the address of the megasas_cmd itself would be used as
3718 * the context. But we wanted to keep the differences between 32 and 64 bit
3719 * systems to the mininum. We always use 32 bit integers for the context. In
3720 * this driver, the 32 bit values are the indices into an array cmd_list.
3721 * This array is used only to look up the megasas_cmd given the context. The
3722 * free commands themselves are maintained in a linked list called cmd_pool.
3723 */
adam radford9c915a82010-12-21 13:34:31 -08003724int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003725{
3726 int i;
3727 int j;
3728 u32 max_cmd;
3729 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303730 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003731
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303732 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003733 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003734
3735 /*
3736 * instance->cmd_list is an array of struct megasas_cmd pointers.
3737 * Allocate the dynamic array first and then allocate individual
3738 * commands.
3739 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003740 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003741
3742 if (!instance->cmd_list) {
3743 printk(KERN_DEBUG "megasas: out of memory\n");
3744 return -ENOMEM;
3745 }
3746
adam radford9c915a82010-12-21 13:34:31 -08003747 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003748
3749 for (i = 0; i < max_cmd; i++) {
3750 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3751 GFP_KERNEL);
3752
3753 if (!instance->cmd_list[i]) {
3754
3755 for (j = 0; j < i; j++)
3756 kfree(instance->cmd_list[j]);
3757
3758 kfree(instance->cmd_list);
3759 instance->cmd_list = NULL;
3760
3761 return -ENOMEM;
3762 }
3763 }
3764
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003765 for (i = 0; i < max_cmd; i++) {
3766 cmd = instance->cmd_list[i];
3767 memset(cmd, 0, sizeof(struct megasas_cmd));
3768 cmd->index = i;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303769 atomic_set(&cmd->mfi_mpt_pthr, MFI_LIST_ADDED);
bo yang39a98552010-09-22 22:36:29 -04003770 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003771 cmd->instance = instance;
3772
3773 list_add_tail(&cmd->list, &instance->cmd_pool);
3774 }
3775
3776 /*
3777 * Create a frame pool and assign one frame to each cmd
3778 */
3779 if (megasas_create_frame_pool(instance)) {
3780 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3781 megasas_free_cmds(instance);
3782 }
3783
3784 return 0;
3785}
3786
Yang, Bo81e403c2009-10-06 14:27:54 -06003787/*
3788 * megasas_get_pd_list_info - Returns FW's pd_list structure
3789 * @instance: Adapter soft state
3790 * @pd_list: pd_list structure
3791 *
3792 * Issues an internal command (DCMD) to get the FW's controller PD
3793 * list structure. This information is mainly used to find out SYSTEM
3794 * supported by the FW.
3795 */
3796static int
3797megasas_get_pd_list(struct megasas_instance *instance)
3798{
3799 int ret = 0, pd_index = 0;
3800 struct megasas_cmd *cmd;
3801 struct megasas_dcmd_frame *dcmd;
3802 struct MR_PD_LIST *ci;
3803 struct MR_PD_ADDRESS *pd_addr;
3804 dma_addr_t ci_h = 0;
3805
3806 cmd = megasas_get_cmd(instance);
3807
3808 if (!cmd) {
3809 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3810 return -ENOMEM;
3811 }
3812
3813 dcmd = &cmd->frame->dcmd;
3814
3815 ci = pci_alloc_consistent(instance->pdev,
3816 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3817
3818 if (!ci) {
3819 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3820 megasas_return_cmd(instance, cmd);
3821 return -ENOMEM;
3822 }
3823
3824 memset(ci, 0, sizeof(*ci));
3825 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3826
3827 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3828 dcmd->mbox.b[1] = 0;
3829 dcmd->cmd = MFI_CMD_DCMD;
3830 dcmd->cmd_status = 0xFF;
3831 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303832 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06003833 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003834 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303835 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3836 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3837 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3838 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06003839
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303840 if (instance->ctrl_context && !instance->mask_interrupts)
3841 ret = megasas_issue_blocked_cmd(instance, cmd,
3842 MEGASAS_BLOCKED_CMD_TIMEOUT);
3843 else
3844 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003845
3846 /*
3847 * the following function will get the instance PD LIST.
3848 */
3849
3850 pd_addr = ci->addr;
3851
3852 if ( ret == 0 &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303853 (le32_to_cpu(ci->count) <
Yang, Bo81e403c2009-10-06 14:27:54 -06003854 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3855
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303856 memset(instance->local_pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06003857 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3858
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303859 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Yang, Bo81e403c2009-10-06 14:27:54 -06003860
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303861 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303862 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303863 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Yang, Bo81e403c2009-10-06 14:27:54 -06003864 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303865 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Yang, Bo81e403c2009-10-06 14:27:54 -06003866 MR_PD_STATE_SYSTEM;
3867 pd_addr++;
3868 }
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303869 memcpy(instance->pd_list, instance->local_pd_list,
3870 sizeof(instance->pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06003871 }
3872
3873 pci_free_consistent(instance->pdev,
3874 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3875 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303876
3877 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3878 megasas_return_mfi_mpt_pthr(instance, cmd,
3879 cmd->mpt_pthr_cmd_blocked);
3880 else
3881 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003882
3883 return ret;
3884}
3885
Yang, Bobdc6fb82009-12-06 08:30:19 -07003886/*
3887 * megasas_get_ld_list_info - Returns FW's ld_list structure
3888 * @instance: Adapter soft state
3889 * @ld_list: ld_list structure
3890 *
3891 * Issues an internal command (DCMD) to get the FW's controller PD
3892 * list structure. This information is mainly used to find out SYSTEM
3893 * supported by the FW.
3894 */
3895static int
3896megasas_get_ld_list(struct megasas_instance *instance)
3897{
3898 int ret = 0, ld_index = 0, ids = 0;
3899 struct megasas_cmd *cmd;
3900 struct megasas_dcmd_frame *dcmd;
3901 struct MR_LD_LIST *ci;
3902 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303903 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003904
3905 cmd = megasas_get_cmd(instance);
3906
3907 if (!cmd) {
3908 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3909 return -ENOMEM;
3910 }
3911
3912 dcmd = &cmd->frame->dcmd;
3913
3914 ci = pci_alloc_consistent(instance->pdev,
3915 sizeof(struct MR_LD_LIST),
3916 &ci_h);
3917
3918 if (!ci) {
3919 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3920 megasas_return_cmd(instance, cmd);
3921 return -ENOMEM;
3922 }
3923
3924 memset(ci, 0, sizeof(*ci));
3925 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3926
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303927 if (instance->supportmax256vd)
3928 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003929 dcmd->cmd = MFI_CMD_DCMD;
3930 dcmd->cmd_status = 0xFF;
3931 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303932 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003933 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303934 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3935 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3936 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3937 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07003938 dcmd->pad_0 = 0;
3939
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303940 if (instance->ctrl_context && !instance->mask_interrupts)
3941 ret = megasas_issue_blocked_cmd(instance, cmd,
3942 MEGASAS_BLOCKED_CMD_TIMEOUT);
3943 else
3944 ret = megasas_issue_polled(instance, cmd);
3945
Yang, Bobdc6fb82009-12-06 08:30:19 -07003946
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303947 ld_count = le32_to_cpu(ci->ldCount);
3948
Yang, Bobdc6fb82009-12-06 08:30:19 -07003949 /* the following function will get the instance PD LIST */
3950
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303951 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3952 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003953
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303954 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003955 if (ci->ldList[ld_index].state != 0) {
3956 ids = ci->ldList[ld_index].ref.targetId;
3957 instance->ld_ids[ids] =
3958 ci->ldList[ld_index].ref.targetId;
3959 }
3960 }
3961 }
3962
3963 pci_free_consistent(instance->pdev,
3964 sizeof(struct MR_LD_LIST),
3965 ci,
3966 ci_h);
3967
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303968 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3969 megasas_return_mfi_mpt_pthr(instance, cmd,
3970 cmd->mpt_pthr_cmd_blocked);
3971 else
3972 megasas_return_cmd(instance, cmd);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003973 return ret;
3974}
3975
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003976/**
adam radford21c9e162013-09-06 15:27:14 -07003977 * megasas_ld_list_query - Returns FW's ld_list structure
3978 * @instance: Adapter soft state
3979 * @ld_list: ld_list structure
3980 *
3981 * Issues an internal command (DCMD) to get the FW's controller PD
3982 * list structure. This information is mainly used to find out SYSTEM
3983 * supported by the FW.
3984 */
3985static int
3986megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3987{
3988 int ret = 0, ld_index = 0, ids = 0;
3989 struct megasas_cmd *cmd;
3990 struct megasas_dcmd_frame *dcmd;
3991 struct MR_LD_TARGETID_LIST *ci;
3992 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303993 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07003994
3995 cmd = megasas_get_cmd(instance);
3996
3997 if (!cmd) {
3998 printk(KERN_WARNING
3999 "megasas:(megasas_ld_list_query): Failed to get cmd\n");
4000 return -ENOMEM;
4001 }
4002
4003 dcmd = &cmd->frame->dcmd;
4004
4005 ci = pci_alloc_consistent(instance->pdev,
4006 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4007
4008 if (!ci) {
4009 printk(KERN_WARNING
4010 "megasas: Failed to alloc mem for ld_list_query\n");
4011 megasas_return_cmd(instance, cmd);
4012 return -ENOMEM;
4013 }
4014
4015 memset(ci, 0, sizeof(*ci));
4016 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4017
4018 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304019 if (instance->supportmax256vd)
4020 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004021
4022 dcmd->cmd = MFI_CMD_DCMD;
4023 dcmd->cmd_status = 0xFF;
4024 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304025 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004026 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304027 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4028 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4029 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4030 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004031 dcmd->pad_0 = 0;
4032
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304033 if (instance->ctrl_context && !instance->mask_interrupts)
4034 ret = megasas_issue_blocked_cmd(instance, cmd,
4035 MEGASAS_BLOCKED_CMD_TIMEOUT);
4036 else
4037 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004038
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304039 tgtid_count = le32_to_cpu(ci->count);
4040
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304041 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
adam radford21c9e162013-09-06 15:27:14 -07004042 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304043 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004044 ids = ci->targetId[ld_index];
4045 instance->ld_ids[ids] = ci->targetId[ld_index];
4046 }
4047
4048 }
4049
4050 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4051 ci, ci_h);
4052
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304053 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4054 megasas_return_mfi_mpt_pthr(instance, cmd,
4055 cmd->mpt_pthr_cmd_blocked);
4056 else
4057 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004058
4059 return ret;
4060}
4061
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304062/*
4063 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4064 * instance : Controller's instance
4065*/
4066static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4067{
4068 struct fusion_context *fusion;
4069 u32 old_map_sz;
4070 u32 new_map_sz;
4071
4072 fusion = instance->ctrl_context;
4073 /* For MFI based controllers return dummy success */
4074 if (!fusion)
4075 return;
4076
4077 instance->supportmax256vd =
4078 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4079 /* Below is additional check to address future FW enhancement */
4080 if (instance->ctrl_info->max_lds > 64)
4081 instance->supportmax256vd = 1;
4082
4083 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4084 * MEGASAS_MAX_DEV_PER_CHANNEL;
4085 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4086 * MEGASAS_MAX_DEV_PER_CHANNEL;
4087 if (instance->supportmax256vd) {
4088 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4089 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4090 } else {
4091 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4092 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4093 }
4094 dev_info(&instance->pdev->dev, "Firmware supports %d VD %d PD\n",
4095 instance->fw_supported_vd_count,
4096 instance->fw_supported_pd_count);
4097 dev_info(&instance->pdev->dev, "Driver supports %d VD %d PD\n",
4098 instance->drv_supported_vd_count,
4099 instance->drv_supported_pd_count);
4100
4101 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4102 (sizeof(struct MR_LD_SPAN_MAP) *
4103 (instance->fw_supported_vd_count - 1));
4104 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4105 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4106 (sizeof(struct MR_LD_SPAN_MAP) *
4107 (instance->drv_supported_vd_count - 1));
4108
4109 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4110
4111
4112 if (instance->supportmax256vd)
4113 fusion->current_map_sz = new_map_sz;
4114 else
4115 fusion->current_map_sz = old_map_sz;
4116
4117}
4118
adam radford21c9e162013-09-06 15:27:14 -07004119/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004120 * megasas_get_controller_info - Returns FW's controller structure
4121 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004122 *
4123 * Issues an internal command (DCMD) to get the FW's controller structure.
4124 * This information is mainly used to find out the maximum IO transfer per
4125 * command supported by the FW.
4126 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304127int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304128megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004129{
4130 int ret = 0;
4131 struct megasas_cmd *cmd;
4132 struct megasas_dcmd_frame *dcmd;
4133 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304134 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004135 dma_addr_t ci_h = 0;
4136
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304137 ctrl_info = instance->ctrl_info;
4138
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004139 cmd = megasas_get_cmd(instance);
4140
4141 if (!cmd) {
4142 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
4143 return -ENOMEM;
4144 }
4145
4146 dcmd = &cmd->frame->dcmd;
4147
4148 ci = pci_alloc_consistent(instance->pdev,
4149 sizeof(struct megasas_ctrl_info), &ci_h);
4150
4151 if (!ci) {
4152 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
4153 megasas_return_cmd(instance, cmd);
4154 return -ENOMEM;
4155 }
4156
4157 memset(ci, 0, sizeof(*ci));
4158 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4159
4160 dcmd->cmd = MFI_CMD_DCMD;
4161 dcmd->cmd_status = 0xFF;
4162 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304163 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004164 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004165 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304166 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4167 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4168 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4169 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304170 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004171
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304172 if (instance->ctrl_context && !instance->mask_interrupts)
4173 ret = megasas_issue_blocked_cmd(instance, cmd,
4174 MEGASAS_BLOCKED_CMD_TIMEOUT);
4175 else
4176 ret = megasas_issue_polled(instance, cmd);
4177
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304178 if (!ret) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004179 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304180 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4181 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4182 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4183 megasas_update_ext_vd_details(instance);
4184 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004185
4186 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4187 ci, ci_h);
4188
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304189 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4190 megasas_return_mfi_mpt_pthr(instance, cmd,
4191 cmd->mpt_pthr_cmd_blocked);
4192 else
4193 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004194 return ret;
4195}
4196
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304197/*
4198 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4199 * to firmware
4200 *
4201 * @instance: Adapter soft state
4202 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4203 MR_CRASH_BUF_TURN_OFF = 0
4204 MR_CRASH_BUF_TURN_ON = 1
4205 * @return 0 on success non-zero on failure.
4206 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4207 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4208 * that driver supports crash dump feature. This DCMD will be sent only if
4209 * crash dump feature is supported by the FW.
4210 *
4211 */
4212int megasas_set_crash_dump_params(struct megasas_instance *instance,
4213 u8 crash_buf_state)
4214{
4215 int ret = 0;
4216 struct megasas_cmd *cmd;
4217 struct megasas_dcmd_frame *dcmd;
4218
4219 cmd = megasas_get_cmd(instance);
4220
4221 if (!cmd) {
4222 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4223 return -ENOMEM;
4224 }
4225
4226
4227 dcmd = &cmd->frame->dcmd;
4228
4229 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4230 dcmd->mbox.b[0] = crash_buf_state;
4231 dcmd->cmd = MFI_CMD_DCMD;
4232 dcmd->cmd_status = 0xFF;
4233 dcmd->sge_count = 1;
4234 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4235 dcmd->timeout = 0;
4236 dcmd->pad_0 = 0;
4237 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4238 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4239 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4240 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4241
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304242 if (instance->ctrl_context && !instance->mask_interrupts)
4243 ret = megasas_issue_blocked_cmd(instance, cmd,
4244 MEGASAS_BLOCKED_CMD_TIMEOUT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304245 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304246 ret = megasas_issue_polled(instance, cmd);
4247
4248 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4249 megasas_return_mfi_mpt_pthr(instance, cmd,
4250 cmd->mpt_pthr_cmd_blocked);
4251 else
4252 megasas_return_cmd(instance, cmd);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304253 return ret;
4254}
4255
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004256/**
bo yang31ea7082007-11-07 12:09:50 -05004257 * megasas_issue_init_mfi - Initializes the FW
4258 * @instance: Adapter soft state
4259 *
4260 * Issues the INIT MFI cmd
4261 */
4262static int
4263megasas_issue_init_mfi(struct megasas_instance *instance)
4264{
4265 u32 context;
4266
4267 struct megasas_cmd *cmd;
4268
4269 struct megasas_init_frame *init_frame;
4270 struct megasas_init_queue_info *initq_info;
4271 dma_addr_t init_frame_h;
4272 dma_addr_t initq_info_h;
4273
4274 /*
4275 * Prepare a init frame. Note the init frame points to queue info
4276 * structure. Each frame has SGL allocated after first 64 bytes. For
4277 * this frame - since we don't need any SGL - we use SGL's space as
4278 * queue info structure
4279 *
4280 * We will not get a NULL command below. We just created the pool.
4281 */
4282 cmd = megasas_get_cmd(instance);
4283
4284 init_frame = (struct megasas_init_frame *)cmd->frame;
4285 initq_info = (struct megasas_init_queue_info *)
4286 ((unsigned long)init_frame + 64);
4287
4288 init_frame_h = cmd->frame_phys_addr;
4289 initq_info_h = init_frame_h + 64;
4290
4291 context = init_frame->context;
4292 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4293 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4294 init_frame->context = context;
4295
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304296 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4297 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004298
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304299 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4300 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004301
4302 init_frame->cmd = MFI_CMD_INIT;
4303 init_frame->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304304 init_frame->queue_info_new_phys_addr_lo =
4305 cpu_to_le32(lower_32_bits(initq_info_h));
4306 init_frame->queue_info_new_phys_addr_hi =
4307 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004308
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304309 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004310
4311 /*
4312 * disable the intr before firing the init frame to FW
4313 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304314 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004315
4316 /*
4317 * Issue the init frame in polled mode
4318 */
4319
4320 if (megasas_issue_polled(instance, cmd)) {
4321 printk(KERN_ERR "megasas: Failed to init firmware\n");
4322 megasas_return_cmd(instance, cmd);
4323 goto fail_fw_init;
4324 }
4325
4326 megasas_return_cmd(instance, cmd);
4327
4328 return 0;
4329
4330fail_fw_init:
4331 return -EINVAL;
4332}
4333
adam radfordcd50ba82010-12-21 10:23:23 -08004334static u32
4335megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004336{
adam radfordcd50ba82010-12-21 10:23:23 -08004337 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004338 u32 context_sz;
4339 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004340
4341 reg_set = instance->reg_set;
4342
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004343 /*
4344 * Get various operational parameters from status register
4345 */
Sumant Patro1341c932006-01-25 12:02:40 -08004346 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004347 /*
4348 * Reduce the max supported cmds by 1. This is to ensure that the
4349 * reply_q_sz (1 more than the max cmd that driver may send)
4350 * does not exceed max cmds that the FW can support
4351 */
4352 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004353 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004354 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004355 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004356 /*
4357 * Create a pool of commands
4358 */
4359 if (megasas_alloc_cmds(instance))
4360 goto fail_alloc_cmds;
4361
4362 /*
4363 * Allocate memory for reply queue. Length of reply queue should
4364 * be _one_ more than the maximum commands handled by the firmware.
4365 *
4366 * Note: When FW completes commands, it places corresponding contex
4367 * values in this circular reply queue. This circular queue is a fairly
4368 * typical producer-consumer queue. FW is the producer (of completed
4369 * commands) and the driver is the consumer.
4370 */
4371 context_sz = sizeof(u32);
4372 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4373
4374 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4375 reply_q_sz,
4376 &instance->reply_queue_h);
4377
4378 if (!instance->reply_queue) {
4379 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
4380 goto fail_reply_queue;
4381 }
4382
bo yang31ea7082007-11-07 12:09:50 -05004383 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004384 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004385
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304386 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304387 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4388 "Fail from %s %d\n", instance->unique_id,
4389 __func__, __LINE__);
4390 goto fail_fw_init;
4391 }
4392
bo yang39a98552010-09-22 22:36:29 -04004393 instance->fw_support_ieee = 0;
4394 instance->fw_support_ieee =
4395 (instance->instancet->read_fw_status_reg(reg_set) &
4396 0x04000000);
4397
4398 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
4399 instance->fw_support_ieee);
4400
4401 if (instance->fw_support_ieee)
4402 instance->flag_ieee = 1;
4403
adam radfordcd50ba82010-12-21 10:23:23 -08004404 return 0;
4405
4406fail_fw_init:
4407
4408 pci_free_consistent(instance->pdev, reply_q_sz,
4409 instance->reply_queue, instance->reply_queue_h);
4410fail_reply_queue:
4411 megasas_free_cmds(instance);
4412
4413fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004414 return 1;
4415}
4416
4417/**
4418 * megasas_init_fw - Initializes the FW
4419 * @instance: Adapter soft state
4420 *
4421 * This is the main function for initializing firmware
4422 */
4423
4424static int megasas_init_fw(struct megasas_instance *instance)
4425{
4426 u32 max_sectors_1;
4427 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304428 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04004429 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08004430 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304431 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08004432 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304433 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07004434 struct IOV_111 *iovPtr;
adam radfordcd50ba82010-12-21 10:23:23 -08004435
4436 /* Find first memory bar */
4437 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4438 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
adam radfordcd50ba82010-12-21 10:23:23 -08004439 if (pci_request_selected_regions(instance->pdev, instance->bar,
4440 "megasas: LSI")) {
4441 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
4442 return -EBUSY;
4443 }
4444
Ben Collins11f8a7b2013-09-13 12:46:44 -04004445 base_addr = pci_resource_start(instance->pdev, instance->bar);
4446 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08004447
4448 if (!instance->reg_set) {
4449 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
4450 goto fail_ioremap;
4451 }
4452
4453 reg_set = instance->reg_set;
4454
4455 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08004456 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07004457 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07004458 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304459 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08004460 instance->instancet = &megasas_instance_template_fusion;
4461 break;
adam radfordcd50ba82010-12-21 10:23:23 -08004462 case PCI_DEVICE_ID_LSI_SAS1078R:
4463 case PCI_DEVICE_ID_LSI_SAS1078DE:
4464 instance->instancet = &megasas_instance_template_ppc;
4465 break;
4466 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4467 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4468 instance->instancet = &megasas_instance_template_gen2;
4469 break;
4470 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4471 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4472 instance->instancet = &megasas_instance_template_skinny;
4473 break;
4474 case PCI_DEVICE_ID_LSI_SAS1064R:
4475 case PCI_DEVICE_ID_DELL_PERC5:
4476 default:
4477 instance->instancet = &megasas_instance_template_xscale;
4478 break;
4479 }
4480
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05304481 if (megasas_transition_to_ready(instance, 0)) {
4482 atomic_set(&instance->fw_reset_no_pci_access, 1);
4483 instance->instancet->adp_reset
4484 (instance, instance->reg_set);
4485 atomic_set(&instance->fw_reset_no_pci_access, 0);
4486 dev_info(&instance->pdev->dev,
4487 "megasas: FW restarted successfully from %s!\n",
4488 __func__);
4489
4490 /*waitting for about 30 second before retry*/
4491 ssleep(30);
4492
4493 if (megasas_transition_to_ready(instance, 0))
4494 goto fail_ready_state;
4495 }
adam radfordcd50ba82010-12-21 10:23:23 -08004496
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304497 /*
4498 * MSI-X host index 0 is common for all adapter.
4499 * It is used for all MPT based Adapters.
4500 */
4501 instance->reply_post_host_index_addr[0] =
4502 (u32 *)((u8 *)instance->reg_set +
4503 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4504
adam radford3f1abce2011-05-11 18:33:47 -07004505 /* Check if MSI-X is supported while in ready state */
4506 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4507 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07004508 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304509 scratch_pad_2 = readl
4510 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07004511 /* Check max MSI-X vectors */
adam radford229fe472014-03-10 02:51:56 -07004512 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4513 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA)) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304514 instance->msix_vectors = (scratch_pad_2
4515 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4516 fw_msix_count = instance->msix_vectors;
adam radford079eadd2012-10-01 19:26:59 -07004517 if (msix_vectors)
4518 instance->msix_vectors =
4519 min(msix_vectors,
4520 instance->msix_vectors);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304521 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
4522 || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
4523 /* Invader/Fury supports more than 8 MSI-X */
4524 instance->msix_vectors = ((scratch_pad_2
4525 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4526 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4527 fw_msix_count = instance->msix_vectors;
4528 /* Save 1-15 reply post index address to local memory
4529 * Index 0 is already saved from reg offset
4530 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4531 */
4532 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4533 instance->reply_post_host_index_addr[loop] =
4534 (u32 *)((u8 *)instance->reg_set +
4535 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4536 + (loop * 0x10));
4537 }
4538 if (msix_vectors)
4539 instance->msix_vectors = min(msix_vectors,
4540 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07004541 } else
4542 instance->msix_vectors = 1;
4543 /* Don't bother allocating more MSI-X vectors than cpus */
4544 instance->msix_vectors = min(instance->msix_vectors,
4545 (unsigned int)num_online_cpus());
4546 for (i = 0; i < instance->msix_vectors; i++)
4547 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004548 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4549 1, instance->msix_vectors);
Jiang Liuc12de882014-11-03 20:44:20 +08004550 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004551 instance->msix_vectors = i;
4552 else
adam radfordc8e858f2011-10-08 18:15:13 -07004553 instance->msix_vectors = 0;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304554
4555 dev_info(&instance->pdev->dev, "[scsi%d]: FW supports"
4556 "<%d> MSIX vector,Online CPUs: <%d>,"
4557 "Current MSIX <%d>\n", instance->host->host_no,
4558 fw_msix_count, (unsigned int)num_online_cpus(),
4559 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07004560 }
adam radford3f1abce2011-05-11 18:33:47 -07004561
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304562 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4563 GFP_KERNEL);
4564 if (instance->ctrl_info == NULL)
4565 goto fail_init_adapter;
4566
4567 /*
4568 * Below are default value for legacy Firmware.
4569 * non-fusion based controllers
4570 */
4571 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4572 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08004573 /* Get operational params, sge flags, send init cmd to controller */
4574 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08004575 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08004576
4577 printk(KERN_ERR "megasas: INIT adapter done\n");
4578
bo yang39a98552010-09-22 22:36:29 -04004579 /** for passthrough
4580 * the following function will get the PD LIST.
4581 */
4582
Yang, Bo81e403c2009-10-06 14:27:54 -06004583 memset(instance->pd_list, 0 ,
4584 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01004585 if (megasas_get_pd_list(instance) < 0) {
4586 printk(KERN_ERR "megasas: failed to get PD list\n");
4587 goto fail_init_adapter;
4588 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004589
Yang, Bobdc6fb82009-12-06 08:30:19 -07004590 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07004591 if (megasas_ld_list_query(instance,
4592 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4593 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004594
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004595 /*
4596 * Compute the max allowed sectors per IO: The controller info has two
4597 * limits on max sectors. Driver should use the minimum of these two.
4598 *
4599 * 1 << stripe_sz_ops.min = max sectors per strip
4600 *
4601 * Note that older firmwares ( < FW ver 30) didn't report information
4602 * to calculate max_sectors_1. So the number ended up as zero always.
4603 */
bo yang14faea92007-11-09 04:14:00 -05004604 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304605 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004606
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304607 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4608 le16_to_cpu(ctrl_info->max_strips_per_io);
4609 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004610
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304611 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304612
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304613 /*Check whether controller is iMR or MR */
4614 if (ctrl_info->memory_size) {
4615 instance->is_imr = 0;
4616 dev_info(&instance->pdev->dev, "Controller type: MR,"
4617 "Memory size is: %dMB\n",
4618 le16_to_cpu(ctrl_info->memory_size));
4619 } else {
4620 instance->is_imr = 1;
4621 dev_info(&instance->pdev->dev,
4622 "Controller type: iMR\n");
4623 }
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304624 instance->disableOnlineCtrlReset =
4625 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304626 instance->mpio = ctrl_info->adapterOperations2.mpio;
4627 instance->UnevenSpanSupport =
4628 ctrl_info->adapterOperations2.supportUnevenSpans;
4629 if (instance->UnevenSpanSupport) {
4630 struct fusion_context *fusion = instance->ctrl_context;
4631
4632 dev_info(&instance->pdev->dev, "FW supports: "
4633 "UnevenSpanSupport=%x\n", instance->UnevenSpanSupport);
4634 if (MR_ValidateMapInfo(instance))
4635 fusion->fast_path_io = 1;
4636 else
4637 fusion->fast_path_io = 0;
4638
4639 }
4640 if (ctrl_info->host_interface.SRIOV) {
4641 if (!ctrl_info->adapterOperations2.activePassive)
4642 instance->PlasmaFW111 = 1;
4643
4644 if (!instance->PlasmaFW111)
4645 instance->requestorId =
4646 ctrl_info->iov.requestorId;
4647 else {
4648 iovPtr = (struct IOV_111 *)((unsigned char *)ctrl_info + IOV_111_OFFSET);
4649 instance->requestorId = iovPtr->requestorId;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304650 }
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304651 dev_warn(&instance->pdev->dev, "I am VF "
4652 "requestorId %d\n", instance->requestorId);
4653 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05304654
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304655 instance->crash_dump_fw_support =
4656 ctrl_info->adapterOperations3.supportCrashDump;
4657 instance->crash_dump_drv_support =
4658 (instance->crash_dump_fw_support &&
4659 instance->crash_dump_buf);
4660 if (instance->crash_dump_drv_support) {
4661 dev_info(&instance->pdev->dev, "Firmware Crash dump "
4662 "feature is supported\n");
4663 megasas_set_crash_dump_params(instance,
4664 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07004665
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304666 } else {
4667 if (instance->crash_dump_buf)
4668 pci_free_consistent(instance->pdev,
4669 CRASH_DMA_BUF_SIZE,
4670 instance->crash_dump_buf,
4671 instance->crash_dump_h);
4672 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05004673 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05304674
4675 instance->secure_jbod_support =
4676 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4677 if (instance->secure_jbod_support)
4678 dev_info(&instance->pdev->dev, "Firmware supports Secure JBOD\n");
bo yang14faea92007-11-09 04:14:00 -05004679 instance->max_sectors_per_req = instance->max_num_sge *
4680 PAGE_SIZE / 512;
4681 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4682 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004683
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05304684 /*
4685 * 1. For fusion adapters, 3 commands for IOCTL and 5 commands
4686 * for driver's internal DCMDs.
4687 * 2. For MFI skinny adapters, 5 commands for IOCTL + driver's
4688 * internal DCMDs.
4689 * 3. For rest of MFI adapters, 27 commands reserved for IOCTLs
4690 * and 5 commands for drivers's internal DCMD.
4691 */
4692 if (instance->ctrl_context) {
4693 instance->max_scsi_cmds = instance->max_fw_cmds -
4694 (MEGASAS_FUSION_INTERNAL_CMDS +
4695 MEGASAS_FUSION_IOCTL_CMDS);
4696 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
4697 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4698 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4699 instance->max_scsi_cmds = instance->max_fw_cmds -
4700 MEGASAS_SKINNY_INT_CMDS;
4701 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
adam radfordc5daa6a2012-07-17 18:20:03 -07004702 } else {
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05304703 instance->max_scsi_cmds = instance->max_fw_cmds -
4704 MEGASAS_INT_CMDS;
4705 sema_init(&instance->ioctl_sem, (MEGASAS_INT_CMDS - 5));
adam radfordc5daa6a2012-07-17 18:20:03 -07004706 }
4707
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05304708 /* Check for valid throttlequeuedepth module parameter */
4709 if (throttlequeuedepth &&
4710 throttlequeuedepth <= instance->max_scsi_cmds)
4711 instance->throttlequeuedepth = throttlequeuedepth;
4712 else
4713 instance->throttlequeuedepth =
4714 MEGASAS_THROTTLE_QUEUE_DEPTH;
4715
Sumant Patro5d018ad2006-10-03 13:13:18 -07004716 /*
4717 * Setup tasklet for cmd completion
4718 */
4719
adam radfordf86c54242011-02-24 20:57:00 -08004720 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05004721 (unsigned long)instance);
4722
adam radford229fe472014-03-10 02:51:56 -07004723 /* Launch SR-IOV heartbeat timer */
4724 if (instance->requestorId) {
4725 if (!megasas_sriov_start_heartbeat(instance, 1))
4726 megasas_start_timer(instance,
4727 &instance->sriov_heartbeat_timer,
4728 megasas_sriov_heartbeat_handler,
4729 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4730 else
4731 instance->skip_heartbeat_timer_del = 1;
4732 }
4733
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004734 return 0;
4735
adam radfordeb1b1232011-02-24 20:55:56 -08004736fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08004737fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304738 kfree(instance->ctrl_info);
4739 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004740 iounmap(instance->reg_set);
4741
4742 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08004743 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004744
4745 return -EINVAL;
4746}
4747
4748/**
4749 * megasas_release_mfi - Reverses the FW initialization
4750 * @intance: Adapter soft state
4751 */
4752static void megasas_release_mfi(struct megasas_instance *instance)
4753{
adam radford9c915a82010-12-21 13:34:31 -08004754 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004755
adam radford9c915a82010-12-21 13:34:31 -08004756 if (instance->reply_queue)
4757 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004758 instance->reply_queue, instance->reply_queue_h);
4759
4760 megasas_free_cmds(instance);
4761
4762 iounmap(instance->reg_set);
4763
adam radfordb6d5d882010-12-14 18:56:07 -08004764 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004765}
4766
4767/**
4768 * megasas_get_seq_num - Gets latest event sequence numbers
4769 * @instance: Adapter soft state
4770 * @eli: FW event log sequence numbers information
4771 *
4772 * FW maintains a log of all events in a non-volatile area. Upper layers would
4773 * usually find out the latest sequence number of the events, the seq number at
4774 * the boot etc. They would "read" all the events below the latest seq number
4775 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4776 * number), they would subsribe to AEN (asynchronous event notification) and
4777 * wait for the events to happen.
4778 */
4779static int
4780megasas_get_seq_num(struct megasas_instance *instance,
4781 struct megasas_evt_log_info *eli)
4782{
4783 struct megasas_cmd *cmd;
4784 struct megasas_dcmd_frame *dcmd;
4785 struct megasas_evt_log_info *el_info;
4786 dma_addr_t el_info_h = 0;
4787
4788 cmd = megasas_get_cmd(instance);
4789
4790 if (!cmd) {
4791 return -ENOMEM;
4792 }
4793
4794 dcmd = &cmd->frame->dcmd;
4795 el_info = pci_alloc_consistent(instance->pdev,
4796 sizeof(struct megasas_evt_log_info),
4797 &el_info_h);
4798
4799 if (!el_info) {
4800 megasas_return_cmd(instance, cmd);
4801 return -ENOMEM;
4802 }
4803
4804 memset(el_info, 0, sizeof(*el_info));
4805 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4806
4807 dcmd->cmd = MFI_CMD_DCMD;
4808 dcmd->cmd_status = 0x0;
4809 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304810 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004811 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004812 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304813 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4814 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4815 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4816 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004817
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304818 if (megasas_issue_blocked_cmd(instance, cmd, 30))
4819 dev_err(&instance->pdev->dev, "Command timedout"
4820 "from %s\n", __func__);
4821 else {
4822 /*
4823 * Copy the data back into callers buffer
4824 */
4825 eli->newest_seq_num = le32_to_cpu(el_info->newest_seq_num);
4826 eli->oldest_seq_num = le32_to_cpu(el_info->oldest_seq_num);
4827 eli->clear_seq_num = le32_to_cpu(el_info->clear_seq_num);
4828 eli->shutdown_seq_num = le32_to_cpu(el_info->shutdown_seq_num);
4829 eli->boot_seq_num = le32_to_cpu(el_info->boot_seq_num);
4830 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004831
4832 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
4833 el_info, el_info_h);
4834
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304835 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4836 megasas_return_mfi_mpt_pthr(instance, cmd,
4837 cmd->mpt_pthr_cmd_blocked);
4838 else
4839 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004840
4841 return 0;
4842}
4843
4844/**
4845 * megasas_register_aen - Registers for asynchronous event notification
4846 * @instance: Adapter soft state
4847 * @seq_num: The starting sequence number
4848 * @class_locale: Class of the event
4849 *
4850 * This function subscribes for AEN for events beyond the @seq_num. It requests
4851 * to be notified if and only if the event is of type @class_locale
4852 */
4853static int
4854megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
4855 u32 class_locale_word)
4856{
4857 int ret_val;
4858 struct megasas_cmd *cmd;
4859 struct megasas_dcmd_frame *dcmd;
4860 union megasas_evt_class_locale curr_aen;
4861 union megasas_evt_class_locale prev_aen;
4862
4863 /*
4864 * If there an AEN pending already (aen_cmd), check if the
4865 * class_locale of that pending AEN is inclusive of the new
4866 * AEN request we currently have. If it is, then we don't have
4867 * to do anything. In other words, whichever events the current
4868 * AEN request is subscribing to, have already been subscribed
4869 * to.
4870 *
4871 * If the old_cmd is _not_ inclusive, then we have to abort
4872 * that command, form a class_locale that is superset of both
4873 * old and current and re-issue to the FW
4874 */
4875
4876 curr_aen.word = class_locale_word;
4877
4878 if (instance->aen_cmd) {
4879
4880 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304881 prev_aen.members.locale = le16_to_cpu(prev_aen.members.locale);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004882
4883 /*
4884 * A class whose enum value is smaller is inclusive of all
4885 * higher values. If a PROGRESS (= -1) was previously
4886 * registered, then a new registration requests for higher
4887 * classes need not be sent to FW. They are automatically
4888 * included.
4889 *
4890 * Locale numbers don't have such hierarchy. They are bitmap
4891 * values
4892 */
4893 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304894 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004895 curr_aen.members.locale)) {
4896 /*
4897 * Previously issued event registration includes
4898 * current request. Nothing to do.
4899 */
4900 return 0;
4901 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304902 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004903
4904 if (prev_aen.members.class < curr_aen.members.class)
4905 curr_aen.members.class = prev_aen.members.class;
4906
4907 instance->aen_cmd->abort_aen = 1;
4908 ret_val = megasas_issue_blocked_abort_cmd(instance,
4909 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304910 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004911
4912 if (ret_val) {
4913 printk(KERN_DEBUG "megasas: Failed to abort "
4914 "previous AEN command\n");
4915 return ret_val;
4916 }
4917 }
4918 }
4919
4920 cmd = megasas_get_cmd(instance);
4921
4922 if (!cmd)
4923 return -ENOMEM;
4924
4925 dcmd = &cmd->frame->dcmd;
4926
4927 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4928
4929 /*
4930 * Prepare DCMD for aen registration
4931 */
4932 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4933
4934 dcmd->cmd = MFI_CMD_DCMD;
4935 dcmd->cmd_status = 0x0;
4936 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304937 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004938 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004939 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304940 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4941 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4942 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04004943 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304944 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4945 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4946 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004947
Yang, Bof4c9a132009-10-06 14:43:28 -06004948 if (instance->aen_cmd != NULL) {
4949 megasas_return_cmd(instance, cmd);
4950 return 0;
4951 }
4952
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004953 /*
4954 * Store reference to the cmd used to register for AEN. When an
4955 * application wants us to register for AEN, we have to abort this
4956 * cmd and re-register with a new EVENT LOCALE supplied by that app
4957 */
4958 instance->aen_cmd = cmd;
4959
4960 /*
4961 * Issue the aen registration frame
4962 */
adam radford9c915a82010-12-21 13:34:31 -08004963 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004964
4965 return 0;
4966}
4967
4968/**
4969 * megasas_start_aen - Subscribes to AEN during driver load time
4970 * @instance: Adapter soft state
4971 */
4972static int megasas_start_aen(struct megasas_instance *instance)
4973{
4974 struct megasas_evt_log_info eli;
4975 union megasas_evt_class_locale class_locale;
4976
4977 /*
4978 * Get the latest sequence number from FW
4979 */
4980 memset(&eli, 0, sizeof(eli));
4981
4982 if (megasas_get_seq_num(instance, &eli))
4983 return -1;
4984
4985 /*
4986 * Register AEN with FW for latest sequence number plus 1
4987 */
4988 class_locale.members.reserved = 0;
4989 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4990 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4991
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304992 return megasas_register_aen(instance,
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304993 eli.newest_seq_num + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304994 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004995}
4996
4997/**
4998 * megasas_io_attach - Attaches this driver to SCSI mid-layer
4999 * @instance: Adapter soft state
5000 */
5001static int megasas_io_attach(struct megasas_instance *instance)
5002{
5003 struct Scsi_Host *host = instance->host;
5004
5005 /*
5006 * Export parameters required by SCSI mid-layer
5007 */
5008 host->irq = instance->pdev->irq;
5009 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305010 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005011 host->this_id = instance->init_id;
5012 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005013
5014 if (instance->fw_support_ieee)
5015 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5016
Yang, Bo1fd10682010-10-12 07:18:50 -06005017 /*
5018 * Check if the module parameter value for max_sectors can be used
5019 */
5020 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5021 instance->max_sectors_per_req = max_sectors;
5022 else {
5023 if (max_sectors) {
5024 if (((instance->pdev->device ==
5025 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5026 (instance->pdev->device ==
5027 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5028 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5029 instance->max_sectors_per_req = max_sectors;
5030 } else {
5031 printk(KERN_INFO "megasas: max_sectors should be > 0"
5032 "and <= %d (or < 1MB for GEN2 controller)\n",
5033 instance->max_sectors_per_req);
5034 }
5035 }
5036 }
5037
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005038 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005039 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005040 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5041 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5042 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005043 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005044
adam radford9c915a82010-12-21 13:34:31 -08005045 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07005046 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005047 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305048 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5049 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08005050 host->hostt->eh_device_reset_handler = NULL;
5051 host->hostt->eh_bus_reset_handler = NULL;
5052 }
5053
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005054 /*
5055 * Notify the mid-layer about the new controller
5056 */
5057 if (scsi_add_host(host, &instance->pdev->dev)) {
5058 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
5059 return -ENODEV;
5060 }
5061
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005062 return 0;
5063}
5064
bo yang31ea7082007-11-07 12:09:50 -05005065static int
5066megasas_set_dma_mask(struct pci_dev *pdev)
5067{
5068 /*
5069 * All our contollers are capable of performing 64-bit DMA
5070 */
5071 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005072 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005073
Yang Hongyang284901a2009-04-06 19:01:15 -07005074 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005075 goto fail_set_dma_mask;
5076 }
5077 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005078 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005079 goto fail_set_dma_mask;
5080 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305081 /*
5082 * Ensure that all data structures are allocated in 32-bit
5083 * memory.
5084 */
5085 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5086 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5087 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5088 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5089 dev_info(&pdev->dev, "set 32bit DMA mask"
5090 "and 32 bit consistent mask\n");
5091 else
5092 goto fail_set_dma_mask;
5093 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305094
bo yang31ea7082007-11-07 12:09:50 -05005095 return 0;
5096
5097fail_set_dma_mask:
5098 return 1;
5099}
5100
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005101/**
5102 * megasas_probe_one - PCI hotplug entry point
5103 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005104 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005105 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005106static int megasas_probe_one(struct pci_dev *pdev,
5107 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005108{
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305109 int rval, pos, i, j, cpu;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005110 struct Scsi_Host *host;
5111 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005112 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305113 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005114
5115 /* Reset MSI-X in the kdump kernel */
5116 if (reset_devices) {
5117 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5118 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005119 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005120 &control);
5121 if (control & PCI_MSIX_FLAGS_ENABLE) {
5122 dev_info(&pdev->dev, "resetting MSI-X\n");
5123 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005124 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005125 control &
5126 ~PCI_MSIX_FLAGS_ENABLE);
5127 }
5128 }
5129 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005130
5131 /*
5132 * Announce PCI information
5133 */
5134 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
5135 pdev->vendor, pdev->device, pdev->subsystem_vendor,
5136 pdev->subsystem_device);
5137
5138 printk("bus %d:slot %d:func %d\n",
5139 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
5140
5141 /*
5142 * PCI prepping: enable device set bus mastering and dma mask
5143 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005144 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005145
5146 if (rval) {
5147 return rval;
5148 }
5149
5150 pci_set_master(pdev);
5151
bo yang31ea7082007-11-07 12:09:50 -05005152 if (megasas_set_dma_mask(pdev))
5153 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005154
5155 host = scsi_host_alloc(&megasas_template,
5156 sizeof(struct megasas_instance));
5157
5158 if (!host) {
5159 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
5160 goto fail_alloc_instance;
5161 }
5162
5163 instance = (struct megasas_instance *)host->hostdata;
5164 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04005165 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08005166 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005167
adam radford9c915a82010-12-21 13:34:31 -08005168 switch (instance->pdev->device) {
5169 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005170 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005171 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305172 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005173 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305174 instance->ctrl_context_pages =
5175 get_order(sizeof(struct fusion_context));
5176 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5177 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005178 if (!instance->ctrl_context) {
5179 printk(KERN_DEBUG "megasas: Failed to allocate "
5180 "memory for Fusion context info\n");
5181 goto fail_alloc_dma_buf;
5182 }
5183 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305184 memset(fusion, 0,
5185 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
adam radford9c915a82010-12-21 13:34:31 -08005186 INIT_LIST_HEAD(&fusion->cmd_pool);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305187 spin_lock_init(&fusion->mpt_pool_lock);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305188 memset(fusion->load_balance_info, 0,
5189 sizeof(struct LD_LOAD_BALANCE_INFO) * MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08005190 }
5191 break;
5192 default: /* For all other supported controllers */
5193
5194 instance->producer =
5195 pci_alloc_consistent(pdev, sizeof(u32),
5196 &instance->producer_h);
5197 instance->consumer =
5198 pci_alloc_consistent(pdev, sizeof(u32),
5199 &instance->consumer_h);
5200
5201 if (!instance->producer || !instance->consumer) {
5202 printk(KERN_DEBUG "megasas: Failed to allocate"
5203 "memory for producer, consumer\n");
5204 goto fail_alloc_dma_buf;
5205 }
5206
5207 *instance->producer = 0;
5208 *instance->consumer = 0;
5209 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005210 }
5211
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305212 /* Crash dump feature related initialisation*/
5213 instance->drv_buf_index = 0;
5214 instance->drv_buf_alloc = 0;
5215 instance->crash_dump_fw_support = 0;
5216 instance->crash_dump_app_support = 0;
5217 instance->fw_crash_state = UNAVAILABLE;
5218 spin_lock_init(&instance->crashdump_lock);
5219 instance->crash_dump_buf = NULL;
5220
5221 if (!reset_devices)
5222 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5223 CRASH_DMA_BUF_SIZE,
5224 &instance->crash_dump_h);
5225 if (!instance->crash_dump_buf)
5226 dev_err(&instance->pdev->dev, "Can't allocate Firmware "
5227 "crash dump DMA buffer\n");
5228
Yang, Boc3518832009-10-06 14:18:02 -06005229 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005230 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005231 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005232 instance->issuepend_done = 1;
5233 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305234 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005235
5236 instance->evt_detail = pci_alloc_consistent(pdev,
5237 sizeof(struct
5238 megasas_evt_detail),
5239 &instance->evt_detail_h);
5240
5241 if (!instance->evt_detail) {
5242 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
5243 "event detail structure\n");
5244 goto fail_alloc_dma_buf;
5245 }
5246
5247 /*
5248 * Initialize locks and queues
5249 */
5250 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005251 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005252
Sumant Patroe4a082c2006-05-30 12:03:37 -07005253 atomic_set(&instance->fw_outstanding,0);
5254
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005255 init_waitqueue_head(&instance->int_cmd_wait_q);
5256 init_waitqueue_head(&instance->abort_cmd_wait_q);
5257
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305258 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005259 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005260 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005261
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005262 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08005263 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005264
5265 /*
5266 * Initialize PCI related and misc parameters
5267 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005268 instance->host = host;
5269 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5270 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305271 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005272
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305273
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005274 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305275 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005276 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005277
Sumant Patro658dced2006-10-03 13:09:14 -07005278 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005279 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005280 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005281 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005282 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305283 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005284
adam radford36807e62011-10-08 18:15:06 -07005285 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005286 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305287 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305288 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08005289 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305290 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5291 } else
adam radford9c915a82010-12-21 13:34:31 -08005292 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005293
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005294 /*
adam radford0a770662011-02-24 20:56:12 -08005295 * Initialize MFI Firmware
5296 */
5297 if (megasas_init_fw(instance))
5298 goto fail_init_mfi;
5299
adam radford229fe472014-03-10 02:51:56 -07005300 if (instance->requestorId) {
5301 if (instance->PlasmaFW111) {
5302 instance->vf_affiliation_111 =
5303 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5304 &instance->vf_affiliation_111_h);
5305 if (!instance->vf_affiliation_111)
5306 printk(KERN_WARNING "megasas: Can't allocate "
5307 "memory for VF affiliation buffer\n");
5308 } else {
5309 instance->vf_affiliation =
5310 pci_alloc_consistent(pdev,
5311 (MAX_LOGICAL_DRIVES + 1) *
5312 sizeof(struct MR_LD_VF_AFFILIATION),
5313 &instance->vf_affiliation_h);
5314 if (!instance->vf_affiliation)
5315 printk(KERN_WARNING "megasas: Can't allocate "
5316 "memory for VF affiliation buffer\n");
5317 }
5318 }
5319
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305320retry_irq_register:
adam radford0a770662011-02-24 20:56:12 -08005321 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005322 * Register IRQ
5323 */
adam radfordc8e858f2011-10-08 18:15:13 -07005324 if (instance->msix_vectors) {
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305325 cpu = cpumask_first(cpu_online_mask);
5326 for (i = 0; i < instance->msix_vectors; i++) {
adam radfordc8e858f2011-10-08 18:15:13 -07005327 instance->irq_context[i].instance = instance;
5328 instance->irq_context[i].MSIxIndex = i;
5329 if (request_irq(instance->msixentry[i].vector,
5330 instance->instancet->service_isr, 0,
5331 "megasas",
5332 &instance->irq_context[i])) {
5333 printk(KERN_DEBUG "megasas: Failed to "
5334 "register IRQ for vector %d.\n", i);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305335 for (j = 0; j < i; j++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305336 if (smp_affinity_enable)
5337 irq_set_affinity_hint(
5338 instance->msixentry[j].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005339 free_irq(
5340 instance->msixentry[j].vector,
5341 &instance->irq_context[j]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305342 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305343 /* Retry irq register for IO_APIC */
5344 instance->msix_vectors = 0;
5345 goto retry_irq_register;
adam radfordc8e858f2011-10-08 18:15:13 -07005346 }
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305347 if (smp_affinity_enable) {
5348 if (irq_set_affinity_hint(instance->msixentry[i].vector,
5349 get_cpu_mask(cpu)))
5350 dev_err(&instance->pdev->dev,
5351 "Error setting affinity hint "
5352 "for cpu %d\n", cpu);
5353 cpu = cpumask_next(cpu, cpu_online_mask);
5354 }
adam radfordc8e858f2011-10-08 18:15:13 -07005355 }
5356 } else {
5357 instance->irq_context[0].instance = instance;
5358 instance->irq_context[0].MSIxIndex = 0;
5359 if (request_irq(pdev->irq, instance->instancet->service_isr,
5360 IRQF_SHARED, "megasas",
5361 &instance->irq_context[0])) {
5362 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5363 goto fail_irq;
5364 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005365 }
5366
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305367 instance->instancet->enable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005368
5369 /*
5370 * Store instance in PCI softstate
5371 */
5372 pci_set_drvdata(pdev, instance);
5373
5374 /*
5375 * Add this controller to megasas_mgmt_info structure so that it
5376 * can be exported to management applications
5377 */
5378 megasas_mgmt_info.count++;
5379 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5380 megasas_mgmt_info.max_index++;
5381
5382 /*
adam radford541f90b2011-05-11 18:34:29 -07005383 * Register with SCSI mid-layer
5384 */
5385 if (megasas_io_attach(instance))
5386 goto fail_io_attach;
5387
5388 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305389 /*
5390 * Trigger SCSI to scan our drives
5391 */
5392 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005393
5394 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005395 * Initiate AEN (Asynchronous Event Notification)
5396 */
5397 if (megasas_start_aen(instance)) {
5398 printk(KERN_DEBUG "megasas: start aen failed\n");
5399 goto fail_start_aen;
5400 }
5401
Adam Radford9ea81f82014-07-09 15:17:57 -07005402 /* Get current SR-IOV LD/VF affiliation */
5403 if (instance->requestorId)
5404 megasas_get_ld_vf_affiliation(instance, 1);
5405
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005406 return 0;
5407
5408 fail_start_aen:
5409 fail_io_attach:
5410 megasas_mgmt_info.count--;
5411 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5412 megasas_mgmt_info.max_index--;
5413
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305414 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005415 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305416 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305417 if (smp_affinity_enable)
5418 irq_set_affinity_hint(
5419 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005420 free_irq(instance->msixentry[i].vector,
5421 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305422 }
adam radfordc8e858f2011-10-08 18:15:13 -07005423 else
5424 free_irq(instance->pdev->irq, &instance->irq_context[0]);
adam radfordeb1b1232011-02-24 20:55:56 -08005425fail_irq:
adam radford36807e62011-10-08 18:15:06 -07005426 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005427 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305428 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5429 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radfordeb1b1232011-02-24 20:55:56 -08005430 megasas_release_fusion(instance);
5431 else
5432 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005433 fail_init_mfi:
adam radfordc8e858f2011-10-08 18:15:13 -07005434 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005435 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005436 fail_alloc_dma_buf:
5437 if (instance->evt_detail)
5438 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5439 instance->evt_detail,
5440 instance->evt_detail_h);
5441
adam radfordeb1b1232011-02-24 20:55:56 -08005442 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005443 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5444 instance->producer_h);
5445 if (instance->consumer)
5446 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5447 instance->consumer_h);
5448 scsi_host_put(host);
5449
5450 fail_alloc_instance:
5451 fail_set_dma_mask:
5452 pci_disable_device(pdev);
5453
5454 return -ENODEV;
5455}
5456
5457/**
5458 * megasas_flush_cache - Requests FW to flush all its caches
5459 * @instance: Adapter soft state
5460 */
5461static void megasas_flush_cache(struct megasas_instance *instance)
5462{
5463 struct megasas_cmd *cmd;
5464 struct megasas_dcmd_frame *dcmd;
5465
bo yang39a98552010-09-22 22:36:29 -04005466 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5467 return;
5468
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005469 cmd = megasas_get_cmd(instance);
5470
5471 if (!cmd)
5472 return;
5473
5474 dcmd = &cmd->frame->dcmd;
5475
5476 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5477
5478 dcmd->cmd = MFI_CMD_DCMD;
5479 dcmd->cmd_status = 0x0;
5480 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305481 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005482 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005483 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005484 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305485 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005486 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5487
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305488 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5489 dev_err(&instance->pdev->dev, "Command timedout"
5490 " from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005491
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305492 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5493 megasas_return_mfi_mpt_pthr(instance, cmd,
5494 cmd->mpt_pthr_cmd_blocked);
5495 else
5496 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005497
5498 return;
5499}
5500
5501/**
5502 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5503 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05005504 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005505 */
bo yang31ea7082007-11-07 12:09:50 -05005506static void megasas_shutdown_controller(struct megasas_instance *instance,
5507 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005508{
5509 struct megasas_cmd *cmd;
5510 struct megasas_dcmd_frame *dcmd;
5511
bo yang39a98552010-09-22 22:36:29 -04005512 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5513 return;
5514
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005515 cmd = megasas_get_cmd(instance);
5516
5517 if (!cmd)
5518 return;
5519
5520 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305521 megasas_issue_blocked_abort_cmd(instance,
5522 instance->aen_cmd, 30);
adam radford9c915a82010-12-21 13:34:31 -08005523 if (instance->map_update_cmd)
5524 megasas_issue_blocked_abort_cmd(instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305525 instance->map_update_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005526 dcmd = &cmd->frame->dcmd;
5527
5528 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5529
5530 dcmd->cmd = MFI_CMD_DCMD;
5531 dcmd->cmd_status = 0x0;
5532 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305533 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005534 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005535 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005536 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305537 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005538
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305539 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5540 dev_err(&instance->pdev->dev, "Command timedout"
5541 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005542
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305543 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5544 megasas_return_mfi_mpt_pthr(instance, cmd,
5545 cmd->mpt_pthr_cmd_blocked);
5546 else
5547 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005548
5549 return;
5550}
5551
Jiri Slaby33139b22008-05-01 17:56:02 +02005552#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005553/**
bo yangad84db22007-11-09 04:40:16 -05005554 * megasas_suspend - driver suspend entry point
5555 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05005556 * @state: PCI power state to suspend routine
5557 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005558static int
bo yang31ea7082007-11-07 12:09:50 -05005559megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5560{
5561 struct Scsi_Host *host;
5562 struct megasas_instance *instance;
adam radfordc8e858f2011-10-08 18:15:13 -07005563 int i;
bo yang31ea7082007-11-07 12:09:50 -05005564
5565 instance = pci_get_drvdata(pdev);
5566 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06005567 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05005568
adam radford229fe472014-03-10 02:51:56 -07005569 /* Shutdown SR-IOV heartbeat timer */
5570 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5571 del_timer_sync(&instance->sriov_heartbeat_timer);
5572
bo yang31ea7082007-11-07 12:09:50 -05005573 megasas_flush_cache(instance);
5574 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005575
5576 /* cancel the delayed work if this work still in queue */
5577 if (instance->ev != NULL) {
5578 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005579 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005580 instance->ev = NULL;
5581 }
5582
bo yang31ea7082007-11-07 12:09:50 -05005583 tasklet_kill(&instance->isr_tasklet);
5584
5585 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305586 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005587
5588 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305589 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305590 if (smp_affinity_enable)
5591 irq_set_affinity_hint(
5592 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005593 free_irq(instance->msixentry[i].vector,
5594 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305595 }
adam radfordc8e858f2011-10-08 18:15:13 -07005596 else
5597 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5598 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005599 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05005600
5601 pci_save_state(pdev);
5602 pci_disable_device(pdev);
5603
5604 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5605
5606 return 0;
5607}
5608
5609/**
5610 * megasas_resume- driver resume entry point
5611 * @pdev: PCI device structure
5612 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005613static int
bo yang31ea7082007-11-07 12:09:50 -05005614megasas_resume(struct pci_dev *pdev)
5615{
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305616 int rval, i, j, cpu;
bo yang31ea7082007-11-07 12:09:50 -05005617 struct Scsi_Host *host;
5618 struct megasas_instance *instance;
5619
5620 instance = pci_get_drvdata(pdev);
5621 host = instance->host;
5622 pci_set_power_state(pdev, PCI_D0);
5623 pci_enable_wake(pdev, PCI_D0, 0);
5624 pci_restore_state(pdev);
5625
5626 /*
5627 * PCI prepping: enable device set bus mastering and dma mask
5628 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005629 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05005630
5631 if (rval) {
5632 printk(KERN_ERR "megasas: Enable device failed\n");
5633 return rval;
5634 }
5635
5636 pci_set_master(pdev);
5637
5638 if (megasas_set_dma_mask(pdev))
5639 goto fail_set_dma_mask;
5640
5641 /*
5642 * Initialize MFI Firmware
5643 */
5644
bo yang31ea7082007-11-07 12:09:50 -05005645 atomic_set(&instance->fw_outstanding, 0);
5646
5647 /*
5648 * We expect the FW state to be READY
5649 */
adam radford058a8fa2011-10-08 18:14:27 -07005650 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05005651 goto fail_ready_state;
5652
adam radford3f1abce2011-05-11 18:33:47 -07005653 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02005654 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005655 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5656 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02005657 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07005658
adam radford9c915a82010-12-21 13:34:31 -08005659 switch (instance->pdev->device) {
5660 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005661 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005662 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305663 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005664 {
5665 megasas_reset_reply_desc(instance);
5666 if (megasas_ioc_init_fusion(instance)) {
5667 megasas_free_cmds(instance);
5668 megasas_free_cmds_fusion(instance);
5669 goto fail_init_mfi;
5670 }
5671 if (!megasas_get_map_info(instance))
5672 megasas_sync_map_info(instance);
5673 }
5674 break;
5675 default:
5676 *instance->producer = 0;
5677 *instance->consumer = 0;
5678 if (megasas_issue_init_mfi(instance))
5679 goto fail_init_mfi;
5680 break;
5681 }
bo yang31ea7082007-11-07 12:09:50 -05005682
adam radford9c915a82010-12-21 13:34:31 -08005683 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5684 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05005685
5686 /*
5687 * Register IRQ
5688 */
adam radfordc8e858f2011-10-08 18:15:13 -07005689 if (instance->msix_vectors) {
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305690 cpu = cpumask_first(cpu_online_mask);
adam radfordc8e858f2011-10-08 18:15:13 -07005691 for (i = 0 ; i < instance->msix_vectors; i++) {
5692 instance->irq_context[i].instance = instance;
5693 instance->irq_context[i].MSIxIndex = i;
5694 if (request_irq(instance->msixentry[i].vector,
5695 instance->instancet->service_isr, 0,
5696 "megasas",
5697 &instance->irq_context[i])) {
5698 printk(KERN_DEBUG "megasas: Failed to "
5699 "register IRQ for vector %d.\n", i);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305700 for (j = 0; j < i; j++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305701 if (smp_affinity_enable)
5702 irq_set_affinity_hint(
5703 instance->msixentry[j].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005704 free_irq(
5705 instance->msixentry[j].vector,
5706 &instance->irq_context[j]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305707 }
adam radfordc8e858f2011-10-08 18:15:13 -07005708 goto fail_irq;
5709 }
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305710
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305711 if (smp_affinity_enable) {
5712 if (irq_set_affinity_hint(instance->msixentry[i].vector,
5713 get_cpu_mask(cpu)))
5714 dev_err(&instance->pdev->dev, "Error "
5715 "setting affinity hint for cpu "
5716 "%d\n", cpu);
5717 cpu = cpumask_next(cpu, cpu_online_mask);
5718 }
adam radfordc8e858f2011-10-08 18:15:13 -07005719 }
5720 } else {
5721 instance->irq_context[0].instance = instance;
5722 instance->irq_context[0].MSIxIndex = 0;
5723 if (request_irq(pdev->irq, instance->instancet->service_isr,
5724 IRQF_SHARED, "megasas",
5725 &instance->irq_context[0])) {
5726 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5727 goto fail_irq;
5728 }
bo yang31ea7082007-11-07 12:09:50 -05005729 }
5730
adam radford229fe472014-03-10 02:51:56 -07005731 /* Re-launch SR-IOV heartbeat timer */
5732 if (instance->requestorId) {
5733 if (!megasas_sriov_start_heartbeat(instance, 0))
5734 megasas_start_timer(instance,
5735 &instance->sriov_heartbeat_timer,
5736 megasas_sriov_heartbeat_handler,
5737 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5738 else
5739 instance->skip_heartbeat_timer_del = 1;
5740 }
5741
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305742 instance->instancet->enable_intr(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06005743 instance->unload = 0;
5744
adam radford541f90b2011-05-11 18:34:29 -07005745 /*
5746 * Initiate AEN (Asynchronous Event Notification)
5747 */
5748 if (megasas_start_aen(instance))
5749 printk(KERN_ERR "megasas: Start AEN failed\n");
5750
bo yang31ea7082007-11-07 12:09:50 -05005751 return 0;
5752
5753fail_irq:
5754fail_init_mfi:
5755 if (instance->evt_detail)
5756 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5757 instance->evt_detail,
5758 instance->evt_detail_h);
5759
5760 if (instance->producer)
5761 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5762 instance->producer_h);
5763 if (instance->consumer)
5764 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5765 instance->consumer_h);
5766 scsi_host_put(host);
5767
5768fail_set_dma_mask:
5769fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02005770fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05005771
5772 pci_disable_device(pdev);
5773
5774 return -ENODEV;
5775}
Jiri Slaby33139b22008-05-01 17:56:02 +02005776#else
5777#define megasas_suspend NULL
5778#define megasas_resume NULL
5779#endif
bo yang31ea7082007-11-07 12:09:50 -05005780
5781/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005782 * megasas_detach_one - PCI hot"un"plug entry point
5783 * @pdev: PCI device structure
5784 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005785static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005786{
5787 int i;
5788 struct Scsi_Host *host;
5789 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08005790 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005791
5792 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06005793 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005794 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08005795 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005796
adam radford229fe472014-03-10 02:51:56 -07005797 /* Shutdown SR-IOV heartbeat timer */
5798 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5799 del_timer_sync(&instance->sriov_heartbeat_timer);
5800
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305801 if (instance->fw_crash_state != UNAVAILABLE)
5802 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005803 scsi_remove_host(instance->host);
5804 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05005805 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005806
5807 /* cancel the delayed work if this work still in queue*/
5808 if (instance->ev != NULL) {
5809 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005810 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005811 instance->ev = NULL;
5812 }
5813
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305814 /* cancel all wait events */
5815 wake_up_all(&instance->int_cmd_wait_q);
5816
Sumant Patro5d018ad2006-10-03 13:13:18 -07005817 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005818
5819 /*
5820 * Take the instance off the instance array. Note that we will not
5821 * decrement the max_index. We let this array be sparse array
5822 */
5823 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5824 if (megasas_mgmt_info.instance[i] == instance) {
5825 megasas_mgmt_info.count--;
5826 megasas_mgmt_info.instance[i] = NULL;
5827
5828 break;
5829 }
5830 }
5831
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305832 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005833
adam radfordc8e858f2011-10-08 18:15:13 -07005834 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305835 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305836 if (smp_affinity_enable)
5837 irq_set_affinity_hint(
5838 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005839 free_irq(instance->msixentry[i].vector,
5840 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305841 }
adam radfordc8e858f2011-10-08 18:15:13 -07005842 else
5843 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5844 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005845 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005846
adam radford9c915a82010-12-21 13:34:31 -08005847 switch (instance->pdev->device) {
5848 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005849 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005850 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305851 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005852 megasas_release_fusion(instance);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305853 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08005854 if (fusion->ld_map[i])
5855 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305856 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08005857 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305858 fusion->ld_map_phys[i]);
5859 if (fusion->ld_drv_map[i])
5860 free_pages((ulong)fusion->ld_drv_map[i],
5861 fusion->drv_map_pages);
5862 }
5863 free_pages((ulong)instance->ctrl_context,
5864 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005865 break;
5866 default:
5867 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08005868 pci_free_consistent(pdev, sizeof(u32),
5869 instance->producer,
5870 instance->producer_h);
5871 pci_free_consistent(pdev, sizeof(u32),
5872 instance->consumer,
5873 instance->consumer_h);
5874 break;
5875 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005876
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305877 kfree(instance->ctrl_info);
5878
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05305879 if (instance->evt_detail)
5880 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5881 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07005882
5883 if (instance->vf_affiliation)
5884 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5885 sizeof(struct MR_LD_VF_AFFILIATION),
5886 instance->vf_affiliation,
5887 instance->vf_affiliation_h);
5888
5889 if (instance->vf_affiliation_111)
5890 pci_free_consistent(pdev,
5891 sizeof(struct MR_LD_VF_AFFILIATION_111),
5892 instance->vf_affiliation_111,
5893 instance->vf_affiliation_111_h);
5894
5895 if (instance->hb_host_mem)
5896 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5897 instance->hb_host_mem,
5898 instance->hb_host_mem_h);
5899
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305900 if (instance->crash_dump_buf)
5901 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5902 instance->crash_dump_buf, instance->crash_dump_h);
5903
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005904 scsi_host_put(host);
5905
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005906 pci_disable_device(pdev);
5907
5908 return;
5909}
5910
5911/**
5912 * megasas_shutdown - Shutdown entry point
5913 * @device: Generic device structure
5914 */
5915static void megasas_shutdown(struct pci_dev *pdev)
5916{
adam radfordc8e858f2011-10-08 18:15:13 -07005917 int i;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005918 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07005919
Yang, Bo0c79e682009-10-06 14:47:35 -06005920 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005921 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07005922 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305923 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005924 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305925 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305926 if (smp_affinity_enable)
5927 irq_set_affinity_hint(
5928 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005929 free_irq(instance->msixentry[i].vector,
5930 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305931 }
adam radfordc8e858f2011-10-08 18:15:13 -07005932 else
5933 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5934 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07005935 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005936}
5937
5938/**
5939 * megasas_mgmt_open - char node "open" entry point
5940 */
5941static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5942{
5943 /*
5944 * Allow only those users with admin rights
5945 */
5946 if (!capable(CAP_SYS_ADMIN))
5947 return -EACCES;
5948
5949 return 0;
5950}
5951
5952/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005953 * megasas_mgmt_fasync - Async notifier registration from applications
5954 *
5955 * This function adds the calling process to a driver global queue. When an
5956 * event occurs, SIGIO will be sent to all processes in this queue.
5957 */
5958static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5959{
5960 int rc;
5961
Arjan van de Ven0b950672006-01-11 13:16:10 +01005962 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005963
5964 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
5965
Arjan van de Ven0b950672006-01-11 13:16:10 +01005966 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005967
5968 if (rc >= 0) {
5969 /* For sanity check when we get ioctl */
5970 filep->private_data = filep;
5971 return 0;
5972 }
5973
5974 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
5975
5976 return rc;
5977}
5978
5979/**
Yang, Boc3518832009-10-06 14:18:02 -06005980 * megasas_mgmt_poll - char node "poll" entry point
5981 * */
5982static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
5983{
5984 unsigned int mask;
5985 unsigned long flags;
5986 poll_wait(file, &megasas_poll_wait, wait);
5987 spin_lock_irqsave(&poll_aen_lock, flags);
5988 if (megasas_poll_wait_aen)
5989 mask = (POLLIN | POLLRDNORM);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305990
Yang, Boc3518832009-10-06 14:18:02 -06005991 else
5992 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305993 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06005994 spin_unlock_irqrestore(&poll_aen_lock, flags);
5995 return mask;
5996}
5997
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305998/*
5999 * megasas_set_crash_dump_params_ioctl:
6000 * Send CRASH_DUMP_MODE DCMD to all controllers
6001 * @cmd: MFI command frame
6002 */
6003
6004static int megasas_set_crash_dump_params_ioctl(
6005 struct megasas_cmd *cmd)
6006{
6007 struct megasas_instance *local_instance;
6008 int i, error = 0;
6009 int crash_support;
6010
6011 crash_support = cmd->frame->dcmd.mbox.w[0];
6012
6013 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6014 local_instance = megasas_mgmt_info.instance[i];
6015 if (local_instance && local_instance->crash_dump_drv_support) {
6016 if ((local_instance->adprecovery ==
6017 MEGASAS_HBA_OPERATIONAL) &&
6018 !megasas_set_crash_dump_params(local_instance,
6019 crash_support)) {
6020 local_instance->crash_dump_app_support =
6021 crash_support;
6022 dev_info(&local_instance->pdev->dev,
6023 "Application firmware crash "
6024 "dump mode set success\n");
6025 error = 0;
6026 } else {
6027 dev_info(&local_instance->pdev->dev,
6028 "Application firmware crash "
6029 "dump mode set failed\n");
6030 error = -1;
6031 }
6032 }
6033 }
6034 return error;
6035}
6036
Yang, Boc3518832009-10-06 14:18:02 -06006037/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006038 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6039 * @instance: Adapter soft state
6040 * @argp: User's ioctl packet
6041 */
6042static int
6043megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6044 struct megasas_iocpacket __user * user_ioc,
6045 struct megasas_iocpacket *ioc)
6046{
6047 struct megasas_sge32 *kern_sge32;
6048 struct megasas_cmd *cmd;
6049 void *kbuff_arr[MAX_IOCTL_SGE];
6050 dma_addr_t buf_handle = 0;
6051 int error = 0, i;
6052 void *sense = NULL;
6053 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006054 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006055
6056 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6057
6058 if (ioc->sge_count > MAX_IOCTL_SGE) {
6059 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
6060 ioc->sge_count, MAX_IOCTL_SGE);
6061 return -EINVAL;
6062 }
6063
6064 cmd = megasas_get_cmd(instance);
6065 if (!cmd) {
6066 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
6067 return -ENOMEM;
6068 }
6069
6070 /*
6071 * User's IOCTL packet has 2 frames (maximum). Copy those two
6072 * frames into our cmd's frames. cmd->frame's context will get
6073 * overwritten when we copy from user's frames. So set that value
6074 * alone separately
6075 */
6076 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306077 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006078 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306079 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6080 MFI_FRAME_SGL64 |
6081 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006082
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306083 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6084 error = megasas_set_crash_dump_params_ioctl(cmd);
6085 megasas_return_cmd(instance, cmd);
6086 return error;
6087 }
6088
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006089 /*
6090 * The management interface between applications and the fw uses
6091 * MFI frames. E.g, RAID configuration changes, LD property changes
6092 * etc are accomplishes through different kinds of MFI frames. The
6093 * driver needs to care only about substituting user buffers with
6094 * kernel buffers in SGLs. The location of SGL is embedded in the
6095 * struct iocpacket itself.
6096 */
6097 kern_sge32 = (struct megasas_sge32 *)
6098 ((unsigned long)cmd->frame + ioc->sgl_off);
6099
6100 /*
6101 * For each user buffer, create a mirror buffer and copy in
6102 */
6103 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006104 if (!ioc->sgl[i].iov_len)
6105 continue;
6106
Sumant Patro9f35fa82007-02-14 12:55:45 -08006107 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006108 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006109 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006110 if (!kbuff_arr[i]) {
6111 printk(KERN_DEBUG "megasas: Failed to alloc "
6112 "kernel SGL buffer for IOCTL \n");
6113 error = -ENOMEM;
6114 goto out;
6115 }
6116
6117 /*
6118 * We don't change the dma_coherent_mask, so
6119 * pci_alloc_consistent only returns 32bit addresses
6120 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306121 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6122 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006123
6124 /*
6125 * We created a kernel buffer corresponding to the
6126 * user buffer. Now copy in from the user buffer
6127 */
6128 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6129 (u32) (ioc->sgl[i].iov_len))) {
6130 error = -EFAULT;
6131 goto out;
6132 }
6133 }
6134
6135 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006136 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6137 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006138 if (!sense) {
6139 error = -ENOMEM;
6140 goto out;
6141 }
6142
6143 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006144 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306145 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006146 }
6147
6148 /*
6149 * Set the sync_cmd flag so that the ISR knows not to complete this
6150 * cmd to the SCSI mid-layer
6151 */
6152 cmd->sync_cmd = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306153 megasas_issue_blocked_cmd(instance, cmd, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006154 cmd->sync_cmd = 0;
6155
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306156 if (instance->unload == 1) {
6157 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6158 "don't submit data to application\n");
6159 goto out;
6160 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006161 /*
6162 * copy out the kernel buffers to user buffers
6163 */
6164 for (i = 0; i < ioc->sge_count; i++) {
6165 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6166 ioc->sgl[i].iov_len)) {
6167 error = -EFAULT;
6168 goto out;
6169 }
6170 }
6171
6172 /*
6173 * copy out the sense
6174 */
6175 if (ioc->sense_len) {
6176 /*
bo yangb70a41e2008-03-18 03:13:06 -04006177 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006178 * sense buffer address
6179 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006180 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6181 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006182
bo yangb70a41e2008-03-18 03:13:06 -04006183 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6184 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05006185 printk(KERN_ERR "megasas: Failed to copy out to user "
6186 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006187 error = -EFAULT;
6188 goto out;
6189 }
6190 }
6191
6192 /*
6193 * copy the status codes returned by the fw
6194 */
6195 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6196 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6197 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
6198 error = -EFAULT;
6199 }
6200
6201 out:
6202 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006203 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006204 sense, sense_handle);
6205 }
6206
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006207 for (i = 0; i < ioc->sge_count; i++) {
6208 if (kbuff_arr[i])
6209 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306210 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006211 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306212 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306213 kbuff_arr[i] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006214 }
6215
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306216 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
6217 megasas_return_mfi_mpt_pthr(instance, cmd,
6218 cmd->mpt_pthr_cmd_blocked);
6219 else
6220 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006221 return error;
6222}
6223
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006224static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6225{
6226 struct megasas_iocpacket __user *user_ioc =
6227 (struct megasas_iocpacket __user *)arg;
6228 struct megasas_iocpacket *ioc;
6229 struct megasas_instance *instance;
6230 int error;
bo yang39a98552010-09-22 22:36:29 -04006231 int i;
6232 unsigned long flags;
6233 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006234
6235 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6236 if (!ioc)
6237 return -ENOMEM;
6238
6239 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6240 error = -EFAULT;
6241 goto out_kfree_ioc;
6242 }
6243
6244 instance = megasas_lookup_instance(ioc->host_no);
6245 if (!instance) {
6246 error = -ENODEV;
6247 goto out_kfree_ioc;
6248 }
6249
adam radford229fe472014-03-10 02:51:56 -07006250 /* Adjust ioctl wait time for VF mode */
6251 if (instance->requestorId)
6252 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6253
6254 /* Block ioctls in VF mode */
6255 if (instance->requestorId && !allow_vf_ioctls) {
6256 error = -ENODEV;
6257 goto out_kfree_ioc;
6258 }
6259
bo yang39a98552010-09-22 22:36:29 -04006260 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6261 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006262 error = -ENODEV;
6263 goto out_kfree_ioc;
6264 }
6265
6266 if (instance->unload == 1) {
6267 error = -ENODEV;
6268 goto out_kfree_ioc;
6269 }
6270
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006271 if (down_interruptible(&instance->ioctl_sem)) {
6272 error = -ERESTARTSYS;
6273 goto out_kfree_ioc;
6274 }
bo yang39a98552010-09-22 22:36:29 -04006275
6276 for (i = 0; i < wait_time; i++) {
6277
6278 spin_lock_irqsave(&instance->hba_lock, flags);
6279 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6280 spin_unlock_irqrestore(&instance->hba_lock, flags);
6281 break;
6282 }
6283 spin_unlock_irqrestore(&instance->hba_lock, flags);
6284
6285 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6286 printk(KERN_NOTICE "megasas: waiting"
6287 "for controller reset to finish\n");
6288 }
6289
6290 msleep(1000);
6291 }
6292
6293 spin_lock_irqsave(&instance->hba_lock, flags);
6294 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6295 spin_unlock_irqrestore(&instance->hba_lock, flags);
6296
6297 printk(KERN_ERR "megaraid_sas: timed out while"
6298 "waiting for HBA to recover\n");
6299 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006300 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006301 }
6302 spin_unlock_irqrestore(&instance->hba_lock, flags);
6303
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006304 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Dan Carpenterc64e4832013-04-16 10:44:19 +03006305 out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006306 up(&instance->ioctl_sem);
6307
6308 out_kfree_ioc:
6309 kfree(ioc);
6310 return error;
6311}
6312
6313static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6314{
6315 struct megasas_instance *instance;
6316 struct megasas_aen aen;
6317 int error;
bo yang39a98552010-09-22 22:36:29 -04006318 int i;
6319 unsigned long flags;
6320 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006321
6322 if (file->private_data != file) {
6323 printk(KERN_DEBUG "megasas: fasync_helper was not "
6324 "called first\n");
6325 return -EINVAL;
6326 }
6327
6328 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6329 return -EFAULT;
6330
6331 instance = megasas_lookup_instance(aen.host_no);
6332
6333 if (!instance)
6334 return -ENODEV;
6335
bo yang39a98552010-09-22 22:36:29 -04006336 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6337 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006338 }
6339
6340 if (instance->unload == 1) {
6341 return -ENODEV;
6342 }
6343
bo yang39a98552010-09-22 22:36:29 -04006344 for (i = 0; i < wait_time; i++) {
6345
6346 spin_lock_irqsave(&instance->hba_lock, flags);
6347 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6348 spin_unlock_irqrestore(&instance->hba_lock,
6349 flags);
6350 break;
6351 }
6352
6353 spin_unlock_irqrestore(&instance->hba_lock, flags);
6354
6355 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6356 printk(KERN_NOTICE "megasas: waiting for"
6357 "controller reset to finish\n");
6358 }
6359
6360 msleep(1000);
6361 }
6362
6363 spin_lock_irqsave(&instance->hba_lock, flags);
6364 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6365 spin_unlock_irqrestore(&instance->hba_lock, flags);
6366 printk(KERN_ERR "megaraid_sas: timed out while waiting"
6367 "for HBA to recover.\n");
6368 return -ENODEV;
6369 }
6370 spin_unlock_irqrestore(&instance->hba_lock, flags);
6371
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006372 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006373 error = megasas_register_aen(instance, aen.seq_num,
6374 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006375 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006376 return error;
6377}
6378
6379/**
6380 * megasas_mgmt_ioctl - char node ioctl entry point
6381 */
6382static long
6383megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6384{
6385 switch (cmd) {
6386 case MEGASAS_IOC_FIRMWARE:
6387 return megasas_mgmt_ioctl_fw(file, arg);
6388
6389 case MEGASAS_IOC_GET_AEN:
6390 return megasas_mgmt_ioctl_aen(file, arg);
6391 }
6392
6393 return -ENOTTY;
6394}
6395
6396#ifdef CONFIG_COMPAT
6397static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6398{
6399 struct compat_megasas_iocpacket __user *cioc =
6400 (struct compat_megasas_iocpacket __user *)arg;
6401 struct megasas_iocpacket __user *ioc =
6402 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6403 int i;
6404 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006405 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006406
Jeff Garzik83aabc12006-10-04 06:34:03 -04006407 if (clear_user(ioc, sizeof(*ioc)))
6408 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006409
6410 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6411 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6412 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6413 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6414 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6415 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6416 return -EFAULT;
6417
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006418 /*
6419 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6420 * sense_len is not null, so prepare the 64bit value under
6421 * the same condition.
6422 */
6423 if (ioc->sense_len) {
6424 void __user **sense_ioc_ptr =
6425 (void __user **)(ioc->frame.raw + ioc->sense_off);
6426 compat_uptr_t *sense_cioc_ptr =
6427 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6428 if (get_user(ptr, sense_cioc_ptr) ||
6429 put_user(compat_ptr(ptr), sense_ioc_ptr))
6430 return -EFAULT;
6431 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006432
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006433 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006434 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6435 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6436 copy_in_user(&ioc->sgl[i].iov_len,
6437 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6438 return -EFAULT;
6439 }
6440
6441 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6442
6443 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6444 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6445 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6446 return -EFAULT;
6447 }
6448 return error;
6449}
6450
6451static long
6452megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6453 unsigned long arg)
6454{
6455 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006456 case MEGASAS_IOC_FIRMWARE32:
6457 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006458 case MEGASAS_IOC_GET_AEN:
6459 return megasas_mgmt_ioctl_aen(file, arg);
6460 }
6461
6462 return -ENOTTY;
6463}
6464#endif
6465
6466/*
6467 * File operations structure for management interface
6468 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006469static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006470 .owner = THIS_MODULE,
6471 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006472 .fasync = megasas_mgmt_fasync,
6473 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006474 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006475#ifdef CONFIG_COMPAT
6476 .compat_ioctl = megasas_mgmt_compat_ioctl,
6477#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006478 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006479};
6480
6481/*
6482 * PCI hotplug support registration structure
6483 */
6484static struct pci_driver megasas_pci_driver = {
6485
6486 .name = "megaraid_sas",
6487 .id_table = megasas_pci_table,
6488 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006489 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006490 .suspend = megasas_suspend,
6491 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006492 .shutdown = megasas_shutdown,
6493};
6494
6495/*
6496 * Sysfs driver attributes
6497 */
6498static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6499{
6500 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6501 MEGASAS_VERSION);
6502}
6503
6504static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6505
6506static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06006507megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6508{
6509 return sprintf(buf, "%u\n", support_poll_for_event);
6510}
6511
6512static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6513 megasas_sysfs_show_support_poll_for_event, NULL);
6514
Yang, Bo837f5fe2010-10-11 06:59:20 -06006515 static ssize_t
6516megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6517{
6518 return sprintf(buf, "%u\n", support_device_change);
6519}
6520
6521static DRIVER_ATTR(support_device_change, S_IRUGO,
6522 megasas_sysfs_show_support_device_change, NULL);
6523
Yang, Bo72c4fd32009-10-06 14:20:59 -06006524static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07006525megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6526{
bo yangad84db22007-11-09 04:40:16 -05006527 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07006528}
6529
6530static ssize_t
6531megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6532{
6533 int retval = count;
6534 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
6535 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6536 retval = -EINVAL;
6537 }
6538 return retval;
6539}
6540
Joe Malicki66dca9b2008-08-14 17:14:48 -04006541static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05006542 megasas_sysfs_set_dbg_lvl);
6543
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006544static void
6545megasas_aen_polling(struct work_struct *work)
6546{
6547 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006548 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006549 struct megasas_instance *instance = ev->instance;
6550 union megasas_evt_class_locale class_locale;
6551 struct Scsi_Host *host;
6552 struct scsi_device *sdev1;
6553 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006554 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006555 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07006556 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006557 int error;
6558
6559 if (!instance) {
6560 printk(KERN_ERR "invalid instance!\n");
6561 kfree(ev);
6562 return;
6563 }
adam radford229fe472014-03-10 02:51:56 -07006564
6565 /* Adjust event workqueue thread wait time for VF mode */
6566 if (instance->requestorId)
6567 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6568
6569 /* Don't run the event workqueue thread if OCR is running */
6570 for (i = 0; i < wait_time; i++) {
6571 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6572 break;
6573 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6574 printk(KERN_NOTICE "megasas: %s waiting for "
6575 "controller reset to finish for scsi%d\n",
6576 __func__, instance->host->host_no);
6577 }
6578 msleep(1000);
6579 }
6580
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006581 instance->ev = NULL;
6582 host = instance->host;
6583 if (instance->evt_detail) {
6584
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306585 switch (le32_to_cpu(instance->evt_detail->code)) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006586 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07006587 if (megasas_get_pd_list(instance) == 0) {
6588 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6589 for (j = 0;
6590 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6591 j++) {
6592
6593 pd_index =
6594 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6595
6596 sdev1 =
6597 scsi_device_lookup(host, i, j, 0);
6598
6599 if (instance->pd_list[pd_index].driveState
6600 == MR_PD_STATE_SYSTEM) {
6601 if (!sdev1) {
6602 scsi_add_device(host, i, j, 0);
6603 }
6604
6605 if (sdev1)
6606 scsi_device_put(sdev1);
6607 }
6608 }
6609 }
6610 }
6611 doscan = 0;
6612 break;
6613
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006614 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07006615 if (megasas_get_pd_list(instance) == 0) {
Yang, Boc9786842009-12-06 08:39:25 -07006616 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6617 for (j = 0;
6618 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6619 j++) {
6620
6621 pd_index =
6622 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6623
6624 sdev1 =
6625 scsi_device_lookup(host, i, j, 0);
6626
6627 if (instance->pd_list[pd_index].driveState
6628 == MR_PD_STATE_SYSTEM) {
6629 if (sdev1) {
6630 scsi_device_put(sdev1);
6631 }
6632 } else {
6633 if (sdev1) {
6634 scsi_remove_device(sdev1);
6635 scsi_device_put(sdev1);
6636 }
6637 }
6638 }
6639 }
6640 }
6641 doscan = 0;
6642 break;
6643
6644 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08006645 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07006646 case MR_EVT_LD_DELETED:
adam radford229fe472014-03-10 02:51:56 -07006647 if (!instance->requestorId ||
6648 (instance->requestorId &&
6649 megasas_get_ld_vf_affiliation(instance, 0))) {
6650 if (megasas_ld_list_query(instance,
6651 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6652 megasas_get_ld_list(instance);
6653 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6654 for (j = 0;
6655 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6656 j++) {
Yang, Boc9786842009-12-06 08:39:25 -07006657
adam radford229fe472014-03-10 02:51:56 -07006658 ld_index =
6659 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006660
adam radford229fe472014-03-10 02:51:56 -07006661 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07006662
adam radford229fe472014-03-10 02:51:56 -07006663 if (instance->ld_ids[ld_index]
6664 != 0xff) {
6665 if (sdev1)
6666 scsi_device_put(sdev1);
6667 } else {
6668 if (sdev1) {
6669 scsi_remove_device(sdev1);
6670 scsi_device_put(sdev1);
6671 }
Yang, Boc9786842009-12-06 08:39:25 -07006672 }
6673 }
adam radford229fe472014-03-10 02:51:56 -07006674 }
6675 doscan = 0;
6676 }
6677 break;
6678 case MR_EVT_LD_CREATED:
6679 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;
6687 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6688 j++) {
6689 ld_index =
6690 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6691
6692 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6693
6694 if (instance->ld_ids[ld_index]
6695 != 0xff) {
6696 if (!sdev1)
6697 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6698 }
6699 if (sdev1)
6700 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006701 }
6702 }
adam radford229fe472014-03-10 02:51:56 -07006703 doscan = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006704 }
Yang, Boc9786842009-12-06 08:39:25 -07006705 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006706 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07006707 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08006708 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006709 doscan = 1;
6710 break;
6711 default:
6712 doscan = 0;
6713 break;
6714 }
6715 } else {
6716 printk(KERN_ERR "invalid evt_detail!\n");
6717 kfree(ev);
6718 return;
6719 }
6720
6721 if (doscan) {
adam radford229fe472014-03-10 02:51:56 -07006722 printk(KERN_INFO "megaraid_sas: scanning for scsi%d...\n",
6723 instance->host->host_no);
Hannes Reinecke58968fc2014-01-16 11:25:36 +01006724 if (megasas_get_pd_list(instance) == 0) {
6725 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6726 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6727 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6728 sdev1 = scsi_device_lookup(host, i, j, 0);
6729 if (instance->pd_list[pd_index].driveState ==
6730 MR_PD_STATE_SYSTEM) {
6731 if (!sdev1) {
6732 scsi_add_device(host, i, j, 0);
6733 }
6734 if (sdev1)
6735 scsi_device_put(sdev1);
6736 } else {
6737 if (sdev1) {
6738 scsi_remove_device(sdev1);
6739 scsi_device_put(sdev1);
6740 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006741 }
6742 }
6743 }
6744 }
Yang, Boc9786842009-12-06 08:39:25 -07006745
adam radford229fe472014-03-10 02:51:56 -07006746 if (!instance->requestorId ||
6747 (instance->requestorId &&
6748 megasas_get_ld_vf_affiliation(instance, 0))) {
6749 if (megasas_ld_list_query(instance,
6750 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6751 megasas_get_ld_list(instance);
6752 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6753 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6754 j++) {
6755 ld_index =
6756 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006757
adam radford229fe472014-03-10 02:51:56 -07006758 sdev1 = scsi_device_lookup(host,
6759 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6760 if (instance->ld_ids[ld_index]
6761 != 0xff) {
6762 if (!sdev1)
6763 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6764 else
6765 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006766 } else {
adam radford229fe472014-03-10 02:51:56 -07006767 if (sdev1) {
6768 scsi_remove_device(sdev1);
6769 scsi_device_put(sdev1);
6770 }
Yang, Boc9786842009-12-06 08:39:25 -07006771 }
6772 }
6773 }
6774 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006775 }
6776
6777 if ( instance->aen_cmd != NULL ) {
6778 kfree(ev);
6779 return ;
6780 }
6781
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306782 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006783
6784 /* Register AEN with FW for latest sequence number plus 1 */
6785 class_locale.members.reserved = 0;
6786 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6787 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6788 mutex_lock(&instance->aen_mutex);
6789 error = megasas_register_aen(instance, seq_num,
6790 class_locale.word);
6791 mutex_unlock(&instance->aen_mutex);
6792
6793 if (error)
6794 printk(KERN_ERR "register aen failed error %x\n", error);
6795
6796 kfree(ev);
6797}
6798
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006799/**
6800 * megasas_init - Driver load entry point
6801 */
6802static int __init megasas_init(void)
6803{
6804 int rval;
6805
6806 /*
6807 * Announce driver version and other information
6808 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05306809 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006810
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07006811 spin_lock_init(&poll_aen_lock);
6812
Yang, Bo72c4fd32009-10-06 14:20:59 -06006813 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06006814 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006815
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006816 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6817
6818 /*
6819 * Register character device node
6820 */
6821 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6822
6823 if (rval < 0) {
6824 printk(KERN_DEBUG "megasas: failed to open device node\n");
6825 return rval;
6826 }
6827
6828 megasas_mgmt_majorno = rval;
6829
6830 /*
6831 * Register ourselves as PCI hotplug module
6832 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00006833 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006834
6835 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09006836 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04006837 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006838 }
6839
Jeff Garzik83aabc12006-10-04 06:34:03 -04006840 rval = driver_create_file(&megasas_pci_driver.driver,
6841 &driver_attr_version);
6842 if (rval)
6843 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006844
6845 rval = driver_create_file(&megasas_pci_driver.driver,
6846 &driver_attr_support_poll_for_event);
6847 if (rval)
6848 goto err_dcf_support_poll_for_event;
6849
Jeff Garzik83aabc12006-10-04 06:34:03 -04006850 rval = driver_create_file(&megasas_pci_driver.driver,
6851 &driver_attr_dbg_lvl);
6852 if (rval)
6853 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05006854 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006855 &driver_attr_support_device_change);
6856 if (rval)
6857 goto err_dcf_support_device_change;
6858
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006859 return rval;
bo yangad84db22007-11-09 04:40:16 -05006860
Yang, Bo837f5fe2010-10-11 06:59:20 -06006861err_dcf_support_device_change:
6862 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05006863 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006864err_dcf_dbg_lvl:
6865 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06006866 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06006867err_dcf_support_poll_for_event:
Jeff Garzik83aabc12006-10-04 06:34:03 -04006868 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6869err_dcf_attr_ver:
6870 pci_unregister_driver(&megasas_pci_driver);
6871err_pcidrv:
6872 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08006873 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006874}
6875
6876/**
6877 * megasas_exit - Driver unload entry point
6878 */
6879static void __exit megasas_exit(void)
6880{
Sumant Patro658dced2006-10-03 13:09:14 -07006881 driver_remove_file(&megasas_pci_driver.driver,
6882 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006883 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006884 &driver_attr_support_poll_for_event);
6885 driver_remove_file(&megasas_pci_driver.driver,
6886 &driver_attr_support_device_change);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006887 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006888
6889 pci_unregister_driver(&megasas_pci_driver);
6890 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6891}
6892
6893module_init(megasas_init);
6894module_exit(megasas_exit);