blob: d8cc9227e1ef4c2a14dc39e48122b25425cdfbef [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 *
adam radfordae590572012-10-01 19:27:34 -07004 * Copyright (c) 2003-2012 LSI Corporation.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005 *
adam radford3f1530c2010-12-14 18:51:48 -08006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040010 *
adam radford3f1530c2010-12-14 18:51:48 -080011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040015 *
adam radford3f1530c2010-12-14 18:51:48 -080016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040019 *
adam radford3f1530c2010-12-14 18:51:48 -080020 * FILE: megaraid_sas_base.c
Adam Radford5e8d9002014-07-09 15:17:58 -070021 * Version : 06.803.02.00-rc1
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040022 *
adam radford3f1530c2010-12-14 18:51:48 -080023 * Authors: LSI Corporation
24 * Sreenivas Bagalkote
25 * Sumant Patro
26 * Bo Yang
adam radford00fa2b12011-02-24 20:57:21 -080027 * Adam Radford <linuxraid@lsi.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040028 *
adam radford3f1530c2010-12-14 18:51:48 -080029 * Send feedback to: <megaraidlinux@lsi.com>
30 *
31 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
32 * ATTN: Linuxraid
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040033 */
34
35#include <linux/kernel.h>
36#include <linux/types.h>
37#include <linux/pci.h>
38#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040039#include <linux/moduleparam.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/interrupt.h>
43#include <linux/delay.h>
44#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040046#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010047#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040048#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080049#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010050#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060051#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040052
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070057#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080058#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040059#include "megaraid_sas.h"
60
bo yangad84db22007-11-09 04:40:16 -050061/*
Yang, Bo1fd10682010-10-12 07:18:50 -060062 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
64 */
65static unsigned int max_sectors;
66module_param_named(max_sectors, max_sectors, int, 0);
67MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
69
adam radford80d9da92010-12-21 10:17:40 -080070static int msix_disable;
71module_param(msix_disable, int, S_IRUGO);
72MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73
adam radford079eadd2012-10-01 19:26:59 -070074static unsigned int msix_vectors;
75module_param(msix_vectors, int, S_IRUGO);
76MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77
adam radford229fe472014-03-10 02:51:56 -070078static int allow_vf_ioctls;
79module_param(allow_vf_ioctls, int, S_IRUGO);
80MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81
adam radfordc5daa6a2012-07-17 18:20:03 -070082static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
83module_param(throttlequeuedepth, int, S_IRUGO);
84MODULE_PARM_DESC(throttlequeuedepth,
85 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86
adam radfordc007b8b2012-07-17 18:20:24 -070087int resetwaittime = MEGASAS_RESET_WAIT_TIME;
88module_param(resetwaittime, int, S_IRUGO);
89MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
90 "before resetting adapter. Default: 180");
91
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053092int smp_affinity_enable = 1;
93module_param(smp_affinity_enable, int, S_IRUGO);
94MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
95
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040096MODULE_LICENSE("GPL");
97MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080098MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050099MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400100
adam radford058a8fa2011-10-08 18:14:27 -0700101int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400102static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700103static int megasas_ld_list_query(struct megasas_instance *instance,
104 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400105static int megasas_issue_init_mfi(struct megasas_instance *instance);
106static int megasas_register_aen(struct megasas_instance *instance,
107 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400108/*
109 * PCI ID table for all supported controllers
110 */
111static struct pci_device_id megasas_pci_table[] = {
112
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
114 /* xscale IOP */
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
116 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
118 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700119 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
120 /* gen2*/
121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
122 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
124 /* skinny*/
125 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
126 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200127 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
128 /* xscale IOP, vega */
129 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
130 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800131 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
132 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700133 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
134 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700135 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
136 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530137 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
138 /* Fury */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200139 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400140};
141
142MODULE_DEVICE_TABLE(pci, megasas_pci_table);
143
144static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700145struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400146static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100147static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400148
Yang, Boc3518832009-10-06 14:18:02 -0600149static int megasas_poll_wait_aen;
150static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600151static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800152u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600153static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700154
Yang, Boc3518832009-10-06 14:18:02 -0600155/* define lock for aen poll */
156spinlock_t poll_aen_lock;
157
adam radford9c915a82010-12-21 13:34:31 -0800158void
bo yang7343eb62007-11-09 04:35:44 -0500159megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
160 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800161static u32
162megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
163static int
164megasas_adp_reset_gen2(struct megasas_instance *instance,
165 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800166static irqreturn_t megasas_isr(int irq, void *devp);
167static u32
168megasas_init_adapter_mfi(struct megasas_instance *instance);
169u32
170megasas_build_and_issue_cmd(struct megasas_instance *instance,
171 struct scsi_cmnd *scmd);
172static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800173void
174megasas_release_fusion(struct megasas_instance *instance);
175int
176megasas_ioc_init_fusion(struct megasas_instance *instance);
177void
178megasas_free_cmds_fusion(struct megasas_instance *instance);
179u8
180megasas_get_map_info(struct megasas_instance *instance);
181int
182megasas_sync_map_info(struct megasas_instance *instance);
183int
adam radford229fe472014-03-10 02:51:56 -0700184wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
185 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800186void megasas_reset_reply_desc(struct megasas_instance *instance);
adam radford229fe472014-03-10 02:51:56 -0700187int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
adam radford9c915a82010-12-21 13:34:31 -0800188void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700189static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
190 int initial);
191int megasas_check_mpio_paths(struct megasas_instance *instance,
192 struct scsi_cmnd *scmd);
adam radfordcd50ba82010-12-21 10:23:23 -0800193
194void
195megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
196{
197 instance->instancet->fire_cmd(instance,
198 cmd->frame_phys_addr, 0, instance->reg_set);
199}
bo yang7343eb62007-11-09 04:35:44 -0500200
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400201/**
202 * megasas_get_cmd - Get a command from the free pool
203 * @instance: Adapter soft state
204 *
205 * Returns a free command from the pool
206 */
adam radford9c915a82010-12-21 13:34:31 -0800207struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400208 *instance)
209{
210 unsigned long flags;
211 struct megasas_cmd *cmd = NULL;
212
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530213 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400214
215 if (!list_empty(&instance->cmd_pool)) {
216 cmd = list_entry((&instance->cmd_pool)->next,
217 struct megasas_cmd, list);
218 list_del_init(&cmd->list);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530219 atomic_set(&cmd->mfi_mpt_pthr, MFI_MPT_DETACHED);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400220 } else {
221 printk(KERN_ERR "megasas: Command pool empty!\n");
222 }
223
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530224 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400225 return cmd;
226}
227
228/**
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530229 * __megasas_return_cmd - Return a cmd to free command pool
230 * @instance: Adapter soft state
231 * @cmd: Command packet to be returned to free command pool
232 */
233inline void
234__megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
235{
236 /*
237 * Don't go ahead and free the MFI frame, if corresponding
238 * MPT frame is not freed(valid for only fusion adapters).
239 * In case of MFI adapters, anyways for any allocated MFI
240 * frame will have cmd->mfi_mpt_mpthr set to MFI_MPT_DETACHED
241 */
242 if (atomic_read(&cmd->mfi_mpt_pthr) != MFI_MPT_DETACHED)
243 return;
244
245 cmd->scmd = NULL;
246 cmd->frame_count = 0;
247 cmd->is_wait_event = 0;
248 cmd->mpt_pthr_cmd_blocked = NULL;
249
250 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
251 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
252 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
253 (reset_devices))
254 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
255
256 atomic_set(&cmd->mfi_mpt_pthr, MFI_LIST_ADDED);
257 list_add(&cmd->list, (&instance->cmd_pool)->next);
258}
259
260/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400261 * megasas_return_cmd - Return a cmd to free command pool
262 * @instance: Adapter soft state
263 * @cmd: Command packet to be returned to free command pool
264 */
adam radford9c915a82010-12-21 13:34:31 -0800265inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400266megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
267{
268 unsigned long flags;
269
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530270 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
271 __megasas_return_cmd(instance, cmd);
272 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400273}
274
Sumant Patro1341c932006-01-25 12:02:40 -0800275
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400276/**
adam radford0d490162010-12-14 19:17:17 -0800277* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800278* (deviceid : 1064R, PERC5) controllers
279*/
280
281/**
282 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400283 * @regs: MFI register set
284 */
285static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530286megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400287{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530288 struct megasas_register_set __iomem *regs;
289 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400290 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400291
292 /* Dummy readl to force pci flush */
293 readl(&regs->outbound_intr_mask);
294}
295
296/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700297 * megasas_disable_intr_xscale -Disables interrupt
298 * @regs: MFI register set
299 */
300static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530301megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700302{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530303 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700304 u32 mask = 0x1f;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530305 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700306 writel(mask, &regs->outbound_intr_mask);
307 /* Dummy readl to force pci flush */
308 readl(&regs->outbound_intr_mask);
309}
310
311/**
Sumant Patro1341c932006-01-25 12:02:40 -0800312 * megasas_read_fw_status_reg_xscale - returns the current FW status value
313 * @regs: MFI register set
314 */
315static u32
316megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
317{
318 return readl(&(regs)->outbound_msg_0);
319}
320/**
321 * megasas_clear_interrupt_xscale - Check & clear interrupt
322 * @regs: MFI register set
323 */
adam radford0d490162010-12-14 19:17:17 -0800324static int
Sumant Patro1341c932006-01-25 12:02:40 -0800325megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
326{
327 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400328 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800329 /*
330 * Check if it is our interrupt
331 */
332 status = readl(&regs->outbound_intr_status);
333
bo yang39a98552010-09-22 22:36:29 -0400334 if (status & MFI_OB_INTR_STATUS_MASK)
335 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
336 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
337 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800338
339 /*
340 * Clear the interrupt by writing back the same value
341 */
bo yang39a98552010-09-22 22:36:29 -0400342 if (mfiStatus)
343 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800344
Yang, Bo06f579d2008-08-10 12:42:37 -0700345 /* Dummy readl to force pci flush */
346 readl(&regs->outbound_intr_status);
347
bo yang39a98552010-09-22 22:36:29 -0400348 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800349}
350
351/**
352 * megasas_fire_cmd_xscale - Sends command to the FW
353 * @frame_phys_addr : Physical address of cmd
354 * @frame_count : Number of frames for the command
355 * @regs : MFI register set
356 */
adam radford0d490162010-12-14 19:17:17 -0800357static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600358megasas_fire_cmd_xscale(struct megasas_instance *instance,
359 dma_addr_t frame_phys_addr,
360 u32 frame_count,
361 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800362{
bo yang39a98552010-09-22 22:36:29 -0400363 unsigned long flags;
364 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800365 writel((frame_phys_addr >> 3)|(frame_count),
366 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400367 spin_unlock_irqrestore(&instance->hba_lock, flags);
368}
369
370/**
371 * megasas_adp_reset_xscale - For controller reset
372 * @regs: MFI register set
373 */
374static int
375megasas_adp_reset_xscale(struct megasas_instance *instance,
376 struct megasas_register_set __iomem *regs)
377{
378 u32 i;
379 u32 pcidata;
380 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
381
382 for (i = 0; i < 3; i++)
383 msleep(1000); /* sleep for 3 secs */
384 pcidata = 0;
385 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
386 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
387 if (pcidata & 0x2) {
388 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
389 pcidata &= ~0x2;
390 pci_write_config_dword(instance->pdev,
391 MFI_1068_PCSR_OFFSET, pcidata);
392
393 for (i = 0; i < 2; i++)
394 msleep(1000); /* need to wait 2 secs again */
395
396 pcidata = 0;
397 pci_read_config_dword(instance->pdev,
398 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
399 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
400 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
401 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
402 pcidata = 0;
403 pci_write_config_dword(instance->pdev,
404 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
405 }
406 }
407 return 0;
408}
409
410/**
411 * megasas_check_reset_xscale - For controller reset check
412 * @regs: MFI register set
413 */
414static int
415megasas_check_reset_xscale(struct megasas_instance *instance,
416 struct megasas_register_set __iomem *regs)
417{
bo yang39a98552010-09-22 22:36:29 -0400418
419 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530420 (le32_to_cpu(*instance->consumer) ==
421 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400422 return 1;
bo yang39a98552010-09-22 22:36:29 -0400423 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800424}
425
426static struct megasas_instance_template megasas_instance_template_xscale = {
427
428 .fire_cmd = megasas_fire_cmd_xscale,
429 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700430 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800431 .clear_intr = megasas_clear_intr_xscale,
432 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400433 .adp_reset = megasas_adp_reset_xscale,
434 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800435 .service_isr = megasas_isr,
436 .tasklet = megasas_complete_cmd_dpc,
437 .init_adapter = megasas_init_adapter_mfi,
438 .build_and_issue_cmd = megasas_build_and_issue_cmd,
439 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800440};
441
442/**
adam radford0d490162010-12-14 19:17:17 -0800443* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800444* to xscale (deviceid : 1064R, PERC5) controllers
445*/
446
447/**
adam radford0d490162010-12-14 19:17:17 -0800448* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800449* controllers
450*/
451
452/**
453 * megasas_enable_intr_ppc - Enables interrupts
454 * @regs: MFI register set
455 */
456static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530457megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800458{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530459 struct megasas_register_set __iomem *regs;
460 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800461 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800462
bo yang39a98552010-09-22 22:36:29 -0400463 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800464
465 /* Dummy readl to force pci flush */
466 readl(&regs->outbound_intr_mask);
467}
468
469/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700470 * megasas_disable_intr_ppc - Disable interrupt
471 * @regs: MFI register set
472 */
473static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530474megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700475{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530476 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700477 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530478 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700479 writel(mask, &regs->outbound_intr_mask);
480 /* Dummy readl to force pci flush */
481 readl(&regs->outbound_intr_mask);
482}
483
484/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800485 * megasas_read_fw_status_reg_ppc - returns the current FW status value
486 * @regs: MFI register set
487 */
488static u32
489megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
490{
491 return readl(&(regs)->outbound_scratch_pad);
492}
493
494/**
495 * megasas_clear_interrupt_ppc - Check & clear interrupt
496 * @regs: MFI register set
497 */
adam radford0d490162010-12-14 19:17:17 -0800498static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800499megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
500{
adam radford3cc6851f92011-05-11 18:34:52 -0700501 u32 status, mfiStatus = 0;
502
Sumant Patrof9876f02006-02-03 15:34:35 -0800503 /*
504 * Check if it is our interrupt
505 */
506 status = readl(&regs->outbound_intr_status);
507
adam radford3cc6851f92011-05-11 18:34:52 -0700508 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
509 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
510
511 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
512 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800513
514 /*
515 * Clear the interrupt by writing back the same value
516 */
517 writel(status, &regs->outbound_doorbell_clear);
518
Yang, Bo06f579d2008-08-10 12:42:37 -0700519 /* Dummy readl to force pci flush */
520 readl(&regs->outbound_doorbell_clear);
521
adam radford3cc6851f92011-05-11 18:34:52 -0700522 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800523}
adam radford3cc6851f92011-05-11 18:34:52 -0700524
Sumant Patrof9876f02006-02-03 15:34:35 -0800525/**
526 * megasas_fire_cmd_ppc - Sends command to the FW
527 * @frame_phys_addr : Physical address of cmd
528 * @frame_count : Number of frames for the command
529 * @regs : MFI register set
530 */
adam radford0d490162010-12-14 19:17:17 -0800531static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600532megasas_fire_cmd_ppc(struct megasas_instance *instance,
533 dma_addr_t frame_phys_addr,
534 u32 frame_count,
535 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800536{
bo yang39a98552010-09-22 22:36:29 -0400537 unsigned long flags;
538 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800539 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800540 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400541 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800542}
543
bo yang39a98552010-09-22 22:36:29 -0400544/**
bo yang39a98552010-09-22 22:36:29 -0400545 * megasas_check_reset_ppc - For controller reset check
546 * @regs: MFI register set
547 */
548static int
549megasas_check_reset_ppc(struct megasas_instance *instance,
550 struct megasas_register_set __iomem *regs)
551{
adam radford3cc6851f92011-05-11 18:34:52 -0700552 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
553 return 1;
554
bo yang39a98552010-09-22 22:36:29 -0400555 return 0;
556}
adam radford3cc6851f92011-05-11 18:34:52 -0700557
Sumant Patrof9876f02006-02-03 15:34:35 -0800558static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800559
Sumant Patrof9876f02006-02-03 15:34:35 -0800560 .fire_cmd = megasas_fire_cmd_ppc,
561 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700562 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800563 .clear_intr = megasas_clear_intr_ppc,
564 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700565 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400566 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800567 .service_isr = megasas_isr,
568 .tasklet = megasas_complete_cmd_dpc,
569 .init_adapter = megasas_init_adapter_mfi,
570 .build_and_issue_cmd = megasas_build_and_issue_cmd,
571 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800572};
573
574/**
Yang, Bo87911122009-10-06 14:31:54 -0600575 * megasas_enable_intr_skinny - Enables interrupts
576 * @regs: MFI register set
577 */
578static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530579megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600580{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530581 struct megasas_register_set __iomem *regs;
582 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600583 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
584
585 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
586
587 /* Dummy readl to force pci flush */
588 readl(&regs->outbound_intr_mask);
589}
590
591/**
592 * megasas_disable_intr_skinny - Disables interrupt
593 * @regs: MFI register set
594 */
595static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530596megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600597{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530598 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600599 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530600 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600601 writel(mask, &regs->outbound_intr_mask);
602 /* Dummy readl to force pci flush */
603 readl(&regs->outbound_intr_mask);
604}
605
606/**
607 * megasas_read_fw_status_reg_skinny - returns the current FW status value
608 * @regs: MFI register set
609 */
610static u32
611megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
612{
613 return readl(&(regs)->outbound_scratch_pad);
614}
615
616/**
617 * megasas_clear_interrupt_skinny - Check & clear interrupt
618 * @regs: MFI register set
619 */
620static int
621megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
622{
623 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800624 u32 mfiStatus = 0;
625
Yang, Bo87911122009-10-06 14:31:54 -0600626 /*
627 * Check if it is our interrupt
628 */
629 status = readl(&regs->outbound_intr_status);
630
631 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400632 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600633 }
634
635 /*
adam radfordebf054b2011-02-24 20:57:15 -0800636 * Check if it is our interrupt
637 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600638 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800639 MFI_STATE_FAULT) {
640 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
641 } else
642 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
643
644 /*
Yang, Bo87911122009-10-06 14:31:54 -0600645 * Clear the interrupt by writing back the same value
646 */
647 writel(status, &regs->outbound_intr_status);
648
649 /*
650 * dummy read to flush PCI
651 */
652 readl(&regs->outbound_intr_status);
653
adam radfordebf054b2011-02-24 20:57:15 -0800654 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600655}
656
657/**
658 * megasas_fire_cmd_skinny - Sends command to the FW
659 * @frame_phys_addr : Physical address of cmd
660 * @frame_count : Number of frames for the command
661 * @regs : MFI register set
662 */
663static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600664megasas_fire_cmd_skinny(struct megasas_instance *instance,
665 dma_addr_t frame_phys_addr,
666 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600667 struct megasas_register_set __iomem *regs)
668{
Yang, Bo0c79e682009-10-06 14:47:35 -0600669 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400670 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530671 writel(upper_32_bits(frame_phys_addr),
672 &(regs)->inbound_high_queue_port);
673 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
674 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400675 spin_unlock_irqrestore(&instance->hba_lock, flags);
676}
677
678/**
bo yang39a98552010-09-22 22:36:29 -0400679 * megasas_check_reset_skinny - For controller reset check
680 * @regs: MFI register set
681 */
682static int
683megasas_check_reset_skinny(struct megasas_instance *instance,
684 struct megasas_register_set __iomem *regs)
685{
adam radford3cc6851f92011-05-11 18:34:52 -0700686 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
687 return 1;
688
bo yang39a98552010-09-22 22:36:29 -0400689 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600690}
691
692static struct megasas_instance_template megasas_instance_template_skinny = {
693
694 .fire_cmd = megasas_fire_cmd_skinny,
695 .enable_intr = megasas_enable_intr_skinny,
696 .disable_intr = megasas_disable_intr_skinny,
697 .clear_intr = megasas_clear_intr_skinny,
698 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800699 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400700 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800701 .service_isr = megasas_isr,
702 .tasklet = megasas_complete_cmd_dpc,
703 .init_adapter = megasas_init_adapter_mfi,
704 .build_and_issue_cmd = megasas_build_and_issue_cmd,
705 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600706};
707
708
709/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700710* The following functions are defined for gen2 (deviceid : 0x78 0x79)
711* controllers
712*/
713
714/**
715 * megasas_enable_intr_gen2 - Enables interrupts
716 * @regs: MFI register set
717 */
718static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530719megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700720{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530721 struct megasas_register_set __iomem *regs;
722 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700723 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
724
725 /* write ~0x00000005 (4 & 1) to the intr mask*/
726 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
727
728 /* Dummy readl to force pci flush */
729 readl(&regs->outbound_intr_mask);
730}
731
732/**
733 * megasas_disable_intr_gen2 - Disables interrupt
734 * @regs: MFI register set
735 */
736static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530737megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700738{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530739 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700740 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530741 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700742 writel(mask, &regs->outbound_intr_mask);
743 /* Dummy readl to force pci flush */
744 readl(&regs->outbound_intr_mask);
745}
746
747/**
748 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
749 * @regs: MFI register set
750 */
751static u32
752megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
753{
754 return readl(&(regs)->outbound_scratch_pad);
755}
756
757/**
758 * megasas_clear_interrupt_gen2 - Check & clear interrupt
759 * @regs: MFI register set
760 */
761static int
762megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
763{
764 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400765 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700766 /*
767 * Check if it is our interrupt
768 */
769 status = readl(&regs->outbound_intr_status);
770
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530771 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400772 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
773 }
774 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
775 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
776 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700777
778 /*
779 * Clear the interrupt by writing back the same value
780 */
bo yang39a98552010-09-22 22:36:29 -0400781 if (mfiStatus)
782 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700783
784 /* Dummy readl to force pci flush */
785 readl(&regs->outbound_intr_status);
786
bo yang39a98552010-09-22 22:36:29 -0400787 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700788}
789/**
790 * megasas_fire_cmd_gen2 - Sends command to the FW
791 * @frame_phys_addr : Physical address of cmd
792 * @frame_count : Number of frames for the command
793 * @regs : MFI register set
794 */
795static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600796megasas_fire_cmd_gen2(struct megasas_instance *instance,
797 dma_addr_t frame_phys_addr,
798 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700799 struct megasas_register_set __iomem *regs)
800{
bo yang39a98552010-09-22 22:36:29 -0400801 unsigned long flags;
802 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700803 writel((frame_phys_addr | (frame_count<<1))|1,
804 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400805 spin_unlock_irqrestore(&instance->hba_lock, flags);
806}
807
808/**
809 * megasas_adp_reset_gen2 - For controller reset
810 * @regs: MFI register set
811 */
812static int
813megasas_adp_reset_gen2(struct megasas_instance *instance,
814 struct megasas_register_set __iomem *reg_set)
815{
816 u32 retry = 0 ;
817 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800818 u32 *seq_offset = &reg_set->seq_offset;
819 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400820
adam radfordebf054b2011-02-24 20:57:15 -0800821 if (instance->instancet == &megasas_instance_template_skinny) {
822 seq_offset = &reg_set->fusion_seq_offset;
823 hostdiag_offset = &reg_set->fusion_host_diag;
824 }
825
826 writel(0, seq_offset);
827 writel(4, seq_offset);
828 writel(0xb, seq_offset);
829 writel(2, seq_offset);
830 writel(7, seq_offset);
831 writel(0xd, seq_offset);
832
bo yang39a98552010-09-22 22:36:29 -0400833 msleep(1000);
834
adam radfordebf054b2011-02-24 20:57:15 -0800835 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400836
837 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
838 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800839 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400840 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
841 retry, HostDiag);
842
843 if (retry++ >= 100)
844 return 1;
845
846 }
847
848 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
849
adam radfordebf054b2011-02-24 20:57:15 -0800850 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400851
852 ssleep(10);
853
adam radfordebf054b2011-02-24 20:57:15 -0800854 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400855 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
856 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800857 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400858 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
859 retry, HostDiag);
860
861 if (retry++ >= 1000)
862 return 1;
863
864 }
865 return 0;
866}
867
868/**
869 * megasas_check_reset_gen2 - For controller reset check
870 * @regs: MFI register set
871 */
872static int
873megasas_check_reset_gen2(struct megasas_instance *instance,
874 struct megasas_register_set __iomem *regs)
875{
Yang, Bo707e09b2010-10-12 07:20:27 -0600876 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
877 return 1;
878 }
879
bo yang39a98552010-09-22 22:36:29 -0400880 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700881}
882
883static struct megasas_instance_template megasas_instance_template_gen2 = {
884
885 .fire_cmd = megasas_fire_cmd_gen2,
886 .enable_intr = megasas_enable_intr_gen2,
887 .disable_intr = megasas_disable_intr_gen2,
888 .clear_intr = megasas_clear_intr_gen2,
889 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400890 .adp_reset = megasas_adp_reset_gen2,
891 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800892 .service_isr = megasas_isr,
893 .tasklet = megasas_complete_cmd_dpc,
894 .init_adapter = megasas_init_adapter_mfi,
895 .build_and_issue_cmd = megasas_build_and_issue_cmd,
896 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700897};
898
899/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800900* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400901* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800902*/
903
adam radford9c915a82010-12-21 13:34:31 -0800904/*
905 * Template added for TB (Fusion)
906 */
907extern struct megasas_instance_template megasas_instance_template_fusion;
908
Sumant Patrof9876f02006-02-03 15:34:35 -0800909/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400910 * megasas_issue_polled - Issues a polling command
911 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800912 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400913 *
914 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
915 */
adam radford9c915a82010-12-21 13:34:31 -0800916int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400917megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
918{
adam radford229fe472014-03-10 02:51:56 -0700919 int seconds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400920
921 struct megasas_header *frame_hdr = &cmd->frame->hdr;
922
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530923 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
924 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400925
926 /*
927 * Issue the frame using inbound queue port
928 */
adam radford9c915a82010-12-21 13:34:31 -0800929 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400930
931 /*
932 * Wait for cmd_status to change
933 */
adam radford229fe472014-03-10 02:51:56 -0700934 if (instance->requestorId)
935 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
936 else
937 seconds = MFI_POLL_TIMEOUT_SECS;
938 return wait_and_poll(instance, cmd, seconds);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400939}
940
941/**
942 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
943 * @instance: Adapter soft state
944 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530945 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400946 *
947 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700948 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400949 * Used to issue ioctl commands.
950 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530951int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400952megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530953 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400954{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530955 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400956 cmd->cmd_status = ENODATA;
957
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530958 cmd->is_wait_event = 1;
adam radford9c915a82010-12-21 13:34:31 -0800959 instance->instancet->issue_dcmd(instance, cmd);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530960 if (timeout) {
961 ret = wait_event_timeout(instance->int_cmd_wait_q,
962 cmd->cmd_status != ENODATA, timeout * HZ);
963 if (!ret)
964 return 1;
965 } else
966 wait_event(instance->int_cmd_wait_q,
967 cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400968
969 return 0;
970}
971
972/**
973 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
974 * @instance: Adapter soft state
975 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530976 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400977 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530978 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400979 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700980 * cmd and waits for return status.
981 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400982 */
983static int
984megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530985 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400986{
987 struct megasas_cmd *cmd;
988 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +0530989 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400990
991 cmd = megasas_get_cmd(instance);
992
993 if (!cmd)
994 return -1;
995
996 abort_fr = &cmd->frame->abort;
997
998 /*
999 * Prepare and issue the abort frame
1000 */
1001 abort_fr->cmd = MFI_CMD_ABORT;
1002 abort_fr->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301003 abort_fr->flags = cpu_to_le16(0);
1004 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1005 abort_fr->abort_mfi_phys_addr_lo =
1006 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1007 abort_fr->abort_mfi_phys_addr_hi =
1008 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001009
1010 cmd->sync_cmd = 1;
1011 cmd->cmd_status = 0xFF;
1012
adam radford9c915a82010-12-21 13:34:31 -08001013 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001014
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301015 if (timeout) {
1016 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1017 cmd->cmd_status != ENODATA, timeout * HZ);
1018 if (!ret) {
1019 dev_err(&instance->pdev->dev, "Command timedout"
1020 "from %s\n", __func__);
1021 return 1;
1022 }
1023 } else
1024 wait_event(instance->abort_cmd_wait_q,
1025 cmd->cmd_status != ENODATA);
1026
bo yang39a98552010-09-22 22:36:29 -04001027 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028
1029 megasas_return_cmd(instance, cmd);
1030 return 0;
1031}
1032
1033/**
1034 * megasas_make_sgl32 - Prepares 32-bit SGL
1035 * @instance: Adapter soft state
1036 * @scp: SCSI command from the mid-layer
1037 * @mfi_sgl: SGL to be filled in
1038 *
1039 * If successful, this function returns the number of SG elements. Otherwise,
1040 * it returnes -1.
1041 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001042static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001043megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1044 union megasas_sgl *mfi_sgl)
1045{
1046 int i;
1047 int sge_count;
1048 struct scatterlist *os_sgl;
1049
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001050 sge_count = scsi_dma_map(scp);
1051 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001052
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001053 if (sge_count) {
1054 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301055 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1056 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001057 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001058 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001059 return sge_count;
1060}
1061
1062/**
1063 * megasas_make_sgl64 - Prepares 64-bit SGL
1064 * @instance: Adapter soft state
1065 * @scp: SCSI command from the mid-layer
1066 * @mfi_sgl: SGL to be filled in
1067 *
1068 * If successful, this function returns the number of SG elements. Otherwise,
1069 * it returnes -1.
1070 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001071static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001072megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1073 union megasas_sgl *mfi_sgl)
1074{
1075 int i;
1076 int sge_count;
1077 struct scatterlist *os_sgl;
1078
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001079 sge_count = scsi_dma_map(scp);
1080 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001081
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001082 if (sge_count) {
1083 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301084 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1085 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001086 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001087 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001088 return sge_count;
1089}
1090
Yang, Bof4c9a132009-10-06 14:43:28 -06001091/**
1092 * megasas_make_sgl_skinny - Prepares IEEE SGL
1093 * @instance: Adapter soft state
1094 * @scp: SCSI command from the mid-layer
1095 * @mfi_sgl: SGL to be filled in
1096 *
1097 * If successful, this function returns the number of SG elements. Otherwise,
1098 * it returnes -1.
1099 */
1100static int
1101megasas_make_sgl_skinny(struct megasas_instance *instance,
1102 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1103{
1104 int i;
1105 int sge_count;
1106 struct scatterlist *os_sgl;
1107
1108 sge_count = scsi_dma_map(scp);
1109
1110 if (sge_count) {
1111 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301112 mfi_sgl->sge_skinny[i].length =
1113 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001114 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301115 cpu_to_le64(sg_dma_address(os_sgl));
1116 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001117 }
1118 }
1119 return sge_count;
1120}
1121
Sumant Patrob1df99d2006-10-03 12:40:47 -07001122 /**
1123 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001124 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001125 * @sge_count : number of sg elements
1126 *
1127 * Returns the number of frames required for numnber of sge's (sge_count)
1128 */
1129
Yang, Bof4c9a132009-10-06 14:43:28 -06001130static u32 megasas_get_frame_count(struct megasas_instance *instance,
1131 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001132{
1133 int num_cnt;
1134 int sge_bytes;
1135 u32 sge_sz;
1136 u32 frame_count=0;
1137
1138 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1139 sizeof(struct megasas_sge32);
1140
Yang, Bof4c9a132009-10-06 14:43:28 -06001141 if (instance->flag_ieee) {
1142 sge_sz = sizeof(struct megasas_sge_skinny);
1143 }
1144
Sumant Patrob1df99d2006-10-03 12:40:47 -07001145 /*
bo yangd532dbe2008-03-17 03:36:43 -04001146 * Main frame can contain 2 SGEs for 64-bit SGLs and
1147 * 3 SGEs for 32-bit SGLs for ldio &
1148 * 1 SGEs for 64-bit SGLs and
1149 * 2 SGEs for 32-bit SGLs for pthru frame
1150 */
1151 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001152 if (instance->flag_ieee == 1) {
1153 num_cnt = sge_count - 1;
1154 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001155 num_cnt = sge_count - 1;
1156 else
1157 num_cnt = sge_count - 2;
1158 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001159 if (instance->flag_ieee == 1) {
1160 num_cnt = sge_count - 1;
1161 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001162 num_cnt = sge_count - 2;
1163 else
1164 num_cnt = sge_count - 3;
1165 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001166
1167 if(num_cnt>0){
1168 sge_bytes = sge_sz * num_cnt;
1169
1170 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1171 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1172 }
1173 /* Main frame */
1174 frame_count +=1;
1175
1176 if (frame_count > 7)
1177 frame_count = 8;
1178 return frame_count;
1179}
1180
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001181/**
1182 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1183 * @instance: Adapter soft state
1184 * @scp: SCSI command
1185 * @cmd: Command to be prepared in
1186 *
1187 * This function prepares CDB commands. These are typcially pass-through
1188 * commands to the devices.
1189 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001190static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001191megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1192 struct megasas_cmd *cmd)
1193{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001194 u32 is_logical;
1195 u32 device_id;
1196 u16 flags = 0;
1197 struct megasas_pthru_frame *pthru;
1198
1199 is_logical = MEGASAS_IS_LOGICAL(scp);
1200 device_id = MEGASAS_DEV_INDEX(instance, scp);
1201 pthru = (struct megasas_pthru_frame *)cmd->frame;
1202
1203 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1204 flags = MFI_FRAME_DIR_WRITE;
1205 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1206 flags = MFI_FRAME_DIR_READ;
1207 else if (scp->sc_data_direction == PCI_DMA_NONE)
1208 flags = MFI_FRAME_DIR_NONE;
1209
Yang, Bof4c9a132009-10-06 14:43:28 -06001210 if (instance->flag_ieee == 1) {
1211 flags |= MFI_FRAME_IEEE;
1212 }
1213
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001214 /*
1215 * Prepare the DCDB frame
1216 */
1217 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1218 pthru->cmd_status = 0x0;
1219 pthru->scsi_status = 0x0;
1220 pthru->target_id = device_id;
1221 pthru->lun = scp->device->lun;
1222 pthru->cdb_len = scp->cmd_len;
1223 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001224 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301225 pthru->flags = cpu_to_le16(flags);
1226 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001227
1228 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1229
1230 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001231 * If the command is for the tape device, set the
1232 * pthru timeout to the os layer timeout value.
1233 */
1234 if (scp->device->type == TYPE_TAPE) {
1235 if ((scp->request->timeout / HZ) > 0xFFFF)
1236 pthru->timeout = 0xFFFF;
1237 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301238 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001239 }
1240
1241 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001242 * Construct SGL
1243 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001244 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301245 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001246 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1247 &pthru->sgl);
1248 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301249 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001250 pthru->sge_count = megasas_make_sgl64(instance, scp,
1251 &pthru->sgl);
1252 } else
1253 pthru->sge_count = megasas_make_sgl32(instance, scp,
1254 &pthru->sgl);
1255
Yang, Bobdc6fb82009-12-06 08:30:19 -07001256 if (pthru->sge_count > instance->max_num_sge) {
1257 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1258 pthru->sge_count);
1259 return 0;
1260 }
1261
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001262 /*
1263 * Sense info specific
1264 */
1265 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301266 pthru->sense_buf_phys_addr_hi =
1267 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1268 pthru->sense_buf_phys_addr_lo =
1269 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001270
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001271 /*
1272 * Compute the total number of frames this command consumes. FW uses
1273 * this number to pull sufficient number of frames from host memory.
1274 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001275 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001276 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001277
1278 return cmd->frame_count;
1279}
1280
1281/**
1282 * megasas_build_ldio - Prepares IOs to logical devices
1283 * @instance: Adapter soft state
1284 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001285 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001286 *
1287 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1288 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001289static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001290megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1291 struct megasas_cmd *cmd)
1292{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001293 u32 device_id;
1294 u8 sc = scp->cmnd[0];
1295 u16 flags = 0;
1296 struct megasas_io_frame *ldio;
1297
1298 device_id = MEGASAS_DEV_INDEX(instance, scp);
1299 ldio = (struct megasas_io_frame *)cmd->frame;
1300
1301 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1302 flags = MFI_FRAME_DIR_WRITE;
1303 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1304 flags = MFI_FRAME_DIR_READ;
1305
Yang, Bof4c9a132009-10-06 14:43:28 -06001306 if (instance->flag_ieee == 1) {
1307 flags |= MFI_FRAME_IEEE;
1308 }
1309
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001310 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001311 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001312 */
1313 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1314 ldio->cmd_status = 0x0;
1315 ldio->scsi_status = 0x0;
1316 ldio->target_id = device_id;
1317 ldio->timeout = 0;
1318 ldio->reserved_0 = 0;
1319 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301320 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001321 ldio->start_lba_hi = 0;
1322 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1323
1324 /*
1325 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1326 */
1327 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301328 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1329 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1330 ((u32) scp->cmnd[2] << 8) |
1331 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001332
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301333 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001334 }
1335
1336 /*
1337 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1338 */
1339 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301340 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1341 ((u32) scp->cmnd[7] << 8));
1342 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1343 ((u32) scp->cmnd[3] << 16) |
1344 ((u32) scp->cmnd[4] << 8) |
1345 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001346 }
1347
1348 /*
1349 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1350 */
1351 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301352 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1353 ((u32) scp->cmnd[7] << 16) |
1354 ((u32) scp->cmnd[8] << 8) |
1355 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001356
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301357 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1358 ((u32) scp->cmnd[3] << 16) |
1359 ((u32) scp->cmnd[4] << 8) |
1360 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001361 }
1362
1363 /*
1364 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1365 */
1366 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301367 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1368 ((u32) scp->cmnd[11] << 16) |
1369 ((u32) scp->cmnd[12] << 8) |
1370 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001371
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301372 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1373 ((u32) scp->cmnd[7] << 16) |
1374 ((u32) scp->cmnd[8] << 8) |
1375 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001376
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301377 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1378 ((u32) scp->cmnd[3] << 16) |
1379 ((u32) scp->cmnd[4] << 8) |
1380 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001381
1382 }
1383
1384 /*
1385 * Construct SGL
1386 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001387 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301388 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001389 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1390 &ldio->sgl);
1391 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301392 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001393 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1394 } else
1395 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1396
Yang, Bobdc6fb82009-12-06 08:30:19 -07001397 if (ldio->sge_count > instance->max_num_sge) {
1398 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1399 ldio->sge_count);
1400 return 0;
1401 }
1402
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001403 /*
1404 * Sense info specific
1405 */
1406 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1407 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301408 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001409
Sumant Patrob1df99d2006-10-03 12:40:47 -07001410 /*
1411 * Compute the total number of frames this command consumes. FW uses
1412 * this number to pull sufficient number of frames from host memory.
1413 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001414 cmd->frame_count = megasas_get_frame_count(instance,
1415 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001416
1417 return cmd->frame_count;
1418}
1419
1420/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001421 * megasas_is_ldio - Checks if the cmd is for logical drive
1422 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001423 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001424 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001425 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001426 */
adam radford9c915a82010-12-21 13:34:31 -08001427inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001428{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001429 if (!MEGASAS_IS_LOGICAL(cmd))
1430 return 0;
1431 switch (cmd->cmnd[0]) {
1432 case READ_10:
1433 case WRITE_10:
1434 case READ_12:
1435 case WRITE_12:
1436 case READ_6:
1437 case WRITE_6:
1438 case READ_16:
1439 case WRITE_16:
1440 return 1;
1441 default:
1442 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001443 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001444}
1445
Sumant Patro658dced2006-10-03 13:09:14 -07001446 /**
1447 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1448 * in FW
1449 * @instance: Adapter soft state
1450 */
1451static inline void
1452megasas_dump_pending_frames(struct megasas_instance *instance)
1453{
1454 struct megasas_cmd *cmd;
1455 int i,n;
1456 union megasas_sgl *mfi_sgl;
1457 struct megasas_io_frame *ldio;
1458 struct megasas_pthru_frame *pthru;
1459 u32 sgcount;
1460 u32 max_cmd = instance->max_fw_cmds;
1461
1462 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1463 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1464 if (IS_DMA64)
1465 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1466 else
1467 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1468
1469 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1470 for (i = 0; i < max_cmd; i++) {
1471 cmd = instance->cmd_list[i];
1472 if(!cmd->scmd)
1473 continue;
1474 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1475 if (megasas_is_ldio(cmd->scmd)){
1476 ldio = (struct megasas_io_frame *)cmd->frame;
1477 mfi_sgl = &ldio->sgl;
1478 sgcount = ldio->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301479 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1480 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1481 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1482 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1483 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001484 }
1485 else {
1486 pthru = (struct megasas_pthru_frame *) cmd->frame;
1487 mfi_sgl = &pthru->sgl;
1488 sgcount = pthru->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301489 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1490 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1491 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1492 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1493 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001494 }
1495 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1496 for (n = 0; n < sgcount; n++){
1497 if (IS_DMA64)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301498 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
1499 le32_to_cpu(mfi_sgl->sge64[n].length),
1500 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001501 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301502 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",
1503 le32_to_cpu(mfi_sgl->sge32[n].length),
1504 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001505 }
1506 }
1507 printk(KERN_ERR "\n");
1508 } /*for max_cmd*/
1509 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1510 for (i = 0; i < max_cmd; i++) {
1511
1512 cmd = instance->cmd_list[i];
1513
1514 if(cmd->sync_cmd == 1){
1515 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1516 }
1517 }
1518 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1519}
1520
adam radfordcd50ba82010-12-21 10:23:23 -08001521u32
1522megasas_build_and_issue_cmd(struct megasas_instance *instance,
1523 struct scsi_cmnd *scmd)
1524{
1525 struct megasas_cmd *cmd;
1526 u32 frame_count;
1527
1528 cmd = megasas_get_cmd(instance);
1529 if (!cmd)
1530 return SCSI_MLQUEUE_HOST_BUSY;
1531
1532 /*
1533 * Logical drive command
1534 */
1535 if (megasas_is_ldio(scmd))
1536 frame_count = megasas_build_ldio(instance, scmd, cmd);
1537 else
1538 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1539
1540 if (!frame_count)
1541 goto out_return_cmd;
1542
1543 cmd->scmd = scmd;
1544 scmd->SCp.ptr = (char *)cmd;
1545
1546 /*
1547 * Issue the command to the FW
1548 */
1549 atomic_inc(&instance->fw_outstanding);
1550
1551 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1552 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001553
1554 return 0;
1555out_return_cmd:
1556 megasas_return_cmd(instance, cmd);
1557 return 1;
1558}
1559
1560
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001561/**
1562 * megasas_queue_command - Queue entry point
1563 * @scmd: SCSI command to be queued
1564 * @done: Callback entry point
1565 */
1566static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301567megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001568{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001569 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001570 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001571
1572 instance = (struct megasas_instance *)
1573 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001574
bo yang39a98552010-09-22 22:36:29 -04001575 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001576 return SCSI_MLQUEUE_HOST_BUSY;
1577
bo yang39a98552010-09-22 22:36:29 -04001578 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301579
adam radford229fe472014-03-10 02:51:56 -07001580 /* Check for an mpio path and adjust behavior */
1581 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1582 if (megasas_check_mpio_paths(instance, scmd) ==
1583 (DID_RESET << 16)) {
1584 spin_unlock_irqrestore(&instance->hba_lock, flags);
1585 return SCSI_MLQUEUE_HOST_BUSY;
1586 } else {
1587 spin_unlock_irqrestore(&instance->hba_lock, flags);
1588 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301589 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001590 return 0;
1591 }
1592 }
1593
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301594 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1595 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford229fe472014-03-10 02:51:56 -07001596 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301597 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301598 return 0;
1599 }
1600
bo yang39a98552010-09-22 22:36:29 -04001601 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1602 spin_unlock_irqrestore(&instance->hba_lock, flags);
1603 return SCSI_MLQUEUE_HOST_BUSY;
1604 }
1605
1606 spin_unlock_irqrestore(&instance->hba_lock, flags);
1607
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001608 scmd->result = 0;
1609
Sumant Patrocb59aa62006-01-25 11:53:25 -08001610 if (MEGASAS_IS_LOGICAL(scmd) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301611 (scmd->device->id >= instance->fw_supported_vd_count ||
1612 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001613 scmd->result = DID_BAD_TARGET << 16;
1614 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001615 }
1616
Sumant Patro02b01e02007-02-14 13:00:55 -08001617 switch (scmd->cmnd[0]) {
1618 case SYNCHRONIZE_CACHE:
1619 /*
1620 * FW takes care of flush cache on its own
1621 * No need to send it down
1622 */
1623 scmd->result = DID_OK << 16;
1624 goto out_done;
1625 default:
1626 break;
1627 }
1628
adam radfordcd50ba82010-12-21 10:23:23 -08001629 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1630 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001631 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001632 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001633
1634 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001635
Sumant Patrocb59aa62006-01-25 11:53:25 -08001636 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301637 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001638 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001639}
1640
Yang, Bo044833b2009-10-06 14:33:06 -06001641static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1642{
1643 int i;
1644
1645 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1646
1647 if ((megasas_mgmt_info.instance[i]) &&
1648 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1649 return megasas_mgmt_info.instance[i];
1650 }
1651
1652 return NULL;
1653}
1654
Christoph Hellwig147aab62006-02-17 12:13:48 +01001655static int megasas_slave_configure(struct scsi_device *sdev)
1656{
Yang, Bo044833b2009-10-06 14:33:06 -06001657 /*
1658 * The RAID firmware may require extended timeouts.
1659 */
1660 blk_queue_rq_timeout(sdev->request_queue,
1661 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301662
Yang, Bo044833b2009-10-06 14:33:06 -06001663 return 0;
1664}
1665
1666static int megasas_slave_alloc(struct scsi_device *sdev)
1667{
1668 u16 pd_index = 0;
1669 struct megasas_instance *instance ;
1670 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301671 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
Yang, Bo044833b2009-10-06 14:33:06 -06001672 /*
1673 * Open the OS scan to the SYSTEM PD
1674 */
1675 pd_index =
1676 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1677 sdev->id;
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301678 if (instance->pd_list[pd_index].driveState ==
1679 MR_PD_STATE_SYSTEM) {
Yang, Bo044833b2009-10-06 14:33:06 -06001680 return 0;
1681 }
1682 return -ENXIO;
1683 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001684 return 0;
1685}
1686
adam radford9c915a82010-12-21 13:34:31 -08001687void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001688{
1689 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001690 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
adam radford36807e62011-10-08 18:15:06 -07001691 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07001692 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301693 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1694 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08001695 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001696 /* Flush */
1697 readl(&instance->reg_set->doorbell);
1698 if (instance->mpio && instance->requestorId)
1699 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001700 } else {
adam radford9c915a82010-12-21 13:34:31 -08001701 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1702 }
1703}
1704
1705 /**
1706 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1707 * restored to max value
1708 * @instance: Adapter soft state
1709 *
1710 */
1711void
1712megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1713{
1714 unsigned long flags;
1715 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001716 && time_after(jiffies, instance->last_time + 5 * HZ)
1717 && atomic_read(&instance->fw_outstanding) <
1718 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001719
1720 spin_lock_irqsave(instance->host->host_lock, flags);
1721 instance->flag &= ~MEGASAS_FW_BUSY;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05301722 if (instance->is_imr) {
adam radford9c915a82010-12-21 13:34:31 -08001723 instance->host->can_queue =
1724 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1725 } else
1726 instance->host->can_queue =
1727 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1728
1729 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001730 }
1731}
1732
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001733/**
bo yang7343eb62007-11-09 04:35:44 -05001734 * megasas_complete_cmd_dpc - Returns FW's controller structure
1735 * @instance_addr: Address of adapter soft state
1736 *
1737 * Tasklet to complete cmds
1738 */
1739static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1740{
1741 u32 producer;
1742 u32 consumer;
1743 u32 context;
1744 struct megasas_cmd *cmd;
1745 struct megasas_instance *instance =
1746 (struct megasas_instance *)instance_addr;
1747 unsigned long flags;
1748
1749 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001750 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001751 return;
1752
1753 spin_lock_irqsave(&instance->completion_lock, flags);
1754
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301755 producer = le32_to_cpu(*instance->producer);
1756 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001757
1758 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301759 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001760 if (context >= instance->max_fw_cmds) {
1761 printk(KERN_ERR "Unexpected context value %x\n",
1762 context);
1763 BUG();
1764 }
bo yang7343eb62007-11-09 04:35:44 -05001765
1766 cmd = instance->cmd_list[context];
1767
1768 megasas_complete_cmd(instance, cmd, DID_OK);
1769
1770 consumer++;
1771 if (consumer == (instance->max_fw_cmds + 1)) {
1772 consumer = 0;
1773 }
1774 }
1775
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301776 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05001777
1778 spin_unlock_irqrestore(&instance->completion_lock, flags);
1779
1780 /*
1781 * Check if we can restore can_queue
1782 */
adam radford9c915a82010-12-21 13:34:31 -08001783 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001784}
1785
adam radford229fe472014-03-10 02:51:56 -07001786/**
1787 * megasas_start_timer - Initializes a timer object
1788 * @instance: Adapter soft state
1789 * @timer: timer object to be initialized
1790 * @fn: timer function
1791 * @interval: time interval between timer function call
1792 *
1793 */
1794void megasas_start_timer(struct megasas_instance *instance,
1795 struct timer_list *timer,
1796 void *fn, unsigned long interval)
1797{
1798 init_timer(timer);
1799 timer->expires = jiffies + interval;
1800 timer->data = (unsigned long)instance;
1801 timer->function = fn;
1802 add_timer(timer);
1803}
1804
Yang, Bo707e09b2010-10-12 07:20:27 -06001805static void
1806megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1807
1808static void
1809process_fw_state_change_wq(struct work_struct *work);
1810
1811void megasas_do_ocr(struct megasas_instance *instance)
1812{
1813 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1814 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1815 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301816 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06001817 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301818 instance->instancet->disable_intr(instance);
Yang, Bo707e09b2010-10-12 07:20:27 -06001819 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1820 instance->issuepend_done = 0;
1821
1822 atomic_set(&instance->fw_outstanding, 0);
1823 megasas_internal_reset_defer_cmds(instance);
1824 process_fw_state_change_wq(&instance->work_init);
1825}
1826
Adam Radford4cbfea82014-07-09 15:17:56 -07001827static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1828 int initial)
adam radford229fe472014-03-10 02:51:56 -07001829{
1830 struct megasas_cmd *cmd;
1831 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07001832 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07001833 dma_addr_t new_affiliation_111_h;
1834 int ld, retval = 0;
1835 u8 thisVf;
1836
1837 cmd = megasas_get_cmd(instance);
1838
1839 if (!cmd) {
Adam Radford4cbfea82014-07-09 15:17:56 -07001840 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation_111:"
1841 "Failed to get cmd for scsi%d.\n",
adam radford229fe472014-03-10 02:51:56 -07001842 instance->host->host_no);
1843 return -ENOMEM;
1844 }
1845
1846 dcmd = &cmd->frame->dcmd;
1847
Adam Radford4cbfea82014-07-09 15:17:56 -07001848 if (!instance->vf_affiliation_111) {
adam radford229fe472014-03-10 02:51:56 -07001849 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1850 "affiliation for scsi%d.\n", instance->host->host_no);
1851 megasas_return_cmd(instance, cmd);
1852 return -ENOMEM;
1853 }
1854
1855 if (initial)
adam radford229fe472014-03-10 02:51:56 -07001856 memset(instance->vf_affiliation_111, 0,
1857 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001858 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07001859 new_affiliation_111 =
1860 pci_alloc_consistent(instance->pdev,
1861 sizeof(struct MR_LD_VF_AFFILIATION_111),
1862 &new_affiliation_111_h);
1863 if (!new_affiliation_111) {
adam radford229fe472014-03-10 02:51:56 -07001864 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
1865 "memory for new affiliation for scsi%d.\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07001866 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07001867 megasas_return_cmd(instance, cmd);
1868 return -ENOMEM;
1869 }
Adam Radford4cbfea82014-07-09 15:17:56 -07001870 memset(new_affiliation_111, 0,
1871 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001872 }
1873
1874 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1875
1876 dcmd->cmd = MFI_CMD_DCMD;
1877 dcmd->cmd_status = 0xFF;
1878 dcmd->sge_count = 1;
1879 dcmd->flags = MFI_FRAME_DIR_BOTH;
1880 dcmd->timeout = 0;
1881 dcmd->pad_0 = 0;
Adam Radford4cbfea82014-07-09 15:17:56 -07001882 dcmd->data_xfer_len = sizeof(struct MR_LD_VF_AFFILIATION_111);
1883 dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111;
adam radford229fe472014-03-10 02:51:56 -07001884
Adam Radford4cbfea82014-07-09 15:17:56 -07001885 if (initial)
1886 dcmd->sgl.sge32[0].phys_addr =
1887 instance->vf_affiliation_111_h;
adam radford229fe472014-03-10 02:51:56 -07001888 else
Adam Radford4cbfea82014-07-09 15:17:56 -07001889 dcmd->sgl.sge32[0].phys_addr = new_affiliation_111_h;
1890
1891 dcmd->sgl.sge32[0].length =
1892 sizeof(struct MR_LD_VF_AFFILIATION_111);
adam radford229fe472014-03-10 02:51:56 -07001893
1894 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
1895 "scsi%d\n", instance->host->host_no);
1896
1897 megasas_issue_blocked_cmd(instance, cmd, 0);
1898
1899 if (dcmd->cmd_status) {
1900 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
1901 " failed with status 0x%x for scsi%d.\n",
1902 dcmd->cmd_status, instance->host->host_no);
1903 retval = 1; /* Do a scan if we couldn't get affiliation */
1904 goto out;
1905 }
1906
1907 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07001908 thisVf = new_affiliation_111->thisVf;
1909 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
1910 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
1911 new_affiliation_111->map[ld].policy[thisVf]) {
1912 printk(KERN_WARNING "megasas: SR-IOV: "
1913 "Got new LD/VF affiliation "
adam radford229fe472014-03-10 02:51:56 -07001914 "for scsi%d.\n",
adam radford229fe472014-03-10 02:51:56 -07001915 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07001916 memcpy(instance->vf_affiliation_111,
1917 new_affiliation_111,
1918 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07001919 retval = 1;
1920 goto out;
1921 }
Adam Radford4cbfea82014-07-09 15:17:56 -07001922 }
1923out:
1924 if (new_affiliation_111) {
1925 pci_free_consistent(instance->pdev,
1926 sizeof(struct MR_LD_VF_AFFILIATION_111),
1927 new_affiliation_111,
1928 new_affiliation_111_h);
1929 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301930
1931 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
1932 megasas_return_mfi_mpt_pthr(instance, cmd,
1933 cmd->mpt_pthr_cmd_blocked);
1934 else
1935 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07001936
1937 return retval;
1938}
1939
1940static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
1941 int initial)
1942{
1943 struct megasas_cmd *cmd;
1944 struct megasas_dcmd_frame *dcmd;
1945 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
1946 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
1947 dma_addr_t new_affiliation_h;
1948 int i, j, retval = 0, found = 0, doscan = 0;
1949 u8 thisVf;
1950
1951 cmd = megasas_get_cmd(instance);
1952
1953 if (!cmd) {
1954 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation12: "
1955 "Failed to get cmd for scsi%d.\n",
1956 instance->host->host_no);
1957 return -ENOMEM;
1958 }
1959
1960 dcmd = &cmd->frame->dcmd;
1961
1962 if (!instance->vf_affiliation) {
1963 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1964 "affiliation for scsi%d.\n", instance->host->host_no);
1965 megasas_return_cmd(instance, cmd);
1966 return -ENOMEM;
1967 }
1968
1969 if (initial)
1970 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
1971 sizeof(struct MR_LD_VF_AFFILIATION));
1972 else {
1973 new_affiliation =
1974 pci_alloc_consistent(instance->pdev,
1975 (MAX_LOGICAL_DRIVES + 1) *
1976 sizeof(struct MR_LD_VF_AFFILIATION),
1977 &new_affiliation_h);
1978 if (!new_affiliation) {
1979 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
1980 "memory for new affiliation for scsi%d.\n",
1981 instance->host->host_no);
1982 megasas_return_cmd(instance, cmd);
1983 return -ENOMEM;
1984 }
1985 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
1986 sizeof(struct MR_LD_VF_AFFILIATION));
1987 }
1988
1989 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1990
1991 dcmd->cmd = MFI_CMD_DCMD;
1992 dcmd->cmd_status = 0xFF;
1993 dcmd->sge_count = 1;
1994 dcmd->flags = MFI_FRAME_DIR_BOTH;
1995 dcmd->timeout = 0;
1996 dcmd->pad_0 = 0;
1997 dcmd->data_xfer_len = (MAX_LOGICAL_DRIVES + 1) *
1998 sizeof(struct MR_LD_VF_AFFILIATION);
1999 dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS;
2000
2001 if (initial)
2002 dcmd->sgl.sge32[0].phys_addr = instance->vf_affiliation_h;
2003 else
2004 dcmd->sgl.sge32[0].phys_addr = new_affiliation_h;
2005
2006 dcmd->sgl.sge32[0].length = (MAX_LOGICAL_DRIVES + 1) *
2007 sizeof(struct MR_LD_VF_AFFILIATION);
2008
2009 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
2010 "scsi%d\n", instance->host->host_no);
2011
2012 megasas_issue_blocked_cmd(instance, cmd, 0);
2013
2014 if (dcmd->cmd_status) {
2015 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
2016 " failed with status 0x%x for scsi%d.\n",
2017 dcmd->cmd_status, instance->host->host_no);
2018 retval = 1; /* Do a scan if we couldn't get affiliation */
2019 goto out;
2020 }
2021
2022 if (!initial) {
2023 if (!new_affiliation->ldCount) {
2024 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2025 "affiliation for passive path for scsi%d.\n",
2026 instance->host->host_no);
2027 retval = 1;
2028 goto out;
2029 }
2030 newmap = new_affiliation->map;
2031 savedmap = instance->vf_affiliation->map;
2032 thisVf = new_affiliation->thisVf;
2033 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2034 found = 0;
2035 for (j = 0; j < instance->vf_affiliation->ldCount;
2036 j++) {
2037 if (newmap->ref.targetId ==
2038 savedmap->ref.targetId) {
2039 found = 1;
2040 if (newmap->policy[thisVf] !=
2041 savedmap->policy[thisVf]) {
2042 doscan = 1;
2043 goto out;
2044 }
adam radford229fe472014-03-10 02:51:56 -07002045 }
2046 savedmap = (struct MR_LD_VF_MAP *)
2047 ((unsigned char *)savedmap +
2048 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002049 }
2050 if (!found && newmap->policy[thisVf] !=
2051 MR_LD_ACCESS_HIDDEN) {
2052 doscan = 1;
2053 goto out;
2054 }
2055 newmap = (struct MR_LD_VF_MAP *)
2056 ((unsigned char *)newmap + newmap->size);
2057 }
2058
2059 newmap = new_affiliation->map;
2060 savedmap = instance->vf_affiliation->map;
2061
2062 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2063 found = 0;
2064 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2065 if (savedmap->ref.targetId ==
2066 newmap->ref.targetId) {
2067 found = 1;
2068 if (savedmap->policy[thisVf] !=
2069 newmap->policy[thisVf]) {
2070 doscan = 1;
2071 goto out;
2072 }
2073 }
adam radford229fe472014-03-10 02:51:56 -07002074 newmap = (struct MR_LD_VF_MAP *)
2075 ((unsigned char *)newmap +
2076 newmap->size);
2077 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002078 if (!found && savedmap->policy[thisVf] !=
2079 MR_LD_ACCESS_HIDDEN) {
2080 doscan = 1;
2081 goto out;
2082 }
2083 savedmap = (struct MR_LD_VF_MAP *)
2084 ((unsigned char *)savedmap +
2085 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002086 }
2087 }
2088out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002089 if (doscan) {
2090 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2091 "affiliation for scsi%d.\n", instance->host->host_no);
2092 memcpy(instance->vf_affiliation, new_affiliation,
2093 new_affiliation->size);
2094 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002095 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002096
2097 if (new_affiliation)
2098 pci_free_consistent(instance->pdev,
2099 (MAX_LOGICAL_DRIVES + 1) *
2100 sizeof(struct MR_LD_VF_AFFILIATION),
2101 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302102 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2103 megasas_return_mfi_mpt_pthr(instance, cmd,
2104 cmd->mpt_pthr_cmd_blocked);
2105 else
2106 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002107
2108 return retval;
2109}
2110
Adam Radford4cbfea82014-07-09 15:17:56 -07002111/* This function will get the current SR-IOV LD/VF affiliation */
2112static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2113 int initial)
2114{
2115 int retval;
2116
2117 if (instance->PlasmaFW111)
2118 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2119 else
2120 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2121 return retval;
2122}
2123
adam radford229fe472014-03-10 02:51:56 -07002124/* This function will tell FW to start the SR-IOV heartbeat */
2125int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2126 int initial)
2127{
2128 struct megasas_cmd *cmd;
2129 struct megasas_dcmd_frame *dcmd;
2130 int retval = 0;
2131
2132 cmd = megasas_get_cmd(instance);
2133
2134 if (!cmd) {
2135 printk(KERN_DEBUG "megasas: megasas_sriov_start_heartbeat: "
2136 "Failed to get cmd for scsi%d.\n",
2137 instance->host->host_no);
2138 return -ENOMEM;
2139 }
2140
2141 dcmd = &cmd->frame->dcmd;
2142
2143 if (initial) {
2144 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002145 pci_zalloc_consistent(instance->pdev,
2146 sizeof(struct MR_CTRL_HB_HOST_MEM),
2147 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002148 if (!instance->hb_host_mem) {
2149 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate"
2150 " memory for heartbeat host memory for "
2151 "scsi%d.\n", instance->host->host_no);
2152 retval = -ENOMEM;
2153 goto out;
2154 }
adam radford229fe472014-03-10 02:51:56 -07002155 }
2156
2157 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2158
2159 dcmd->mbox.s[0] = sizeof(struct MR_CTRL_HB_HOST_MEM);
2160 dcmd->cmd = MFI_CMD_DCMD;
2161 dcmd->cmd_status = 0xFF;
2162 dcmd->sge_count = 1;
2163 dcmd->flags = MFI_FRAME_DIR_BOTH;
2164 dcmd->timeout = 0;
2165 dcmd->pad_0 = 0;
2166 dcmd->data_xfer_len = sizeof(struct MR_CTRL_HB_HOST_MEM);
2167 dcmd->opcode = MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC;
2168 dcmd->sgl.sge32[0].phys_addr = instance->hb_host_mem_h;
2169 dcmd->sgl.sge32[0].length = sizeof(struct MR_CTRL_HB_HOST_MEM);
2170
2171 printk(KERN_WARNING "megasas: SR-IOV: Starting heartbeat for scsi%d\n",
2172 instance->host->host_no);
2173
2174 if (!megasas_issue_polled(instance, cmd)) {
2175 retval = 0;
2176 } else {
2177 printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2178 "_MEM_ALLOC DCMD timed out for scsi%d\n",
2179 instance->host->host_no);
2180 retval = 1;
2181 goto out;
2182 }
2183
2184
2185 if (dcmd->cmd_status) {
2186 printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2187 "_MEM_ALLOC DCMD failed with status 0x%x for scsi%d\n",
2188 dcmd->cmd_status,
2189 instance->host->host_no);
2190 retval = 1;
2191 goto out;
2192 }
2193
2194out:
2195 megasas_return_cmd(instance, cmd);
2196
2197 return retval;
2198}
2199
2200/* Handler for SR-IOV heartbeat */
2201void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2202{
2203 struct megasas_instance *instance =
2204 (struct megasas_instance *)instance_addr;
2205
2206 if (instance->hb_host_mem->HB.fwCounter !=
2207 instance->hb_host_mem->HB.driverCounter) {
2208 instance->hb_host_mem->HB.driverCounter =
2209 instance->hb_host_mem->HB.fwCounter;
2210 mod_timer(&instance->sriov_heartbeat_timer,
2211 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2212 } else {
2213 printk(KERN_WARNING "megasas: SR-IOV: Heartbeat never "
2214 "completed for scsi%d\n", instance->host->host_no);
2215 schedule_work(&instance->work_init);
2216 }
2217}
2218
bo yang7343eb62007-11-09 04:35:44 -05002219/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002220 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2221 * @instance: Adapter soft state
2222 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002223 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002224 * complete all its outstanding commands. Returns error if one or more IOs
2225 * are pending after this time period. It also marks the controller dead.
2226 */
2227static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2228{
2229 int i;
bo yang39a98552010-09-22 22:36:29 -04002230 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002231 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002232 u8 adprecovery;
2233 unsigned long flags;
2234 struct list_head clist_local;
2235 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002236 u32 fw_state;
2237 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04002238
2239 spin_lock_irqsave(&instance->hba_lock, flags);
2240 adprecovery = instance->adprecovery;
2241 spin_unlock_irqrestore(&instance->hba_lock, flags);
2242
2243 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2244
2245 INIT_LIST_HEAD(&clist_local);
2246 spin_lock_irqsave(&instance->hba_lock, flags);
2247 list_splice_init(&instance->internal_reset_pending_q,
2248 &clist_local);
2249 spin_unlock_irqrestore(&instance->hba_lock, flags);
2250
2251 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
2252 for (i = 0; i < wait_time; i++) {
2253 msleep(1000);
2254 spin_lock_irqsave(&instance->hba_lock, flags);
2255 adprecovery = instance->adprecovery;
2256 spin_unlock_irqrestore(&instance->hba_lock, flags);
2257 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2258 break;
2259 }
2260
2261 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2262 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
2263 spin_lock_irqsave(&instance->hba_lock, flags);
2264 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2265 spin_unlock_irqrestore(&instance->hba_lock, flags);
2266 return FAILED;
2267 }
2268
2269 reset_index = 0;
2270 while (!list_empty(&clist_local)) {
2271 reset_cmd = list_entry((&clist_local)->next,
2272 struct megasas_cmd, list);
2273 list_del_init(&reset_cmd->list);
2274 if (reset_cmd->scmd) {
2275 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002276 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002277 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002278 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002279
2280 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2281 megasas_return_cmd(instance, reset_cmd);
2282 } else if (reset_cmd->sync_cmd) {
2283 printk(KERN_NOTICE "megasas:%p synch cmds"
2284 "reset queue\n",
2285 reset_cmd);
2286
2287 reset_cmd->cmd_status = ENODATA;
2288 instance->instancet->fire_cmd(instance,
2289 reset_cmd->frame_phys_addr,
2290 0, instance->reg_set);
2291 } else {
2292 printk(KERN_NOTICE "megasas: %p unexpected"
2293 "cmds lst\n",
2294 reset_cmd);
2295 }
2296 reset_index++;
2297 }
2298
2299 return SUCCESS;
2300 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002301
adam radfordc007b8b2012-07-17 18:20:24 -07002302 for (i = 0; i < resetwaittime; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002303
Sumant Patroe4a082c2006-05-30 12:03:37 -07002304 int outstanding = atomic_read(&instance->fw_outstanding);
2305
2306 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002307 break;
2308
2309 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2310 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002311 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002312 /*
2313 * Call cmd completion routine. Cmd to be
2314 * be completed directly without depending on isr.
2315 */
2316 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002317 }
2318
2319 msleep(1000);
2320 }
2321
Yang, Bo707e09b2010-10-12 07:20:27 -06002322 i = 0;
2323 kill_adapter_flag = 0;
2324 do {
2325 fw_state = instance->instancet->read_fw_status_reg(
2326 instance->reg_set) & MFI_STATE_MASK;
2327 if ((fw_state == MFI_STATE_FAULT) &&
2328 (instance->disableOnlineCtrlReset == 0)) {
2329 if (i == 3) {
2330 kill_adapter_flag = 2;
2331 break;
2332 }
2333 megasas_do_ocr(instance);
2334 kill_adapter_flag = 1;
2335
2336 /* wait for 1 secs to let FW finish the pending cmds */
2337 msleep(1000);
2338 }
2339 i++;
2340 } while (i <= 3);
2341
2342 if (atomic_read(&instance->fw_outstanding) &&
2343 !kill_adapter_flag) {
2344 if (instance->disableOnlineCtrlReset == 0) {
2345
2346 megasas_do_ocr(instance);
2347
2348 /* wait for 5 secs to let FW finish the pending cmds */
2349 for (i = 0; i < wait_time; i++) {
2350 int outstanding =
2351 atomic_read(&instance->fw_outstanding);
2352 if (!outstanding)
2353 return SUCCESS;
2354 msleep(1000);
2355 }
2356 }
2357 }
2358
2359 if (atomic_read(&instance->fw_outstanding) ||
2360 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04002361 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002362 /*
2363 * Send signal to FW to stop processing any pending cmds.
2364 * The controller will be taken offline by the OS now.
2365 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002366 if ((instance->pdev->device ==
2367 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2368 (instance->pdev->device ==
2369 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2370 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08002371 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002372 } else {
2373 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07002374 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06002375 }
Sumant Patro658dced2006-10-03 13:09:14 -07002376 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04002377 spin_lock_irqsave(&instance->hba_lock, flags);
2378 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2379 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002380 return FAILED;
2381 }
2382
bo yang39a98552010-09-22 22:36:29 -04002383 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
2384
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002385 return SUCCESS;
2386}
2387
2388/**
2389 * megasas_generic_reset - Generic reset routine
2390 * @scmd: Mid-layer SCSI command
2391 *
2392 * This routine implements a generic reset handler for device, bus and host
2393 * reset requests. Device, bus and host specific reset handlers can use this
2394 * function after they do their specific tasks.
2395 */
2396static int megasas_generic_reset(struct scsi_cmnd *scmd)
2397{
2398 int ret_val;
2399 struct megasas_instance *instance;
2400
2401 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2402
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002403 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2404 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002405
bo yang39a98552010-09-22 22:36:29 -04002406 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002407 printk(KERN_ERR "megasas: cannot recover from previous reset "
2408 "failures\n");
2409 return FAILED;
2410 }
2411
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002412 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002413 if (ret_val == SUCCESS)
2414 printk(KERN_NOTICE "megasas: reset successful \n");
2415 else
2416 printk(KERN_ERR "megasas: failed to do reset\n");
2417
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002418 return ret_val;
2419}
2420
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002421/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002422 * megasas_reset_timer - quiesce the adapter if required
2423 * @scmd: scsi cmnd
2424 *
2425 * Sets the FW busy flag and reduces the host->can_queue if the
2426 * cmd has not been completed within the timeout period.
2427 */
2428static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002429blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002430{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002431 struct megasas_instance *instance;
2432 unsigned long flags;
2433
2434 if (time_after(jiffies, scmd->jiffies_at_alloc +
2435 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002436 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002437 }
2438
adam radfordf575c5d2011-10-13 16:01:12 -07002439 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002440 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2441 /* FW is busy, throttle IO */
2442 spin_lock_irqsave(instance->host->host_lock, flags);
2443
adam radfordc5daa6a2012-07-17 18:20:03 -07002444 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002445 instance->last_time = jiffies;
2446 instance->flag |= MEGASAS_FW_BUSY;
2447
2448 spin_unlock_irqrestore(instance->host->host_lock, flags);
2449 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002450 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002451}
2452
2453/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002454 * megasas_reset_device - Device reset handler entry point
2455 */
2456static int megasas_reset_device(struct scsi_cmnd *scmd)
2457{
2458 int ret;
2459
2460 /*
2461 * First wait for all commands to complete
2462 */
2463 ret = megasas_generic_reset(scmd);
2464
2465 return ret;
2466}
2467
2468/**
2469 * megasas_reset_bus_host - Bus & host reset handler entry point
2470 */
2471static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2472{
2473 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002474 struct megasas_instance *instance;
2475 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002476
2477 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002478 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002479 */
adam radford36807e62011-10-08 18:15:06 -07002480 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07002481 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302482 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2483 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radford229fe472014-03-10 02:51:56 -07002484 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002485 else
2486 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002487
2488 return ret;
2489}
2490
2491/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002492 * megasas_bios_param - Returns disk geometry for a disk
2493 * @sdev: device handle
2494 * @bdev: block device
2495 * @capacity: drive capacity
2496 * @geom: geometry parameters
2497 */
2498static int
2499megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2500 sector_t capacity, int geom[])
2501{
2502 int heads;
2503 int sectors;
2504 sector_t cylinders;
2505 unsigned long tmp;
2506 /* Default heads (64) & sectors (32) */
2507 heads = 64;
2508 sectors = 32;
2509
2510 tmp = heads * sectors;
2511 cylinders = capacity;
2512
2513 sector_div(cylinders, tmp);
2514
2515 /*
2516 * Handle extended translation size for logical drives > 1Gb
2517 */
2518
2519 if (capacity >= 0x200000) {
2520 heads = 255;
2521 sectors = 63;
2522 tmp = heads*sectors;
2523 cylinders = capacity;
2524 sector_div(cylinders, tmp);
2525 }
2526
2527 geom[0] = heads;
2528 geom[1] = sectors;
2529 geom[2] = cylinders;
2530
2531 return 0;
2532}
2533
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002534static void megasas_aen_polling(struct work_struct *work);
2535
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002536/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002537 * megasas_service_aen - Processes an event notification
2538 * @instance: Adapter soft state
2539 * @cmd: AEN command completed by the ISR
2540 *
2541 * For AEN, driver sends a command down to FW that is held by the FW till an
2542 * event occurs. When an event of interest occurs, FW completes the command
2543 * that it was previously holding.
2544 *
2545 * This routines sends SIGIO signal to processes that have registered with the
2546 * driver for AEN.
2547 */
2548static void
2549megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2550{
Yang, Boc3518832009-10-06 14:18:02 -06002551 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002552 /*
2553 * Don't signal app if it is just an aborted previously registered aen
2554 */
Yang, Boc3518832009-10-06 14:18:02 -06002555 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2556 spin_lock_irqsave(&poll_aen_lock, flags);
2557 megasas_poll_wait_aen = 1;
2558 spin_unlock_irqrestore(&poll_aen_lock, flags);
2559 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002560 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002561 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002562 else
2563 cmd->abort_aen = 0;
2564
2565 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302566
2567 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2568 megasas_return_mfi_mpt_pthr(instance, cmd,
2569 cmd->mpt_pthr_cmd_blocked);
2570 else
2571 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002572
bo yang39a98552010-09-22 22:36:29 -04002573 if ((instance->unload == 0) &&
2574 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002575 struct megasas_aen_event *ev;
2576 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2577 if (!ev) {
2578 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2579 } else {
2580 ev->instance = instance;
2581 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002582 INIT_DELAYED_WORK(&ev->hotplug_work,
2583 megasas_aen_polling);
2584 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002585 }
2586 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002587}
2588
adam radford4bcde502011-07-26 15:42:52 -07002589static int megasas_change_queue_depth(struct scsi_device *sdev,
2590 int queue_depth, int reason)
2591{
2592 if (reason != SCSI_QDEPTH_DEFAULT)
2593 return -EOPNOTSUPP;
2594
2595 if (queue_depth > sdev->host->can_queue)
2596 queue_depth = sdev->host->can_queue;
2597 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2598 queue_depth);
2599
2600 return queue_depth;
2601}
2602
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302603static ssize_t
2604megasas_fw_crash_buffer_store(struct device *cdev,
2605 struct device_attribute *attr, const char *buf, size_t count)
2606{
2607 struct Scsi_Host *shost = class_to_shost(cdev);
2608 struct megasas_instance *instance =
2609 (struct megasas_instance *) shost->hostdata;
2610 int val = 0;
2611 unsigned long flags;
2612
2613 if (kstrtoint(buf, 0, &val) != 0)
2614 return -EINVAL;
2615
2616 spin_lock_irqsave(&instance->crashdump_lock, flags);
2617 instance->fw_crash_buffer_offset = val;
2618 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2619 return strlen(buf);
2620}
2621
2622static ssize_t
2623megasas_fw_crash_buffer_show(struct device *cdev,
2624 struct device_attribute *attr, char *buf)
2625{
2626 struct Scsi_Host *shost = class_to_shost(cdev);
2627 struct megasas_instance *instance =
2628 (struct megasas_instance *) shost->hostdata;
2629 u32 size;
2630 unsigned long buff_addr;
2631 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2632 unsigned long src_addr;
2633 unsigned long flags;
2634 u32 buff_offset;
2635
2636 spin_lock_irqsave(&instance->crashdump_lock, flags);
2637 buff_offset = instance->fw_crash_buffer_offset;
2638 if (!instance->crash_dump_buf &&
2639 !((instance->fw_crash_state == AVAILABLE) ||
2640 (instance->fw_crash_state == COPYING))) {
2641 dev_err(&instance->pdev->dev,
2642 "Firmware crash dump is not available\n");
2643 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2644 return -EINVAL;
2645 }
2646
2647 buff_addr = (unsigned long) buf;
2648
2649 if (buff_offset >
2650 (instance->fw_crash_buffer_size * dmachunk)) {
2651 dev_err(&instance->pdev->dev,
2652 "Firmware crash dump offset is out of range\n");
2653 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2654 return 0;
2655 }
2656
2657 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2658 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2659
2660 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2661 (buff_offset % dmachunk);
2662 memcpy(buf, (void *)src_addr, size);
2663 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2664
2665 return size;
2666}
2667
2668static ssize_t
2669megasas_fw_crash_buffer_size_show(struct device *cdev,
2670 struct device_attribute *attr, char *buf)
2671{
2672 struct Scsi_Host *shost = class_to_shost(cdev);
2673 struct megasas_instance *instance =
2674 (struct megasas_instance *) shost->hostdata;
2675
2676 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2677 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2678}
2679
2680static ssize_t
2681megasas_fw_crash_state_store(struct device *cdev,
2682 struct device_attribute *attr, const char *buf, size_t count)
2683{
2684 struct Scsi_Host *shost = class_to_shost(cdev);
2685 struct megasas_instance *instance =
2686 (struct megasas_instance *) shost->hostdata;
2687 int val = 0;
2688 unsigned long flags;
2689
2690 if (kstrtoint(buf, 0, &val) != 0)
2691 return -EINVAL;
2692
2693 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2694 dev_err(&instance->pdev->dev, "application updates invalid "
2695 "firmware crash state\n");
2696 return -EINVAL;
2697 }
2698
2699 instance->fw_crash_state = val;
2700
2701 if ((val == COPIED) || (val == COPY_ERROR)) {
2702 spin_lock_irqsave(&instance->crashdump_lock, flags);
2703 megasas_free_host_crash_buffer(instance);
2704 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2705 if (val == COPY_ERROR)
2706 dev_info(&instance->pdev->dev, "application failed to "
2707 "copy Firmware crash dump\n");
2708 else
2709 dev_info(&instance->pdev->dev, "Firmware crash dump "
2710 "copied successfully\n");
2711 }
2712 return strlen(buf);
2713}
2714
2715static ssize_t
2716megasas_fw_crash_state_show(struct device *cdev,
2717 struct device_attribute *attr, char *buf)
2718{
2719 struct Scsi_Host *shost = class_to_shost(cdev);
2720 struct megasas_instance *instance =
2721 (struct megasas_instance *) shost->hostdata;
2722 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2723}
2724
2725static ssize_t
2726megasas_page_size_show(struct device *cdev,
2727 struct device_attribute *attr, char *buf)
2728{
2729 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2730}
2731
2732static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2733 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2734static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2735 megasas_fw_crash_buffer_size_show, NULL);
2736static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2737 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2738static DEVICE_ATTR(page_size, S_IRUGO,
2739 megasas_page_size_show, NULL);
2740
2741struct device_attribute *megaraid_host_attrs[] = {
2742 &dev_attr_fw_crash_buffer_size,
2743 &dev_attr_fw_crash_buffer,
2744 &dev_attr_fw_crash_state,
2745 &dev_attr_page_size,
2746 NULL,
2747};
2748
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002749/*
2750 * Scsi host template for megaraid_sas driver
2751 */
2752static struct scsi_host_template megasas_template = {
2753
2754 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002755 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002756 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002757 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002758 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002759 .queuecommand = megasas_queue_command,
2760 .eh_device_reset_handler = megasas_reset_device,
2761 .eh_bus_reset_handler = megasas_reset_bus_host,
2762 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002763 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302764 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002765 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002766 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002767 .change_queue_depth = megasas_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002768 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002769};
2770
2771/**
2772 * megasas_complete_int_cmd - Completes an internal command
2773 * @instance: Adapter soft state
2774 * @cmd: Command to be completed
2775 *
2776 * The megasas_issue_blocked_cmd() function waits for a command to complete
2777 * after it issues a command. This function wakes up that waiting routine by
2778 * calling wake_up() on the wait queue.
2779 */
2780static void
2781megasas_complete_int_cmd(struct megasas_instance *instance,
2782 struct megasas_cmd *cmd)
2783{
2784 cmd->cmd_status = cmd->frame->io.cmd_status;
2785
2786 if (cmd->cmd_status == ENODATA) {
2787 cmd->cmd_status = 0;
2788 }
2789 wake_up(&instance->int_cmd_wait_q);
2790}
2791
2792/**
2793 * megasas_complete_abort - Completes aborting a command
2794 * @instance: Adapter soft state
2795 * @cmd: Cmd that was issued to abort another cmd
2796 *
adam radford0d490162010-12-14 19:17:17 -08002797 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2798 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799 * wakes up all functions waiting on the same wait queue.
2800 */
2801static void
2802megasas_complete_abort(struct megasas_instance *instance,
2803 struct megasas_cmd *cmd)
2804{
2805 if (cmd->sync_cmd) {
2806 cmd->sync_cmd = 0;
2807 cmd->cmd_status = 0;
2808 wake_up(&instance->abort_cmd_wait_q);
2809 }
2810
2811 return;
2812}
2813
2814/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002815 * megasas_complete_cmd - Completes a command
2816 * @instance: Adapter soft state
2817 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002818 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002819 * SCSI mid-layer instead of the value returned
2820 * by the FW. This should be used if caller wants
2821 * an alternate status (as in the case of aborted
2822 * commands)
2823 */
adam radford9c915a82010-12-21 13:34:31 -08002824void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002825megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2826 u8 alt_status)
2827{
2828 int exception = 0;
2829 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002830 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002831 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302832 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002833
bo yang39a98552010-09-22 22:36:29 -04002834 /* flag for the retry reset */
2835 cmd->retry_for_fw_reset = 0;
2836
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002837 if (cmd->scmd)
2838 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002839
2840 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002841 case MFI_CMD_INVALID:
2842 /* Some older 1068 controller FW may keep a pended
2843 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2844 when booting the kdump kernel. Ignore this command to
2845 prevent a kernel panic on shutdown of the kdump kernel. */
2846 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2847 "completed.\n");
2848 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2849 "other than PERC5, please upgrade your firmware.\n");
2850 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002851 case MFI_CMD_PD_SCSI_IO:
2852 case MFI_CMD_LD_SCSI_IO:
2853
2854 /*
2855 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2856 * issued either through an IO path or an IOCTL path. If it
2857 * was via IOCTL, we will send it to internal completion.
2858 */
2859 if (cmd->sync_cmd) {
2860 cmd->sync_cmd = 0;
2861 megasas_complete_int_cmd(instance, cmd);
2862 break;
2863 }
2864
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002865 case MFI_CMD_LD_READ:
2866 case MFI_CMD_LD_WRITE:
2867
2868 if (alt_status) {
2869 cmd->scmd->result = alt_status << 16;
2870 exception = 1;
2871 }
2872
2873 if (exception) {
2874
Sumant Patroe4a082c2006-05-30 12:03:37 -07002875 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002876
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002877 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002878 cmd->scmd->scsi_done(cmd->scmd);
2879 megasas_return_cmd(instance, cmd);
2880
2881 break;
2882 }
2883
2884 switch (hdr->cmd_status) {
2885
2886 case MFI_STAT_OK:
2887 cmd->scmd->result = DID_OK << 16;
2888 break;
2889
2890 case MFI_STAT_SCSI_IO_FAILED:
2891 case MFI_STAT_LD_INIT_IN_PROGRESS:
2892 cmd->scmd->result =
2893 (DID_ERROR << 16) | hdr->scsi_status;
2894 break;
2895
2896 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2897
2898 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2899
2900 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2901 memset(cmd->scmd->sense_buffer, 0,
2902 SCSI_SENSE_BUFFERSIZE);
2903 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2904 hdr->sense_len);
2905
2906 cmd->scmd->result |= DRIVER_SENSE << 24;
2907 }
2908
2909 break;
2910
2911 case MFI_STAT_LD_OFFLINE:
2912 case MFI_STAT_DEVICE_NOT_FOUND:
2913 cmd->scmd->result = DID_BAD_TARGET << 16;
2914 break;
2915
2916 default:
2917 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2918 hdr->cmd_status);
2919 cmd->scmd->result = DID_ERROR << 16;
2920 break;
2921 }
2922
Sumant Patroe4a082c2006-05-30 12:03:37 -07002923 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002924
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002925 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002926 cmd->scmd->scsi_done(cmd->scmd);
2927 megasas_return_cmd(instance, cmd);
2928
2929 break;
2930
2931 case MFI_CMD_SMP:
2932 case MFI_CMD_STP:
2933 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302934 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08002935 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302936 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2937 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302938 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08002939 spin_lock_irqsave(instance->host->host_lock, flags);
2940 if (cmd->frame->hdr.cmd_status != 0) {
2941 if (cmd->frame->hdr.cmd_status !=
2942 MFI_STAT_NOT_FOUND)
2943 printk(KERN_WARNING "megasas: map sync"
2944 "failed, status = 0x%x.\n",
2945 cmd->frame->hdr.cmd_status);
2946 else {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302947 megasas_return_mfi_mpt_pthr(instance,
2948 cmd, cmd->mpt_pthr_cmd_blocked);
adam radford9c915a82010-12-21 13:34:31 -08002949 spin_unlock_irqrestore(
2950 instance->host->host_lock,
2951 flags);
2952 break;
2953 }
2954 } else
2955 instance->map_id++;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302956 megasas_return_mfi_mpt_pthr(instance, cmd,
2957 cmd->mpt_pthr_cmd_blocked);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302958
2959 /*
2960 * Set fast path IO to ZERO.
2961 * Validate Map will set proper value.
2962 * Meanwhile all IOs will go as LD IO.
2963 */
2964 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08002965 fusion->fast_path_io = 1;
2966 else
2967 fusion->fast_path_io = 0;
2968 megasas_sync_map_info(instance);
2969 spin_unlock_irqrestore(instance->host->host_lock,
2970 flags);
2971 break;
2972 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302973 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2974 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06002975 spin_lock_irqsave(&poll_aen_lock, flags);
2976 megasas_poll_wait_aen = 0;
2977 spin_unlock_irqrestore(&poll_aen_lock, flags);
2978 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002979
2980 /*
2981 * See if got an event notification
2982 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302983 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002984 megasas_service_aen(instance, cmd);
2985 else
2986 megasas_complete_int_cmd(instance, cmd);
2987
2988 break;
2989
2990 case MFI_CMD_ABORT:
2991 /*
2992 * Cmd issued to abort another cmd returned
2993 */
2994 megasas_complete_abort(instance, cmd);
2995 break;
2996
2997 default:
2998 printk("megasas: Unknown command completed! [0x%X]\n",
2999 hdr->cmd);
3000 break;
3001 }
3002}
3003
3004/**
bo yang39a98552010-09-22 22:36:29 -04003005 * megasas_issue_pending_cmds_again - issue all pending cmds
3006 * in FW again because of the fw reset
3007 * @instance: Adapter soft state
3008 */
3009static inline void
3010megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3011{
3012 struct megasas_cmd *cmd;
3013 struct list_head clist_local;
3014 union megasas_evt_class_locale class_locale;
3015 unsigned long flags;
3016 u32 seq_num;
3017
3018 INIT_LIST_HEAD(&clist_local);
3019 spin_lock_irqsave(&instance->hba_lock, flags);
3020 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3021 spin_unlock_irqrestore(&instance->hba_lock, flags);
3022
3023 while (!list_empty(&clist_local)) {
3024 cmd = list_entry((&clist_local)->next,
3025 struct megasas_cmd, list);
3026 list_del_init(&cmd->list);
3027
3028 if (cmd->sync_cmd || cmd->scmd) {
3029 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
3030 "detected to be pending while HBA reset.\n",
3031 cmd, cmd->scmd, cmd->sync_cmd);
3032
3033 cmd->retry_for_fw_reset++;
3034
3035 if (cmd->retry_for_fw_reset == 3) {
3036 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
3037 "was tried multiple times during reset."
3038 "Shutting down the HBA\n",
3039 cmd, cmd->scmd, cmd->sync_cmd);
3040 megaraid_sas_kill_hba(instance);
3041
3042 instance->adprecovery =
3043 MEGASAS_HW_CRITICAL_ERROR;
3044 return;
3045 }
3046 }
3047
3048 if (cmd->sync_cmd == 1) {
3049 if (cmd->scmd) {
3050 printk(KERN_NOTICE "megaraid_sas: unexpected"
3051 "cmd attached to internal command!\n");
3052 }
3053 printk(KERN_NOTICE "megasas: %p synchronous cmd"
3054 "on the internal reset queue,"
3055 "issue it again.\n", cmd);
3056 cmd->cmd_status = ENODATA;
3057 instance->instancet->fire_cmd(instance,
3058 cmd->frame_phys_addr ,
3059 0, instance->reg_set);
3060 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003061 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003062 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003063 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003064
3065 atomic_inc(&instance->fw_outstanding);
3066 instance->instancet->fire_cmd(instance,
3067 cmd->frame_phys_addr,
3068 cmd->frame_count-1, instance->reg_set);
3069 } else {
3070 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
3071 "internal reset defer list while re-issue!!\n",
3072 cmd);
3073 }
3074 }
3075
3076 if (instance->aen_cmd) {
3077 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
3078 megasas_return_cmd(instance, instance->aen_cmd);
3079
3080 instance->aen_cmd = NULL;
3081 }
3082
3083 /*
3084 * Initiate AEN (Asynchronous Event Notification)
3085 */
3086 seq_num = instance->last_seq_num;
3087 class_locale.members.reserved = 0;
3088 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3089 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3090
3091 megasas_register_aen(instance, seq_num, class_locale.word);
3092}
3093
3094/**
3095 * Move the internal reset pending commands to a deferred queue.
3096 *
3097 * We move the commands pending at internal reset time to a
3098 * pending queue. This queue would be flushed after successful
3099 * completion of the internal reset sequence. if the internal reset
3100 * did not complete in time, the kernel reset handler would flush
3101 * these commands.
3102 **/
3103static void
3104megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3105{
3106 struct megasas_cmd *cmd;
3107 int i;
3108 u32 max_cmd = instance->max_fw_cmds;
3109 u32 defer_index;
3110 unsigned long flags;
3111
3112 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303113 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003114 for (i = 0; i < max_cmd; i++) {
3115 cmd = instance->cmd_list[i];
3116 if (cmd->sync_cmd == 1 || cmd->scmd) {
3117 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
3118 "on the defer queue as internal\n",
3119 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3120
3121 if (!list_empty(&cmd->list)) {
3122 printk(KERN_NOTICE "megaraid_sas: ERROR while"
3123 " moving this cmd:%p, %d %p, it was"
3124 "discovered on some list?\n",
3125 cmd, cmd->sync_cmd, cmd->scmd);
3126
3127 list_del_init(&cmd->list);
3128 }
3129 defer_index++;
3130 list_add_tail(&cmd->list,
3131 &instance->internal_reset_pending_q);
3132 }
3133 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303134 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003135}
3136
3137
3138static void
3139process_fw_state_change_wq(struct work_struct *work)
3140{
3141 struct megasas_instance *instance =
3142 container_of(work, struct megasas_instance, work_init);
3143 u32 wait;
3144 unsigned long flags;
3145
3146 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3147 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
3148 instance->adprecovery);
3149 return ;
3150 }
3151
3152 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3153 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
3154 "state, restarting it...\n");
3155
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303156 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003157 atomic_set(&instance->fw_outstanding, 0);
3158
3159 atomic_set(&instance->fw_reset_no_pci_access, 1);
3160 instance->instancet->adp_reset(instance, instance->reg_set);
3161 atomic_set(&instance->fw_reset_no_pci_access, 0 );
3162
3163 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
3164 "initiating next stage...\n");
3165
3166 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
3167 "state 2 starting...\n");
3168
3169 /*waitting for about 20 second before start the second init*/
3170 for (wait = 0; wait < 30; wait++) {
3171 msleep(1000);
3172 }
3173
adam radford058a8fa2011-10-08 18:14:27 -07003174 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04003175 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
3176
3177 megaraid_sas_kill_hba(instance);
3178 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
3179 return ;
3180 }
3181
3182 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3183 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3184 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3185 ) {
3186 *instance->consumer = *instance->producer;
3187 } else {
3188 *instance->consumer = 0;
3189 *instance->producer = 0;
3190 }
3191
3192 megasas_issue_init_mfi(instance);
3193
3194 spin_lock_irqsave(&instance->hba_lock, flags);
3195 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3196 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303197 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003198
3199 megasas_issue_pending_cmds_again(instance);
3200 instance->issuepend_done = 1;
3201 }
3202 return ;
3203}
3204
3205/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003206 * megasas_deplete_reply_queue - Processes all completed commands
3207 * @instance: Adapter soft state
3208 * @alt_status: Alternate status to be returned to
3209 * SCSI mid-layer instead of the status
3210 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003211 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003212 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003213static int
bo yang39a98552010-09-22 22:36:29 -04003214megasas_deplete_reply_queue(struct megasas_instance *instance,
3215 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003216{
bo yang39a98552010-09-22 22:36:29 -04003217 u32 mfiStatus;
3218 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003219
bo yang39a98552010-09-22 22:36:29 -04003220 if ((mfiStatus = instance->instancet->check_reset(instance,
3221 instance->reg_set)) == 1) {
3222 return IRQ_HANDLED;
3223 }
3224
3225 if ((mfiStatus = instance->instancet->clear_intr(
3226 instance->reg_set)
3227 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003228 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003229 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003230 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003231 }
3232
3233 instance->mfiStatus = mfiStatus;
3234
3235 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3236 fw_state = instance->instancet->read_fw_status_reg(
3237 instance->reg_set) & MFI_STATE_MASK;
3238
3239 if (fw_state != MFI_STATE_FAULT) {
3240 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
3241 fw_state);
3242 }
3243
3244 if ((fw_state == MFI_STATE_FAULT) &&
3245 (instance->disableOnlineCtrlReset == 0)) {
3246 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
3247
3248 if ((instance->pdev->device ==
3249 PCI_DEVICE_ID_LSI_SAS1064R) ||
3250 (instance->pdev->device ==
3251 PCI_DEVICE_ID_DELL_PERC5) ||
3252 (instance->pdev->device ==
3253 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3254
3255 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303256 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003257 }
3258
3259
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303260 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003261 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3262 instance->issuepend_done = 0;
3263
3264 atomic_set(&instance->fw_outstanding, 0);
3265 megasas_internal_reset_defer_cmds(instance);
3266
3267 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
3268 fw_state, instance->adprecovery);
3269
3270 schedule_work(&instance->work_init);
3271 return IRQ_HANDLED;
3272
3273 } else {
3274 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
3275 fw_state, instance->disableOnlineCtrlReset);
3276 }
3277 }
3278
Sumant Patro5d018ad2006-10-03 13:13:18 -07003279 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003280 return IRQ_HANDLED;
3281}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003282/**
3283 * megasas_isr - isr entry point
3284 */
David Howells7d12e782006-10-05 14:55:46 +01003285static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003286{
adam radfordc8e858f2011-10-08 18:15:13 -07003287 struct megasas_irq_context *irq_context = devp;
3288 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003289 unsigned long flags;
3290 irqreturn_t rc;
3291
adam radfordc8e858f2011-10-08 18:15:13 -07003292 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003293 return IRQ_HANDLED;
3294
bo yang39a98552010-09-22 22:36:29 -04003295 spin_lock_irqsave(&instance->hba_lock, flags);
3296 rc = megasas_deplete_reply_queue(instance, DID_OK);
3297 spin_unlock_irqrestore(&instance->hba_lock, flags);
3298
3299 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003300}
3301
3302/**
3303 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003304 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003305 *
3306 * During the initialization, FW passes can potentially be in any one of
3307 * several possible states. If the FW in operational, waiting-for-handshake
3308 * states, driver must take steps to bring it to ready state. Otherwise, it
3309 * has to wait for the ready state.
3310 */
adam radford9c915a82010-12-21 13:34:31 -08003311int
adam radford058a8fa2011-10-08 18:14:27 -07003312megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003313{
3314 int i;
3315 u8 max_wait;
3316 u32 fw_state;
3317 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003318 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003319
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003320 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3321 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003322
Sumant Patroe3bbff92006-10-03 12:28:49 -07003323 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08003324 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
3325 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003326
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003327 while (fw_state != MFI_STATE_READY) {
3328
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003329 switch (fw_state) {
3330
3331 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003332 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003333 if (ocr) {
3334 max_wait = MEGASAS_RESET_WAIT_TIME;
3335 cur_state = MFI_STATE_FAULT;
3336 break;
3337 } else
3338 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003339
3340 case MFI_STATE_WAIT_HANDSHAKE:
3341 /*
3342 * Set the CLR bit in inbound doorbell
3343 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003344 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003345 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3346 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003347 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3348 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303349 PCI_DEVICE_ID_LSI_FUSION) ||
adam radford36807e62011-10-08 18:15:06 -07003350 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003351 PCI_DEVICE_ID_LSI_PLASMA) ||
3352 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303353 PCI_DEVICE_ID_LSI_INVADER) ||
3354 (instance->pdev->device ==
3355 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003356 writel(
3357 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003358 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06003359 } else {
3360 writel(
3361 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3362 &instance->reg_set->inbound_doorbell);
3363 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003364
Yang, Bo7218df62009-10-06 14:52:20 -06003365 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003366 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3367 break;
3368
Sumant Patroe3bbff92006-10-03 12:28:49 -07003369 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003370 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003371 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3372 (instance->pdev->device ==
3373 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3374 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07003375 PCI_DEVICE_ID_LSI_FUSION) ||
3376 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003377 PCI_DEVICE_ID_LSI_PLASMA) ||
3378 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303379 PCI_DEVICE_ID_LSI_INVADER) ||
3380 (instance->pdev->device ==
3381 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003382 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003383 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06003384 } else
3385 writel(MFI_INIT_HOTPLUG,
3386 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003387
Yang, Bo7218df62009-10-06 14:52:20 -06003388 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003389 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3390 break;
3391
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003392 case MFI_STATE_OPERATIONAL:
3393 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003394 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003395 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303396 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003397 if ((instance->pdev->device ==
3398 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3399 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003400 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3401 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07003402 == PCI_DEVICE_ID_LSI_FUSION) ||
3403 (instance->pdev->device
adam radford229fe472014-03-10 02:51:56 -07003404 == PCI_DEVICE_ID_LSI_PLASMA) ||
3405 (instance->pdev->device
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303406 == PCI_DEVICE_ID_LSI_INVADER) ||
3407 (instance->pdev->device
3408 == PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06003409 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003410 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07003411 if ((instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303412 PCI_DEVICE_ID_LSI_FUSION) ||
3413 (instance->pdev->device ==
adam radford229fe472014-03-10 02:51:56 -07003414 PCI_DEVICE_ID_LSI_PLASMA) ||
3415 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303416 PCI_DEVICE_ID_LSI_INVADER) ||
3417 (instance->pdev->device ==
3418 PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08003419 for (i = 0; i < (10 * 1000); i += 20) {
3420 if (readl(
3421 &instance->
3422 reg_set->
3423 doorbell) & 1)
3424 msleep(20);
3425 else
3426 break;
3427 }
3428 }
Yang, Bo87911122009-10-06 14:31:54 -06003429 } else
3430 writel(MFI_RESET_FLAGS,
3431 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003432
Yang, Bo7218df62009-10-06 14:52:20 -06003433 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003434 cur_state = MFI_STATE_OPERATIONAL;
3435 break;
3436
3437 case MFI_STATE_UNDEFINED:
3438 /*
3439 * This state should not last for more than 2 seconds
3440 */
Yang, Bo7218df62009-10-06 14:52:20 -06003441 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003442 cur_state = MFI_STATE_UNDEFINED;
3443 break;
3444
3445 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003446 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003447 cur_state = MFI_STATE_BB_INIT;
3448 break;
3449
3450 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003451 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003452 cur_state = MFI_STATE_FW_INIT;
3453 break;
3454
3455 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003456 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003457 cur_state = MFI_STATE_FW_INIT_2;
3458 break;
3459
3460 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003461 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003462 cur_state = MFI_STATE_DEVICE_SCAN;
3463 break;
3464
3465 case MFI_STATE_FLUSH_CACHE:
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_FLUSH_CACHE;
3468 break;
3469
3470 default:
3471 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
3472 fw_state);
3473 return -ENODEV;
3474 }
3475
3476 /*
3477 * The cur_state should not last for more than max_wait secs
3478 */
3479 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003480 curr_abs_state = instance->instancet->
3481 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003482
Yang, Bo7218df62009-10-06 14:52:20 -06003483 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003484 msleep(1);
3485 } else
3486 break;
3487 }
3488
3489 /*
3490 * Return error if fw_state hasn't changed after max_wait
3491 */
Yang, Bo7218df62009-10-06 14:52:20 -06003492 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003493 printk(KERN_DEBUG "FW state [%d] hasn't changed "
3494 "in %d secs\n", fw_state, max_wait);
3495 return -ENODEV;
3496 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003497
3498 abs_state = curr_abs_state;
3499 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003500 }
adam radford0d490162010-12-14 19:17:17 -08003501 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003502
3503 return 0;
3504}
3505
3506/**
3507 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3508 * @instance: Adapter soft state
3509 */
3510static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3511{
3512 int i;
adam radford9c915a82010-12-21 13:34:31 -08003513 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003514 struct megasas_cmd *cmd;
3515
3516 if (!instance->frame_dma_pool)
3517 return;
3518
3519 /*
3520 * Return all frames to pool
3521 */
3522 for (i = 0; i < max_cmd; i++) {
3523
3524 cmd = instance->cmd_list[i];
3525
3526 if (cmd->frame)
3527 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3528 cmd->frame_phys_addr);
3529
3530 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003531 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003532 cmd->sense_phys_addr);
3533 }
3534
3535 /*
3536 * Now destroy the pool itself
3537 */
3538 pci_pool_destroy(instance->frame_dma_pool);
3539 pci_pool_destroy(instance->sense_dma_pool);
3540
3541 instance->frame_dma_pool = NULL;
3542 instance->sense_dma_pool = NULL;
3543}
3544
3545/**
3546 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3547 * @instance: Adapter soft state
3548 *
3549 * Each command packet has an embedded DMA memory buffer that is used for
3550 * filling MFI frame and the SG list that immediately follows the frame. This
3551 * function creates those DMA memory buffers for each command packet by using
3552 * PCI pool facility.
3553 */
3554static int megasas_create_frame_pool(struct megasas_instance *instance)
3555{
3556 int i;
3557 u32 max_cmd;
3558 u32 sge_sz;
3559 u32 sgl_sz;
3560 u32 total_sz;
3561 u32 frame_count;
3562 struct megasas_cmd *cmd;
3563
adam radford9c915a82010-12-21 13:34:31 -08003564 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565
3566 /*
3567 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3568 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3569 */
3570 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3571 sizeof(struct megasas_sge32);
3572
Yang, Bof4c9a132009-10-06 14:43:28 -06003573 if (instance->flag_ieee) {
3574 sge_sz = sizeof(struct megasas_sge_skinny);
3575 }
3576
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003577 /*
3578 * Calculated the number of 64byte frames required for SGL
3579 */
3580 sgl_sz = sge_sz * instance->max_num_sge;
3581 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04003582 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003583
3584 /*
3585 * We need one extra frame for the MFI command
3586 */
3587 frame_count++;
3588
3589 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3590 /*
3591 * Use DMA pool facility provided by PCI layer
3592 */
3593 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3594 instance->pdev, total_sz, 64,
3595 0);
3596
3597 if (!instance->frame_dma_pool) {
3598 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
3599 return -ENOMEM;
3600 }
3601
3602 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3603 instance->pdev, 128, 4, 0);
3604
3605 if (!instance->sense_dma_pool) {
3606 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
3607
3608 pci_pool_destroy(instance->frame_dma_pool);
3609 instance->frame_dma_pool = NULL;
3610
3611 return -ENOMEM;
3612 }
3613
3614 /*
3615 * Allocate and attach a frame to each of the commands in cmd_list.
3616 * By making cmd->index as the context instead of the &cmd, we can
3617 * always use 32bit context regardless of the architecture
3618 */
3619 for (i = 0; i < max_cmd; i++) {
3620
3621 cmd = instance->cmd_list[i];
3622
3623 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3624 GFP_KERNEL, &cmd->frame_phys_addr);
3625
3626 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3627 GFP_KERNEL, &cmd->sense_phys_addr);
3628
3629 /*
3630 * megasas_teardown_frame_pool() takes care of freeing
3631 * whatever has been allocated
3632 */
3633 if (!cmd->frame || !cmd->sense) {
3634 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
3635 megasas_teardown_frame_pool(instance);
3636 return -ENOMEM;
3637 }
3638
Yang, Bo707e09b2010-10-12 07:20:27 -06003639 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303640 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003641 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07003642 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
adam radford229fe472014-03-10 02:51:56 -07003643 (instance->pdev->device != PCI_DEVICE_ID_LSI_PLASMA) &&
adam radforde5f93a32011-10-08 18:15:19 -07003644 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303645 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
adam radforde5f93a32011-10-08 18:15:19 -07003646 (reset_devices))
3647 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003648 }
3649
3650 return 0;
3651}
3652
3653/**
3654 * megasas_free_cmds - Free all the cmds in the free cmd pool
3655 * @instance: Adapter soft state
3656 */
adam radford9c915a82010-12-21 13:34:31 -08003657void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003658{
3659 int i;
3660 /* First free the MFI frame pool */
3661 megasas_teardown_frame_pool(instance);
3662
3663 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003664 for (i = 0; i < instance->max_mfi_cmds; i++)
3665
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003666 kfree(instance->cmd_list[i]);
3667
3668 /* Free the cmd_list buffer itself */
3669 kfree(instance->cmd_list);
3670 instance->cmd_list = NULL;
3671
3672 INIT_LIST_HEAD(&instance->cmd_pool);
3673}
3674
3675/**
3676 * megasas_alloc_cmds - Allocates the command packets
3677 * @instance: Adapter soft state
3678 *
3679 * Each command that is issued to the FW, whether IO commands from the OS or
3680 * internal commands like IOCTLs, are wrapped in local data structure called
3681 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3682 * the FW.
3683 *
3684 * Each frame has a 32-bit field called context (tag). This context is used
3685 * to get back the megasas_cmd from the frame when a frame gets completed in
3686 * the ISR. Typically the address of the megasas_cmd itself would be used as
3687 * the context. But we wanted to keep the differences between 32 and 64 bit
3688 * systems to the mininum. We always use 32 bit integers for the context. In
3689 * this driver, the 32 bit values are the indices into an array cmd_list.
3690 * This array is used only to look up the megasas_cmd given the context. The
3691 * free commands themselves are maintained in a linked list called cmd_pool.
3692 */
adam radford9c915a82010-12-21 13:34:31 -08003693int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003694{
3695 int i;
3696 int j;
3697 u32 max_cmd;
3698 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303699 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003700
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303701 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003702 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003703
3704 /*
3705 * instance->cmd_list is an array of struct megasas_cmd pointers.
3706 * Allocate the dynamic array first and then allocate individual
3707 * commands.
3708 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003709 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003710
3711 if (!instance->cmd_list) {
3712 printk(KERN_DEBUG "megasas: out of memory\n");
3713 return -ENOMEM;
3714 }
3715
adam radford9c915a82010-12-21 13:34:31 -08003716 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003717
3718 for (i = 0; i < max_cmd; i++) {
3719 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3720 GFP_KERNEL);
3721
3722 if (!instance->cmd_list[i]) {
3723
3724 for (j = 0; j < i; j++)
3725 kfree(instance->cmd_list[j]);
3726
3727 kfree(instance->cmd_list);
3728 instance->cmd_list = NULL;
3729
3730 return -ENOMEM;
3731 }
3732 }
3733
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003734 for (i = 0; i < max_cmd; i++) {
3735 cmd = instance->cmd_list[i];
3736 memset(cmd, 0, sizeof(struct megasas_cmd));
3737 cmd->index = i;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303738 atomic_set(&cmd->mfi_mpt_pthr, MFI_LIST_ADDED);
bo yang39a98552010-09-22 22:36:29 -04003739 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003740 cmd->instance = instance;
3741
3742 list_add_tail(&cmd->list, &instance->cmd_pool);
3743 }
3744
3745 /*
3746 * Create a frame pool and assign one frame to each cmd
3747 */
3748 if (megasas_create_frame_pool(instance)) {
3749 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3750 megasas_free_cmds(instance);
3751 }
3752
3753 return 0;
3754}
3755
Yang, Bo81e403c2009-10-06 14:27:54 -06003756/*
3757 * megasas_get_pd_list_info - Returns FW's pd_list structure
3758 * @instance: Adapter soft state
3759 * @pd_list: pd_list structure
3760 *
3761 * Issues an internal command (DCMD) to get the FW's controller PD
3762 * list structure. This information is mainly used to find out SYSTEM
3763 * supported by the FW.
3764 */
3765static int
3766megasas_get_pd_list(struct megasas_instance *instance)
3767{
3768 int ret = 0, pd_index = 0;
3769 struct megasas_cmd *cmd;
3770 struct megasas_dcmd_frame *dcmd;
3771 struct MR_PD_LIST *ci;
3772 struct MR_PD_ADDRESS *pd_addr;
3773 dma_addr_t ci_h = 0;
3774
3775 cmd = megasas_get_cmd(instance);
3776
3777 if (!cmd) {
3778 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3779 return -ENOMEM;
3780 }
3781
3782 dcmd = &cmd->frame->dcmd;
3783
3784 ci = pci_alloc_consistent(instance->pdev,
3785 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3786
3787 if (!ci) {
3788 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3789 megasas_return_cmd(instance, cmd);
3790 return -ENOMEM;
3791 }
3792
3793 memset(ci, 0, sizeof(*ci));
3794 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3795
3796 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3797 dcmd->mbox.b[1] = 0;
3798 dcmd->cmd = MFI_CMD_DCMD;
3799 dcmd->cmd_status = 0xFF;
3800 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303801 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06003802 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003803 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303804 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3805 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3806 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3807 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06003808
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303809 if (instance->ctrl_context && !instance->mask_interrupts)
3810 ret = megasas_issue_blocked_cmd(instance, cmd,
3811 MEGASAS_BLOCKED_CMD_TIMEOUT);
3812 else
3813 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003814
3815 /*
3816 * the following function will get the instance PD LIST.
3817 */
3818
3819 pd_addr = ci->addr;
3820
3821 if ( ret == 0 &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303822 (le32_to_cpu(ci->count) <
Yang, Bo81e403c2009-10-06 14:27:54 -06003823 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3824
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303825 memset(instance->local_pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06003826 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3827
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303828 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Yang, Bo81e403c2009-10-06 14:27:54 -06003829
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303830 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303831 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303832 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Yang, Bo81e403c2009-10-06 14:27:54 -06003833 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303834 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Yang, Bo81e403c2009-10-06 14:27:54 -06003835 MR_PD_STATE_SYSTEM;
3836 pd_addr++;
3837 }
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303838 memcpy(instance->pd_list, instance->local_pd_list,
3839 sizeof(instance->pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06003840 }
3841
3842 pci_free_consistent(instance->pdev,
3843 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3844 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303845
3846 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3847 megasas_return_mfi_mpt_pthr(instance, cmd,
3848 cmd->mpt_pthr_cmd_blocked);
3849 else
3850 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06003851
3852 return ret;
3853}
3854
Yang, Bobdc6fb82009-12-06 08:30:19 -07003855/*
3856 * megasas_get_ld_list_info - Returns FW's ld_list structure
3857 * @instance: Adapter soft state
3858 * @ld_list: ld_list structure
3859 *
3860 * Issues an internal command (DCMD) to get the FW's controller PD
3861 * list structure. This information is mainly used to find out SYSTEM
3862 * supported by the FW.
3863 */
3864static int
3865megasas_get_ld_list(struct megasas_instance *instance)
3866{
3867 int ret = 0, ld_index = 0, ids = 0;
3868 struct megasas_cmd *cmd;
3869 struct megasas_dcmd_frame *dcmd;
3870 struct MR_LD_LIST *ci;
3871 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303872 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003873
3874 cmd = megasas_get_cmd(instance);
3875
3876 if (!cmd) {
3877 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3878 return -ENOMEM;
3879 }
3880
3881 dcmd = &cmd->frame->dcmd;
3882
3883 ci = pci_alloc_consistent(instance->pdev,
3884 sizeof(struct MR_LD_LIST),
3885 &ci_h);
3886
3887 if (!ci) {
3888 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3889 megasas_return_cmd(instance, cmd);
3890 return -ENOMEM;
3891 }
3892
3893 memset(ci, 0, sizeof(*ci));
3894 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3895
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303896 if (instance->supportmax256vd)
3897 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003898 dcmd->cmd = MFI_CMD_DCMD;
3899 dcmd->cmd_status = 0xFF;
3900 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303901 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003902 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303903 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3904 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3905 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3906 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07003907 dcmd->pad_0 = 0;
3908
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303909 if (instance->ctrl_context && !instance->mask_interrupts)
3910 ret = megasas_issue_blocked_cmd(instance, cmd,
3911 MEGASAS_BLOCKED_CMD_TIMEOUT);
3912 else
3913 ret = megasas_issue_polled(instance, cmd);
3914
Yang, Bobdc6fb82009-12-06 08:30:19 -07003915
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303916 ld_count = le32_to_cpu(ci->ldCount);
3917
Yang, Bobdc6fb82009-12-06 08:30:19 -07003918 /* the following function will get the instance PD LIST */
3919
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303920 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3921 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003922
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303923 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003924 if (ci->ldList[ld_index].state != 0) {
3925 ids = ci->ldList[ld_index].ref.targetId;
3926 instance->ld_ids[ids] =
3927 ci->ldList[ld_index].ref.targetId;
3928 }
3929 }
3930 }
3931
3932 pci_free_consistent(instance->pdev,
3933 sizeof(struct MR_LD_LIST),
3934 ci,
3935 ci_h);
3936
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303937 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3938 megasas_return_mfi_mpt_pthr(instance, cmd,
3939 cmd->mpt_pthr_cmd_blocked);
3940 else
3941 megasas_return_cmd(instance, cmd);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003942 return ret;
3943}
3944
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003945/**
adam radford21c9e162013-09-06 15:27:14 -07003946 * megasas_ld_list_query - Returns FW's ld_list structure
3947 * @instance: Adapter soft state
3948 * @ld_list: ld_list structure
3949 *
3950 * Issues an internal command (DCMD) to get the FW's controller PD
3951 * list structure. This information is mainly used to find out SYSTEM
3952 * supported by the FW.
3953 */
3954static int
3955megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3956{
3957 int ret = 0, ld_index = 0, ids = 0;
3958 struct megasas_cmd *cmd;
3959 struct megasas_dcmd_frame *dcmd;
3960 struct MR_LD_TARGETID_LIST *ci;
3961 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303962 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07003963
3964 cmd = megasas_get_cmd(instance);
3965
3966 if (!cmd) {
3967 printk(KERN_WARNING
3968 "megasas:(megasas_ld_list_query): Failed to get cmd\n");
3969 return -ENOMEM;
3970 }
3971
3972 dcmd = &cmd->frame->dcmd;
3973
3974 ci = pci_alloc_consistent(instance->pdev,
3975 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
3976
3977 if (!ci) {
3978 printk(KERN_WARNING
3979 "megasas: Failed to alloc mem for ld_list_query\n");
3980 megasas_return_cmd(instance, cmd);
3981 return -ENOMEM;
3982 }
3983
3984 memset(ci, 0, sizeof(*ci));
3985 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3986
3987 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05303988 if (instance->supportmax256vd)
3989 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07003990
3991 dcmd->cmd = MFI_CMD_DCMD;
3992 dcmd->cmd_status = 0xFF;
3993 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303994 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07003995 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303996 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3997 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
3998 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3999 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004000 dcmd->pad_0 = 0;
4001
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304002 if (instance->ctrl_context && !instance->mask_interrupts)
4003 ret = megasas_issue_blocked_cmd(instance, cmd,
4004 MEGASAS_BLOCKED_CMD_TIMEOUT);
4005 else
4006 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004007
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304008 tgtid_count = le32_to_cpu(ci->count);
4009
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304010 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
adam radford21c9e162013-09-06 15:27:14 -07004011 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304012 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004013 ids = ci->targetId[ld_index];
4014 instance->ld_ids[ids] = ci->targetId[ld_index];
4015 }
4016
4017 }
4018
4019 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4020 ci, ci_h);
4021
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304022 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4023 megasas_return_mfi_mpt_pthr(instance, cmd,
4024 cmd->mpt_pthr_cmd_blocked);
4025 else
4026 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004027
4028 return ret;
4029}
4030
4031/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004032 * megasas_get_controller_info - Returns FW's controller structure
4033 * @instance: Adapter soft state
4034 * @ctrl_info: Controller information structure
4035 *
4036 * Issues an internal command (DCMD) to get the FW's controller structure.
4037 * This information is mainly used to find out the maximum IO transfer per
4038 * command supported by the FW.
4039 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304040int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004041megasas_get_ctrl_info(struct megasas_instance *instance,
4042 struct megasas_ctrl_info *ctrl_info)
4043{
4044 int ret = 0;
4045 struct megasas_cmd *cmd;
4046 struct megasas_dcmd_frame *dcmd;
4047 struct megasas_ctrl_info *ci;
4048 dma_addr_t ci_h = 0;
4049
4050 cmd = megasas_get_cmd(instance);
4051
4052 if (!cmd) {
4053 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
4054 return -ENOMEM;
4055 }
4056
4057 dcmd = &cmd->frame->dcmd;
4058
4059 ci = pci_alloc_consistent(instance->pdev,
4060 sizeof(struct megasas_ctrl_info), &ci_h);
4061
4062 if (!ci) {
4063 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
4064 megasas_return_cmd(instance, cmd);
4065 return -ENOMEM;
4066 }
4067
4068 memset(ci, 0, sizeof(*ci));
4069 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4070
4071 dcmd->cmd = MFI_CMD_DCMD;
4072 dcmd->cmd_status = 0xFF;
4073 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304074 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004075 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004076 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304077 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4078 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4079 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4080 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304081 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004082
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304083 if (instance->ctrl_context && !instance->mask_interrupts)
4084 ret = megasas_issue_blocked_cmd(instance, cmd,
4085 MEGASAS_BLOCKED_CMD_TIMEOUT);
4086 else
4087 ret = megasas_issue_polled(instance, cmd);
4088
4089 if (!ret)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004090 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004091
4092 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4093 ci, ci_h);
4094
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304095 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4096 megasas_return_mfi_mpt_pthr(instance, cmd,
4097 cmd->mpt_pthr_cmd_blocked);
4098 else
4099 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004100 return ret;
4101}
4102
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304103/*
4104 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4105 * to firmware
4106 *
4107 * @instance: Adapter soft state
4108 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4109 MR_CRASH_BUF_TURN_OFF = 0
4110 MR_CRASH_BUF_TURN_ON = 1
4111 * @return 0 on success non-zero on failure.
4112 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4113 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4114 * that driver supports crash dump feature. This DCMD will be sent only if
4115 * crash dump feature is supported by the FW.
4116 *
4117 */
4118int megasas_set_crash_dump_params(struct megasas_instance *instance,
4119 u8 crash_buf_state)
4120{
4121 int ret = 0;
4122 struct megasas_cmd *cmd;
4123 struct megasas_dcmd_frame *dcmd;
4124
4125 cmd = megasas_get_cmd(instance);
4126
4127 if (!cmd) {
4128 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4129 return -ENOMEM;
4130 }
4131
4132
4133 dcmd = &cmd->frame->dcmd;
4134
4135 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4136 dcmd->mbox.b[0] = crash_buf_state;
4137 dcmd->cmd = MFI_CMD_DCMD;
4138 dcmd->cmd_status = 0xFF;
4139 dcmd->sge_count = 1;
4140 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4141 dcmd->timeout = 0;
4142 dcmd->pad_0 = 0;
4143 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4144 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4145 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4146 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4147
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304148 if (instance->ctrl_context && !instance->mask_interrupts)
4149 ret = megasas_issue_blocked_cmd(instance, cmd,
4150 MEGASAS_BLOCKED_CMD_TIMEOUT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304151 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304152 ret = megasas_issue_polled(instance, cmd);
4153
4154 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4155 megasas_return_mfi_mpt_pthr(instance, cmd,
4156 cmd->mpt_pthr_cmd_blocked);
4157 else
4158 megasas_return_cmd(instance, cmd);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304159 return ret;
4160}
4161
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004162/**
bo yang31ea7082007-11-07 12:09:50 -05004163 * megasas_issue_init_mfi - Initializes the FW
4164 * @instance: Adapter soft state
4165 *
4166 * Issues the INIT MFI cmd
4167 */
4168static int
4169megasas_issue_init_mfi(struct megasas_instance *instance)
4170{
4171 u32 context;
4172
4173 struct megasas_cmd *cmd;
4174
4175 struct megasas_init_frame *init_frame;
4176 struct megasas_init_queue_info *initq_info;
4177 dma_addr_t init_frame_h;
4178 dma_addr_t initq_info_h;
4179
4180 /*
4181 * Prepare a init frame. Note the init frame points to queue info
4182 * structure. Each frame has SGL allocated after first 64 bytes. For
4183 * this frame - since we don't need any SGL - we use SGL's space as
4184 * queue info structure
4185 *
4186 * We will not get a NULL command below. We just created the pool.
4187 */
4188 cmd = megasas_get_cmd(instance);
4189
4190 init_frame = (struct megasas_init_frame *)cmd->frame;
4191 initq_info = (struct megasas_init_queue_info *)
4192 ((unsigned long)init_frame + 64);
4193
4194 init_frame_h = cmd->frame_phys_addr;
4195 initq_info_h = init_frame_h + 64;
4196
4197 context = init_frame->context;
4198 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4199 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4200 init_frame->context = context;
4201
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304202 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4203 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004204
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304205 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4206 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004207
4208 init_frame->cmd = MFI_CMD_INIT;
4209 init_frame->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304210 init_frame->queue_info_new_phys_addr_lo =
4211 cpu_to_le32(lower_32_bits(initq_info_h));
4212 init_frame->queue_info_new_phys_addr_hi =
4213 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004214
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304215 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004216
4217 /*
4218 * disable the intr before firing the init frame to FW
4219 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304220 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004221
4222 /*
4223 * Issue the init frame in polled mode
4224 */
4225
4226 if (megasas_issue_polled(instance, cmd)) {
4227 printk(KERN_ERR "megasas: Failed to init firmware\n");
4228 megasas_return_cmd(instance, cmd);
4229 goto fail_fw_init;
4230 }
4231
4232 megasas_return_cmd(instance, cmd);
4233
4234 return 0;
4235
4236fail_fw_init:
4237 return -EINVAL;
4238}
4239
adam radfordcd50ba82010-12-21 10:23:23 -08004240static u32
4241megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004242{
adam radfordcd50ba82010-12-21 10:23:23 -08004243 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004244 u32 context_sz;
4245 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004246
4247 reg_set = instance->reg_set;
4248
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004249 /*
4250 * Get various operational parameters from status register
4251 */
Sumant Patro1341c932006-01-25 12:02:40 -08004252 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004253 /*
4254 * Reduce the max supported cmds by 1. This is to ensure that the
4255 * reply_q_sz (1 more than the max cmd that driver may send)
4256 * does not exceed max cmds that the FW can support
4257 */
4258 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004259 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004260 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004261 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004262 /*
4263 * Create a pool of commands
4264 */
4265 if (megasas_alloc_cmds(instance))
4266 goto fail_alloc_cmds;
4267
4268 /*
4269 * Allocate memory for reply queue. Length of reply queue should
4270 * be _one_ more than the maximum commands handled by the firmware.
4271 *
4272 * Note: When FW completes commands, it places corresponding contex
4273 * values in this circular reply queue. This circular queue is a fairly
4274 * typical producer-consumer queue. FW is the producer (of completed
4275 * commands) and the driver is the consumer.
4276 */
4277 context_sz = sizeof(u32);
4278 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4279
4280 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4281 reply_q_sz,
4282 &instance->reply_queue_h);
4283
4284 if (!instance->reply_queue) {
4285 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
4286 goto fail_reply_queue;
4287 }
4288
bo yang31ea7082007-11-07 12:09:50 -05004289 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004290 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004291
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304292 if (megasas_get_ctrl_info(instance, instance->ctrl_info)) {
4293 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4294 "Fail from %s %d\n", instance->unique_id,
4295 __func__, __LINE__);
4296 goto fail_fw_init;
4297 }
4298
bo yang39a98552010-09-22 22:36:29 -04004299 instance->fw_support_ieee = 0;
4300 instance->fw_support_ieee =
4301 (instance->instancet->read_fw_status_reg(reg_set) &
4302 0x04000000);
4303
4304 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
4305 instance->fw_support_ieee);
4306
4307 if (instance->fw_support_ieee)
4308 instance->flag_ieee = 1;
4309
adam radfordcd50ba82010-12-21 10:23:23 -08004310 return 0;
4311
4312fail_fw_init:
4313
4314 pci_free_consistent(instance->pdev, reply_q_sz,
4315 instance->reply_queue, instance->reply_queue_h);
4316fail_reply_queue:
4317 megasas_free_cmds(instance);
4318
4319fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004320 return 1;
4321}
4322
4323/**
4324 * megasas_init_fw - Initializes the FW
4325 * @instance: Adapter soft state
4326 *
4327 * This is the main function for initializing firmware
4328 */
4329
4330static int megasas_init_fw(struct megasas_instance *instance)
4331{
4332 u32 max_sectors_1;
4333 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304334 u32 tmp_sectors, msix_enable, scratch_pad_2;
Ben Collins11f8a7b2013-09-13 12:46:44 -04004335 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08004336 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304337 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08004338 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304339 int i, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07004340 struct IOV_111 *iovPtr;
adam radfordcd50ba82010-12-21 10:23:23 -08004341
4342 /* Find first memory bar */
4343 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4344 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
adam radfordcd50ba82010-12-21 10:23:23 -08004345 if (pci_request_selected_regions(instance->pdev, instance->bar,
4346 "megasas: LSI")) {
4347 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
4348 return -EBUSY;
4349 }
4350
Ben Collins11f8a7b2013-09-13 12:46:44 -04004351 base_addr = pci_resource_start(instance->pdev, instance->bar);
4352 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08004353
4354 if (!instance->reg_set) {
4355 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
4356 goto fail_ioremap;
4357 }
4358
4359 reg_set = instance->reg_set;
4360
4361 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08004362 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07004363 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07004364 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304365 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08004366 instance->instancet = &megasas_instance_template_fusion;
4367 break;
adam radfordcd50ba82010-12-21 10:23:23 -08004368 case PCI_DEVICE_ID_LSI_SAS1078R:
4369 case PCI_DEVICE_ID_LSI_SAS1078DE:
4370 instance->instancet = &megasas_instance_template_ppc;
4371 break;
4372 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4373 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4374 instance->instancet = &megasas_instance_template_gen2;
4375 break;
4376 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4377 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4378 instance->instancet = &megasas_instance_template_skinny;
4379 break;
4380 case PCI_DEVICE_ID_LSI_SAS1064R:
4381 case PCI_DEVICE_ID_DELL_PERC5:
4382 default:
4383 instance->instancet = &megasas_instance_template_xscale;
4384 break;
4385 }
4386
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05304387 if (megasas_transition_to_ready(instance, 0)) {
4388 atomic_set(&instance->fw_reset_no_pci_access, 1);
4389 instance->instancet->adp_reset
4390 (instance, instance->reg_set);
4391 atomic_set(&instance->fw_reset_no_pci_access, 0);
4392 dev_info(&instance->pdev->dev,
4393 "megasas: FW restarted successfully from %s!\n",
4394 __func__);
4395
4396 /*waitting for about 30 second before retry*/
4397 ssleep(30);
4398
4399 if (megasas_transition_to_ready(instance, 0))
4400 goto fail_ready_state;
4401 }
adam radfordcd50ba82010-12-21 10:23:23 -08004402
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304403 /*
4404 * MSI-X host index 0 is common for all adapter.
4405 * It is used for all MPT based Adapters.
4406 */
4407 instance->reply_post_host_index_addr[0] =
4408 (u32 *)((u8 *)instance->reg_set +
4409 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4410
adam radford3f1abce2011-05-11 18:33:47 -07004411 /* Check if MSI-X is supported while in ready state */
4412 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4413 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07004414 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304415 scratch_pad_2 = readl
4416 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07004417 /* Check max MSI-X vectors */
adam radford229fe472014-03-10 02:51:56 -07004418 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4419 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA)) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304420 instance->msix_vectors = (scratch_pad_2
4421 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4422 fw_msix_count = instance->msix_vectors;
adam radford079eadd2012-10-01 19:26:59 -07004423 if (msix_vectors)
4424 instance->msix_vectors =
4425 min(msix_vectors,
4426 instance->msix_vectors);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304427 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
4428 || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
4429 /* Invader/Fury supports more than 8 MSI-X */
4430 instance->msix_vectors = ((scratch_pad_2
4431 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4432 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4433 fw_msix_count = instance->msix_vectors;
4434 /* Save 1-15 reply post index address to local memory
4435 * Index 0 is already saved from reg offset
4436 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4437 */
4438 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4439 instance->reply_post_host_index_addr[loop] =
4440 (u32 *)((u8 *)instance->reg_set +
4441 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4442 + (loop * 0x10));
4443 }
4444 if (msix_vectors)
4445 instance->msix_vectors = min(msix_vectors,
4446 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07004447 } else
4448 instance->msix_vectors = 1;
4449 /* Don't bother allocating more MSI-X vectors than cpus */
4450 instance->msix_vectors = min(instance->msix_vectors,
4451 (unsigned int)num_online_cpus());
4452 for (i = 0; i < instance->msix_vectors; i++)
4453 instance->msixentry[i].entry = i;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02004454 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4455 1, instance->msix_vectors);
4456 if (i)
4457 instance->msix_vectors = i;
4458 else
adam radfordc8e858f2011-10-08 18:15:13 -07004459 instance->msix_vectors = 0;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304460
4461 dev_info(&instance->pdev->dev, "[scsi%d]: FW supports"
4462 "<%d> MSIX vector,Online CPUs: <%d>,"
4463 "Current MSIX <%d>\n", instance->host->host_no,
4464 fw_msix_count, (unsigned int)num_online_cpus(),
4465 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07004466 }
adam radford3f1abce2011-05-11 18:33:47 -07004467
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304468 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4469 GFP_KERNEL);
4470 if (instance->ctrl_info == NULL)
4471 goto fail_init_adapter;
4472
4473 /*
4474 * Below are default value for legacy Firmware.
4475 * non-fusion based controllers
4476 */
4477 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4478 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08004479 /* Get operational params, sge flags, send init cmd to controller */
4480 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08004481 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08004482
4483 printk(KERN_ERR "megasas: INIT adapter done\n");
4484
bo yang39a98552010-09-22 22:36:29 -04004485 /** for passthrough
4486 * the following function will get the PD LIST.
4487 */
4488
Yang, Bo81e403c2009-10-06 14:27:54 -06004489 memset(instance->pd_list, 0 ,
4490 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01004491 if (megasas_get_pd_list(instance) < 0) {
4492 printk(KERN_ERR "megasas: failed to get PD list\n");
4493 goto fail_init_adapter;
4494 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004495
Yang, Bobdc6fb82009-12-06 08:30:19 -07004496 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07004497 if (megasas_ld_list_query(instance,
4498 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4499 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004500
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004501 /*
4502 * Compute the max allowed sectors per IO: The controller info has two
4503 * limits on max sectors. Driver should use the minimum of these two.
4504 *
4505 * 1 << stripe_sz_ops.min = max sectors per strip
4506 *
4507 * Note that older firmwares ( < FW ver 30) didn't report information
4508 * to calculate max_sectors_1. So the number ended up as zero always.
4509 */
bo yang14faea92007-11-09 04:14:00 -05004510 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304511 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004512
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304513 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4514 le16_to_cpu(ctrl_info->max_strips_per_io);
4515 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004516
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304517 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304518
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304519 /*Check whether controller is iMR or MR */
4520 if (ctrl_info->memory_size) {
4521 instance->is_imr = 0;
4522 dev_info(&instance->pdev->dev, "Controller type: MR,"
4523 "Memory size is: %dMB\n",
4524 le16_to_cpu(ctrl_info->memory_size));
4525 } else {
4526 instance->is_imr = 1;
4527 dev_info(&instance->pdev->dev,
4528 "Controller type: iMR\n");
4529 }
4530 /* OnOffProperties are converted into CPU arch*/
4531 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4532 instance->disableOnlineCtrlReset =
4533 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4534 /* adapterOperations2 are converted into CPU arch*/
4535 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4536 instance->mpio = ctrl_info->adapterOperations2.mpio;
4537 instance->UnevenSpanSupport =
4538 ctrl_info->adapterOperations2.supportUnevenSpans;
4539 if (instance->UnevenSpanSupport) {
4540 struct fusion_context *fusion = instance->ctrl_context;
4541
4542 dev_info(&instance->pdev->dev, "FW supports: "
4543 "UnevenSpanSupport=%x\n", instance->UnevenSpanSupport);
4544 if (MR_ValidateMapInfo(instance))
4545 fusion->fast_path_io = 1;
4546 else
4547 fusion->fast_path_io = 0;
4548
4549 }
4550 if (ctrl_info->host_interface.SRIOV) {
4551 if (!ctrl_info->adapterOperations2.activePassive)
4552 instance->PlasmaFW111 = 1;
4553
4554 if (!instance->PlasmaFW111)
4555 instance->requestorId =
4556 ctrl_info->iov.requestorId;
4557 else {
4558 iovPtr = (struct IOV_111 *)((unsigned char *)ctrl_info + IOV_111_OFFSET);
4559 instance->requestorId = iovPtr->requestorId;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304560 }
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304561 dev_warn(&instance->pdev->dev, "I am VF "
4562 "requestorId %d\n", instance->requestorId);
4563 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05304564
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304565 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4566 instance->crash_dump_fw_support =
4567 ctrl_info->adapterOperations3.supportCrashDump;
4568 instance->crash_dump_drv_support =
4569 (instance->crash_dump_fw_support &&
4570 instance->crash_dump_buf);
4571 if (instance->crash_dump_drv_support) {
4572 dev_info(&instance->pdev->dev, "Firmware Crash dump "
4573 "feature is supported\n");
4574 megasas_set_crash_dump_params(instance,
4575 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07004576
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304577 } else {
4578 if (instance->crash_dump_buf)
4579 pci_free_consistent(instance->pdev,
4580 CRASH_DMA_BUF_SIZE,
4581 instance->crash_dump_buf,
4582 instance->crash_dump_h);
4583 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05004584 }
bo yang14faea92007-11-09 04:14:00 -05004585 instance->max_sectors_per_req = instance->max_num_sge *
4586 PAGE_SIZE / 512;
4587 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4588 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004589
4590 kfree(ctrl_info);
4591
adam radfordc5daa6a2012-07-17 18:20:03 -07004592 /* Check for valid throttlequeuedepth module parameter */
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304593 if (instance->is_imr) {
adam radfordc5daa6a2012-07-17 18:20:03 -07004594 if (throttlequeuedepth > (instance->max_fw_cmds -
4595 MEGASAS_SKINNY_INT_CMDS))
4596 instance->throttlequeuedepth =
4597 MEGASAS_THROTTLE_QUEUE_DEPTH;
4598 else
4599 instance->throttlequeuedepth = throttlequeuedepth;
4600 } else {
4601 if (throttlequeuedepth > (instance->max_fw_cmds -
4602 MEGASAS_INT_CMDS))
4603 instance->throttlequeuedepth =
4604 MEGASAS_THROTTLE_QUEUE_DEPTH;
4605 else
4606 instance->throttlequeuedepth = throttlequeuedepth;
4607 }
4608
Sumant Patro5d018ad2006-10-03 13:13:18 -07004609 /*
4610 * Setup tasklet for cmd completion
4611 */
4612
adam radfordf86c5422011-02-24 20:57:00 -08004613 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05004614 (unsigned long)instance);
4615
adam radford229fe472014-03-10 02:51:56 -07004616 /* Launch SR-IOV heartbeat timer */
4617 if (instance->requestorId) {
4618 if (!megasas_sriov_start_heartbeat(instance, 1))
4619 megasas_start_timer(instance,
4620 &instance->sriov_heartbeat_timer,
4621 megasas_sriov_heartbeat_handler,
4622 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4623 else
4624 instance->skip_heartbeat_timer_del = 1;
4625 }
4626
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004627 return 0;
4628
adam radfordeb1b1232011-02-24 20:55:56 -08004629fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08004630fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304631 kfree(instance->ctrl_info);
4632 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004633 iounmap(instance->reg_set);
4634
4635 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08004636 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004637
4638 return -EINVAL;
4639}
4640
4641/**
4642 * megasas_release_mfi - Reverses the FW initialization
4643 * @intance: Adapter soft state
4644 */
4645static void megasas_release_mfi(struct megasas_instance *instance)
4646{
adam radford9c915a82010-12-21 13:34:31 -08004647 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004648
adam radford9c915a82010-12-21 13:34:31 -08004649 if (instance->reply_queue)
4650 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004651 instance->reply_queue, instance->reply_queue_h);
4652
4653 megasas_free_cmds(instance);
4654
4655 iounmap(instance->reg_set);
4656
adam radfordb6d5d882010-12-14 18:56:07 -08004657 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004658}
4659
4660/**
4661 * megasas_get_seq_num - Gets latest event sequence numbers
4662 * @instance: Adapter soft state
4663 * @eli: FW event log sequence numbers information
4664 *
4665 * FW maintains a log of all events in a non-volatile area. Upper layers would
4666 * usually find out the latest sequence number of the events, the seq number at
4667 * the boot etc. They would "read" all the events below the latest seq number
4668 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4669 * number), they would subsribe to AEN (asynchronous event notification) and
4670 * wait for the events to happen.
4671 */
4672static int
4673megasas_get_seq_num(struct megasas_instance *instance,
4674 struct megasas_evt_log_info *eli)
4675{
4676 struct megasas_cmd *cmd;
4677 struct megasas_dcmd_frame *dcmd;
4678 struct megasas_evt_log_info *el_info;
4679 dma_addr_t el_info_h = 0;
4680
4681 cmd = megasas_get_cmd(instance);
4682
4683 if (!cmd) {
4684 return -ENOMEM;
4685 }
4686
4687 dcmd = &cmd->frame->dcmd;
4688 el_info = pci_alloc_consistent(instance->pdev,
4689 sizeof(struct megasas_evt_log_info),
4690 &el_info_h);
4691
4692 if (!el_info) {
4693 megasas_return_cmd(instance, cmd);
4694 return -ENOMEM;
4695 }
4696
4697 memset(el_info, 0, sizeof(*el_info));
4698 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4699
4700 dcmd->cmd = MFI_CMD_DCMD;
4701 dcmd->cmd_status = 0x0;
4702 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304703 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004704 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004705 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304706 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4707 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4708 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4709 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004710
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304711 if (megasas_issue_blocked_cmd(instance, cmd, 30))
4712 dev_err(&instance->pdev->dev, "Command timedout"
4713 "from %s\n", __func__);
4714 else {
4715 /*
4716 * Copy the data back into callers buffer
4717 */
4718 eli->newest_seq_num = le32_to_cpu(el_info->newest_seq_num);
4719 eli->oldest_seq_num = le32_to_cpu(el_info->oldest_seq_num);
4720 eli->clear_seq_num = le32_to_cpu(el_info->clear_seq_num);
4721 eli->shutdown_seq_num = le32_to_cpu(el_info->shutdown_seq_num);
4722 eli->boot_seq_num = le32_to_cpu(el_info->boot_seq_num);
4723 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004724
4725 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
4726 el_info, el_info_h);
4727
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304728 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4729 megasas_return_mfi_mpt_pthr(instance, cmd,
4730 cmd->mpt_pthr_cmd_blocked);
4731 else
4732 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004733
4734 return 0;
4735}
4736
4737/**
4738 * megasas_register_aen - Registers for asynchronous event notification
4739 * @instance: Adapter soft state
4740 * @seq_num: The starting sequence number
4741 * @class_locale: Class of the event
4742 *
4743 * This function subscribes for AEN for events beyond the @seq_num. It requests
4744 * to be notified if and only if the event is of type @class_locale
4745 */
4746static int
4747megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
4748 u32 class_locale_word)
4749{
4750 int ret_val;
4751 struct megasas_cmd *cmd;
4752 struct megasas_dcmd_frame *dcmd;
4753 union megasas_evt_class_locale curr_aen;
4754 union megasas_evt_class_locale prev_aen;
4755
4756 /*
4757 * If there an AEN pending already (aen_cmd), check if the
4758 * class_locale of that pending AEN is inclusive of the new
4759 * AEN request we currently have. If it is, then we don't have
4760 * to do anything. In other words, whichever events the current
4761 * AEN request is subscribing to, have already been subscribed
4762 * to.
4763 *
4764 * If the old_cmd is _not_ inclusive, then we have to abort
4765 * that command, form a class_locale that is superset of both
4766 * old and current and re-issue to the FW
4767 */
4768
4769 curr_aen.word = class_locale_word;
4770
4771 if (instance->aen_cmd) {
4772
4773 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304774 prev_aen.members.locale = le16_to_cpu(prev_aen.members.locale);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004775
4776 /*
4777 * A class whose enum value is smaller is inclusive of all
4778 * higher values. If a PROGRESS (= -1) was previously
4779 * registered, then a new registration requests for higher
4780 * classes need not be sent to FW. They are automatically
4781 * included.
4782 *
4783 * Locale numbers don't have such hierarchy. They are bitmap
4784 * values
4785 */
4786 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304787 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004788 curr_aen.members.locale)) {
4789 /*
4790 * Previously issued event registration includes
4791 * current request. Nothing to do.
4792 */
4793 return 0;
4794 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304795 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004796
4797 if (prev_aen.members.class < curr_aen.members.class)
4798 curr_aen.members.class = prev_aen.members.class;
4799
4800 instance->aen_cmd->abort_aen = 1;
4801 ret_val = megasas_issue_blocked_abort_cmd(instance,
4802 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05304803 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004804
4805 if (ret_val) {
4806 printk(KERN_DEBUG "megasas: Failed to abort "
4807 "previous AEN command\n");
4808 return ret_val;
4809 }
4810 }
4811 }
4812
4813 cmd = megasas_get_cmd(instance);
4814
4815 if (!cmd)
4816 return -ENOMEM;
4817
4818 dcmd = &cmd->frame->dcmd;
4819
4820 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4821
4822 /*
4823 * Prepare DCMD for aen registration
4824 */
4825 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4826
4827 dcmd->cmd = MFI_CMD_DCMD;
4828 dcmd->cmd_status = 0x0;
4829 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304830 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004831 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004832 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304833 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4834 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4835 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04004836 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304837 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4838 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4839 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004840
Yang, Bof4c9a132009-10-06 14:43:28 -06004841 if (instance->aen_cmd != NULL) {
4842 megasas_return_cmd(instance, cmd);
4843 return 0;
4844 }
4845
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004846 /*
4847 * Store reference to the cmd used to register for AEN. When an
4848 * application wants us to register for AEN, we have to abort this
4849 * cmd and re-register with a new EVENT LOCALE supplied by that app
4850 */
4851 instance->aen_cmd = cmd;
4852
4853 /*
4854 * Issue the aen registration frame
4855 */
adam radford9c915a82010-12-21 13:34:31 -08004856 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004857
4858 return 0;
4859}
4860
4861/**
4862 * megasas_start_aen - Subscribes to AEN during driver load time
4863 * @instance: Adapter soft state
4864 */
4865static int megasas_start_aen(struct megasas_instance *instance)
4866{
4867 struct megasas_evt_log_info eli;
4868 union megasas_evt_class_locale class_locale;
4869
4870 /*
4871 * Get the latest sequence number from FW
4872 */
4873 memset(&eli, 0, sizeof(eli));
4874
4875 if (megasas_get_seq_num(instance, &eli))
4876 return -1;
4877
4878 /*
4879 * Register AEN with FW for latest sequence number plus 1
4880 */
4881 class_locale.members.reserved = 0;
4882 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4883 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4884
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304885 return megasas_register_aen(instance,
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304886 eli.newest_seq_num + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304887 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004888}
4889
4890/**
4891 * megasas_io_attach - Attaches this driver to SCSI mid-layer
4892 * @instance: Adapter soft state
4893 */
4894static int megasas_io_attach(struct megasas_instance *instance)
4895{
4896 struct Scsi_Host *host = instance->host;
4897
4898 /*
4899 * Export parameters required by SCSI mid-layer
4900 */
4901 host->irq = instance->pdev->irq;
4902 host->unique_id = instance->unique_id;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304903 if (instance->is_imr) {
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004904 host->can_queue =
4905 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
4906 } else
4907 host->can_queue =
4908 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004909 host->this_id = instance->init_id;
4910 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08004911
4912 if (instance->fw_support_ieee)
4913 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
4914
Yang, Bo1fd10682010-10-12 07:18:50 -06004915 /*
4916 * Check if the module parameter value for max_sectors can be used
4917 */
4918 if (max_sectors && max_sectors < instance->max_sectors_per_req)
4919 instance->max_sectors_per_req = max_sectors;
4920 else {
4921 if (max_sectors) {
4922 if (((instance->pdev->device ==
4923 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
4924 (instance->pdev->device ==
4925 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
4926 (max_sectors <= MEGASAS_MAX_SECTORS)) {
4927 instance->max_sectors_per_req = max_sectors;
4928 } else {
4929 printk(KERN_INFO "megasas: max_sectors should be > 0"
4930 "and <= %d (or < 1MB for GEN2 controller)\n",
4931 instance->max_sectors_per_req);
4932 }
4933 }
4934 }
4935
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004936 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08004937 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004938 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
4939 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
4940 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08004941 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004942
adam radford9c915a82010-12-21 13:34:31 -08004943 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07004944 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07004945 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304946 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4947 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08004948 host->hostt->eh_device_reset_handler = NULL;
4949 host->hostt->eh_bus_reset_handler = NULL;
4950 }
4951
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004952 /*
4953 * Notify the mid-layer about the new controller
4954 */
4955 if (scsi_add_host(host, &instance->pdev->dev)) {
4956 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
4957 return -ENODEV;
4958 }
4959
4960 /*
4961 * Trigger SCSI to scan our drives
4962 */
4963 scsi_scan_host(host);
4964 return 0;
4965}
4966
bo yang31ea7082007-11-07 12:09:50 -05004967static int
4968megasas_set_dma_mask(struct pci_dev *pdev)
4969{
4970 /*
4971 * All our contollers are capable of performing 64-bit DMA
4972 */
4973 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07004974 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05004975
Yang Hongyang284901a2009-04-06 19:01:15 -07004976 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05004977 goto fail_set_dma_mask;
4978 }
4979 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004980 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05004981 goto fail_set_dma_mask;
4982 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05304983 /*
4984 * Ensure that all data structures are allocated in 32-bit
4985 * memory.
4986 */
4987 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
4988 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
4989 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
4990 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
4991 dev_info(&pdev->dev, "set 32bit DMA mask"
4992 "and 32 bit consistent mask\n");
4993 else
4994 goto fail_set_dma_mask;
4995 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304996
bo yang31ea7082007-11-07 12:09:50 -05004997 return 0;
4998
4999fail_set_dma_mask:
5000 return 1;
5001}
5002
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005003/**
5004 * megasas_probe_one - PCI hotplug entry point
5005 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005006 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005007 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005008static int megasas_probe_one(struct pci_dev *pdev,
5009 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005010{
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305011 int rval, pos, i, j, cpu;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005012 struct Scsi_Host *host;
5013 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005014 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305015 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005016
5017 /* Reset MSI-X in the kdump kernel */
5018 if (reset_devices) {
5019 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5020 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005021 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005022 &control);
5023 if (control & PCI_MSIX_FLAGS_ENABLE) {
5024 dev_info(&pdev->dev, "resetting MSI-X\n");
5025 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005026 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005027 control &
5028 ~PCI_MSIX_FLAGS_ENABLE);
5029 }
5030 }
5031 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005032
5033 /*
5034 * Announce PCI information
5035 */
5036 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
5037 pdev->vendor, pdev->device, pdev->subsystem_vendor,
5038 pdev->subsystem_device);
5039
5040 printk("bus %d:slot %d:func %d\n",
5041 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
5042
5043 /*
5044 * PCI prepping: enable device set bus mastering and dma mask
5045 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005046 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005047
5048 if (rval) {
5049 return rval;
5050 }
5051
5052 pci_set_master(pdev);
5053
bo yang31ea7082007-11-07 12:09:50 -05005054 if (megasas_set_dma_mask(pdev))
5055 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005056
5057 host = scsi_host_alloc(&megasas_template,
5058 sizeof(struct megasas_instance));
5059
5060 if (!host) {
5061 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
5062 goto fail_alloc_instance;
5063 }
5064
5065 instance = (struct megasas_instance *)host->hostdata;
5066 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04005067 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08005068 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005069
adam radford9c915a82010-12-21 13:34:31 -08005070 switch (instance->pdev->device) {
5071 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005072 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005073 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305074 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005075 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305076 instance->ctrl_context_pages =
5077 get_order(sizeof(struct fusion_context));
5078 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5079 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005080 if (!instance->ctrl_context) {
5081 printk(KERN_DEBUG "megasas: Failed to allocate "
5082 "memory for Fusion context info\n");
5083 goto fail_alloc_dma_buf;
5084 }
5085 fusion = instance->ctrl_context;
5086 INIT_LIST_HEAD(&fusion->cmd_pool);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305087 spin_lock_init(&fusion->mpt_pool_lock);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305088 memset(fusion->load_balance_info, 0,
5089 sizeof(struct LD_LOAD_BALANCE_INFO) * MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08005090 }
5091 break;
5092 default: /* For all other supported controllers */
5093
5094 instance->producer =
5095 pci_alloc_consistent(pdev, sizeof(u32),
5096 &instance->producer_h);
5097 instance->consumer =
5098 pci_alloc_consistent(pdev, sizeof(u32),
5099 &instance->consumer_h);
5100
5101 if (!instance->producer || !instance->consumer) {
5102 printk(KERN_DEBUG "megasas: Failed to allocate"
5103 "memory for producer, consumer\n");
5104 goto fail_alloc_dma_buf;
5105 }
5106
5107 *instance->producer = 0;
5108 *instance->consumer = 0;
5109 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005110 }
5111
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305112 /* Crash dump feature related initialisation*/
5113 instance->drv_buf_index = 0;
5114 instance->drv_buf_alloc = 0;
5115 instance->crash_dump_fw_support = 0;
5116 instance->crash_dump_app_support = 0;
5117 instance->fw_crash_state = UNAVAILABLE;
5118 spin_lock_init(&instance->crashdump_lock);
5119 instance->crash_dump_buf = NULL;
5120
5121 if (!reset_devices)
5122 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5123 CRASH_DMA_BUF_SIZE,
5124 &instance->crash_dump_h);
5125 if (!instance->crash_dump_buf)
5126 dev_err(&instance->pdev->dev, "Can't allocate Firmware "
5127 "crash dump DMA buffer\n");
5128
Yang, Boc3518832009-10-06 14:18:02 -06005129 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005130 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005131 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005132 instance->issuepend_done = 1;
5133 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305134 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005135
5136 instance->evt_detail = pci_alloc_consistent(pdev,
5137 sizeof(struct
5138 megasas_evt_detail),
5139 &instance->evt_detail_h);
5140
5141 if (!instance->evt_detail) {
5142 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
5143 "event detail structure\n");
5144 goto fail_alloc_dma_buf;
5145 }
5146
5147 /*
5148 * Initialize locks and queues
5149 */
5150 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005151 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005152
Sumant Patroe4a082c2006-05-30 12:03:37 -07005153 atomic_set(&instance->fw_outstanding,0);
5154
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005155 init_waitqueue_head(&instance->int_cmd_wait_q);
5156 init_waitqueue_head(&instance->abort_cmd_wait_q);
5157
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305158 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005159 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05005160 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005161
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005162 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08005163 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005164
5165 /*
5166 * Initialize PCI related and misc parameters
5167 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005168 instance->host = host;
5169 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5170 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305171 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005172
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005173 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5174 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06005175 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005176 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5177 } else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305178 sema_init(&instance->ioctl_sem, (MEGASAS_INT_CMDS - 5));
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005179
Sumant Patro658dced2006-10-03 13:09:14 -07005180 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005181 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005182 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005183 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005184 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305185 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005186
adam radford36807e62011-10-08 18:15:06 -07005187 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005188 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305189 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305190 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08005191 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305192 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5193 } else
adam radford9c915a82010-12-21 13:34:31 -08005194 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005195
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005196 /*
adam radford0a770662011-02-24 20:56:12 -08005197 * Initialize MFI Firmware
5198 */
5199 if (megasas_init_fw(instance))
5200 goto fail_init_mfi;
5201
adam radford229fe472014-03-10 02:51:56 -07005202 if (instance->requestorId) {
5203 if (instance->PlasmaFW111) {
5204 instance->vf_affiliation_111 =
5205 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5206 &instance->vf_affiliation_111_h);
5207 if (!instance->vf_affiliation_111)
5208 printk(KERN_WARNING "megasas: Can't allocate "
5209 "memory for VF affiliation buffer\n");
5210 } else {
5211 instance->vf_affiliation =
5212 pci_alloc_consistent(pdev,
5213 (MAX_LOGICAL_DRIVES + 1) *
5214 sizeof(struct MR_LD_VF_AFFILIATION),
5215 &instance->vf_affiliation_h);
5216 if (!instance->vf_affiliation)
5217 printk(KERN_WARNING "megasas: Can't allocate "
5218 "memory for VF affiliation buffer\n");
5219 }
5220 }
5221
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305222retry_irq_register:
adam radford0a770662011-02-24 20:56:12 -08005223 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005224 * Register IRQ
5225 */
adam radfordc8e858f2011-10-08 18:15:13 -07005226 if (instance->msix_vectors) {
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305227 cpu = cpumask_first(cpu_online_mask);
5228 for (i = 0; i < instance->msix_vectors; i++) {
adam radfordc8e858f2011-10-08 18:15:13 -07005229 instance->irq_context[i].instance = instance;
5230 instance->irq_context[i].MSIxIndex = i;
5231 if (request_irq(instance->msixentry[i].vector,
5232 instance->instancet->service_isr, 0,
5233 "megasas",
5234 &instance->irq_context[i])) {
5235 printk(KERN_DEBUG "megasas: Failed to "
5236 "register IRQ for vector %d.\n", i);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305237 for (j = 0; j < i; j++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305238 if (smp_affinity_enable)
5239 irq_set_affinity_hint(
5240 instance->msixentry[j].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005241 free_irq(
5242 instance->msixentry[j].vector,
5243 &instance->irq_context[j]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305244 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305245 /* Retry irq register for IO_APIC */
5246 instance->msix_vectors = 0;
5247 goto retry_irq_register;
adam radfordc8e858f2011-10-08 18:15:13 -07005248 }
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305249 if (smp_affinity_enable) {
5250 if (irq_set_affinity_hint(instance->msixentry[i].vector,
5251 get_cpu_mask(cpu)))
5252 dev_err(&instance->pdev->dev,
5253 "Error setting affinity hint "
5254 "for cpu %d\n", cpu);
5255 cpu = cpumask_next(cpu, cpu_online_mask);
5256 }
adam radfordc8e858f2011-10-08 18:15:13 -07005257 }
5258 } else {
5259 instance->irq_context[0].instance = instance;
5260 instance->irq_context[0].MSIxIndex = 0;
5261 if (request_irq(pdev->irq, instance->instancet->service_isr,
5262 IRQF_SHARED, "megasas",
5263 &instance->irq_context[0])) {
5264 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5265 goto fail_irq;
5266 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005267 }
5268
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305269 instance->instancet->enable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005270
5271 /*
5272 * Store instance in PCI softstate
5273 */
5274 pci_set_drvdata(pdev, instance);
5275
5276 /*
5277 * Add this controller to megasas_mgmt_info structure so that it
5278 * can be exported to management applications
5279 */
5280 megasas_mgmt_info.count++;
5281 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5282 megasas_mgmt_info.max_index++;
5283
5284 /*
adam radford541f90b2011-05-11 18:34:29 -07005285 * Register with SCSI mid-layer
5286 */
5287 if (megasas_io_attach(instance))
5288 goto fail_io_attach;
5289
5290 instance->unload = 0;
5291
5292 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005293 * Initiate AEN (Asynchronous Event Notification)
5294 */
5295 if (megasas_start_aen(instance)) {
5296 printk(KERN_DEBUG "megasas: start aen failed\n");
5297 goto fail_start_aen;
5298 }
5299
Adam Radford9ea81f82014-07-09 15:17:57 -07005300 /* Get current SR-IOV LD/VF affiliation */
5301 if (instance->requestorId)
5302 megasas_get_ld_vf_affiliation(instance, 1);
5303
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005304 return 0;
5305
5306 fail_start_aen:
5307 fail_io_attach:
5308 megasas_mgmt_info.count--;
5309 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5310 megasas_mgmt_info.max_index--;
5311
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305312 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005313 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305314 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305315 if (smp_affinity_enable)
5316 irq_set_affinity_hint(
5317 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005318 free_irq(instance->msixentry[i].vector,
5319 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305320 }
adam radfordc8e858f2011-10-08 18:15:13 -07005321 else
5322 free_irq(instance->pdev->irq, &instance->irq_context[0]);
adam radfordeb1b1232011-02-24 20:55:56 -08005323fail_irq:
adam radford36807e62011-10-08 18:15:06 -07005324 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
adam radford229fe472014-03-10 02:51:56 -07005325 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305326 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5327 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radfordeb1b1232011-02-24 20:55:56 -08005328 megasas_release_fusion(instance);
5329 else
5330 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005331 fail_init_mfi:
adam radfordc8e858f2011-10-08 18:15:13 -07005332 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08005333 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005334 fail_alloc_dma_buf:
5335 if (instance->evt_detail)
5336 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5337 instance->evt_detail,
5338 instance->evt_detail_h);
5339
adam radfordeb1b1232011-02-24 20:55:56 -08005340 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005341 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5342 instance->producer_h);
5343 if (instance->consumer)
5344 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5345 instance->consumer_h);
5346 scsi_host_put(host);
5347
5348 fail_alloc_instance:
5349 fail_set_dma_mask:
5350 pci_disable_device(pdev);
5351
5352 return -ENODEV;
5353}
5354
5355/**
5356 * megasas_flush_cache - Requests FW to flush all its caches
5357 * @instance: Adapter soft state
5358 */
5359static void megasas_flush_cache(struct megasas_instance *instance)
5360{
5361 struct megasas_cmd *cmd;
5362 struct megasas_dcmd_frame *dcmd;
5363
bo yang39a98552010-09-22 22:36:29 -04005364 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5365 return;
5366
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005367 cmd = megasas_get_cmd(instance);
5368
5369 if (!cmd)
5370 return;
5371
5372 dcmd = &cmd->frame->dcmd;
5373
5374 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5375
5376 dcmd->cmd = MFI_CMD_DCMD;
5377 dcmd->cmd_status = 0x0;
5378 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305379 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005380 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005381 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005382 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305383 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005384 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5385
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305386 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5387 dev_err(&instance->pdev->dev, "Command timedout"
5388 " from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005389
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305390 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5391 megasas_return_mfi_mpt_pthr(instance, cmd,
5392 cmd->mpt_pthr_cmd_blocked);
5393 else
5394 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005395
5396 return;
5397}
5398
5399/**
5400 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5401 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05005402 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005403 */
bo yang31ea7082007-11-07 12:09:50 -05005404static void megasas_shutdown_controller(struct megasas_instance *instance,
5405 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005406{
5407 struct megasas_cmd *cmd;
5408 struct megasas_dcmd_frame *dcmd;
5409
bo yang39a98552010-09-22 22:36:29 -04005410 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5411 return;
5412
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005413 cmd = megasas_get_cmd(instance);
5414
5415 if (!cmd)
5416 return;
5417
5418 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305419 megasas_issue_blocked_abort_cmd(instance,
5420 instance->aen_cmd, 30);
adam radford9c915a82010-12-21 13:34:31 -08005421 if (instance->map_update_cmd)
5422 megasas_issue_blocked_abort_cmd(instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305423 instance->map_update_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005424 dcmd = &cmd->frame->dcmd;
5425
5426 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5427
5428 dcmd->cmd = MFI_CMD_DCMD;
5429 dcmd->cmd_status = 0x0;
5430 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305431 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005432 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005433 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005434 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305435 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005436
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305437 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5438 dev_err(&instance->pdev->dev, "Command timedout"
5439 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005440
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305441 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5442 megasas_return_mfi_mpt_pthr(instance, cmd,
5443 cmd->mpt_pthr_cmd_blocked);
5444 else
5445 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005446
5447 return;
5448}
5449
Jiri Slaby33139b22008-05-01 17:56:02 +02005450#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005451/**
bo yangad84db22007-11-09 04:40:16 -05005452 * megasas_suspend - driver suspend entry point
5453 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05005454 * @state: PCI power state to suspend routine
5455 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005456static int
bo yang31ea7082007-11-07 12:09:50 -05005457megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5458{
5459 struct Scsi_Host *host;
5460 struct megasas_instance *instance;
adam radfordc8e858f2011-10-08 18:15:13 -07005461 int i;
bo yang31ea7082007-11-07 12:09:50 -05005462
5463 instance = pci_get_drvdata(pdev);
5464 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06005465 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05005466
adam radford229fe472014-03-10 02:51:56 -07005467 /* Shutdown SR-IOV heartbeat timer */
5468 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5469 del_timer_sync(&instance->sriov_heartbeat_timer);
5470
bo yang31ea7082007-11-07 12:09:50 -05005471 megasas_flush_cache(instance);
5472 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005473
5474 /* cancel the delayed work if this work still in queue */
5475 if (instance->ev != NULL) {
5476 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005477 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005478 instance->ev = NULL;
5479 }
5480
bo yang31ea7082007-11-07 12:09:50 -05005481 tasklet_kill(&instance->isr_tasklet);
5482
5483 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305484 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005485
5486 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305487 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305488 if (smp_affinity_enable)
5489 irq_set_affinity_hint(
5490 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005491 free_irq(instance->msixentry[i].vector,
5492 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305493 }
adam radfordc8e858f2011-10-08 18:15:13 -07005494 else
5495 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5496 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005497 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05005498
5499 pci_save_state(pdev);
5500 pci_disable_device(pdev);
5501
5502 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5503
5504 return 0;
5505}
5506
5507/**
5508 * megasas_resume- driver resume entry point
5509 * @pdev: PCI device structure
5510 */
Jiri Slaby33139b22008-05-01 17:56:02 +02005511static int
bo yang31ea7082007-11-07 12:09:50 -05005512megasas_resume(struct pci_dev *pdev)
5513{
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305514 int rval, i, j, cpu;
bo yang31ea7082007-11-07 12:09:50 -05005515 struct Scsi_Host *host;
5516 struct megasas_instance *instance;
5517
5518 instance = pci_get_drvdata(pdev);
5519 host = instance->host;
5520 pci_set_power_state(pdev, PCI_D0);
5521 pci_enable_wake(pdev, PCI_D0, 0);
5522 pci_restore_state(pdev);
5523
5524 /*
5525 * PCI prepping: enable device set bus mastering and dma mask
5526 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005527 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05005528
5529 if (rval) {
5530 printk(KERN_ERR "megasas: Enable device failed\n");
5531 return rval;
5532 }
5533
5534 pci_set_master(pdev);
5535
5536 if (megasas_set_dma_mask(pdev))
5537 goto fail_set_dma_mask;
5538
5539 /*
5540 * Initialize MFI Firmware
5541 */
5542
bo yang31ea7082007-11-07 12:09:50 -05005543 atomic_set(&instance->fw_outstanding, 0);
5544
5545 /*
5546 * We expect the FW state to be READY
5547 */
adam radford058a8fa2011-10-08 18:14:27 -07005548 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05005549 goto fail_ready_state;
5550
adam radford3f1abce2011-05-11 18:33:47 -07005551 /* Now re-enable MSI-X */
Alexander Gordeevdd088122014-08-18 08:01:43 +02005552 if (instance->msix_vectors &&
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005553 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5554 instance->msix_vectors))
Alexander Gordeevdd088122014-08-18 08:01:43 +02005555 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07005556
adam radford9c915a82010-12-21 13:34:31 -08005557 switch (instance->pdev->device) {
5558 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005559 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005560 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305561 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005562 {
5563 megasas_reset_reply_desc(instance);
5564 if (megasas_ioc_init_fusion(instance)) {
5565 megasas_free_cmds(instance);
5566 megasas_free_cmds_fusion(instance);
5567 goto fail_init_mfi;
5568 }
5569 if (!megasas_get_map_info(instance))
5570 megasas_sync_map_info(instance);
5571 }
5572 break;
5573 default:
5574 *instance->producer = 0;
5575 *instance->consumer = 0;
5576 if (megasas_issue_init_mfi(instance))
5577 goto fail_init_mfi;
5578 break;
5579 }
bo yang31ea7082007-11-07 12:09:50 -05005580
adam radford9c915a82010-12-21 13:34:31 -08005581 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5582 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05005583
5584 /*
5585 * Register IRQ
5586 */
adam radfordc8e858f2011-10-08 18:15:13 -07005587 if (instance->msix_vectors) {
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305588 cpu = cpumask_first(cpu_online_mask);
adam radfordc8e858f2011-10-08 18:15:13 -07005589 for (i = 0 ; i < instance->msix_vectors; i++) {
5590 instance->irq_context[i].instance = instance;
5591 instance->irq_context[i].MSIxIndex = i;
5592 if (request_irq(instance->msixentry[i].vector,
5593 instance->instancet->service_isr, 0,
5594 "megasas",
5595 &instance->irq_context[i])) {
5596 printk(KERN_DEBUG "megasas: Failed to "
5597 "register IRQ for vector %d.\n", i);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305598 for (j = 0; j < i; j++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305599 if (smp_affinity_enable)
5600 irq_set_affinity_hint(
5601 instance->msixentry[j].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005602 free_irq(
5603 instance->msixentry[j].vector,
5604 &instance->irq_context[j]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305605 }
adam radfordc8e858f2011-10-08 18:15:13 -07005606 goto fail_irq;
5607 }
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305608
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305609 if (smp_affinity_enable) {
5610 if (irq_set_affinity_hint(instance->msixentry[i].vector,
5611 get_cpu_mask(cpu)))
5612 dev_err(&instance->pdev->dev, "Error "
5613 "setting affinity hint for cpu "
5614 "%d\n", cpu);
5615 cpu = cpumask_next(cpu, cpu_online_mask);
5616 }
adam radfordc8e858f2011-10-08 18:15:13 -07005617 }
5618 } else {
5619 instance->irq_context[0].instance = instance;
5620 instance->irq_context[0].MSIxIndex = 0;
5621 if (request_irq(pdev->irq, instance->instancet->service_isr,
5622 IRQF_SHARED, "megasas",
5623 &instance->irq_context[0])) {
5624 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5625 goto fail_irq;
5626 }
bo yang31ea7082007-11-07 12:09:50 -05005627 }
5628
adam radford229fe472014-03-10 02:51:56 -07005629 /* Re-launch SR-IOV heartbeat timer */
5630 if (instance->requestorId) {
5631 if (!megasas_sriov_start_heartbeat(instance, 0))
5632 megasas_start_timer(instance,
5633 &instance->sriov_heartbeat_timer,
5634 megasas_sriov_heartbeat_handler,
5635 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5636 else
5637 instance->skip_heartbeat_timer_del = 1;
5638 }
5639
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305640 instance->instancet->enable_intr(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06005641 instance->unload = 0;
5642
adam radford541f90b2011-05-11 18:34:29 -07005643 /*
5644 * Initiate AEN (Asynchronous Event Notification)
5645 */
5646 if (megasas_start_aen(instance))
5647 printk(KERN_ERR "megasas: Start AEN failed\n");
5648
bo yang31ea7082007-11-07 12:09:50 -05005649 return 0;
5650
5651fail_irq:
5652fail_init_mfi:
5653 if (instance->evt_detail)
5654 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5655 instance->evt_detail,
5656 instance->evt_detail_h);
5657
5658 if (instance->producer)
5659 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5660 instance->producer_h);
5661 if (instance->consumer)
5662 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5663 instance->consumer_h);
5664 scsi_host_put(host);
5665
5666fail_set_dma_mask:
5667fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02005668fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05005669
5670 pci_disable_device(pdev);
5671
5672 return -ENODEV;
5673}
Jiri Slaby33139b22008-05-01 17:56:02 +02005674#else
5675#define megasas_suspend NULL
5676#define megasas_resume NULL
5677#endif
bo yang31ea7082007-11-07 12:09:50 -05005678
5679/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005680 * megasas_detach_one - PCI hot"un"plug entry point
5681 * @pdev: PCI device structure
5682 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005683static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005684{
5685 int i;
5686 struct Scsi_Host *host;
5687 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08005688 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005689
5690 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06005691 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005692 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08005693 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005694
adam radford229fe472014-03-10 02:51:56 -07005695 /* Shutdown SR-IOV heartbeat timer */
5696 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5697 del_timer_sync(&instance->sriov_heartbeat_timer);
5698
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305699 if (instance->fw_crash_state != UNAVAILABLE)
5700 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005701 scsi_remove_host(instance->host);
5702 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05005703 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005704
5705 /* cancel the delayed work if this work still in queue*/
5706 if (instance->ev != NULL) {
5707 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005708 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005709 instance->ev = NULL;
5710 }
5711
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305712 /* cancel all wait events */
5713 wake_up_all(&instance->int_cmd_wait_q);
5714
Sumant Patro5d018ad2006-10-03 13:13:18 -07005715 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005716
5717 /*
5718 * Take the instance off the instance array. Note that we will not
5719 * decrement the max_index. We let this array be sparse array
5720 */
5721 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5722 if (megasas_mgmt_info.instance[i] == instance) {
5723 megasas_mgmt_info.count--;
5724 megasas_mgmt_info.instance[i] = NULL;
5725
5726 break;
5727 }
5728 }
5729
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305730 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005731
adam radfordc8e858f2011-10-08 18:15:13 -07005732 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305733 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305734 if (smp_affinity_enable)
5735 irq_set_affinity_hint(
5736 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005737 free_irq(instance->msixentry[i].vector,
5738 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305739 }
adam radfordc8e858f2011-10-08 18:15:13 -07005740 else
5741 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5742 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08005743 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005744
adam radford9c915a82010-12-21 13:34:31 -08005745 switch (instance->pdev->device) {
5746 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005747 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005748 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305749 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08005750 megasas_release_fusion(instance);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305751 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08005752 if (fusion->ld_map[i])
5753 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305754 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08005755 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305756 fusion->ld_map_phys[i]);
5757 if (fusion->ld_drv_map[i])
5758 free_pages((ulong)fusion->ld_drv_map[i],
5759 fusion->drv_map_pages);
5760 }
5761 free_pages((ulong)instance->ctrl_context,
5762 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005763 break;
5764 default:
5765 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08005766 pci_free_consistent(pdev, sizeof(u32),
5767 instance->producer,
5768 instance->producer_h);
5769 pci_free_consistent(pdev, sizeof(u32),
5770 instance->consumer,
5771 instance->consumer_h);
5772 break;
5773 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005774
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305775 kfree(instance->ctrl_info);
5776
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05305777 if (instance->evt_detail)
5778 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5779 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07005780
5781 if (instance->vf_affiliation)
5782 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5783 sizeof(struct MR_LD_VF_AFFILIATION),
5784 instance->vf_affiliation,
5785 instance->vf_affiliation_h);
5786
5787 if (instance->vf_affiliation_111)
5788 pci_free_consistent(pdev,
5789 sizeof(struct MR_LD_VF_AFFILIATION_111),
5790 instance->vf_affiliation_111,
5791 instance->vf_affiliation_111_h);
5792
5793 if (instance->hb_host_mem)
5794 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5795 instance->hb_host_mem,
5796 instance->hb_host_mem_h);
5797
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305798 if (instance->crash_dump_buf)
5799 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5800 instance->crash_dump_buf, instance->crash_dump_h);
5801
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005802 scsi_host_put(host);
5803
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005804 pci_disable_device(pdev);
5805
5806 return;
5807}
5808
5809/**
5810 * megasas_shutdown - Shutdown entry point
5811 * @device: Generic device structure
5812 */
5813static void megasas_shutdown(struct pci_dev *pdev)
5814{
adam radfordc8e858f2011-10-08 18:15:13 -07005815 int i;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005816 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07005817
Yang, Bo0c79e682009-10-06 14:47:35 -06005818 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005819 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07005820 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305821 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07005822 if (instance->msix_vectors)
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305823 for (i = 0; i < instance->msix_vectors; i++) {
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +05305824 if (smp_affinity_enable)
5825 irq_set_affinity_hint(
5826 instance->msixentry[i].vector, NULL);
adam radfordc8e858f2011-10-08 18:15:13 -07005827 free_irq(instance->msixentry[i].vector,
5828 &instance->irq_context[i]);
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05305829 }
adam radfordc8e858f2011-10-08 18:15:13 -07005830 else
5831 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5832 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07005833 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005834}
5835
5836/**
5837 * megasas_mgmt_open - char node "open" entry point
5838 */
5839static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5840{
5841 /*
5842 * Allow only those users with admin rights
5843 */
5844 if (!capable(CAP_SYS_ADMIN))
5845 return -EACCES;
5846
5847 return 0;
5848}
5849
5850/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005851 * megasas_mgmt_fasync - Async notifier registration from applications
5852 *
5853 * This function adds the calling process to a driver global queue. When an
5854 * event occurs, SIGIO will be sent to all processes in this queue.
5855 */
5856static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5857{
5858 int rc;
5859
Arjan van de Ven0b950672006-01-11 13:16:10 +01005860 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005861
5862 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
5863
Arjan van de Ven0b950672006-01-11 13:16:10 +01005864 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005865
5866 if (rc >= 0) {
5867 /* For sanity check when we get ioctl */
5868 filep->private_data = filep;
5869 return 0;
5870 }
5871
5872 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
5873
5874 return rc;
5875}
5876
5877/**
Yang, Boc3518832009-10-06 14:18:02 -06005878 * megasas_mgmt_poll - char node "poll" entry point
5879 * */
5880static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
5881{
5882 unsigned int mask;
5883 unsigned long flags;
5884 poll_wait(file, &megasas_poll_wait, wait);
5885 spin_lock_irqsave(&poll_aen_lock, flags);
5886 if (megasas_poll_wait_aen)
5887 mask = (POLLIN | POLLRDNORM);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305888
Yang, Boc3518832009-10-06 14:18:02 -06005889 else
5890 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305891 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06005892 spin_unlock_irqrestore(&poll_aen_lock, flags);
5893 return mask;
5894}
5895
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305896/*
5897 * megasas_set_crash_dump_params_ioctl:
5898 * Send CRASH_DUMP_MODE DCMD to all controllers
5899 * @cmd: MFI command frame
5900 */
5901
5902static int megasas_set_crash_dump_params_ioctl(
5903 struct megasas_cmd *cmd)
5904{
5905 struct megasas_instance *local_instance;
5906 int i, error = 0;
5907 int crash_support;
5908
5909 crash_support = cmd->frame->dcmd.mbox.w[0];
5910
5911 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5912 local_instance = megasas_mgmt_info.instance[i];
5913 if (local_instance && local_instance->crash_dump_drv_support) {
5914 if ((local_instance->adprecovery ==
5915 MEGASAS_HBA_OPERATIONAL) &&
5916 !megasas_set_crash_dump_params(local_instance,
5917 crash_support)) {
5918 local_instance->crash_dump_app_support =
5919 crash_support;
5920 dev_info(&local_instance->pdev->dev,
5921 "Application firmware crash "
5922 "dump mode set success\n");
5923 error = 0;
5924 } else {
5925 dev_info(&local_instance->pdev->dev,
5926 "Application firmware crash "
5927 "dump mode set failed\n");
5928 error = -1;
5929 }
5930 }
5931 }
5932 return error;
5933}
5934
Yang, Boc3518832009-10-06 14:18:02 -06005935/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005936 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
5937 * @instance: Adapter soft state
5938 * @argp: User's ioctl packet
5939 */
5940static int
5941megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
5942 struct megasas_iocpacket __user * user_ioc,
5943 struct megasas_iocpacket *ioc)
5944{
5945 struct megasas_sge32 *kern_sge32;
5946 struct megasas_cmd *cmd;
5947 void *kbuff_arr[MAX_IOCTL_SGE];
5948 dma_addr_t buf_handle = 0;
5949 int error = 0, i;
5950 void *sense = NULL;
5951 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06005952 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005953
5954 memset(kbuff_arr, 0, sizeof(kbuff_arr));
5955
5956 if (ioc->sge_count > MAX_IOCTL_SGE) {
5957 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
5958 ioc->sge_count, MAX_IOCTL_SGE);
5959 return -EINVAL;
5960 }
5961
5962 cmd = megasas_get_cmd(instance);
5963 if (!cmd) {
5964 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
5965 return -ENOMEM;
5966 }
5967
5968 /*
5969 * User's IOCTL packet has 2 frames (maximum). Copy those two
5970 * frames into our cmd's frames. cmd->frame's context will get
5971 * overwritten when we copy from user's frames. So set that value
5972 * alone separately
5973 */
5974 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305975 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06005976 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305977 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
5978 MFI_FRAME_SGL64 |
5979 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005980
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305981 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
5982 error = megasas_set_crash_dump_params_ioctl(cmd);
5983 megasas_return_cmd(instance, cmd);
5984 return error;
5985 }
5986
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005987 /*
5988 * The management interface between applications and the fw uses
5989 * MFI frames. E.g, RAID configuration changes, LD property changes
5990 * etc are accomplishes through different kinds of MFI frames. The
5991 * driver needs to care only about substituting user buffers with
5992 * kernel buffers in SGLs. The location of SGL is embedded in the
5993 * struct iocpacket itself.
5994 */
5995 kern_sge32 = (struct megasas_sge32 *)
5996 ((unsigned long)cmd->frame + ioc->sgl_off);
5997
5998 /*
5999 * For each user buffer, create a mirror buffer and copy in
6000 */
6001 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006002 if (!ioc->sgl[i].iov_len)
6003 continue;
6004
Sumant Patro9f35fa82007-02-14 12:55:45 -08006005 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006006 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006007 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006008 if (!kbuff_arr[i]) {
6009 printk(KERN_DEBUG "megasas: Failed to alloc "
6010 "kernel SGL buffer for IOCTL \n");
6011 error = -ENOMEM;
6012 goto out;
6013 }
6014
6015 /*
6016 * We don't change the dma_coherent_mask, so
6017 * pci_alloc_consistent only returns 32bit addresses
6018 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306019 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6020 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006021
6022 /*
6023 * We created a kernel buffer corresponding to the
6024 * user buffer. Now copy in from the user buffer
6025 */
6026 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6027 (u32) (ioc->sgl[i].iov_len))) {
6028 error = -EFAULT;
6029 goto out;
6030 }
6031 }
6032
6033 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006034 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6035 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006036 if (!sense) {
6037 error = -ENOMEM;
6038 goto out;
6039 }
6040
6041 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006042 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306043 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006044 }
6045
6046 /*
6047 * Set the sync_cmd flag so that the ISR knows not to complete this
6048 * cmd to the SCSI mid-layer
6049 */
6050 cmd->sync_cmd = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306051 megasas_issue_blocked_cmd(instance, cmd, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006052 cmd->sync_cmd = 0;
6053
6054 /*
6055 * copy out the kernel buffers to user buffers
6056 */
6057 for (i = 0; i < ioc->sge_count; i++) {
6058 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6059 ioc->sgl[i].iov_len)) {
6060 error = -EFAULT;
6061 goto out;
6062 }
6063 }
6064
6065 /*
6066 * copy out the sense
6067 */
6068 if (ioc->sense_len) {
6069 /*
bo yangb70a41e2008-03-18 03:13:06 -04006070 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006071 * sense buffer address
6072 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006073 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6074 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006075
bo yangb70a41e2008-03-18 03:13:06 -04006076 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6077 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05006078 printk(KERN_ERR "megasas: Failed to copy out to user "
6079 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006080 error = -EFAULT;
6081 goto out;
6082 }
6083 }
6084
6085 /*
6086 * copy the status codes returned by the fw
6087 */
6088 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6089 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6090 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
6091 error = -EFAULT;
6092 }
6093
6094 out:
6095 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006096 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006097 sense, sense_handle);
6098 }
6099
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006100 for (i = 0; i < ioc->sge_count; i++) {
6101 if (kbuff_arr[i])
6102 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306103 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006104 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306105 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306106 kbuff_arr[i] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006107 }
6108
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306109 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
6110 megasas_return_mfi_mpt_pthr(instance, cmd,
6111 cmd->mpt_pthr_cmd_blocked);
6112 else
6113 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006114 return error;
6115}
6116
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006117static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6118{
6119 struct megasas_iocpacket __user *user_ioc =
6120 (struct megasas_iocpacket __user *)arg;
6121 struct megasas_iocpacket *ioc;
6122 struct megasas_instance *instance;
6123 int error;
bo yang39a98552010-09-22 22:36:29 -04006124 int i;
6125 unsigned long flags;
6126 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006127
6128 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6129 if (!ioc)
6130 return -ENOMEM;
6131
6132 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6133 error = -EFAULT;
6134 goto out_kfree_ioc;
6135 }
6136
6137 instance = megasas_lookup_instance(ioc->host_no);
6138 if (!instance) {
6139 error = -ENODEV;
6140 goto out_kfree_ioc;
6141 }
6142
adam radford229fe472014-03-10 02:51:56 -07006143 /* Adjust ioctl wait time for VF mode */
6144 if (instance->requestorId)
6145 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6146
6147 /* Block ioctls in VF mode */
6148 if (instance->requestorId && !allow_vf_ioctls) {
6149 error = -ENODEV;
6150 goto out_kfree_ioc;
6151 }
6152
bo yang39a98552010-09-22 22:36:29 -04006153 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6154 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006155 error = -ENODEV;
6156 goto out_kfree_ioc;
6157 }
6158
6159 if (instance->unload == 1) {
6160 error = -ENODEV;
6161 goto out_kfree_ioc;
6162 }
6163
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006164 /*
6165 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
6166 */
6167 if (down_interruptible(&instance->ioctl_sem)) {
6168 error = -ERESTARTSYS;
6169 goto out_kfree_ioc;
6170 }
bo yang39a98552010-09-22 22:36:29 -04006171
6172 for (i = 0; i < wait_time; i++) {
6173
6174 spin_lock_irqsave(&instance->hba_lock, flags);
6175 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6176 spin_unlock_irqrestore(&instance->hba_lock, flags);
6177 break;
6178 }
6179 spin_unlock_irqrestore(&instance->hba_lock, flags);
6180
6181 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6182 printk(KERN_NOTICE "megasas: waiting"
6183 "for controller reset to finish\n");
6184 }
6185
6186 msleep(1000);
6187 }
6188
6189 spin_lock_irqsave(&instance->hba_lock, flags);
6190 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6191 spin_unlock_irqrestore(&instance->hba_lock, flags);
6192
6193 printk(KERN_ERR "megaraid_sas: timed out while"
6194 "waiting for HBA to recover\n");
6195 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006196 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006197 }
6198 spin_unlock_irqrestore(&instance->hba_lock, flags);
6199
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006200 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Dan Carpenterc64e4832013-04-16 10:44:19 +03006201 out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006202 up(&instance->ioctl_sem);
6203
6204 out_kfree_ioc:
6205 kfree(ioc);
6206 return error;
6207}
6208
6209static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6210{
6211 struct megasas_instance *instance;
6212 struct megasas_aen aen;
6213 int error;
bo yang39a98552010-09-22 22:36:29 -04006214 int i;
6215 unsigned long flags;
6216 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006217
6218 if (file->private_data != file) {
6219 printk(KERN_DEBUG "megasas: fasync_helper was not "
6220 "called first\n");
6221 return -EINVAL;
6222 }
6223
6224 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6225 return -EFAULT;
6226
6227 instance = megasas_lookup_instance(aen.host_no);
6228
6229 if (!instance)
6230 return -ENODEV;
6231
bo yang39a98552010-09-22 22:36:29 -04006232 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6233 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006234 }
6235
6236 if (instance->unload == 1) {
6237 return -ENODEV;
6238 }
6239
bo yang39a98552010-09-22 22:36:29 -04006240 for (i = 0; i < wait_time; i++) {
6241
6242 spin_lock_irqsave(&instance->hba_lock, flags);
6243 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6244 spin_unlock_irqrestore(&instance->hba_lock,
6245 flags);
6246 break;
6247 }
6248
6249 spin_unlock_irqrestore(&instance->hba_lock, flags);
6250
6251 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6252 printk(KERN_NOTICE "megasas: waiting for"
6253 "controller reset to finish\n");
6254 }
6255
6256 msleep(1000);
6257 }
6258
6259 spin_lock_irqsave(&instance->hba_lock, flags);
6260 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6261 spin_unlock_irqrestore(&instance->hba_lock, flags);
6262 printk(KERN_ERR "megaraid_sas: timed out while waiting"
6263 "for HBA to recover.\n");
6264 return -ENODEV;
6265 }
6266 spin_unlock_irqrestore(&instance->hba_lock, flags);
6267
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006268 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006269 error = megasas_register_aen(instance, aen.seq_num,
6270 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02006271 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006272 return error;
6273}
6274
6275/**
6276 * megasas_mgmt_ioctl - char node ioctl entry point
6277 */
6278static long
6279megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6280{
6281 switch (cmd) {
6282 case MEGASAS_IOC_FIRMWARE:
6283 return megasas_mgmt_ioctl_fw(file, arg);
6284
6285 case MEGASAS_IOC_GET_AEN:
6286 return megasas_mgmt_ioctl_aen(file, arg);
6287 }
6288
6289 return -ENOTTY;
6290}
6291
6292#ifdef CONFIG_COMPAT
6293static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6294{
6295 struct compat_megasas_iocpacket __user *cioc =
6296 (struct compat_megasas_iocpacket __user *)arg;
6297 struct megasas_iocpacket __user *ioc =
6298 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6299 int i;
6300 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006301 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006302
Jeff Garzik83aabc12006-10-04 06:34:03 -04006303 if (clear_user(ioc, sizeof(*ioc)))
6304 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006305
6306 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6307 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6308 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6309 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6310 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6311 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6312 return -EFAULT;
6313
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006314 /*
6315 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6316 * sense_len is not null, so prepare the 64bit value under
6317 * the same condition.
6318 */
6319 if (ioc->sense_len) {
6320 void __user **sense_ioc_ptr =
6321 (void __user **)(ioc->frame.raw + ioc->sense_off);
6322 compat_uptr_t *sense_cioc_ptr =
6323 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6324 if (get_user(ptr, sense_cioc_ptr) ||
6325 put_user(compat_ptr(ptr), sense_ioc_ptr))
6326 return -EFAULT;
6327 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006328
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006329 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006330 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6331 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6332 copy_in_user(&ioc->sgl[i].iov_len,
6333 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6334 return -EFAULT;
6335 }
6336
6337 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6338
6339 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6340 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6341 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6342 return -EFAULT;
6343 }
6344 return error;
6345}
6346
6347static long
6348megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6349 unsigned long arg)
6350{
6351 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08006352 case MEGASAS_IOC_FIRMWARE32:
6353 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006354 case MEGASAS_IOC_GET_AEN:
6355 return megasas_mgmt_ioctl_aen(file, arg);
6356 }
6357
6358 return -ENOTTY;
6359}
6360#endif
6361
6362/*
6363 * File operations structure for management interface
6364 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08006365static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006366 .owner = THIS_MODULE,
6367 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006368 .fasync = megasas_mgmt_fasync,
6369 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06006370 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006371#ifdef CONFIG_COMPAT
6372 .compat_ioctl = megasas_mgmt_compat_ioctl,
6373#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02006374 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006375};
6376
6377/*
6378 * PCI hotplug support registration structure
6379 */
6380static struct pci_driver megasas_pci_driver = {
6381
6382 .name = "megaraid_sas",
6383 .id_table = megasas_pci_table,
6384 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006385 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05006386 .suspend = megasas_suspend,
6387 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006388 .shutdown = megasas_shutdown,
6389};
6390
6391/*
6392 * Sysfs driver attributes
6393 */
6394static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6395{
6396 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6397 MEGASAS_VERSION);
6398}
6399
6400static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6401
6402static ssize_t
6403megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6404{
6405 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6406 MEGASAS_RELDATE);
6407}
6408
6409static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
6410 NULL);
6411
Sumant Patro658dced2006-10-03 13:09:14 -07006412static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06006413megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6414{
6415 return sprintf(buf, "%u\n", support_poll_for_event);
6416}
6417
6418static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6419 megasas_sysfs_show_support_poll_for_event, NULL);
6420
Yang, Bo837f5fe2010-10-11 06:59:20 -06006421 static ssize_t
6422megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6423{
6424 return sprintf(buf, "%u\n", support_device_change);
6425}
6426
6427static DRIVER_ATTR(support_device_change, S_IRUGO,
6428 megasas_sysfs_show_support_device_change, NULL);
6429
Yang, Bo72c4fd32009-10-06 14:20:59 -06006430static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07006431megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6432{
bo yangad84db22007-11-09 04:40:16 -05006433 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07006434}
6435
6436static ssize_t
6437megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6438{
6439 int retval = count;
6440 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
6441 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6442 retval = -EINVAL;
6443 }
6444 return retval;
6445}
6446
Joe Malicki66dca9b2008-08-14 17:14:48 -04006447static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05006448 megasas_sysfs_set_dbg_lvl);
6449
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006450static void
6451megasas_aen_polling(struct work_struct *work)
6452{
6453 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006454 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006455 struct megasas_instance *instance = ev->instance;
6456 union megasas_evt_class_locale class_locale;
6457 struct Scsi_Host *host;
6458 struct scsi_device *sdev1;
6459 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006460 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006461 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07006462 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006463 int error;
6464
6465 if (!instance) {
6466 printk(KERN_ERR "invalid instance!\n");
6467 kfree(ev);
6468 return;
6469 }
adam radford229fe472014-03-10 02:51:56 -07006470
6471 /* Adjust event workqueue thread wait time for VF mode */
6472 if (instance->requestorId)
6473 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6474
6475 /* Don't run the event workqueue thread if OCR is running */
6476 for (i = 0; i < wait_time; i++) {
6477 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6478 break;
6479 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6480 printk(KERN_NOTICE "megasas: %s waiting for "
6481 "controller reset to finish for scsi%d\n",
6482 __func__, instance->host->host_no);
6483 }
6484 msleep(1000);
6485 }
6486
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006487 instance->ev = NULL;
6488 host = instance->host;
6489 if (instance->evt_detail) {
6490
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306491 switch (le32_to_cpu(instance->evt_detail->code)) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006492 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07006493 if (megasas_get_pd_list(instance) == 0) {
6494 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6495 for (j = 0;
6496 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6497 j++) {
6498
6499 pd_index =
6500 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6501
6502 sdev1 =
6503 scsi_device_lookup(host, i, j, 0);
6504
6505 if (instance->pd_list[pd_index].driveState
6506 == MR_PD_STATE_SYSTEM) {
6507 if (!sdev1) {
6508 scsi_add_device(host, i, j, 0);
6509 }
6510
6511 if (sdev1)
6512 scsi_device_put(sdev1);
6513 }
6514 }
6515 }
6516 }
6517 doscan = 0;
6518 break;
6519
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006520 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07006521 if (megasas_get_pd_list(instance) == 0) {
Yang, Boc9786842009-12-06 08:39:25 -07006522 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6523 for (j = 0;
6524 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6525 j++) {
6526
6527 pd_index =
6528 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6529
6530 sdev1 =
6531 scsi_device_lookup(host, i, j, 0);
6532
6533 if (instance->pd_list[pd_index].driveState
6534 == MR_PD_STATE_SYSTEM) {
6535 if (sdev1) {
6536 scsi_device_put(sdev1);
6537 }
6538 } else {
6539 if (sdev1) {
6540 scsi_remove_device(sdev1);
6541 scsi_device_put(sdev1);
6542 }
6543 }
6544 }
6545 }
6546 }
6547 doscan = 0;
6548 break;
6549
6550 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08006551 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07006552 case MR_EVT_LD_DELETED:
adam radford229fe472014-03-10 02:51:56 -07006553 if (!instance->requestorId ||
6554 (instance->requestorId &&
6555 megasas_get_ld_vf_affiliation(instance, 0))) {
6556 if (megasas_ld_list_query(instance,
6557 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6558 megasas_get_ld_list(instance);
6559 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6560 for (j = 0;
6561 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6562 j++) {
Yang, Boc9786842009-12-06 08:39:25 -07006563
adam radford229fe472014-03-10 02:51:56 -07006564 ld_index =
6565 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006566
adam radford229fe472014-03-10 02:51:56 -07006567 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07006568
adam radford229fe472014-03-10 02:51:56 -07006569 if (instance->ld_ids[ld_index]
6570 != 0xff) {
6571 if (sdev1)
6572 scsi_device_put(sdev1);
6573 } else {
6574 if (sdev1) {
6575 scsi_remove_device(sdev1);
6576 scsi_device_put(sdev1);
6577 }
Yang, Boc9786842009-12-06 08:39:25 -07006578 }
6579 }
adam radford229fe472014-03-10 02:51:56 -07006580 }
6581 doscan = 0;
6582 }
6583 break;
6584 case MR_EVT_LD_CREATED:
6585 if (!instance->requestorId ||
6586 (instance->requestorId &&
6587 megasas_get_ld_vf_affiliation(instance, 0))) {
6588 if (megasas_ld_list_query(instance,
6589 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6590 megasas_get_ld_list(instance);
6591 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6592 for (j = 0;
6593 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6594 j++) {
6595 ld_index =
6596 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6597
6598 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6599
6600 if (instance->ld_ids[ld_index]
6601 != 0xff) {
6602 if (!sdev1)
6603 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6604 }
6605 if (sdev1)
6606 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006607 }
6608 }
adam radford229fe472014-03-10 02:51:56 -07006609 doscan = 0;
Yang, Boc9786842009-12-06 08:39:25 -07006610 }
Yang, Boc9786842009-12-06 08:39:25 -07006611 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006612 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07006613 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08006614 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006615 doscan = 1;
6616 break;
6617 default:
6618 doscan = 0;
6619 break;
6620 }
6621 } else {
6622 printk(KERN_ERR "invalid evt_detail!\n");
6623 kfree(ev);
6624 return;
6625 }
6626
6627 if (doscan) {
adam radford229fe472014-03-10 02:51:56 -07006628 printk(KERN_INFO "megaraid_sas: scanning for scsi%d...\n",
6629 instance->host->host_no);
Hannes Reinecke58968fc2014-01-16 11:25:36 +01006630 if (megasas_get_pd_list(instance) == 0) {
6631 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6632 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6633 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6634 sdev1 = scsi_device_lookup(host, i, j, 0);
6635 if (instance->pd_list[pd_index].driveState ==
6636 MR_PD_STATE_SYSTEM) {
6637 if (!sdev1) {
6638 scsi_add_device(host, i, j, 0);
6639 }
6640 if (sdev1)
6641 scsi_device_put(sdev1);
6642 } else {
6643 if (sdev1) {
6644 scsi_remove_device(sdev1);
6645 scsi_device_put(sdev1);
6646 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006647 }
6648 }
6649 }
6650 }
Yang, Boc9786842009-12-06 08:39:25 -07006651
adam radford229fe472014-03-10 02:51:56 -07006652 if (!instance->requestorId ||
6653 (instance->requestorId &&
6654 megasas_get_ld_vf_affiliation(instance, 0))) {
6655 if (megasas_ld_list_query(instance,
6656 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6657 megasas_get_ld_list(instance);
6658 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6659 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6660 j++) {
6661 ld_index =
6662 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
Yang, Boc9786842009-12-06 08:39:25 -07006663
adam radford229fe472014-03-10 02:51:56 -07006664 sdev1 = scsi_device_lookup(host,
6665 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6666 if (instance->ld_ids[ld_index]
6667 != 0xff) {
6668 if (!sdev1)
6669 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6670 else
6671 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07006672 } else {
adam radford229fe472014-03-10 02:51:56 -07006673 if (sdev1) {
6674 scsi_remove_device(sdev1);
6675 scsi_device_put(sdev1);
6676 }
Yang, Boc9786842009-12-06 08:39:25 -07006677 }
6678 }
6679 }
6680 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006681 }
6682
6683 if ( instance->aen_cmd != NULL ) {
6684 kfree(ev);
6685 return ;
6686 }
6687
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306688 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006689
6690 /* Register AEN with FW for latest sequence number plus 1 */
6691 class_locale.members.reserved = 0;
6692 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6693 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6694 mutex_lock(&instance->aen_mutex);
6695 error = megasas_register_aen(instance, seq_num,
6696 class_locale.word);
6697 mutex_unlock(&instance->aen_mutex);
6698
6699 if (error)
6700 printk(KERN_ERR "register aen failed error %x\n", error);
6701
6702 kfree(ev);
6703}
6704
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006705/**
6706 * megasas_init - Driver load entry point
6707 */
6708static int __init megasas_init(void)
6709{
6710 int rval;
6711
6712 /*
6713 * Announce driver version and other information
6714 */
6715 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
6716 MEGASAS_EXT_VERSION);
6717
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07006718 spin_lock_init(&poll_aen_lock);
6719
Yang, Bo72c4fd32009-10-06 14:20:59 -06006720 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06006721 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006722
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006723 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6724
6725 /*
6726 * Register character device node
6727 */
6728 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6729
6730 if (rval < 0) {
6731 printk(KERN_DEBUG "megasas: failed to open device node\n");
6732 return rval;
6733 }
6734
6735 megasas_mgmt_majorno = rval;
6736
6737 /*
6738 * Register ourselves as PCI hotplug module
6739 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00006740 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006741
6742 if (rval) {
6743 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04006744 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006745 }
6746
Jeff Garzik83aabc12006-10-04 06:34:03 -04006747 rval = driver_create_file(&megasas_pci_driver.driver,
6748 &driver_attr_version);
6749 if (rval)
6750 goto err_dcf_attr_ver;
6751 rval = driver_create_file(&megasas_pci_driver.driver,
6752 &driver_attr_release_date);
6753 if (rval)
6754 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06006755
6756 rval = driver_create_file(&megasas_pci_driver.driver,
6757 &driver_attr_support_poll_for_event);
6758 if (rval)
6759 goto err_dcf_support_poll_for_event;
6760
Jeff Garzik83aabc12006-10-04 06:34:03 -04006761 rval = driver_create_file(&megasas_pci_driver.driver,
6762 &driver_attr_dbg_lvl);
6763 if (rval)
6764 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05006765 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006766 &driver_attr_support_device_change);
6767 if (rval)
6768 goto err_dcf_support_device_change;
6769
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006770 return rval;
bo yangad84db22007-11-09 04:40:16 -05006771
Yang, Bo837f5fe2010-10-11 06:59:20 -06006772err_dcf_support_device_change:
6773 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05006774 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006775err_dcf_dbg_lvl:
6776 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06006777 &driver_attr_support_poll_for_event);
6778
6779err_dcf_support_poll_for_event:
6780 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04006781 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06006782
Jeff Garzik83aabc12006-10-04 06:34:03 -04006783err_dcf_rel_date:
6784 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6785err_dcf_attr_ver:
6786 pci_unregister_driver(&megasas_pci_driver);
6787err_pcidrv:
6788 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08006789 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006790}
6791
6792/**
6793 * megasas_exit - Driver unload entry point
6794 */
6795static void __exit megasas_exit(void)
6796{
Sumant Patro658dced2006-10-03 13:09:14 -07006797 driver_remove_file(&megasas_pci_driver.driver,
6798 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04006799 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06006800 &driver_attr_support_poll_for_event);
6801 driver_remove_file(&megasas_pci_driver.driver,
6802 &driver_attr_support_device_change);
6803 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04006804 &driver_attr_release_date);
6805 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006806
6807 pci_unregister_driver(&megasas_pci_driver);
6808 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6809}
6810
6811module_init(megasas_init);
6812module_exit(megasas_exit);