blob: 0a743a5d16477a5e168f02a77a99e90f201caac3 [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 radford2b4857c2013-08-31 16:54:28 -070021 * Version : 06.700.06.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 radfordc5daa6a2012-07-17 18:20:03 -070078static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
79module_param(throttlequeuedepth, int, S_IRUGO);
80MODULE_PARM_DESC(throttlequeuedepth,
81 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
82
adam radfordc007b8b2012-07-17 18:20:24 -070083int resetwaittime = MEGASAS_RESET_WAIT_TIME;
84module_param(resetwaittime, int, S_IRUGO);
85MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
86 "before resetting adapter. Default: 180");
87
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040088MODULE_LICENSE("GPL");
89MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080090MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050091MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040092
adam radford058a8fa2011-10-08 18:14:27 -070093int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -040094static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -070095static int megasas_ld_list_query(struct megasas_instance *instance,
96 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -040097static int megasas_issue_init_mfi(struct megasas_instance *instance);
98static int megasas_register_aen(struct megasas_instance *instance,
99 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400100/*
101 * PCI ID table for all supported controllers
102 */
103static struct pci_device_id megasas_pci_table[] = {
104
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
106 /* xscale IOP */
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
108 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
110 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
112 /* gen2*/
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
114 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
116 /* skinny*/
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
118 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200119 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
120 /* xscale IOP, vega */
121 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
122 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
124 /* Fusion */
adam radford36807e62011-10-08 18:15:06 -0700125 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
126 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530127 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
128 /* Fury */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200129 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400130};
131
132MODULE_DEVICE_TABLE(pci, megasas_pci_table);
133
134static int megasas_mgmt_majorno;
135static struct megasas_mgmt_info megasas_mgmt_info;
136static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100137static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400138
Yang, Boc3518832009-10-06 14:18:02 -0600139static int megasas_poll_wait_aen;
140static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600141static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800142u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600143static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700144
Yang, Boc3518832009-10-06 14:18:02 -0600145/* define lock for aen poll */
146spinlock_t poll_aen_lock;
147
adam radford9c915a82010-12-21 13:34:31 -0800148void
bo yang7343eb62007-11-09 04:35:44 -0500149megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
150 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800151static u32
152megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
153static int
154megasas_adp_reset_gen2(struct megasas_instance *instance,
155 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800156static irqreturn_t megasas_isr(int irq, void *devp);
157static u32
158megasas_init_adapter_mfi(struct megasas_instance *instance);
159u32
160megasas_build_and_issue_cmd(struct megasas_instance *instance,
161 struct scsi_cmnd *scmd);
162static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800163void
164megasas_release_fusion(struct megasas_instance *instance);
165int
166megasas_ioc_init_fusion(struct megasas_instance *instance);
167void
168megasas_free_cmds_fusion(struct megasas_instance *instance);
169u8
170megasas_get_map_info(struct megasas_instance *instance);
171int
172megasas_sync_map_info(struct megasas_instance *instance);
173int
174wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
175void megasas_reset_reply_desc(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -0800176int megasas_reset_fusion(struct Scsi_Host *shost);
177void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800178
179void
180megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
181{
182 instance->instancet->fire_cmd(instance,
183 cmd->frame_phys_addr, 0, instance->reg_set);
184}
bo yang7343eb62007-11-09 04:35:44 -0500185
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400186/**
187 * megasas_get_cmd - Get a command from the free pool
188 * @instance: Adapter soft state
189 *
190 * Returns a free command from the pool
191 */
adam radford9c915a82010-12-21 13:34:31 -0800192struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400193 *instance)
194{
195 unsigned long flags;
196 struct megasas_cmd *cmd = NULL;
197
198 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
199
200 if (!list_empty(&instance->cmd_pool)) {
201 cmd = list_entry((&instance->cmd_pool)->next,
202 struct megasas_cmd, list);
203 list_del_init(&cmd->list);
204 } else {
205 printk(KERN_ERR "megasas: Command pool empty!\n");
206 }
207
208 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
209 return cmd;
210}
211
212/**
213 * megasas_return_cmd - Return a cmd to free command pool
214 * @instance: Adapter soft state
215 * @cmd: Command packet to be returned to free command pool
216 */
adam radford9c915a82010-12-21 13:34:31 -0800217inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400218megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
219{
220 unsigned long flags;
221
222 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
223
224 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800225 cmd->frame_count = 0;
adam radforde5f93a32011-10-08 18:15:19 -0700226 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
227 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530228 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
adam radforde5f93a32011-10-08 18:15:19 -0700229 (reset_devices))
230 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400231 list_add_tail(&cmd->list, &instance->cmd_pool);
232
233 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
234}
235
Sumant Patro1341c932006-01-25 12:02:40 -0800236
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400237/**
adam radford0d490162010-12-14 19:17:17 -0800238* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800239* (deviceid : 1064R, PERC5) controllers
240*/
241
242/**
243 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400244 * @regs: MFI register set
245 */
246static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530247megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400248{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530249 struct megasas_register_set __iomem *regs;
250 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400251 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400252
253 /* Dummy readl to force pci flush */
254 readl(&regs->outbound_intr_mask);
255}
256
257/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700258 * megasas_disable_intr_xscale -Disables interrupt
259 * @regs: MFI register set
260 */
261static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530262megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700263{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530264 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700265 u32 mask = 0x1f;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530266 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700267 writel(mask, &regs->outbound_intr_mask);
268 /* Dummy readl to force pci flush */
269 readl(&regs->outbound_intr_mask);
270}
271
272/**
Sumant Patro1341c932006-01-25 12:02:40 -0800273 * megasas_read_fw_status_reg_xscale - returns the current FW status value
274 * @regs: MFI register set
275 */
276static u32
277megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
278{
279 return readl(&(regs)->outbound_msg_0);
280}
281/**
282 * megasas_clear_interrupt_xscale - Check & clear interrupt
283 * @regs: MFI register set
284 */
adam radford0d490162010-12-14 19:17:17 -0800285static int
Sumant Patro1341c932006-01-25 12:02:40 -0800286megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
287{
288 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400289 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800290 /*
291 * Check if it is our interrupt
292 */
293 status = readl(&regs->outbound_intr_status);
294
bo yang39a98552010-09-22 22:36:29 -0400295 if (status & MFI_OB_INTR_STATUS_MASK)
296 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
297 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
298 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800299
300 /*
301 * Clear the interrupt by writing back the same value
302 */
bo yang39a98552010-09-22 22:36:29 -0400303 if (mfiStatus)
304 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800305
Yang, Bo06f579d2008-08-10 12:42:37 -0700306 /* Dummy readl to force pci flush */
307 readl(&regs->outbound_intr_status);
308
bo yang39a98552010-09-22 22:36:29 -0400309 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800310}
311
312/**
313 * megasas_fire_cmd_xscale - Sends command to the FW
314 * @frame_phys_addr : Physical address of cmd
315 * @frame_count : Number of frames for the command
316 * @regs : MFI register set
317 */
adam radford0d490162010-12-14 19:17:17 -0800318static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600319megasas_fire_cmd_xscale(struct megasas_instance *instance,
320 dma_addr_t frame_phys_addr,
321 u32 frame_count,
322 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800323{
bo yang39a98552010-09-22 22:36:29 -0400324 unsigned long flags;
325 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800326 writel((frame_phys_addr >> 3)|(frame_count),
327 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400328 spin_unlock_irqrestore(&instance->hba_lock, flags);
329}
330
331/**
332 * megasas_adp_reset_xscale - For controller reset
333 * @regs: MFI register set
334 */
335static int
336megasas_adp_reset_xscale(struct megasas_instance *instance,
337 struct megasas_register_set __iomem *regs)
338{
339 u32 i;
340 u32 pcidata;
341 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
342
343 for (i = 0; i < 3; i++)
344 msleep(1000); /* sleep for 3 secs */
345 pcidata = 0;
346 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
347 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
348 if (pcidata & 0x2) {
349 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
350 pcidata &= ~0x2;
351 pci_write_config_dword(instance->pdev,
352 MFI_1068_PCSR_OFFSET, pcidata);
353
354 for (i = 0; i < 2; i++)
355 msleep(1000); /* need to wait 2 secs again */
356
357 pcidata = 0;
358 pci_read_config_dword(instance->pdev,
359 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
360 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
361 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
362 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
363 pcidata = 0;
364 pci_write_config_dword(instance->pdev,
365 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
366 }
367 }
368 return 0;
369}
370
371/**
372 * megasas_check_reset_xscale - For controller reset check
373 * @regs: MFI register set
374 */
375static int
376megasas_check_reset_xscale(struct megasas_instance *instance,
377 struct megasas_register_set __iomem *regs)
378{
bo yang39a98552010-09-22 22:36:29 -0400379
380 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530381 (le32_to_cpu(*instance->consumer) ==
382 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400383 return 1;
bo yang39a98552010-09-22 22:36:29 -0400384 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800385}
386
387static struct megasas_instance_template megasas_instance_template_xscale = {
388
389 .fire_cmd = megasas_fire_cmd_xscale,
390 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700391 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800392 .clear_intr = megasas_clear_intr_xscale,
393 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400394 .adp_reset = megasas_adp_reset_xscale,
395 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800396 .service_isr = megasas_isr,
397 .tasklet = megasas_complete_cmd_dpc,
398 .init_adapter = megasas_init_adapter_mfi,
399 .build_and_issue_cmd = megasas_build_and_issue_cmd,
400 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800401};
402
403/**
adam radford0d490162010-12-14 19:17:17 -0800404* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800405* to xscale (deviceid : 1064R, PERC5) controllers
406*/
407
408/**
adam radford0d490162010-12-14 19:17:17 -0800409* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800410* controllers
411*/
412
413/**
414 * megasas_enable_intr_ppc - Enables interrupts
415 * @regs: MFI register set
416 */
417static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530418megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800419{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530420 struct megasas_register_set __iomem *regs;
421 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800422 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800423
bo yang39a98552010-09-22 22:36:29 -0400424 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800425
426 /* Dummy readl to force pci flush */
427 readl(&regs->outbound_intr_mask);
428}
429
430/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700431 * megasas_disable_intr_ppc - Disable interrupt
432 * @regs: MFI register set
433 */
434static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530435megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700436{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530437 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700438 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530439 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700440 writel(mask, &regs->outbound_intr_mask);
441 /* Dummy readl to force pci flush */
442 readl(&regs->outbound_intr_mask);
443}
444
445/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800446 * megasas_read_fw_status_reg_ppc - returns the current FW status value
447 * @regs: MFI register set
448 */
449static u32
450megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
451{
452 return readl(&(regs)->outbound_scratch_pad);
453}
454
455/**
456 * megasas_clear_interrupt_ppc - Check & clear interrupt
457 * @regs: MFI register set
458 */
adam radford0d490162010-12-14 19:17:17 -0800459static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800460megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
461{
adam radford3cc6851f92011-05-11 18:34:52 -0700462 u32 status, mfiStatus = 0;
463
Sumant Patrof9876f02006-02-03 15:34:35 -0800464 /*
465 * Check if it is our interrupt
466 */
467 status = readl(&regs->outbound_intr_status);
468
adam radford3cc6851f92011-05-11 18:34:52 -0700469 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
470 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
471
472 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
473 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800474
475 /*
476 * Clear the interrupt by writing back the same value
477 */
478 writel(status, &regs->outbound_doorbell_clear);
479
Yang, Bo06f579d2008-08-10 12:42:37 -0700480 /* Dummy readl to force pci flush */
481 readl(&regs->outbound_doorbell_clear);
482
adam radford3cc6851f92011-05-11 18:34:52 -0700483 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800484}
adam radford3cc6851f92011-05-11 18:34:52 -0700485
Sumant Patrof9876f02006-02-03 15:34:35 -0800486/**
487 * megasas_fire_cmd_ppc - Sends command to the FW
488 * @frame_phys_addr : Physical address of cmd
489 * @frame_count : Number of frames for the command
490 * @regs : MFI register set
491 */
adam radford0d490162010-12-14 19:17:17 -0800492static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600493megasas_fire_cmd_ppc(struct megasas_instance *instance,
494 dma_addr_t frame_phys_addr,
495 u32 frame_count,
496 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800497{
bo yang39a98552010-09-22 22:36:29 -0400498 unsigned long flags;
499 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800500 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800501 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400502 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800503}
504
bo yang39a98552010-09-22 22:36:29 -0400505/**
bo yang39a98552010-09-22 22:36:29 -0400506 * megasas_check_reset_ppc - For controller reset check
507 * @regs: MFI register set
508 */
509static int
510megasas_check_reset_ppc(struct megasas_instance *instance,
511 struct megasas_register_set __iomem *regs)
512{
adam radford3cc6851f92011-05-11 18:34:52 -0700513 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
514 return 1;
515
bo yang39a98552010-09-22 22:36:29 -0400516 return 0;
517}
adam radford3cc6851f92011-05-11 18:34:52 -0700518
Sumant Patrof9876f02006-02-03 15:34:35 -0800519static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800520
Sumant Patrof9876f02006-02-03 15:34:35 -0800521 .fire_cmd = megasas_fire_cmd_ppc,
522 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700523 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800524 .clear_intr = megasas_clear_intr_ppc,
525 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700526 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400527 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800528 .service_isr = megasas_isr,
529 .tasklet = megasas_complete_cmd_dpc,
530 .init_adapter = megasas_init_adapter_mfi,
531 .build_and_issue_cmd = megasas_build_and_issue_cmd,
532 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800533};
534
535/**
Yang, Bo87911122009-10-06 14:31:54 -0600536 * megasas_enable_intr_skinny - Enables interrupts
537 * @regs: MFI register set
538 */
539static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530540megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600541{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530542 struct megasas_register_set __iomem *regs;
543 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600544 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
545
546 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
547
548 /* Dummy readl to force pci flush */
549 readl(&regs->outbound_intr_mask);
550}
551
552/**
553 * megasas_disable_intr_skinny - Disables interrupt
554 * @regs: MFI register set
555 */
556static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530557megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600558{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530559 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600560 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530561 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600562 writel(mask, &regs->outbound_intr_mask);
563 /* Dummy readl to force pci flush */
564 readl(&regs->outbound_intr_mask);
565}
566
567/**
568 * megasas_read_fw_status_reg_skinny - returns the current FW status value
569 * @regs: MFI register set
570 */
571static u32
572megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
573{
574 return readl(&(regs)->outbound_scratch_pad);
575}
576
577/**
578 * megasas_clear_interrupt_skinny - Check & clear interrupt
579 * @regs: MFI register set
580 */
581static int
582megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
583{
584 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800585 u32 mfiStatus = 0;
586
Yang, Bo87911122009-10-06 14:31:54 -0600587 /*
588 * Check if it is our interrupt
589 */
590 status = readl(&regs->outbound_intr_status);
591
592 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400593 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600594 }
595
596 /*
adam radfordebf054b2011-02-24 20:57:15 -0800597 * Check if it is our interrupt
598 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600599 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800600 MFI_STATE_FAULT) {
601 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
602 } else
603 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
604
605 /*
Yang, Bo87911122009-10-06 14:31:54 -0600606 * Clear the interrupt by writing back the same value
607 */
608 writel(status, &regs->outbound_intr_status);
609
610 /*
611 * dummy read to flush PCI
612 */
613 readl(&regs->outbound_intr_status);
614
adam radfordebf054b2011-02-24 20:57:15 -0800615 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600616}
617
618/**
619 * megasas_fire_cmd_skinny - Sends command to the FW
620 * @frame_phys_addr : Physical address of cmd
621 * @frame_count : Number of frames for the command
622 * @regs : MFI register set
623 */
624static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600625megasas_fire_cmd_skinny(struct megasas_instance *instance,
626 dma_addr_t frame_phys_addr,
627 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600628 struct megasas_register_set __iomem *regs)
629{
Yang, Bo0c79e682009-10-06 14:47:35 -0600630 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400631 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530632 writel(upper_32_bits(frame_phys_addr),
633 &(regs)->inbound_high_queue_port);
634 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
635 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400636 spin_unlock_irqrestore(&instance->hba_lock, flags);
637}
638
639/**
bo yang39a98552010-09-22 22:36:29 -0400640 * megasas_check_reset_skinny - For controller reset check
641 * @regs: MFI register set
642 */
643static int
644megasas_check_reset_skinny(struct megasas_instance *instance,
645 struct megasas_register_set __iomem *regs)
646{
adam radford3cc6851f92011-05-11 18:34:52 -0700647 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
648 return 1;
649
bo yang39a98552010-09-22 22:36:29 -0400650 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600651}
652
653static struct megasas_instance_template megasas_instance_template_skinny = {
654
655 .fire_cmd = megasas_fire_cmd_skinny,
656 .enable_intr = megasas_enable_intr_skinny,
657 .disable_intr = megasas_disable_intr_skinny,
658 .clear_intr = megasas_clear_intr_skinny,
659 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800660 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400661 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800662 .service_isr = megasas_isr,
663 .tasklet = megasas_complete_cmd_dpc,
664 .init_adapter = megasas_init_adapter_mfi,
665 .build_and_issue_cmd = megasas_build_and_issue_cmd,
666 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600667};
668
669
670/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700671* The following functions are defined for gen2 (deviceid : 0x78 0x79)
672* controllers
673*/
674
675/**
676 * megasas_enable_intr_gen2 - Enables interrupts
677 * @regs: MFI register set
678 */
679static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530680megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700681{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530682 struct megasas_register_set __iomem *regs;
683 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700684 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
685
686 /* write ~0x00000005 (4 & 1) to the intr mask*/
687 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
688
689 /* Dummy readl to force pci flush */
690 readl(&regs->outbound_intr_mask);
691}
692
693/**
694 * megasas_disable_intr_gen2 - Disables interrupt
695 * @regs: MFI register set
696 */
697static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530698megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700699{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530700 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700701 u32 mask = 0xFFFFFFFF;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530702 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700703 writel(mask, &regs->outbound_intr_mask);
704 /* Dummy readl to force pci flush */
705 readl(&regs->outbound_intr_mask);
706}
707
708/**
709 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
710 * @regs: MFI register set
711 */
712static u32
713megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
714{
715 return readl(&(regs)->outbound_scratch_pad);
716}
717
718/**
719 * megasas_clear_interrupt_gen2 - Check & clear interrupt
720 * @regs: MFI register set
721 */
722static int
723megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
724{
725 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400726 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700727 /*
728 * Check if it is our interrupt
729 */
730 status = readl(&regs->outbound_intr_status);
731
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530732 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400733 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
734 }
735 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
736 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
737 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700738
739 /*
740 * Clear the interrupt by writing back the same value
741 */
bo yang39a98552010-09-22 22:36:29 -0400742 if (mfiStatus)
743 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700744
745 /* Dummy readl to force pci flush */
746 readl(&regs->outbound_intr_status);
747
bo yang39a98552010-09-22 22:36:29 -0400748 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700749}
750/**
751 * megasas_fire_cmd_gen2 - Sends command to the FW
752 * @frame_phys_addr : Physical address of cmd
753 * @frame_count : Number of frames for the command
754 * @regs : MFI register set
755 */
756static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600757megasas_fire_cmd_gen2(struct megasas_instance *instance,
758 dma_addr_t frame_phys_addr,
759 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700760 struct megasas_register_set __iomem *regs)
761{
bo yang39a98552010-09-22 22:36:29 -0400762 unsigned long flags;
763 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700764 writel((frame_phys_addr | (frame_count<<1))|1,
765 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400766 spin_unlock_irqrestore(&instance->hba_lock, flags);
767}
768
769/**
770 * megasas_adp_reset_gen2 - For controller reset
771 * @regs: MFI register set
772 */
773static int
774megasas_adp_reset_gen2(struct megasas_instance *instance,
775 struct megasas_register_set __iomem *reg_set)
776{
777 u32 retry = 0 ;
778 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800779 u32 *seq_offset = &reg_set->seq_offset;
780 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400781
adam radfordebf054b2011-02-24 20:57:15 -0800782 if (instance->instancet == &megasas_instance_template_skinny) {
783 seq_offset = &reg_set->fusion_seq_offset;
784 hostdiag_offset = &reg_set->fusion_host_diag;
785 }
786
787 writel(0, seq_offset);
788 writel(4, seq_offset);
789 writel(0xb, seq_offset);
790 writel(2, seq_offset);
791 writel(7, seq_offset);
792 writel(0xd, seq_offset);
793
bo yang39a98552010-09-22 22:36:29 -0400794 msleep(1000);
795
adam radfordebf054b2011-02-24 20:57:15 -0800796 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400797
798 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
799 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800800 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400801 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
802 retry, HostDiag);
803
804 if (retry++ >= 100)
805 return 1;
806
807 }
808
809 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
810
adam radfordebf054b2011-02-24 20:57:15 -0800811 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400812
813 ssleep(10);
814
adam radfordebf054b2011-02-24 20:57:15 -0800815 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400816 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
817 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800818 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400819 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
820 retry, HostDiag);
821
822 if (retry++ >= 1000)
823 return 1;
824
825 }
826 return 0;
827}
828
829/**
830 * megasas_check_reset_gen2 - For controller reset check
831 * @regs: MFI register set
832 */
833static int
834megasas_check_reset_gen2(struct megasas_instance *instance,
835 struct megasas_register_set __iomem *regs)
836{
Yang, Bo707e09b2010-10-12 07:20:27 -0600837 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
838 return 1;
839 }
840
bo yang39a98552010-09-22 22:36:29 -0400841 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700842}
843
844static struct megasas_instance_template megasas_instance_template_gen2 = {
845
846 .fire_cmd = megasas_fire_cmd_gen2,
847 .enable_intr = megasas_enable_intr_gen2,
848 .disable_intr = megasas_disable_intr_gen2,
849 .clear_intr = megasas_clear_intr_gen2,
850 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400851 .adp_reset = megasas_adp_reset_gen2,
852 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800853 .service_isr = megasas_isr,
854 .tasklet = megasas_complete_cmd_dpc,
855 .init_adapter = megasas_init_adapter_mfi,
856 .build_and_issue_cmd = megasas_build_and_issue_cmd,
857 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700858};
859
860/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800861* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400862* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800863*/
864
adam radford9c915a82010-12-21 13:34:31 -0800865/*
866 * Template added for TB (Fusion)
867 */
868extern struct megasas_instance_template megasas_instance_template_fusion;
869
Sumant Patrof9876f02006-02-03 15:34:35 -0800870/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400871 * megasas_issue_polled - Issues a polling command
872 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800873 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400874 *
875 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
876 */
adam radford9c915a82010-12-21 13:34:31 -0800877int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400878megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
879{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400880
881 struct megasas_header *frame_hdr = &cmd->frame->hdr;
882
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530883 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
884 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400885
886 /*
887 * Issue the frame using inbound queue port
888 */
adam radford9c915a82010-12-21 13:34:31 -0800889 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400890
891 /*
892 * Wait for cmd_status to change
893 */
adam radford9c915a82010-12-21 13:34:31 -0800894 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400895}
896
897/**
898 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
899 * @instance: Adapter soft state
900 * @cmd: Command to be issued
901 *
902 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700903 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400904 * Used to issue ioctl commands.
905 */
906static int
907megasas_issue_blocked_cmd(struct megasas_instance *instance,
908 struct megasas_cmd *cmd)
909{
910 cmd->cmd_status = ENODATA;
911
adam radford9c915a82010-12-21 13:34:31 -0800912 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400913
bo yang39a98552010-09-22 22:36:29 -0400914 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400915
916 return 0;
917}
918
919/**
920 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
921 * @instance: Adapter soft state
922 * @cmd_to_abort: Previously issued cmd to be aborted
923 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100924 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400925 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700926 * cmd and waits for return status.
927 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400928 */
929static int
930megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
931 struct megasas_cmd *cmd_to_abort)
932{
933 struct megasas_cmd *cmd;
934 struct megasas_abort_frame *abort_fr;
935
936 cmd = megasas_get_cmd(instance);
937
938 if (!cmd)
939 return -1;
940
941 abort_fr = &cmd->frame->abort;
942
943 /*
944 * Prepare and issue the abort frame
945 */
946 abort_fr->cmd = MFI_CMD_ABORT;
947 abort_fr->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530948 abort_fr->flags = cpu_to_le16(0);
949 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
950 abort_fr->abort_mfi_phys_addr_lo =
951 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
952 abort_fr->abort_mfi_phys_addr_hi =
953 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400954
955 cmd->sync_cmd = 1;
956 cmd->cmd_status = 0xFF;
957
adam radford9c915a82010-12-21 13:34:31 -0800958 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400959
960 /*
961 * Wait for this cmd to complete
962 */
bo yang39a98552010-09-22 22:36:29 -0400963 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
964 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400965
966 megasas_return_cmd(instance, cmd);
967 return 0;
968}
969
970/**
971 * megasas_make_sgl32 - Prepares 32-bit SGL
972 * @instance: Adapter soft state
973 * @scp: SCSI command from the mid-layer
974 * @mfi_sgl: SGL to be filled in
975 *
976 * If successful, this function returns the number of SG elements. Otherwise,
977 * it returnes -1.
978 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800979static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400980megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
981 union megasas_sgl *mfi_sgl)
982{
983 int i;
984 int sge_count;
985 struct scatterlist *os_sgl;
986
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900987 sge_count = scsi_dma_map(scp);
988 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900990 if (sge_count) {
991 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530992 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
993 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900994 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400995 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996 return sge_count;
997}
998
999/**
1000 * megasas_make_sgl64 - Prepares 64-bit SGL
1001 * @instance: Adapter soft state
1002 * @scp: SCSI command from the mid-layer
1003 * @mfi_sgl: SGL to be filled in
1004 *
1005 * If successful, this function returns the number of SG elements. Otherwise,
1006 * it returnes -1.
1007 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001008static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001009megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1010 union megasas_sgl *mfi_sgl)
1011{
1012 int i;
1013 int sge_count;
1014 struct scatterlist *os_sgl;
1015
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001016 sge_count = scsi_dma_map(scp);
1017 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001018
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001019 if (sge_count) {
1020 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301021 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1022 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001023 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001024 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001025 return sge_count;
1026}
1027
Yang, Bof4c9a132009-10-06 14:43:28 -06001028/**
1029 * megasas_make_sgl_skinny - Prepares IEEE SGL
1030 * @instance: Adapter soft state
1031 * @scp: SCSI command from the mid-layer
1032 * @mfi_sgl: SGL to be filled in
1033 *
1034 * If successful, this function returns the number of SG elements. Otherwise,
1035 * it returnes -1.
1036 */
1037static int
1038megasas_make_sgl_skinny(struct megasas_instance *instance,
1039 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1040{
1041 int i;
1042 int sge_count;
1043 struct scatterlist *os_sgl;
1044
1045 sge_count = scsi_dma_map(scp);
1046
1047 if (sge_count) {
1048 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301049 mfi_sgl->sge_skinny[i].length =
1050 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001051 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301052 cpu_to_le64(sg_dma_address(os_sgl));
1053 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001054 }
1055 }
1056 return sge_count;
1057}
1058
Sumant Patrob1df99d2006-10-03 12:40:47 -07001059 /**
1060 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001061 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001062 * @sge_count : number of sg elements
1063 *
1064 * Returns the number of frames required for numnber of sge's (sge_count)
1065 */
1066
Yang, Bof4c9a132009-10-06 14:43:28 -06001067static u32 megasas_get_frame_count(struct megasas_instance *instance,
1068 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001069{
1070 int num_cnt;
1071 int sge_bytes;
1072 u32 sge_sz;
1073 u32 frame_count=0;
1074
1075 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1076 sizeof(struct megasas_sge32);
1077
Yang, Bof4c9a132009-10-06 14:43:28 -06001078 if (instance->flag_ieee) {
1079 sge_sz = sizeof(struct megasas_sge_skinny);
1080 }
1081
Sumant Patrob1df99d2006-10-03 12:40:47 -07001082 /*
bo yangd532dbe2008-03-17 03:36:43 -04001083 * Main frame can contain 2 SGEs for 64-bit SGLs and
1084 * 3 SGEs for 32-bit SGLs for ldio &
1085 * 1 SGEs for 64-bit SGLs and
1086 * 2 SGEs for 32-bit SGLs for pthru frame
1087 */
1088 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001089 if (instance->flag_ieee == 1) {
1090 num_cnt = sge_count - 1;
1091 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001092 num_cnt = sge_count - 1;
1093 else
1094 num_cnt = sge_count - 2;
1095 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001096 if (instance->flag_ieee == 1) {
1097 num_cnt = sge_count - 1;
1098 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001099 num_cnt = sge_count - 2;
1100 else
1101 num_cnt = sge_count - 3;
1102 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001103
1104 if(num_cnt>0){
1105 sge_bytes = sge_sz * num_cnt;
1106
1107 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1108 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1109 }
1110 /* Main frame */
1111 frame_count +=1;
1112
1113 if (frame_count > 7)
1114 frame_count = 8;
1115 return frame_count;
1116}
1117
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001118/**
1119 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1120 * @instance: Adapter soft state
1121 * @scp: SCSI command
1122 * @cmd: Command to be prepared in
1123 *
1124 * This function prepares CDB commands. These are typcially pass-through
1125 * commands to the devices.
1126 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001127static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001128megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1129 struct megasas_cmd *cmd)
1130{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001131 u32 is_logical;
1132 u32 device_id;
1133 u16 flags = 0;
1134 struct megasas_pthru_frame *pthru;
1135
1136 is_logical = MEGASAS_IS_LOGICAL(scp);
1137 device_id = MEGASAS_DEV_INDEX(instance, scp);
1138 pthru = (struct megasas_pthru_frame *)cmd->frame;
1139
1140 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1141 flags = MFI_FRAME_DIR_WRITE;
1142 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1143 flags = MFI_FRAME_DIR_READ;
1144 else if (scp->sc_data_direction == PCI_DMA_NONE)
1145 flags = MFI_FRAME_DIR_NONE;
1146
Yang, Bof4c9a132009-10-06 14:43:28 -06001147 if (instance->flag_ieee == 1) {
1148 flags |= MFI_FRAME_IEEE;
1149 }
1150
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001151 /*
1152 * Prepare the DCDB frame
1153 */
1154 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1155 pthru->cmd_status = 0x0;
1156 pthru->scsi_status = 0x0;
1157 pthru->target_id = device_id;
1158 pthru->lun = scp->device->lun;
1159 pthru->cdb_len = scp->cmd_len;
1160 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001161 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301162 pthru->flags = cpu_to_le16(flags);
1163 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001164
1165 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1166
1167 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001168 * If the command is for the tape device, set the
1169 * pthru timeout to the os layer timeout value.
1170 */
1171 if (scp->device->type == TYPE_TAPE) {
1172 if ((scp->request->timeout / HZ) > 0xFFFF)
1173 pthru->timeout = 0xFFFF;
1174 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301175 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001176 }
1177
1178 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001179 * Construct SGL
1180 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001181 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301182 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001183 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1184 &pthru->sgl);
1185 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301186 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001187 pthru->sge_count = megasas_make_sgl64(instance, scp,
1188 &pthru->sgl);
1189 } else
1190 pthru->sge_count = megasas_make_sgl32(instance, scp,
1191 &pthru->sgl);
1192
Yang, Bobdc6fb82009-12-06 08:30:19 -07001193 if (pthru->sge_count > instance->max_num_sge) {
1194 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1195 pthru->sge_count);
1196 return 0;
1197 }
1198
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001199 /*
1200 * Sense info specific
1201 */
1202 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301203 pthru->sense_buf_phys_addr_hi =
1204 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1205 pthru->sense_buf_phys_addr_lo =
1206 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001207
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001208 /*
1209 * Compute the total number of frames this command consumes. FW uses
1210 * this number to pull sufficient number of frames from host memory.
1211 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001212 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001213 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001214
1215 return cmd->frame_count;
1216}
1217
1218/**
1219 * megasas_build_ldio - Prepares IOs to logical devices
1220 * @instance: Adapter soft state
1221 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001222 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001223 *
1224 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1225 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001226static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001227megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1228 struct megasas_cmd *cmd)
1229{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001230 u32 device_id;
1231 u8 sc = scp->cmnd[0];
1232 u16 flags = 0;
1233 struct megasas_io_frame *ldio;
1234
1235 device_id = MEGASAS_DEV_INDEX(instance, scp);
1236 ldio = (struct megasas_io_frame *)cmd->frame;
1237
1238 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1239 flags = MFI_FRAME_DIR_WRITE;
1240 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1241 flags = MFI_FRAME_DIR_READ;
1242
Yang, Bof4c9a132009-10-06 14:43:28 -06001243 if (instance->flag_ieee == 1) {
1244 flags |= MFI_FRAME_IEEE;
1245 }
1246
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001247 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001248 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001249 */
1250 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1251 ldio->cmd_status = 0x0;
1252 ldio->scsi_status = 0x0;
1253 ldio->target_id = device_id;
1254 ldio->timeout = 0;
1255 ldio->reserved_0 = 0;
1256 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301257 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001258 ldio->start_lba_hi = 0;
1259 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1260
1261 /*
1262 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1263 */
1264 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301265 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1266 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1267 ((u32) scp->cmnd[2] << 8) |
1268 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001269
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301270 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001271 }
1272
1273 /*
1274 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1275 */
1276 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301277 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1278 ((u32) scp->cmnd[7] << 8));
1279 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1280 ((u32) scp->cmnd[3] << 16) |
1281 ((u32) scp->cmnd[4] << 8) |
1282 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001283 }
1284
1285 /*
1286 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1287 */
1288 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301289 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1290 ((u32) scp->cmnd[7] << 16) |
1291 ((u32) scp->cmnd[8] << 8) |
1292 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001293
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301294 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1295 ((u32) scp->cmnd[3] << 16) |
1296 ((u32) scp->cmnd[4] << 8) |
1297 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001298 }
1299
1300 /*
1301 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1302 */
1303 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301304 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1305 ((u32) scp->cmnd[11] << 16) |
1306 ((u32) scp->cmnd[12] << 8) |
1307 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001308
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301309 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1310 ((u32) scp->cmnd[7] << 16) |
1311 ((u32) scp->cmnd[8] << 8) |
1312 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001313
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301314 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1315 ((u32) scp->cmnd[3] << 16) |
1316 ((u32) scp->cmnd[4] << 8) |
1317 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001318
1319 }
1320
1321 /*
1322 * Construct SGL
1323 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001324 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301325 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001326 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1327 &ldio->sgl);
1328 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301329 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001330 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1331 } else
1332 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1333
Yang, Bobdc6fb82009-12-06 08:30:19 -07001334 if (ldio->sge_count > instance->max_num_sge) {
1335 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1336 ldio->sge_count);
1337 return 0;
1338 }
1339
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001340 /*
1341 * Sense info specific
1342 */
1343 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1344 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301345 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001346
Sumant Patrob1df99d2006-10-03 12:40:47 -07001347 /*
1348 * Compute the total number of frames this command consumes. FW uses
1349 * this number to pull sufficient number of frames from host memory.
1350 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001351 cmd->frame_count = megasas_get_frame_count(instance,
1352 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001353
1354 return cmd->frame_count;
1355}
1356
1357/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001358 * megasas_is_ldio - Checks if the cmd is for logical drive
1359 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001360 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001361 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001362 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001363 */
adam radford9c915a82010-12-21 13:34:31 -08001364inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001365{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001366 if (!MEGASAS_IS_LOGICAL(cmd))
1367 return 0;
1368 switch (cmd->cmnd[0]) {
1369 case READ_10:
1370 case WRITE_10:
1371 case READ_12:
1372 case WRITE_12:
1373 case READ_6:
1374 case WRITE_6:
1375 case READ_16:
1376 case WRITE_16:
1377 return 1;
1378 default:
1379 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001380 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001381}
1382
Sumant Patro658dced2006-10-03 13:09:14 -07001383 /**
1384 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1385 * in FW
1386 * @instance: Adapter soft state
1387 */
1388static inline void
1389megasas_dump_pending_frames(struct megasas_instance *instance)
1390{
1391 struct megasas_cmd *cmd;
1392 int i,n;
1393 union megasas_sgl *mfi_sgl;
1394 struct megasas_io_frame *ldio;
1395 struct megasas_pthru_frame *pthru;
1396 u32 sgcount;
1397 u32 max_cmd = instance->max_fw_cmds;
1398
1399 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1400 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1401 if (IS_DMA64)
1402 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1403 else
1404 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1405
1406 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1407 for (i = 0; i < max_cmd; i++) {
1408 cmd = instance->cmd_list[i];
1409 if(!cmd->scmd)
1410 continue;
1411 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1412 if (megasas_is_ldio(cmd->scmd)){
1413 ldio = (struct megasas_io_frame *)cmd->frame;
1414 mfi_sgl = &ldio->sgl;
1415 sgcount = ldio->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301416 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1417 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1418 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1419 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1420 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001421 }
1422 else {
1423 pthru = (struct megasas_pthru_frame *) cmd->frame;
1424 mfi_sgl = &pthru->sgl;
1425 sgcount = pthru->sge_count;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301426 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1427 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1428 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1429 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1430 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001431 }
1432 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1433 for (n = 0; n < sgcount; n++){
1434 if (IS_DMA64)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301435 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
1436 le32_to_cpu(mfi_sgl->sge64[n].length),
1437 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001438 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301439 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",
1440 le32_to_cpu(mfi_sgl->sge32[n].length),
1441 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001442 }
1443 }
1444 printk(KERN_ERR "\n");
1445 } /*for max_cmd*/
1446 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1447 for (i = 0; i < max_cmd; i++) {
1448
1449 cmd = instance->cmd_list[i];
1450
1451 if(cmd->sync_cmd == 1){
1452 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1453 }
1454 }
1455 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1456}
1457
adam radfordcd50ba82010-12-21 10:23:23 -08001458u32
1459megasas_build_and_issue_cmd(struct megasas_instance *instance,
1460 struct scsi_cmnd *scmd)
1461{
1462 struct megasas_cmd *cmd;
1463 u32 frame_count;
1464
1465 cmd = megasas_get_cmd(instance);
1466 if (!cmd)
1467 return SCSI_MLQUEUE_HOST_BUSY;
1468
1469 /*
1470 * Logical drive command
1471 */
1472 if (megasas_is_ldio(scmd))
1473 frame_count = megasas_build_ldio(instance, scmd, cmd);
1474 else
1475 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1476
1477 if (!frame_count)
1478 goto out_return_cmd;
1479
1480 cmd->scmd = scmd;
1481 scmd->SCp.ptr = (char *)cmd;
1482
1483 /*
1484 * Issue the command to the FW
1485 */
1486 atomic_inc(&instance->fw_outstanding);
1487
1488 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1489 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001490
1491 return 0;
1492out_return_cmd:
1493 megasas_return_cmd(instance, cmd);
1494 return 1;
1495}
1496
1497
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001498/**
1499 * megasas_queue_command - Queue entry point
1500 * @scmd: SCSI command to be queued
1501 * @done: Callback entry point
1502 */
1503static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001504megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001505{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001506 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001507 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001508
1509 instance = (struct megasas_instance *)
1510 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001511
bo yang39a98552010-09-22 22:36:29 -04001512 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001513 return SCSI_MLQUEUE_HOST_BUSY;
1514
bo yang39a98552010-09-22 22:36:29 -04001515 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301516
1517 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1518 spin_unlock_irqrestore(&instance->hba_lock, flags);
1519 scmd->result = DID_ERROR << 16;
1520 done(scmd);
1521 return 0;
1522 }
1523
bo yang39a98552010-09-22 22:36:29 -04001524 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1525 spin_unlock_irqrestore(&instance->hba_lock, flags);
1526 return SCSI_MLQUEUE_HOST_BUSY;
1527 }
1528
1529 spin_unlock_irqrestore(&instance->hba_lock, flags);
1530
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001531 scmd->scsi_done = done;
1532 scmd->result = 0;
1533
Sumant Patrocb59aa62006-01-25 11:53:25 -08001534 if (MEGASAS_IS_LOGICAL(scmd) &&
1535 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1536 scmd->result = DID_BAD_TARGET << 16;
1537 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001538 }
1539
Sumant Patro02b01e02007-02-14 13:00:55 -08001540 switch (scmd->cmnd[0]) {
1541 case SYNCHRONIZE_CACHE:
1542 /*
1543 * FW takes care of flush cache on its own
1544 * No need to send it down
1545 */
1546 scmd->result = DID_OK << 16;
1547 goto out_done;
1548 default:
1549 break;
1550 }
1551
adam radfordcd50ba82010-12-21 10:23:23 -08001552 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1553 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001554 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001555 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001556
1557 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001558
Sumant Patrocb59aa62006-01-25 11:53:25 -08001559 out_done:
1560 done(scmd);
1561 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001562}
1563
Jeff Garzikf2812332010-11-16 02:10:29 -05001564static DEF_SCSI_QCMD(megasas_queue_command)
1565
Yang, Bo044833b2009-10-06 14:33:06 -06001566static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1567{
1568 int i;
1569
1570 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1571
1572 if ((megasas_mgmt_info.instance[i]) &&
1573 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1574 return megasas_mgmt_info.instance[i];
1575 }
1576
1577 return NULL;
1578}
1579
Christoph Hellwig147aab62006-02-17 12:13:48 +01001580static int megasas_slave_configure(struct scsi_device *sdev)
1581{
Yang, Bo044833b2009-10-06 14:33:06 -06001582 u16 pd_index = 0;
1583 struct megasas_instance *instance ;
1584
1585 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001586
1587 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001588 * Don't export physical disk devices to the disk driver.
1589 *
1590 * FIXME: Currently we don't export them to the midlayer at all.
1591 * That will be fixed once LSI engineers have audited the
1592 * firmware for possible issues.
1593 */
1594 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1595 sdev->type == TYPE_DISK) {
1596 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1597 sdev->id;
1598 if (instance->pd_list[pd_index].driveState ==
1599 MR_PD_STATE_SYSTEM) {
1600 blk_queue_rq_timeout(sdev->request_queue,
1601 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1602 return 0;
1603 }
1604 return -ENXIO;
1605 }
1606
1607 /*
1608 * The RAID firmware may require extended timeouts.
1609 */
1610 blk_queue_rq_timeout(sdev->request_queue,
1611 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1612 return 0;
1613}
1614
1615static int megasas_slave_alloc(struct scsi_device *sdev)
1616{
1617 u16 pd_index = 0;
1618 struct megasas_instance *instance ;
1619 instance = megasas_lookup_instance(sdev->host->host_no);
1620 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1621 (sdev->type == TYPE_DISK)) {
1622 /*
1623 * Open the OS scan to the SYSTEM PD
1624 */
1625 pd_index =
1626 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1627 sdev->id;
1628 if ((instance->pd_list[pd_index].driveState ==
1629 MR_PD_STATE_SYSTEM) &&
1630 (instance->pd_list[pd_index].driveType ==
1631 TYPE_DISK)) {
1632 return 0;
1633 }
1634 return -ENXIO;
1635 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001636 return 0;
1637}
1638
adam radford9c915a82010-12-21 13:34:31 -08001639void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001640{
1641 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001642 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
adam radford36807e62011-10-08 18:15:06 -07001643 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301644 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1645 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08001646 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001647 } else {
adam radford9c915a82010-12-21 13:34:31 -08001648 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1649 }
1650}
1651
1652 /**
1653 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1654 * restored to max value
1655 * @instance: Adapter soft state
1656 *
1657 */
1658void
1659megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1660{
1661 unsigned long flags;
1662 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001663 && time_after(jiffies, instance->last_time + 5 * HZ)
1664 && atomic_read(&instance->fw_outstanding) <
1665 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001666
1667 spin_lock_irqsave(instance->host->host_lock, flags);
1668 instance->flag &= ~MEGASAS_FW_BUSY;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05301669 if (instance->is_imr) {
adam radford9c915a82010-12-21 13:34:31 -08001670 instance->host->can_queue =
1671 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1672 } else
1673 instance->host->can_queue =
1674 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1675
1676 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001677 }
1678}
1679
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001680/**
bo yang7343eb62007-11-09 04:35:44 -05001681 * megasas_complete_cmd_dpc - Returns FW's controller structure
1682 * @instance_addr: Address of adapter soft state
1683 *
1684 * Tasklet to complete cmds
1685 */
1686static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1687{
1688 u32 producer;
1689 u32 consumer;
1690 u32 context;
1691 struct megasas_cmd *cmd;
1692 struct megasas_instance *instance =
1693 (struct megasas_instance *)instance_addr;
1694 unsigned long flags;
1695
1696 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001697 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001698 return;
1699
1700 spin_lock_irqsave(&instance->completion_lock, flags);
1701
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301702 producer = le32_to_cpu(*instance->producer);
1703 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001704
1705 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301706 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001707 if (context >= instance->max_fw_cmds) {
1708 printk(KERN_ERR "Unexpected context value %x\n",
1709 context);
1710 BUG();
1711 }
bo yang7343eb62007-11-09 04:35:44 -05001712
1713 cmd = instance->cmd_list[context];
1714
1715 megasas_complete_cmd(instance, cmd, DID_OK);
1716
1717 consumer++;
1718 if (consumer == (instance->max_fw_cmds + 1)) {
1719 consumer = 0;
1720 }
1721 }
1722
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301723 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05001724
1725 spin_unlock_irqrestore(&instance->completion_lock, flags);
1726
1727 /*
1728 * Check if we can restore can_queue
1729 */
adam radford9c915a82010-12-21 13:34:31 -08001730 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001731}
1732
Yang, Bo707e09b2010-10-12 07:20:27 -06001733static void
1734megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1735
1736static void
1737process_fw_state_change_wq(struct work_struct *work);
1738
1739void megasas_do_ocr(struct megasas_instance *instance)
1740{
1741 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1742 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1743 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301744 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06001745 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301746 instance->instancet->disable_intr(instance);
Yang, Bo707e09b2010-10-12 07:20:27 -06001747 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1748 instance->issuepend_done = 0;
1749
1750 atomic_set(&instance->fw_outstanding, 0);
1751 megasas_internal_reset_defer_cmds(instance);
1752 process_fw_state_change_wq(&instance->work_init);
1753}
1754
bo yang7343eb62007-11-09 04:35:44 -05001755/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001756 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1757 * @instance: Adapter soft state
1758 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001759 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001760 * complete all its outstanding commands. Returns error if one or more IOs
1761 * are pending after this time period. It also marks the controller dead.
1762 */
1763static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1764{
1765 int i;
bo yang39a98552010-09-22 22:36:29 -04001766 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001767 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001768 u8 adprecovery;
1769 unsigned long flags;
1770 struct list_head clist_local;
1771 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001772 u32 fw_state;
1773 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001774
1775 spin_lock_irqsave(&instance->hba_lock, flags);
1776 adprecovery = instance->adprecovery;
1777 spin_unlock_irqrestore(&instance->hba_lock, flags);
1778
1779 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1780
1781 INIT_LIST_HEAD(&clist_local);
1782 spin_lock_irqsave(&instance->hba_lock, flags);
1783 list_splice_init(&instance->internal_reset_pending_q,
1784 &clist_local);
1785 spin_unlock_irqrestore(&instance->hba_lock, flags);
1786
1787 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1788 for (i = 0; i < wait_time; i++) {
1789 msleep(1000);
1790 spin_lock_irqsave(&instance->hba_lock, flags);
1791 adprecovery = instance->adprecovery;
1792 spin_unlock_irqrestore(&instance->hba_lock, flags);
1793 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1794 break;
1795 }
1796
1797 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1798 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1799 spin_lock_irqsave(&instance->hba_lock, flags);
1800 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1801 spin_unlock_irqrestore(&instance->hba_lock, flags);
1802 return FAILED;
1803 }
1804
1805 reset_index = 0;
1806 while (!list_empty(&clist_local)) {
1807 reset_cmd = list_entry((&clist_local)->next,
1808 struct megasas_cmd, list);
1809 list_del_init(&reset_cmd->list);
1810 if (reset_cmd->scmd) {
1811 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001812 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001813 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001814 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001815
1816 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1817 megasas_return_cmd(instance, reset_cmd);
1818 } else if (reset_cmd->sync_cmd) {
1819 printk(KERN_NOTICE "megasas:%p synch cmds"
1820 "reset queue\n",
1821 reset_cmd);
1822
1823 reset_cmd->cmd_status = ENODATA;
1824 instance->instancet->fire_cmd(instance,
1825 reset_cmd->frame_phys_addr,
1826 0, instance->reg_set);
1827 } else {
1828 printk(KERN_NOTICE "megasas: %p unexpected"
1829 "cmds lst\n",
1830 reset_cmd);
1831 }
1832 reset_index++;
1833 }
1834
1835 return SUCCESS;
1836 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001837
adam radfordc007b8b2012-07-17 18:20:24 -07001838 for (i = 0; i < resetwaittime; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001839
Sumant Patroe4a082c2006-05-30 12:03:37 -07001840 int outstanding = atomic_read(&instance->fw_outstanding);
1841
1842 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001843 break;
1844
1845 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1846 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001847 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001848 /*
1849 * Call cmd completion routine. Cmd to be
1850 * be completed directly without depending on isr.
1851 */
1852 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001853 }
1854
1855 msleep(1000);
1856 }
1857
Yang, Bo707e09b2010-10-12 07:20:27 -06001858 i = 0;
1859 kill_adapter_flag = 0;
1860 do {
1861 fw_state = instance->instancet->read_fw_status_reg(
1862 instance->reg_set) & MFI_STATE_MASK;
1863 if ((fw_state == MFI_STATE_FAULT) &&
1864 (instance->disableOnlineCtrlReset == 0)) {
1865 if (i == 3) {
1866 kill_adapter_flag = 2;
1867 break;
1868 }
1869 megasas_do_ocr(instance);
1870 kill_adapter_flag = 1;
1871
1872 /* wait for 1 secs to let FW finish the pending cmds */
1873 msleep(1000);
1874 }
1875 i++;
1876 } while (i <= 3);
1877
1878 if (atomic_read(&instance->fw_outstanding) &&
1879 !kill_adapter_flag) {
1880 if (instance->disableOnlineCtrlReset == 0) {
1881
1882 megasas_do_ocr(instance);
1883
1884 /* wait for 5 secs to let FW finish the pending cmds */
1885 for (i = 0; i < wait_time; i++) {
1886 int outstanding =
1887 atomic_read(&instance->fw_outstanding);
1888 if (!outstanding)
1889 return SUCCESS;
1890 msleep(1000);
1891 }
1892 }
1893 }
1894
1895 if (atomic_read(&instance->fw_outstanding) ||
1896 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001897 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001898 /*
1899 * Send signal to FW to stop processing any pending cmds.
1900 * The controller will be taken offline by the OS now.
1901 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001902 if ((instance->pdev->device ==
1903 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1904 (instance->pdev->device ==
1905 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1906 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001907 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001908 } else {
1909 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001910 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001911 }
Sumant Patro658dced2006-10-03 13:09:14 -07001912 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001913 spin_lock_irqsave(&instance->hba_lock, flags);
1914 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1915 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001916 return FAILED;
1917 }
1918
bo yang39a98552010-09-22 22:36:29 -04001919 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1920
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001921 return SUCCESS;
1922}
1923
1924/**
1925 * megasas_generic_reset - Generic reset routine
1926 * @scmd: Mid-layer SCSI command
1927 *
1928 * This routine implements a generic reset handler for device, bus and host
1929 * reset requests. Device, bus and host specific reset handlers can use this
1930 * function after they do their specific tasks.
1931 */
1932static int megasas_generic_reset(struct scsi_cmnd *scmd)
1933{
1934 int ret_val;
1935 struct megasas_instance *instance;
1936
1937 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1938
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001939 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1940 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001941
bo yang39a98552010-09-22 22:36:29 -04001942 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001943 printk(KERN_ERR "megasas: cannot recover from previous reset "
1944 "failures\n");
1945 return FAILED;
1946 }
1947
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001948 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001949 if (ret_val == SUCCESS)
1950 printk(KERN_NOTICE "megasas: reset successful \n");
1951 else
1952 printk(KERN_ERR "megasas: failed to do reset\n");
1953
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001954 return ret_val;
1955}
1956
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001957/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001958 * megasas_reset_timer - quiesce the adapter if required
1959 * @scmd: scsi cmnd
1960 *
1961 * Sets the FW busy flag and reduces the host->can_queue if the
1962 * cmd has not been completed within the timeout period.
1963 */
1964static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001965blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001966{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001967 struct megasas_instance *instance;
1968 unsigned long flags;
1969
1970 if (time_after(jiffies, scmd->jiffies_at_alloc +
1971 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001972 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001973 }
1974
adam radfordf575c5d2011-10-13 16:01:12 -07001975 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001976 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1977 /* FW is busy, throttle IO */
1978 spin_lock_irqsave(instance->host->host_lock, flags);
1979
adam radfordc5daa6a2012-07-17 18:20:03 -07001980 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001981 instance->last_time = jiffies;
1982 instance->flag |= MEGASAS_FW_BUSY;
1983
1984 spin_unlock_irqrestore(instance->host->host_lock, flags);
1985 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001986 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001987}
1988
1989/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001990 * megasas_reset_device - Device reset handler entry point
1991 */
1992static int megasas_reset_device(struct scsi_cmnd *scmd)
1993{
1994 int ret;
1995
1996 /*
1997 * First wait for all commands to complete
1998 */
1999 ret = megasas_generic_reset(scmd);
2000
2001 return ret;
2002}
2003
2004/**
2005 * megasas_reset_bus_host - Bus & host reset handler entry point
2006 */
2007static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2008{
2009 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002010 struct megasas_instance *instance;
2011 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002012
2013 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002014 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002015 */
adam radford36807e62011-10-08 18:15:06 -07002016 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302017 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2018 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radford9c915a82010-12-21 13:34:31 -08002019 ret = megasas_reset_fusion(scmd->device->host);
2020 else
2021 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002022
2023 return ret;
2024}
2025
2026/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002027 * megasas_bios_param - Returns disk geometry for a disk
2028 * @sdev: device handle
2029 * @bdev: block device
2030 * @capacity: drive capacity
2031 * @geom: geometry parameters
2032 */
2033static int
2034megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2035 sector_t capacity, int geom[])
2036{
2037 int heads;
2038 int sectors;
2039 sector_t cylinders;
2040 unsigned long tmp;
2041 /* Default heads (64) & sectors (32) */
2042 heads = 64;
2043 sectors = 32;
2044
2045 tmp = heads * sectors;
2046 cylinders = capacity;
2047
2048 sector_div(cylinders, tmp);
2049
2050 /*
2051 * Handle extended translation size for logical drives > 1Gb
2052 */
2053
2054 if (capacity >= 0x200000) {
2055 heads = 255;
2056 sectors = 63;
2057 tmp = heads*sectors;
2058 cylinders = capacity;
2059 sector_div(cylinders, tmp);
2060 }
2061
2062 geom[0] = heads;
2063 geom[1] = sectors;
2064 geom[2] = cylinders;
2065
2066 return 0;
2067}
2068
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002069static void megasas_aen_polling(struct work_struct *work);
2070
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002071/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002072 * megasas_service_aen - Processes an event notification
2073 * @instance: Adapter soft state
2074 * @cmd: AEN command completed by the ISR
2075 *
2076 * For AEN, driver sends a command down to FW that is held by the FW till an
2077 * event occurs. When an event of interest occurs, FW completes the command
2078 * that it was previously holding.
2079 *
2080 * This routines sends SIGIO signal to processes that have registered with the
2081 * driver for AEN.
2082 */
2083static void
2084megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2085{
Yang, Boc3518832009-10-06 14:18:02 -06002086 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002087 /*
2088 * Don't signal app if it is just an aborted previously registered aen
2089 */
Yang, Boc3518832009-10-06 14:18:02 -06002090 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2091 spin_lock_irqsave(&poll_aen_lock, flags);
2092 megasas_poll_wait_aen = 1;
2093 spin_unlock_irqrestore(&poll_aen_lock, flags);
2094 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002095 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002096 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002097 else
2098 cmd->abort_aen = 0;
2099
2100 instance->aen_cmd = NULL;
2101 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002102
bo yang39a98552010-09-22 22:36:29 -04002103 if ((instance->unload == 0) &&
2104 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002105 struct megasas_aen_event *ev;
2106 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2107 if (!ev) {
2108 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2109 } else {
2110 ev->instance = instance;
2111 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002112 INIT_DELAYED_WORK(&ev->hotplug_work,
2113 megasas_aen_polling);
2114 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002115 }
2116 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002117}
2118
adam radford4bcde502011-07-26 15:42:52 -07002119static int megasas_change_queue_depth(struct scsi_device *sdev,
2120 int queue_depth, int reason)
2121{
2122 if (reason != SCSI_QDEPTH_DEFAULT)
2123 return -EOPNOTSUPP;
2124
2125 if (queue_depth > sdev->host->can_queue)
2126 queue_depth = sdev->host->can_queue;
2127 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2128 queue_depth);
2129
2130 return queue_depth;
2131}
2132
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002133/*
2134 * Scsi host template for megaraid_sas driver
2135 */
2136static struct scsi_host_template megasas_template = {
2137
2138 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002139 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002140 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002141 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002142 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002143 .queuecommand = megasas_queue_command,
2144 .eh_device_reset_handler = megasas_reset_device,
2145 .eh_bus_reset_handler = megasas_reset_bus_host,
2146 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002147 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002148 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002149 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002150 .change_queue_depth = megasas_change_queue_depth,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002151};
2152
2153/**
2154 * megasas_complete_int_cmd - Completes an internal command
2155 * @instance: Adapter soft state
2156 * @cmd: Command to be completed
2157 *
2158 * The megasas_issue_blocked_cmd() function waits for a command to complete
2159 * after it issues a command. This function wakes up that waiting routine by
2160 * calling wake_up() on the wait queue.
2161 */
2162static void
2163megasas_complete_int_cmd(struct megasas_instance *instance,
2164 struct megasas_cmd *cmd)
2165{
2166 cmd->cmd_status = cmd->frame->io.cmd_status;
2167
2168 if (cmd->cmd_status == ENODATA) {
2169 cmd->cmd_status = 0;
2170 }
2171 wake_up(&instance->int_cmd_wait_q);
2172}
2173
2174/**
2175 * megasas_complete_abort - Completes aborting a command
2176 * @instance: Adapter soft state
2177 * @cmd: Cmd that was issued to abort another cmd
2178 *
adam radford0d490162010-12-14 19:17:17 -08002179 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2180 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002181 * wakes up all functions waiting on the same wait queue.
2182 */
2183static void
2184megasas_complete_abort(struct megasas_instance *instance,
2185 struct megasas_cmd *cmd)
2186{
2187 if (cmd->sync_cmd) {
2188 cmd->sync_cmd = 0;
2189 cmd->cmd_status = 0;
2190 wake_up(&instance->abort_cmd_wait_q);
2191 }
2192
2193 return;
2194}
2195
2196/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002197 * megasas_complete_cmd - Completes a command
2198 * @instance: Adapter soft state
2199 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002200 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002201 * SCSI mid-layer instead of the value returned
2202 * by the FW. This should be used if caller wants
2203 * an alternate status (as in the case of aborted
2204 * commands)
2205 */
adam radford9c915a82010-12-21 13:34:31 -08002206void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002207megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2208 u8 alt_status)
2209{
2210 int exception = 0;
2211 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002212 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002213 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302214 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002215
bo yang39a98552010-09-22 22:36:29 -04002216 /* flag for the retry reset */
2217 cmd->retry_for_fw_reset = 0;
2218
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002219 if (cmd->scmd)
2220 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002221
2222 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002223 case MFI_CMD_INVALID:
2224 /* Some older 1068 controller FW may keep a pended
2225 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2226 when booting the kdump kernel. Ignore this command to
2227 prevent a kernel panic on shutdown of the kdump kernel. */
2228 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2229 "completed.\n");
2230 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2231 "other than PERC5, please upgrade your firmware.\n");
2232 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002233 case MFI_CMD_PD_SCSI_IO:
2234 case MFI_CMD_LD_SCSI_IO:
2235
2236 /*
2237 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2238 * issued either through an IO path or an IOCTL path. If it
2239 * was via IOCTL, we will send it to internal completion.
2240 */
2241 if (cmd->sync_cmd) {
2242 cmd->sync_cmd = 0;
2243 megasas_complete_int_cmd(instance, cmd);
2244 break;
2245 }
2246
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002247 case MFI_CMD_LD_READ:
2248 case MFI_CMD_LD_WRITE:
2249
2250 if (alt_status) {
2251 cmd->scmd->result = alt_status << 16;
2252 exception = 1;
2253 }
2254
2255 if (exception) {
2256
Sumant Patroe4a082c2006-05-30 12:03:37 -07002257 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002258
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002259 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002260 cmd->scmd->scsi_done(cmd->scmd);
2261 megasas_return_cmd(instance, cmd);
2262
2263 break;
2264 }
2265
2266 switch (hdr->cmd_status) {
2267
2268 case MFI_STAT_OK:
2269 cmd->scmd->result = DID_OK << 16;
2270 break;
2271
2272 case MFI_STAT_SCSI_IO_FAILED:
2273 case MFI_STAT_LD_INIT_IN_PROGRESS:
2274 cmd->scmd->result =
2275 (DID_ERROR << 16) | hdr->scsi_status;
2276 break;
2277
2278 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2279
2280 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2281
2282 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2283 memset(cmd->scmd->sense_buffer, 0,
2284 SCSI_SENSE_BUFFERSIZE);
2285 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2286 hdr->sense_len);
2287
2288 cmd->scmd->result |= DRIVER_SENSE << 24;
2289 }
2290
2291 break;
2292
2293 case MFI_STAT_LD_OFFLINE:
2294 case MFI_STAT_DEVICE_NOT_FOUND:
2295 cmd->scmd->result = DID_BAD_TARGET << 16;
2296 break;
2297
2298 default:
2299 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2300 hdr->cmd_status);
2301 cmd->scmd->result = DID_ERROR << 16;
2302 break;
2303 }
2304
Sumant Patroe4a082c2006-05-30 12:03:37 -07002305 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002306
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002307 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002308 cmd->scmd->scsi_done(cmd->scmd);
2309 megasas_return_cmd(instance, cmd);
2310
2311 break;
2312
2313 case MFI_CMD_SMP:
2314 case MFI_CMD_STP:
2315 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302316 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08002317 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302318 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2319 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302320 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08002321 spin_lock_irqsave(instance->host->host_lock, flags);
2322 if (cmd->frame->hdr.cmd_status != 0) {
2323 if (cmd->frame->hdr.cmd_status !=
2324 MFI_STAT_NOT_FOUND)
2325 printk(KERN_WARNING "megasas: map sync"
2326 "failed, status = 0x%x.\n",
2327 cmd->frame->hdr.cmd_status);
2328 else {
2329 megasas_return_cmd(instance, cmd);
2330 spin_unlock_irqrestore(
2331 instance->host->host_lock,
2332 flags);
2333 break;
2334 }
2335 } else
2336 instance->map_id++;
2337 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05302338
2339 /*
2340 * Set fast path IO to ZERO.
2341 * Validate Map will set proper value.
2342 * Meanwhile all IOs will go as LD IO.
2343 */
2344 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08002345 fusion->fast_path_io = 1;
2346 else
2347 fusion->fast_path_io = 0;
2348 megasas_sync_map_info(instance);
2349 spin_unlock_irqrestore(instance->host->host_lock,
2350 flags);
2351 break;
2352 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302353 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2354 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06002355 spin_lock_irqsave(&poll_aen_lock, flags);
2356 megasas_poll_wait_aen = 0;
2357 spin_unlock_irqrestore(&poll_aen_lock, flags);
2358 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002359
2360 /*
2361 * See if got an event notification
2362 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302363 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002364 megasas_service_aen(instance, cmd);
2365 else
2366 megasas_complete_int_cmd(instance, cmd);
2367
2368 break;
2369
2370 case MFI_CMD_ABORT:
2371 /*
2372 * Cmd issued to abort another cmd returned
2373 */
2374 megasas_complete_abort(instance, cmd);
2375 break;
2376
2377 default:
2378 printk("megasas: Unknown command completed! [0x%X]\n",
2379 hdr->cmd);
2380 break;
2381 }
2382}
2383
2384/**
bo yang39a98552010-09-22 22:36:29 -04002385 * megasas_issue_pending_cmds_again - issue all pending cmds
2386 * in FW again because of the fw reset
2387 * @instance: Adapter soft state
2388 */
2389static inline void
2390megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2391{
2392 struct megasas_cmd *cmd;
2393 struct list_head clist_local;
2394 union megasas_evt_class_locale class_locale;
2395 unsigned long flags;
2396 u32 seq_num;
2397
2398 INIT_LIST_HEAD(&clist_local);
2399 spin_lock_irqsave(&instance->hba_lock, flags);
2400 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2401 spin_unlock_irqrestore(&instance->hba_lock, flags);
2402
2403 while (!list_empty(&clist_local)) {
2404 cmd = list_entry((&clist_local)->next,
2405 struct megasas_cmd, list);
2406 list_del_init(&cmd->list);
2407
2408 if (cmd->sync_cmd || cmd->scmd) {
2409 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2410 "detected to be pending while HBA reset.\n",
2411 cmd, cmd->scmd, cmd->sync_cmd);
2412
2413 cmd->retry_for_fw_reset++;
2414
2415 if (cmd->retry_for_fw_reset == 3) {
2416 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2417 "was tried multiple times during reset."
2418 "Shutting down the HBA\n",
2419 cmd, cmd->scmd, cmd->sync_cmd);
2420 megaraid_sas_kill_hba(instance);
2421
2422 instance->adprecovery =
2423 MEGASAS_HW_CRITICAL_ERROR;
2424 return;
2425 }
2426 }
2427
2428 if (cmd->sync_cmd == 1) {
2429 if (cmd->scmd) {
2430 printk(KERN_NOTICE "megaraid_sas: unexpected"
2431 "cmd attached to internal command!\n");
2432 }
2433 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2434 "on the internal reset queue,"
2435 "issue it again.\n", cmd);
2436 cmd->cmd_status = ENODATA;
2437 instance->instancet->fire_cmd(instance,
2438 cmd->frame_phys_addr ,
2439 0, instance->reg_set);
2440 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002441 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002442 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002443 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002444
2445 atomic_inc(&instance->fw_outstanding);
2446 instance->instancet->fire_cmd(instance,
2447 cmd->frame_phys_addr,
2448 cmd->frame_count-1, instance->reg_set);
2449 } else {
2450 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2451 "internal reset defer list while re-issue!!\n",
2452 cmd);
2453 }
2454 }
2455
2456 if (instance->aen_cmd) {
2457 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2458 megasas_return_cmd(instance, instance->aen_cmd);
2459
2460 instance->aen_cmd = NULL;
2461 }
2462
2463 /*
2464 * Initiate AEN (Asynchronous Event Notification)
2465 */
2466 seq_num = instance->last_seq_num;
2467 class_locale.members.reserved = 0;
2468 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2469 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2470
2471 megasas_register_aen(instance, seq_num, class_locale.word);
2472}
2473
2474/**
2475 * Move the internal reset pending commands to a deferred queue.
2476 *
2477 * We move the commands pending at internal reset time to a
2478 * pending queue. This queue would be flushed after successful
2479 * completion of the internal reset sequence. if the internal reset
2480 * did not complete in time, the kernel reset handler would flush
2481 * these commands.
2482 **/
2483static void
2484megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2485{
2486 struct megasas_cmd *cmd;
2487 int i;
2488 u32 max_cmd = instance->max_fw_cmds;
2489 u32 defer_index;
2490 unsigned long flags;
2491
2492 defer_index = 0;
2493 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2494 for (i = 0; i < max_cmd; i++) {
2495 cmd = instance->cmd_list[i];
2496 if (cmd->sync_cmd == 1 || cmd->scmd) {
2497 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2498 "on the defer queue as internal\n",
2499 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2500
2501 if (!list_empty(&cmd->list)) {
2502 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2503 " moving this cmd:%p, %d %p, it was"
2504 "discovered on some list?\n",
2505 cmd, cmd->sync_cmd, cmd->scmd);
2506
2507 list_del_init(&cmd->list);
2508 }
2509 defer_index++;
2510 list_add_tail(&cmd->list,
2511 &instance->internal_reset_pending_q);
2512 }
2513 }
2514 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2515}
2516
2517
2518static void
2519process_fw_state_change_wq(struct work_struct *work)
2520{
2521 struct megasas_instance *instance =
2522 container_of(work, struct megasas_instance, work_init);
2523 u32 wait;
2524 unsigned long flags;
2525
2526 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2527 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2528 instance->adprecovery);
2529 return ;
2530 }
2531
2532 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2533 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2534 "state, restarting it...\n");
2535
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302536 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04002537 atomic_set(&instance->fw_outstanding, 0);
2538
2539 atomic_set(&instance->fw_reset_no_pci_access, 1);
2540 instance->instancet->adp_reset(instance, instance->reg_set);
2541 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2542
2543 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2544 "initiating next stage...\n");
2545
2546 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2547 "state 2 starting...\n");
2548
2549 /*waitting for about 20 second before start the second init*/
2550 for (wait = 0; wait < 30; wait++) {
2551 msleep(1000);
2552 }
2553
adam radford058a8fa2011-10-08 18:14:27 -07002554 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04002555 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2556
2557 megaraid_sas_kill_hba(instance);
2558 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2559 return ;
2560 }
2561
2562 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2563 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2564 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2565 ) {
2566 *instance->consumer = *instance->producer;
2567 } else {
2568 *instance->consumer = 0;
2569 *instance->producer = 0;
2570 }
2571
2572 megasas_issue_init_mfi(instance);
2573
2574 spin_lock_irqsave(&instance->hba_lock, flags);
2575 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2576 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302577 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04002578
2579 megasas_issue_pending_cmds_again(instance);
2580 instance->issuepend_done = 1;
2581 }
2582 return ;
2583}
2584
2585/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002586 * megasas_deplete_reply_queue - Processes all completed commands
2587 * @instance: Adapter soft state
2588 * @alt_status: Alternate status to be returned to
2589 * SCSI mid-layer instead of the status
2590 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002591 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002592 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002593static int
bo yang39a98552010-09-22 22:36:29 -04002594megasas_deplete_reply_queue(struct megasas_instance *instance,
2595 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002596{
bo yang39a98552010-09-22 22:36:29 -04002597 u32 mfiStatus;
2598 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002599
bo yang39a98552010-09-22 22:36:29 -04002600 if ((mfiStatus = instance->instancet->check_reset(instance,
2601 instance->reg_set)) == 1) {
2602 return IRQ_HANDLED;
2603 }
2604
2605 if ((mfiStatus = instance->instancet->clear_intr(
2606 instance->reg_set)
2607 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002608 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07002609 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08002610 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002611 }
2612
2613 instance->mfiStatus = mfiStatus;
2614
2615 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2616 fw_state = instance->instancet->read_fw_status_reg(
2617 instance->reg_set) & MFI_STATE_MASK;
2618
2619 if (fw_state != MFI_STATE_FAULT) {
2620 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2621 fw_state);
2622 }
2623
2624 if ((fw_state == MFI_STATE_FAULT) &&
2625 (instance->disableOnlineCtrlReset == 0)) {
2626 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2627
2628 if ((instance->pdev->device ==
2629 PCI_DEVICE_ID_LSI_SAS1064R) ||
2630 (instance->pdev->device ==
2631 PCI_DEVICE_ID_DELL_PERC5) ||
2632 (instance->pdev->device ==
2633 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2634
2635 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302636 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04002637 }
2638
2639
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302640 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04002641 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2642 instance->issuepend_done = 0;
2643
2644 atomic_set(&instance->fw_outstanding, 0);
2645 megasas_internal_reset_defer_cmds(instance);
2646
2647 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2648 fw_state, instance->adprecovery);
2649
2650 schedule_work(&instance->work_init);
2651 return IRQ_HANDLED;
2652
2653 } else {
2654 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2655 fw_state, instance->disableOnlineCtrlReset);
2656 }
2657 }
2658
Sumant Patro5d018ad2006-10-03 13:13:18 -07002659 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002660 return IRQ_HANDLED;
2661}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002662/**
2663 * megasas_isr - isr entry point
2664 */
David Howells7d12e782006-10-05 14:55:46 +01002665static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002666{
adam radfordc8e858f2011-10-08 18:15:13 -07002667 struct megasas_irq_context *irq_context = devp;
2668 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04002669 unsigned long flags;
2670 irqreturn_t rc;
2671
adam radfordc8e858f2011-10-08 18:15:13 -07002672 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04002673 return IRQ_HANDLED;
2674
bo yang39a98552010-09-22 22:36:29 -04002675 spin_lock_irqsave(&instance->hba_lock, flags);
2676 rc = megasas_deplete_reply_queue(instance, DID_OK);
2677 spin_unlock_irqrestore(&instance->hba_lock, flags);
2678
2679 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002680}
2681
2682/**
2683 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002684 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002685 *
2686 * During the initialization, FW passes can potentially be in any one of
2687 * several possible states. If the FW in operational, waiting-for-handshake
2688 * states, driver must take steps to bring it to ready state. Otherwise, it
2689 * has to wait for the ready state.
2690 */
adam radford9c915a82010-12-21 13:34:31 -08002691int
adam radford058a8fa2011-10-08 18:14:27 -07002692megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002693{
2694 int i;
2695 u8 max_wait;
2696 u32 fw_state;
2697 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002698 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002699
Sumant Patro1341c932006-01-25 12:02:40 -08002700 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002701
Sumant Patroe3bbff92006-10-03 12:28:49 -07002702 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002703 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2704 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002705
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002706 while (fw_state != MFI_STATE_READY) {
2707
Yang, Bo7218df62009-10-06 14:52:20 -06002708 abs_state =
2709 instance->instancet->read_fw_status_reg(instance->reg_set);
2710
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002711 switch (fw_state) {
2712
2713 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002714 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07002715 if (ocr) {
2716 max_wait = MEGASAS_RESET_WAIT_TIME;
2717 cur_state = MFI_STATE_FAULT;
2718 break;
2719 } else
2720 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002721
2722 case MFI_STATE_WAIT_HANDSHAKE:
2723 /*
2724 * Set the CLR bit in inbound doorbell
2725 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002726 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002727 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2728 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002729 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2730 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302731 PCI_DEVICE_ID_LSI_FUSION) ||
adam radford36807e62011-10-08 18:15:06 -07002732 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302733 PCI_DEVICE_ID_LSI_INVADER) ||
2734 (instance->pdev->device ==
2735 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06002736 writel(
2737 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002738 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002739 } else {
2740 writel(
2741 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2742 &instance->reg_set->inbound_doorbell);
2743 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002744
Yang, Bo7218df62009-10-06 14:52:20 -06002745 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002746 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2747 break;
2748
Sumant Patroe3bbff92006-10-03 12:28:49 -07002749 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002750 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002751 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2752 (instance->pdev->device ==
2753 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2754 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002755 PCI_DEVICE_ID_LSI_FUSION) ||
2756 (instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302757 PCI_DEVICE_ID_LSI_INVADER) ||
2758 (instance->pdev->device ==
2759 PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06002760 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002761 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002762 } else
2763 writel(MFI_INIT_HOTPLUG,
2764 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002765
Yang, Bo7218df62009-10-06 14:52:20 -06002766 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002767 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2768 break;
2769
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002770 case MFI_STATE_OPERATIONAL:
2771 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002772 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002773 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302774 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06002775 if ((instance->pdev->device ==
2776 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2777 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002778 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2779 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07002780 == PCI_DEVICE_ID_LSI_FUSION) ||
2781 (instance->pdev->device
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302782 == PCI_DEVICE_ID_LSI_INVADER) ||
2783 (instance->pdev->device
2784 == PCI_DEVICE_ID_LSI_FURY)) {
Yang, Bo87911122009-10-06 14:31:54 -06002785 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002786 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07002787 if ((instance->pdev->device ==
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302788 PCI_DEVICE_ID_LSI_FUSION) ||
2789 (instance->pdev->device ==
2790 PCI_DEVICE_ID_LSI_INVADER) ||
2791 (instance->pdev->device ==
2792 PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08002793 for (i = 0; i < (10 * 1000); i += 20) {
2794 if (readl(
2795 &instance->
2796 reg_set->
2797 doorbell) & 1)
2798 msleep(20);
2799 else
2800 break;
2801 }
2802 }
Yang, Bo87911122009-10-06 14:31:54 -06002803 } else
2804 writel(MFI_RESET_FLAGS,
2805 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002806
Yang, Bo7218df62009-10-06 14:52:20 -06002807 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002808 cur_state = MFI_STATE_OPERATIONAL;
2809 break;
2810
2811 case MFI_STATE_UNDEFINED:
2812 /*
2813 * This state should not last for more than 2 seconds
2814 */
Yang, Bo7218df62009-10-06 14:52:20 -06002815 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002816 cur_state = MFI_STATE_UNDEFINED;
2817 break;
2818
2819 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002820 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002821 cur_state = MFI_STATE_BB_INIT;
2822 break;
2823
2824 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002825 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002826 cur_state = MFI_STATE_FW_INIT;
2827 break;
2828
2829 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002830 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002831 cur_state = MFI_STATE_FW_INIT_2;
2832 break;
2833
2834 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002835 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002836 cur_state = MFI_STATE_DEVICE_SCAN;
2837 break;
2838
2839 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002840 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002841 cur_state = MFI_STATE_FLUSH_CACHE;
2842 break;
2843
2844 default:
2845 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2846 fw_state);
2847 return -ENODEV;
2848 }
2849
2850 /*
2851 * The cur_state should not last for more than max_wait secs
2852 */
2853 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002854 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002855 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002856 curr_abs_state =
2857 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002858
Yang, Bo7218df62009-10-06 14:52:20 -06002859 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002860 msleep(1);
2861 } else
2862 break;
2863 }
2864
2865 /*
2866 * Return error if fw_state hasn't changed after max_wait
2867 */
Yang, Bo7218df62009-10-06 14:52:20 -06002868 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002869 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2870 "in %d secs\n", fw_state, max_wait);
2871 return -ENODEV;
2872 }
bo yang39a98552010-09-22 22:36:29 -04002873 }
adam radford0d490162010-12-14 19:17:17 -08002874 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002875
2876 return 0;
2877}
2878
2879/**
2880 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2881 * @instance: Adapter soft state
2882 */
2883static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2884{
2885 int i;
adam radford9c915a82010-12-21 13:34:31 -08002886 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002887 struct megasas_cmd *cmd;
2888
2889 if (!instance->frame_dma_pool)
2890 return;
2891
2892 /*
2893 * Return all frames to pool
2894 */
2895 for (i = 0; i < max_cmd; i++) {
2896
2897 cmd = instance->cmd_list[i];
2898
2899 if (cmd->frame)
2900 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2901 cmd->frame_phys_addr);
2902
2903 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002904 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002905 cmd->sense_phys_addr);
2906 }
2907
2908 /*
2909 * Now destroy the pool itself
2910 */
2911 pci_pool_destroy(instance->frame_dma_pool);
2912 pci_pool_destroy(instance->sense_dma_pool);
2913
2914 instance->frame_dma_pool = NULL;
2915 instance->sense_dma_pool = NULL;
2916}
2917
2918/**
2919 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2920 * @instance: Adapter soft state
2921 *
2922 * Each command packet has an embedded DMA memory buffer that is used for
2923 * filling MFI frame and the SG list that immediately follows the frame. This
2924 * function creates those DMA memory buffers for each command packet by using
2925 * PCI pool facility.
2926 */
2927static int megasas_create_frame_pool(struct megasas_instance *instance)
2928{
2929 int i;
2930 u32 max_cmd;
2931 u32 sge_sz;
2932 u32 sgl_sz;
2933 u32 total_sz;
2934 u32 frame_count;
2935 struct megasas_cmd *cmd;
2936
adam radford9c915a82010-12-21 13:34:31 -08002937 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002938
2939 /*
2940 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2941 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2942 */
2943 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2944 sizeof(struct megasas_sge32);
2945
Yang, Bof4c9a132009-10-06 14:43:28 -06002946 if (instance->flag_ieee) {
2947 sge_sz = sizeof(struct megasas_sge_skinny);
2948 }
2949
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002950 /*
2951 * Calculated the number of 64byte frames required for SGL
2952 */
2953 sgl_sz = sge_sz * instance->max_num_sge;
2954 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002955 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002956
2957 /*
2958 * We need one extra frame for the MFI command
2959 */
2960 frame_count++;
2961
2962 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2963 /*
2964 * Use DMA pool facility provided by PCI layer
2965 */
2966 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2967 instance->pdev, total_sz, 64,
2968 0);
2969
2970 if (!instance->frame_dma_pool) {
2971 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2972 return -ENOMEM;
2973 }
2974
2975 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2976 instance->pdev, 128, 4, 0);
2977
2978 if (!instance->sense_dma_pool) {
2979 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2980
2981 pci_pool_destroy(instance->frame_dma_pool);
2982 instance->frame_dma_pool = NULL;
2983
2984 return -ENOMEM;
2985 }
2986
2987 /*
2988 * Allocate and attach a frame to each of the commands in cmd_list.
2989 * By making cmd->index as the context instead of the &cmd, we can
2990 * always use 32bit context regardless of the architecture
2991 */
2992 for (i = 0; i < max_cmd; i++) {
2993
2994 cmd = instance->cmd_list[i];
2995
2996 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2997 GFP_KERNEL, &cmd->frame_phys_addr);
2998
2999 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3000 GFP_KERNEL, &cmd->sense_phys_addr);
3001
3002 /*
3003 * megasas_teardown_frame_pool() takes care of freeing
3004 * whatever has been allocated
3005 */
3006 if (!cmd->frame || !cmd->sense) {
3007 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
3008 megasas_teardown_frame_pool(instance);
3009 return -ENOMEM;
3010 }
3011
Yang, Bo707e09b2010-10-12 07:20:27 -06003012 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303013 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003014 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07003015 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
3016 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303017 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
adam radforde5f93a32011-10-08 18:15:19 -07003018 (reset_devices))
3019 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003020 }
3021
3022 return 0;
3023}
3024
3025/**
3026 * megasas_free_cmds - Free all the cmds in the free cmd pool
3027 * @instance: Adapter soft state
3028 */
adam radford9c915a82010-12-21 13:34:31 -08003029void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003030{
3031 int i;
3032 /* First free the MFI frame pool */
3033 megasas_teardown_frame_pool(instance);
3034
3035 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003036 for (i = 0; i < instance->max_mfi_cmds; i++)
3037
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003038 kfree(instance->cmd_list[i]);
3039
3040 /* Free the cmd_list buffer itself */
3041 kfree(instance->cmd_list);
3042 instance->cmd_list = NULL;
3043
3044 INIT_LIST_HEAD(&instance->cmd_pool);
3045}
3046
3047/**
3048 * megasas_alloc_cmds - Allocates the command packets
3049 * @instance: Adapter soft state
3050 *
3051 * Each command that is issued to the FW, whether IO commands from the OS or
3052 * internal commands like IOCTLs, are wrapped in local data structure called
3053 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3054 * the FW.
3055 *
3056 * Each frame has a 32-bit field called context (tag). This context is used
3057 * to get back the megasas_cmd from the frame when a frame gets completed in
3058 * the ISR. Typically the address of the megasas_cmd itself would be used as
3059 * the context. But we wanted to keep the differences between 32 and 64 bit
3060 * systems to the mininum. We always use 32 bit integers for the context. In
3061 * this driver, the 32 bit values are the indices into an array cmd_list.
3062 * This array is used only to look up the megasas_cmd given the context. The
3063 * free commands themselves are maintained in a linked list called cmd_pool.
3064 */
adam radford9c915a82010-12-21 13:34:31 -08003065int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003066{
3067 int i;
3068 int j;
3069 u32 max_cmd;
3070 struct megasas_cmd *cmd;
3071
adam radford9c915a82010-12-21 13:34:31 -08003072 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003073
3074 /*
3075 * instance->cmd_list is an array of struct megasas_cmd pointers.
3076 * Allocate the dynamic array first and then allocate individual
3077 * commands.
3078 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003079 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003080
3081 if (!instance->cmd_list) {
3082 printk(KERN_DEBUG "megasas: out of memory\n");
3083 return -ENOMEM;
3084 }
3085
adam radford9c915a82010-12-21 13:34:31 -08003086 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003087
3088 for (i = 0; i < max_cmd; i++) {
3089 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3090 GFP_KERNEL);
3091
3092 if (!instance->cmd_list[i]) {
3093
3094 for (j = 0; j < i; j++)
3095 kfree(instance->cmd_list[j]);
3096
3097 kfree(instance->cmd_list);
3098 instance->cmd_list = NULL;
3099
3100 return -ENOMEM;
3101 }
3102 }
3103
3104 /*
3105 * Add all the commands to command pool (instance->cmd_pool)
3106 */
3107 for (i = 0; i < max_cmd; i++) {
3108 cmd = instance->cmd_list[i];
3109 memset(cmd, 0, sizeof(struct megasas_cmd));
3110 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003111 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003112 cmd->instance = instance;
3113
3114 list_add_tail(&cmd->list, &instance->cmd_pool);
3115 }
3116
3117 /*
3118 * Create a frame pool and assign one frame to each cmd
3119 */
3120 if (megasas_create_frame_pool(instance)) {
3121 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3122 megasas_free_cmds(instance);
3123 }
3124
3125 return 0;
3126}
3127
Yang, Bo81e403c2009-10-06 14:27:54 -06003128/*
3129 * megasas_get_pd_list_info - Returns FW's pd_list structure
3130 * @instance: Adapter soft state
3131 * @pd_list: pd_list structure
3132 *
3133 * Issues an internal command (DCMD) to get the FW's controller PD
3134 * list structure. This information is mainly used to find out SYSTEM
3135 * supported by the FW.
3136 */
3137static int
3138megasas_get_pd_list(struct megasas_instance *instance)
3139{
3140 int ret = 0, pd_index = 0;
3141 struct megasas_cmd *cmd;
3142 struct megasas_dcmd_frame *dcmd;
3143 struct MR_PD_LIST *ci;
3144 struct MR_PD_ADDRESS *pd_addr;
3145 dma_addr_t ci_h = 0;
3146
3147 cmd = megasas_get_cmd(instance);
3148
3149 if (!cmd) {
3150 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3151 return -ENOMEM;
3152 }
3153
3154 dcmd = &cmd->frame->dcmd;
3155
3156 ci = pci_alloc_consistent(instance->pdev,
3157 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3158
3159 if (!ci) {
3160 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3161 megasas_return_cmd(instance, cmd);
3162 return -ENOMEM;
3163 }
3164
3165 memset(ci, 0, sizeof(*ci));
3166 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3167
3168 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3169 dcmd->mbox.b[1] = 0;
3170 dcmd->cmd = MFI_CMD_DCMD;
3171 dcmd->cmd_status = 0xFF;
3172 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303173 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06003174 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003175 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303176 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3177 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3178 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3179 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06003180
3181 if (!megasas_issue_polled(instance, cmd)) {
3182 ret = 0;
3183 } else {
3184 ret = -1;
3185 }
3186
3187 /*
3188 * the following function will get the instance PD LIST.
3189 */
3190
3191 pd_addr = ci->addr;
3192
3193 if ( ret == 0 &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303194 (le32_to_cpu(ci->count) <
Yang, Bo81e403c2009-10-06 14:27:54 -06003195 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3196
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303197 memset(instance->local_pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06003198 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3199
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303200 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Yang, Bo81e403c2009-10-06 14:27:54 -06003201
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303202 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303203 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303204 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Yang, Bo81e403c2009-10-06 14:27:54 -06003205 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303206 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Yang, Bo81e403c2009-10-06 14:27:54 -06003207 MR_PD_STATE_SYSTEM;
3208 pd_addr++;
3209 }
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05303210 memcpy(instance->pd_list, instance->local_pd_list,
3211 sizeof(instance->pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06003212 }
3213
3214 pci_free_consistent(instance->pdev,
3215 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3216 ci, ci_h);
3217 megasas_return_cmd(instance, cmd);
3218
3219 return ret;
3220}
3221
Yang, Bobdc6fb82009-12-06 08:30:19 -07003222/*
3223 * megasas_get_ld_list_info - Returns FW's ld_list structure
3224 * @instance: Adapter soft state
3225 * @ld_list: ld_list structure
3226 *
3227 * Issues an internal command (DCMD) to get the FW's controller PD
3228 * list structure. This information is mainly used to find out SYSTEM
3229 * supported by the FW.
3230 */
3231static int
3232megasas_get_ld_list(struct megasas_instance *instance)
3233{
3234 int ret = 0, ld_index = 0, ids = 0;
3235 struct megasas_cmd *cmd;
3236 struct megasas_dcmd_frame *dcmd;
3237 struct MR_LD_LIST *ci;
3238 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303239 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07003240
3241 cmd = megasas_get_cmd(instance);
3242
3243 if (!cmd) {
3244 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3245 return -ENOMEM;
3246 }
3247
3248 dcmd = &cmd->frame->dcmd;
3249
3250 ci = pci_alloc_consistent(instance->pdev,
3251 sizeof(struct MR_LD_LIST),
3252 &ci_h);
3253
3254 if (!ci) {
3255 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3256 megasas_return_cmd(instance, cmd);
3257 return -ENOMEM;
3258 }
3259
3260 memset(ci, 0, sizeof(*ci));
3261 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3262
3263 dcmd->cmd = MFI_CMD_DCMD;
3264 dcmd->cmd_status = 0xFF;
3265 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303266 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003267 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303268 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3269 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3270 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3271 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07003272 dcmd->pad_0 = 0;
3273
3274 if (!megasas_issue_polled(instance, cmd)) {
3275 ret = 0;
3276 } else {
3277 ret = -1;
3278 }
3279
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303280 ld_count = le32_to_cpu(ci->ldCount);
3281
Yang, Bobdc6fb82009-12-06 08:30:19 -07003282 /* the following function will get the instance PD LIST */
3283
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303284 if ((ret == 0) && (ld_count <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003285 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3286
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303287 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003288 if (ci->ldList[ld_index].state != 0) {
3289 ids = ci->ldList[ld_index].ref.targetId;
3290 instance->ld_ids[ids] =
3291 ci->ldList[ld_index].ref.targetId;
3292 }
3293 }
3294 }
3295
3296 pci_free_consistent(instance->pdev,
3297 sizeof(struct MR_LD_LIST),
3298 ci,
3299 ci_h);
3300
3301 megasas_return_cmd(instance, cmd);
3302 return ret;
3303}
3304
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003305/**
adam radford21c9e162013-09-06 15:27:14 -07003306 * megasas_ld_list_query - Returns FW's ld_list structure
3307 * @instance: Adapter soft state
3308 * @ld_list: ld_list structure
3309 *
3310 * Issues an internal command (DCMD) to get the FW's controller PD
3311 * list structure. This information is mainly used to find out SYSTEM
3312 * supported by the FW.
3313 */
3314static int
3315megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3316{
3317 int ret = 0, ld_index = 0, ids = 0;
3318 struct megasas_cmd *cmd;
3319 struct megasas_dcmd_frame *dcmd;
3320 struct MR_LD_TARGETID_LIST *ci;
3321 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303322 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07003323
3324 cmd = megasas_get_cmd(instance);
3325
3326 if (!cmd) {
3327 printk(KERN_WARNING
3328 "megasas:(megasas_ld_list_query): Failed to get cmd\n");
3329 return -ENOMEM;
3330 }
3331
3332 dcmd = &cmd->frame->dcmd;
3333
3334 ci = pci_alloc_consistent(instance->pdev,
3335 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
3336
3337 if (!ci) {
3338 printk(KERN_WARNING
3339 "megasas: Failed to alloc mem for ld_list_query\n");
3340 megasas_return_cmd(instance, cmd);
3341 return -ENOMEM;
3342 }
3343
3344 memset(ci, 0, sizeof(*ci));
3345 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3346
3347 dcmd->mbox.b[0] = query_type;
3348
3349 dcmd->cmd = MFI_CMD_DCMD;
3350 dcmd->cmd_status = 0xFF;
3351 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303352 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07003353 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303354 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3355 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
3356 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3357 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07003358 dcmd->pad_0 = 0;
3359
3360 if (!megasas_issue_polled(instance, cmd) && !dcmd->cmd_status) {
3361 ret = 0;
3362 } else {
3363 /* On failure, call older LD list DCMD */
3364 ret = 1;
3365 }
3366
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303367 tgtid_count = le32_to_cpu(ci->count);
3368
3369 if ((ret == 0) && (tgtid_count <= (MAX_LOGICAL_DRIVES))) {
adam radford21c9e162013-09-06 15:27:14 -07003370 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303371 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07003372 ids = ci->targetId[ld_index];
3373 instance->ld_ids[ids] = ci->targetId[ld_index];
3374 }
3375
3376 }
3377
3378 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
3379 ci, ci_h);
3380
3381 megasas_return_cmd(instance, cmd);
3382
3383 return ret;
3384}
3385
3386/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003387 * megasas_get_controller_info - Returns FW's controller structure
3388 * @instance: Adapter soft state
3389 * @ctrl_info: Controller information structure
3390 *
3391 * Issues an internal command (DCMD) to get the FW's controller structure.
3392 * This information is mainly used to find out the maximum IO transfer per
3393 * command supported by the FW.
3394 */
3395static int
3396megasas_get_ctrl_info(struct megasas_instance *instance,
3397 struct megasas_ctrl_info *ctrl_info)
3398{
3399 int ret = 0;
3400 struct megasas_cmd *cmd;
3401 struct megasas_dcmd_frame *dcmd;
3402 struct megasas_ctrl_info *ci;
3403 dma_addr_t ci_h = 0;
3404
3405 cmd = megasas_get_cmd(instance);
3406
3407 if (!cmd) {
3408 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3409 return -ENOMEM;
3410 }
3411
3412 dcmd = &cmd->frame->dcmd;
3413
3414 ci = pci_alloc_consistent(instance->pdev,
3415 sizeof(struct megasas_ctrl_info), &ci_h);
3416
3417 if (!ci) {
3418 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3419 megasas_return_cmd(instance, cmd);
3420 return -ENOMEM;
3421 }
3422
3423 memset(ci, 0, sizeof(*ci));
3424 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3425
3426 dcmd->cmd = MFI_CMD_DCMD;
3427 dcmd->cmd_status = 0xFF;
3428 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303429 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003430 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003431 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303432 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
3433 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
3434 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3435 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003436
3437 if (!megasas_issue_polled(instance, cmd)) {
3438 ret = 0;
3439 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3440 } else {
3441 ret = -1;
3442 }
3443
3444 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3445 ci, ci_h);
3446
3447 megasas_return_cmd(instance, cmd);
3448 return ret;
3449}
3450
3451/**
bo yang31ea7082007-11-07 12:09:50 -05003452 * megasas_issue_init_mfi - Initializes the FW
3453 * @instance: Adapter soft state
3454 *
3455 * Issues the INIT MFI cmd
3456 */
3457static int
3458megasas_issue_init_mfi(struct megasas_instance *instance)
3459{
3460 u32 context;
3461
3462 struct megasas_cmd *cmd;
3463
3464 struct megasas_init_frame *init_frame;
3465 struct megasas_init_queue_info *initq_info;
3466 dma_addr_t init_frame_h;
3467 dma_addr_t initq_info_h;
3468
3469 /*
3470 * Prepare a init frame. Note the init frame points to queue info
3471 * structure. Each frame has SGL allocated after first 64 bytes. For
3472 * this frame - since we don't need any SGL - we use SGL's space as
3473 * queue info structure
3474 *
3475 * We will not get a NULL command below. We just created the pool.
3476 */
3477 cmd = megasas_get_cmd(instance);
3478
3479 init_frame = (struct megasas_init_frame *)cmd->frame;
3480 initq_info = (struct megasas_init_queue_info *)
3481 ((unsigned long)init_frame + 64);
3482
3483 init_frame_h = cmd->frame_phys_addr;
3484 initq_info_h = init_frame_h + 64;
3485
3486 context = init_frame->context;
3487 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3488 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3489 init_frame->context = context;
3490
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303491 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
3492 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05003493
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303494 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
3495 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05003496
3497 init_frame->cmd = MFI_CMD_INIT;
3498 init_frame->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303499 init_frame->queue_info_new_phys_addr_lo =
3500 cpu_to_le32(lower_32_bits(initq_info_h));
3501 init_frame->queue_info_new_phys_addr_hi =
3502 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05003503
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303504 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05003505
3506 /*
3507 * disable the intr before firing the init frame to FW
3508 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303509 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05003510
3511 /*
3512 * Issue the init frame in polled mode
3513 */
3514
3515 if (megasas_issue_polled(instance, cmd)) {
3516 printk(KERN_ERR "megasas: Failed to init firmware\n");
3517 megasas_return_cmd(instance, cmd);
3518 goto fail_fw_init;
3519 }
3520
3521 megasas_return_cmd(instance, cmd);
3522
3523 return 0;
3524
3525fail_fw_init:
3526 return -EINVAL;
3527}
3528
adam radfordcd50ba82010-12-21 10:23:23 -08003529static u32
3530megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003531{
adam radfordcd50ba82010-12-21 10:23:23 -08003532 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003533 u32 context_sz;
3534 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003535
3536 reg_set = instance->reg_set;
3537
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003538 /*
3539 * Get various operational parameters from status register
3540 */
Sumant Patro1341c932006-01-25 12:02:40 -08003541 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003542 /*
3543 * Reduce the max supported cmds by 1. This is to ensure that the
3544 * reply_q_sz (1 more than the max cmd that driver may send)
3545 * does not exceed max cmds that the FW can support
3546 */
3547 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003548 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003549 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003550 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003551 /*
3552 * Create a pool of commands
3553 */
3554 if (megasas_alloc_cmds(instance))
3555 goto fail_alloc_cmds;
3556
3557 /*
3558 * Allocate memory for reply queue. Length of reply queue should
3559 * be _one_ more than the maximum commands handled by the firmware.
3560 *
3561 * Note: When FW completes commands, it places corresponding contex
3562 * values in this circular reply queue. This circular queue is a fairly
3563 * typical producer-consumer queue. FW is the producer (of completed
3564 * commands) and the driver is the consumer.
3565 */
3566 context_sz = sizeof(u32);
3567 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3568
3569 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3570 reply_q_sz,
3571 &instance->reply_queue_h);
3572
3573 if (!instance->reply_queue) {
3574 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3575 goto fail_reply_queue;
3576 }
3577
bo yang31ea7082007-11-07 12:09:50 -05003578 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003579 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003580
bo yang39a98552010-09-22 22:36:29 -04003581 instance->fw_support_ieee = 0;
3582 instance->fw_support_ieee =
3583 (instance->instancet->read_fw_status_reg(reg_set) &
3584 0x04000000);
3585
3586 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3587 instance->fw_support_ieee);
3588
3589 if (instance->fw_support_ieee)
3590 instance->flag_ieee = 1;
3591
adam radfordcd50ba82010-12-21 10:23:23 -08003592 return 0;
3593
3594fail_fw_init:
3595
3596 pci_free_consistent(instance->pdev, reply_q_sz,
3597 instance->reply_queue, instance->reply_queue_h);
3598fail_reply_queue:
3599 megasas_free_cmds(instance);
3600
3601fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003602 return 1;
3603}
3604
3605/**
3606 * megasas_init_fw - Initializes the FW
3607 * @instance: Adapter soft state
3608 *
3609 * This is the main function for initializing firmware
3610 */
3611
3612static int megasas_init_fw(struct megasas_instance *instance)
3613{
3614 u32 max_sectors_1;
3615 u32 max_sectors_2;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303616 u32 tmp_sectors, msix_enable, scratch_pad_2;
adam radfordcd50ba82010-12-21 10:23:23 -08003617 struct megasas_register_set __iomem *reg_set;
3618 struct megasas_ctrl_info *ctrl_info;
3619 unsigned long bar_list;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303620 int i, loop, fw_msix_count = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08003621
3622 /* Find first memory bar */
3623 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3624 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3625 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3626 if (pci_request_selected_regions(instance->pdev, instance->bar,
3627 "megasas: LSI")) {
3628 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3629 return -EBUSY;
3630 }
3631
3632 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3633
3634 if (!instance->reg_set) {
3635 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3636 goto fail_ioremap;
3637 }
3638
3639 reg_set = instance->reg_set;
3640
3641 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003642 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07003643 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05303644 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08003645 instance->instancet = &megasas_instance_template_fusion;
3646 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003647 case PCI_DEVICE_ID_LSI_SAS1078R:
3648 case PCI_DEVICE_ID_LSI_SAS1078DE:
3649 instance->instancet = &megasas_instance_template_ppc;
3650 break;
3651 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3652 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3653 instance->instancet = &megasas_instance_template_gen2;
3654 break;
3655 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3656 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3657 instance->instancet = &megasas_instance_template_skinny;
3658 break;
3659 case PCI_DEVICE_ID_LSI_SAS1064R:
3660 case PCI_DEVICE_ID_DELL_PERC5:
3661 default:
3662 instance->instancet = &megasas_instance_template_xscale;
3663 break;
3664 }
3665
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05303666 if (megasas_transition_to_ready(instance, 0)) {
3667 atomic_set(&instance->fw_reset_no_pci_access, 1);
3668 instance->instancet->adp_reset
3669 (instance, instance->reg_set);
3670 atomic_set(&instance->fw_reset_no_pci_access, 0);
3671 dev_info(&instance->pdev->dev,
3672 "megasas: FW restarted successfully from %s!\n",
3673 __func__);
3674
3675 /*waitting for about 30 second before retry*/
3676 ssleep(30);
3677
3678 if (megasas_transition_to_ready(instance, 0))
3679 goto fail_ready_state;
3680 }
adam radfordcd50ba82010-12-21 10:23:23 -08003681
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303682 /*
3683 * MSI-X host index 0 is common for all adapter.
3684 * It is used for all MPT based Adapters.
3685 */
3686 instance->reply_post_host_index_addr[0] =
3687 (u32 *)((u8 *)instance->reg_set +
3688 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
3689
adam radford3f1abce2011-05-11 18:33:47 -07003690 /* Check if MSI-X is supported while in ready state */
3691 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3692 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07003693 if (msix_enable && !msix_disable) {
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303694 scratch_pad_2 = readl
3695 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07003696 /* Check max MSI-X vectors */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303697 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
3698 instance->msix_vectors = (scratch_pad_2
3699 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
3700 fw_msix_count = instance->msix_vectors;
adam radford079eadd2012-10-01 19:26:59 -07003701 if (msix_vectors)
3702 instance->msix_vectors =
3703 min(msix_vectors,
3704 instance->msix_vectors);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303705 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
3706 || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
3707 /* Invader/Fury supports more than 8 MSI-X */
3708 instance->msix_vectors = ((scratch_pad_2
3709 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
3710 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
3711 fw_msix_count = instance->msix_vectors;
3712 /* Save 1-15 reply post index address to local memory
3713 * Index 0 is already saved from reg offset
3714 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
3715 */
3716 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
3717 instance->reply_post_host_index_addr[loop] =
3718 (u32 *)((u8 *)instance->reg_set +
3719 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
3720 + (loop * 0x10));
3721 }
3722 if (msix_vectors)
3723 instance->msix_vectors = min(msix_vectors,
3724 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07003725 } else
3726 instance->msix_vectors = 1;
3727 /* Don't bother allocating more MSI-X vectors than cpus */
3728 instance->msix_vectors = min(instance->msix_vectors,
3729 (unsigned int)num_online_cpus());
3730 for (i = 0; i < instance->msix_vectors; i++)
3731 instance->msixentry[i].entry = i;
3732 i = pci_enable_msix(instance->pdev, instance->msixentry,
3733 instance->msix_vectors);
3734 if (i >= 0) {
3735 if (i) {
3736 if (!pci_enable_msix(instance->pdev,
3737 instance->msixentry, i))
3738 instance->msix_vectors = i;
3739 else
3740 instance->msix_vectors = 0;
3741 }
3742 } else
3743 instance->msix_vectors = 0;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303744
3745 dev_info(&instance->pdev->dev, "[scsi%d]: FW supports"
3746 "<%d> MSIX vector,Online CPUs: <%d>,"
3747 "Current MSIX <%d>\n", instance->host->host_no,
3748 fw_msix_count, (unsigned int)num_online_cpus(),
3749 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07003750 }
adam radford3f1abce2011-05-11 18:33:47 -07003751
adam radfordcd50ba82010-12-21 10:23:23 -08003752 /* Get operational params, sge flags, send init cmd to controller */
3753 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003754 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003755
3756 printk(KERN_ERR "megasas: INIT adapter done\n");
3757
bo yang39a98552010-09-22 22:36:29 -04003758 /** for passthrough
3759 * the following function will get the PD LIST.
3760 */
3761
Yang, Bo81e403c2009-10-06 14:27:54 -06003762 memset(instance->pd_list, 0 ,
3763 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3764 megasas_get_pd_list(instance);
3765
Yang, Bobdc6fb82009-12-06 08:30:19 -07003766 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
adam radford21c9e162013-09-06 15:27:14 -07003767 if (megasas_ld_list_query(instance,
3768 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
3769 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07003770
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003771 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3772
3773 /*
3774 * Compute the max allowed sectors per IO: The controller info has two
3775 * limits on max sectors. Driver should use the minimum of these two.
3776 *
3777 * 1 << stripe_sz_ops.min = max sectors per strip
3778 *
3779 * Note that older firmwares ( < FW ver 30) didn't report information
3780 * to calculate max_sectors_1. So the number ended up as zero always.
3781 */
bo yang14faea92007-11-09 04:14:00 -05003782 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003783 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3784
3785 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303786 le16_to_cpu(ctrl_info->max_strips_per_io);
3787 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003788
bo yang14faea92007-11-09 04:14:00 -05003789 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05303790
3791 /*Check whether controller is iMR or MR */
3792 if (ctrl_info->memory_size) {
3793 instance->is_imr = 0;
3794 dev_info(&instance->pdev->dev, "Controller type: MR,"
3795 "Memory size is: %dMB\n",
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303796 le16_to_cpu(ctrl_info->memory_size));
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05303797 } else {
3798 instance->is_imr = 1;
3799 dev_info(&instance->pdev->dev,
3800 "Controller type: iMR\n");
3801 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303802 /* OnOffProperties are converted into CPU arch*/
3803 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
bo yang39a98552010-09-22 22:36:29 -04003804 instance->disableOnlineCtrlReset =
3805 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303806 /* adapterOperations2 are converted into CPU arch*/
3807 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303808 instance->UnevenSpanSupport =
3809 ctrl_info->adapterOperations2.supportUnevenSpans;
3810 if (instance->UnevenSpanSupport) {
3811 struct fusion_context *fusion = instance->ctrl_context;
3812 dev_info(&instance->pdev->dev, "FW supports: "
3813 "UnevenSpanSupport=%x\n", instance->UnevenSpanSupport);
3814 if (MR_ValidateMapInfo(instance))
3815 fusion->fast_path_io = 1;
3816 else
3817 fusion->fast_path_io = 0;
3818
3819 }
bo yang14faea92007-11-09 04:14:00 -05003820 }
bo yang14faea92007-11-09 04:14:00 -05003821 instance->max_sectors_per_req = instance->max_num_sge *
3822 PAGE_SIZE / 512;
3823 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3824 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003825
3826 kfree(ctrl_info);
3827
adam radfordc5daa6a2012-07-17 18:20:03 -07003828 /* Check for valid throttlequeuedepth module parameter */
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05303829 if (instance->is_imr) {
adam radfordc5daa6a2012-07-17 18:20:03 -07003830 if (throttlequeuedepth > (instance->max_fw_cmds -
3831 MEGASAS_SKINNY_INT_CMDS))
3832 instance->throttlequeuedepth =
3833 MEGASAS_THROTTLE_QUEUE_DEPTH;
3834 else
3835 instance->throttlequeuedepth = throttlequeuedepth;
3836 } else {
3837 if (throttlequeuedepth > (instance->max_fw_cmds -
3838 MEGASAS_INT_CMDS))
3839 instance->throttlequeuedepth =
3840 MEGASAS_THROTTLE_QUEUE_DEPTH;
3841 else
3842 instance->throttlequeuedepth = throttlequeuedepth;
3843 }
3844
Sumant Patro5d018ad2006-10-03 13:13:18 -07003845 /*
3846 * Setup tasklet for cmd completion
3847 */
3848
adam radfordf86c5422011-02-24 20:57:00 -08003849 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003850 (unsigned long)instance);
3851
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003852 return 0;
3853
adam radfordeb1b1232011-02-24 20:55:56 -08003854fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003855fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003856 iounmap(instance->reg_set);
3857
3858 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003859 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003860
3861 return -EINVAL;
3862}
3863
3864/**
3865 * megasas_release_mfi - Reverses the FW initialization
3866 * @intance: Adapter soft state
3867 */
3868static void megasas_release_mfi(struct megasas_instance *instance)
3869{
adam radford9c915a82010-12-21 13:34:31 -08003870 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003871
adam radford9c915a82010-12-21 13:34:31 -08003872 if (instance->reply_queue)
3873 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003874 instance->reply_queue, instance->reply_queue_h);
3875
3876 megasas_free_cmds(instance);
3877
3878 iounmap(instance->reg_set);
3879
adam radfordb6d5d882010-12-14 18:56:07 -08003880 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003881}
3882
3883/**
3884 * megasas_get_seq_num - Gets latest event sequence numbers
3885 * @instance: Adapter soft state
3886 * @eli: FW event log sequence numbers information
3887 *
3888 * FW maintains a log of all events in a non-volatile area. Upper layers would
3889 * usually find out the latest sequence number of the events, the seq number at
3890 * the boot etc. They would "read" all the events below the latest seq number
3891 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3892 * number), they would subsribe to AEN (asynchronous event notification) and
3893 * wait for the events to happen.
3894 */
3895static int
3896megasas_get_seq_num(struct megasas_instance *instance,
3897 struct megasas_evt_log_info *eli)
3898{
3899 struct megasas_cmd *cmd;
3900 struct megasas_dcmd_frame *dcmd;
3901 struct megasas_evt_log_info *el_info;
3902 dma_addr_t el_info_h = 0;
3903
3904 cmd = megasas_get_cmd(instance);
3905
3906 if (!cmd) {
3907 return -ENOMEM;
3908 }
3909
3910 dcmd = &cmd->frame->dcmd;
3911 el_info = pci_alloc_consistent(instance->pdev,
3912 sizeof(struct megasas_evt_log_info),
3913 &el_info_h);
3914
3915 if (!el_info) {
3916 megasas_return_cmd(instance, cmd);
3917 return -ENOMEM;
3918 }
3919
3920 memset(el_info, 0, sizeof(*el_info));
3921 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3922
3923 dcmd->cmd = MFI_CMD_DCMD;
3924 dcmd->cmd_status = 0x0;
3925 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303926 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003927 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003928 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303929 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
3930 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
3931 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
3932 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003933
3934 megasas_issue_blocked_cmd(instance, cmd);
3935
3936 /*
3937 * Copy the data back into callers buffer
3938 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303939 eli->newest_seq_num = le32_to_cpu(el_info->newest_seq_num);
3940 eli->oldest_seq_num = le32_to_cpu(el_info->oldest_seq_num);
3941 eli->clear_seq_num = le32_to_cpu(el_info->clear_seq_num);
3942 eli->shutdown_seq_num = le32_to_cpu(el_info->shutdown_seq_num);
3943 eli->boot_seq_num = le32_to_cpu(el_info->boot_seq_num);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003944
3945 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3946 el_info, el_info_h);
3947
3948 megasas_return_cmd(instance, cmd);
3949
3950 return 0;
3951}
3952
3953/**
3954 * megasas_register_aen - Registers for asynchronous event notification
3955 * @instance: Adapter soft state
3956 * @seq_num: The starting sequence number
3957 * @class_locale: Class of the event
3958 *
3959 * This function subscribes for AEN for events beyond the @seq_num. It requests
3960 * to be notified if and only if the event is of type @class_locale
3961 */
3962static int
3963megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3964 u32 class_locale_word)
3965{
3966 int ret_val;
3967 struct megasas_cmd *cmd;
3968 struct megasas_dcmd_frame *dcmd;
3969 union megasas_evt_class_locale curr_aen;
3970 union megasas_evt_class_locale prev_aen;
3971
3972 /*
3973 * If there an AEN pending already (aen_cmd), check if the
3974 * class_locale of that pending AEN is inclusive of the new
3975 * AEN request we currently have. If it is, then we don't have
3976 * to do anything. In other words, whichever events the current
3977 * AEN request is subscribing to, have already been subscribed
3978 * to.
3979 *
3980 * If the old_cmd is _not_ inclusive, then we have to abort
3981 * that command, form a class_locale that is superset of both
3982 * old and current and re-issue to the FW
3983 */
3984
3985 curr_aen.word = class_locale_word;
3986
3987 if (instance->aen_cmd) {
3988
3989 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303990 prev_aen.members.locale = le16_to_cpu(prev_aen.members.locale);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003991
3992 /*
3993 * A class whose enum value is smaller is inclusive of all
3994 * higher values. If a PROGRESS (= -1) was previously
3995 * registered, then a new registration requests for higher
3996 * classes need not be sent to FW. They are automatically
3997 * included.
3998 *
3999 * Locale numbers don't have such hierarchy. They are bitmap
4000 * values
4001 */
4002 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304003 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004004 curr_aen.members.locale)) {
4005 /*
4006 * Previously issued event registration includes
4007 * current request. Nothing to do.
4008 */
4009 return 0;
4010 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304011 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004012
4013 if (prev_aen.members.class < curr_aen.members.class)
4014 curr_aen.members.class = prev_aen.members.class;
4015
4016 instance->aen_cmd->abort_aen = 1;
4017 ret_val = megasas_issue_blocked_abort_cmd(instance,
4018 instance->
4019 aen_cmd);
4020
4021 if (ret_val) {
4022 printk(KERN_DEBUG "megasas: Failed to abort "
4023 "previous AEN command\n");
4024 return ret_val;
4025 }
4026 }
4027 }
4028
4029 cmd = megasas_get_cmd(instance);
4030
4031 if (!cmd)
4032 return -ENOMEM;
4033
4034 dcmd = &cmd->frame->dcmd;
4035
4036 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4037
4038 /*
4039 * Prepare DCMD for aen registration
4040 */
4041 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4042
4043 dcmd->cmd = MFI_CMD_DCMD;
4044 dcmd->cmd_status = 0x0;
4045 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304046 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004047 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004048 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304049 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4050 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4051 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04004052 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304053 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4054 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4055 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004056
Yang, Bof4c9a132009-10-06 14:43:28 -06004057 if (instance->aen_cmd != NULL) {
4058 megasas_return_cmd(instance, cmd);
4059 return 0;
4060 }
4061
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004062 /*
4063 * Store reference to the cmd used to register for AEN. When an
4064 * application wants us to register for AEN, we have to abort this
4065 * cmd and re-register with a new EVENT LOCALE supplied by that app
4066 */
4067 instance->aen_cmd = cmd;
4068
4069 /*
4070 * Issue the aen registration frame
4071 */
adam radford9c915a82010-12-21 13:34:31 -08004072 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004073
4074 return 0;
4075}
4076
4077/**
4078 * megasas_start_aen - Subscribes to AEN during driver load time
4079 * @instance: Adapter soft state
4080 */
4081static int megasas_start_aen(struct megasas_instance *instance)
4082{
4083 struct megasas_evt_log_info eli;
4084 union megasas_evt_class_locale class_locale;
4085
4086 /*
4087 * Get the latest sequence number from FW
4088 */
4089 memset(&eli, 0, sizeof(eli));
4090
4091 if (megasas_get_seq_num(instance, &eli))
4092 return -1;
4093
4094 /*
4095 * Register AEN with FW for latest sequence number plus 1
4096 */
4097 class_locale.members.reserved = 0;
4098 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4099 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4100
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304101 return megasas_register_aen(instance,
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05304102 eli.newest_seq_num + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304103 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004104}
4105
4106/**
4107 * megasas_io_attach - Attaches this driver to SCSI mid-layer
4108 * @instance: Adapter soft state
4109 */
4110static int megasas_io_attach(struct megasas_instance *instance)
4111{
4112 struct Scsi_Host *host = instance->host;
4113
4114 /*
4115 * Export parameters required by SCSI mid-layer
4116 */
4117 host->irq = instance->pdev->irq;
4118 host->unique_id = instance->unique_id;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304119 if (instance->is_imr) {
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004120 host->can_queue =
4121 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
4122 } else
4123 host->can_queue =
4124 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004125 host->this_id = instance->init_id;
4126 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08004127
4128 if (instance->fw_support_ieee)
4129 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
4130
Yang, Bo1fd10682010-10-12 07:18:50 -06004131 /*
4132 * Check if the module parameter value for max_sectors can be used
4133 */
4134 if (max_sectors && max_sectors < instance->max_sectors_per_req)
4135 instance->max_sectors_per_req = max_sectors;
4136 else {
4137 if (max_sectors) {
4138 if (((instance->pdev->device ==
4139 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
4140 (instance->pdev->device ==
4141 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
4142 (max_sectors <= MEGASAS_MAX_SECTORS)) {
4143 instance->max_sectors_per_req = max_sectors;
4144 } else {
4145 printk(KERN_INFO "megasas: max_sectors should be > 0"
4146 "and <= %d (or < 1MB for GEN2 controller)\n",
4147 instance->max_sectors_per_req);
4148 }
4149 }
4150 }
4151
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004152 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08004153 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004154 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
4155 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
4156 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08004157 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004158
adam radford9c915a82010-12-21 13:34:31 -08004159 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07004160 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304161 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4162 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford9c915a82010-12-21 13:34:31 -08004163 host->hostt->eh_device_reset_handler = NULL;
4164 host->hostt->eh_bus_reset_handler = NULL;
4165 }
4166
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004167 /*
4168 * Notify the mid-layer about the new controller
4169 */
4170 if (scsi_add_host(host, &instance->pdev->dev)) {
4171 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
4172 return -ENODEV;
4173 }
4174
4175 /*
4176 * Trigger SCSI to scan our drives
4177 */
4178 scsi_scan_host(host);
4179 return 0;
4180}
4181
bo yang31ea7082007-11-07 12:09:50 -05004182static int
4183megasas_set_dma_mask(struct pci_dev *pdev)
4184{
4185 /*
4186 * All our contollers are capable of performing 64-bit DMA
4187 */
4188 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07004189 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05004190
Yang Hongyang284901a2009-04-06 19:01:15 -07004191 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05004192 goto fail_set_dma_mask;
4193 }
4194 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004195 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05004196 goto fail_set_dma_mask;
4197 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304198
bo yang31ea7082007-11-07 12:09:50 -05004199 return 0;
4200
4201fail_set_dma_mask:
4202 return 1;
4203}
4204
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004205/**
4206 * megasas_probe_one - PCI hotplug entry point
4207 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08004208 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004209 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004210static int megasas_probe_one(struct pci_dev *pdev,
4211 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004212{
adam radfordc8e858f2011-10-08 18:15:13 -07004213 int rval, pos, i, j;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004214 struct Scsi_Host *host;
4215 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08004216 u16 control = 0;
4217
4218 /* Reset MSI-X in the kdump kernel */
4219 if (reset_devices) {
4220 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
4221 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06004222 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08004223 &control);
4224 if (control & PCI_MSIX_FLAGS_ENABLE) {
4225 dev_info(&pdev->dev, "resetting MSI-X\n");
4226 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06004227 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08004228 control &
4229 ~PCI_MSIX_FLAGS_ENABLE);
4230 }
4231 }
4232 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004233
4234 /*
4235 * Announce PCI information
4236 */
4237 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4238 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4239 pdev->subsystem_device);
4240
4241 printk("bus %d:slot %d:func %d\n",
4242 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4243
4244 /*
4245 * PCI prepping: enable device set bus mastering and dma mask
4246 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004247 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004248
4249 if (rval) {
4250 return rval;
4251 }
4252
4253 pci_set_master(pdev);
4254
bo yang31ea7082007-11-07 12:09:50 -05004255 if (megasas_set_dma_mask(pdev))
4256 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004257
4258 host = scsi_host_alloc(&megasas_template,
4259 sizeof(struct megasas_instance));
4260
4261 if (!host) {
4262 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4263 goto fail_alloc_instance;
4264 }
4265
4266 instance = (struct megasas_instance *)host->hostdata;
4267 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04004268 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08004269 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004270
adam radford9c915a82010-12-21 13:34:31 -08004271 switch (instance->pdev->device) {
4272 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004273 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304274 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08004275 {
4276 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004277
adam radford9c915a82010-12-21 13:34:31 -08004278 instance->ctrl_context =
4279 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4280 if (!instance->ctrl_context) {
4281 printk(KERN_DEBUG "megasas: Failed to allocate "
4282 "memory for Fusion context info\n");
4283 goto fail_alloc_dma_buf;
4284 }
4285 fusion = instance->ctrl_context;
4286 INIT_LIST_HEAD(&fusion->cmd_pool);
4287 spin_lock_init(&fusion->cmd_pool_lock);
4288 }
4289 break;
4290 default: /* For all other supported controllers */
4291
4292 instance->producer =
4293 pci_alloc_consistent(pdev, sizeof(u32),
4294 &instance->producer_h);
4295 instance->consumer =
4296 pci_alloc_consistent(pdev, sizeof(u32),
4297 &instance->consumer_h);
4298
4299 if (!instance->producer || !instance->consumer) {
4300 printk(KERN_DEBUG "megasas: Failed to allocate"
4301 "memory for producer, consumer\n");
4302 goto fail_alloc_dma_buf;
4303 }
4304
4305 *instance->producer = 0;
4306 *instance->consumer = 0;
4307 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004308 }
4309
Yang, Boc3518832009-10-06 14:18:02 -06004310 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004311 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004312 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004313 instance->issuepend_done = 1;
4314 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05304315 instance->is_imr = 0;
bo yang39a98552010-09-22 22:36:29 -04004316 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004317
4318 instance->evt_detail = pci_alloc_consistent(pdev,
4319 sizeof(struct
4320 megasas_evt_detail),
4321 &instance->evt_detail_h);
4322
4323 if (!instance->evt_detail) {
4324 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4325 "event detail structure\n");
4326 goto fail_alloc_dma_buf;
4327 }
4328
4329 /*
4330 * Initialize locks and queues
4331 */
4332 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004333 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004334
Sumant Patroe4a082c2006-05-30 12:03:37 -07004335 atomic_set(&instance->fw_outstanding,0);
4336
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004337 init_waitqueue_head(&instance->int_cmd_wait_q);
4338 init_waitqueue_head(&instance->abort_cmd_wait_q);
4339
4340 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004341 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004342 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004343
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004344 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004345 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004346
4347 /*
4348 * Initialize PCI related and misc parameters
4349 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004350 instance->host = host;
4351 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4352 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4353
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004354 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4355 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004356 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004357 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4358 } else
4359 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4360
Sumant Patro658dced2006-10-03 13:09:14 -07004361 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004362 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004363 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004364 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004365 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05304366 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04004367
adam radford36807e62011-10-08 18:15:06 -07004368 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304369 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4370 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radford9c915a82010-12-21 13:34:31 -08004371 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4372 else
4373 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004374
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004375 /*
adam radford0a770662011-02-24 20:56:12 -08004376 * Initialize MFI Firmware
4377 */
4378 if (megasas_init_fw(instance))
4379 goto fail_init_mfi;
4380
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304381retry_irq_register:
adam radford0a770662011-02-24 20:56:12 -08004382 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004383 * Register IRQ
4384 */
adam radfordc8e858f2011-10-08 18:15:13 -07004385 if (instance->msix_vectors) {
4386 for (i = 0 ; i < instance->msix_vectors; i++) {
4387 instance->irq_context[i].instance = instance;
4388 instance->irq_context[i].MSIxIndex = i;
4389 if (request_irq(instance->msixentry[i].vector,
4390 instance->instancet->service_isr, 0,
4391 "megasas",
4392 &instance->irq_context[i])) {
4393 printk(KERN_DEBUG "megasas: Failed to "
4394 "register IRQ for vector %d.\n", i);
4395 for (j = 0 ; j < i ; j++)
4396 free_irq(
4397 instance->msixentry[j].vector,
4398 &instance->irq_context[j]);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304399 /* Retry irq register for IO_APIC */
4400 instance->msix_vectors = 0;
4401 goto retry_irq_register;
adam radfordc8e858f2011-10-08 18:15:13 -07004402 }
4403 }
4404 } else {
4405 instance->irq_context[0].instance = instance;
4406 instance->irq_context[0].MSIxIndex = 0;
4407 if (request_irq(pdev->irq, instance->instancet->service_isr,
4408 IRQF_SHARED, "megasas",
4409 &instance->irq_context[0])) {
4410 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4411 goto fail_irq;
4412 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004413 }
4414
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304415 instance->instancet->enable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004416
4417 /*
4418 * Store instance in PCI softstate
4419 */
4420 pci_set_drvdata(pdev, instance);
4421
4422 /*
4423 * Add this controller to megasas_mgmt_info structure so that it
4424 * can be exported to management applications
4425 */
4426 megasas_mgmt_info.count++;
4427 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4428 megasas_mgmt_info.max_index++;
4429
4430 /*
adam radford541f90b2011-05-11 18:34:29 -07004431 * Register with SCSI mid-layer
4432 */
4433 if (megasas_io_attach(instance))
4434 goto fail_io_attach;
4435
4436 instance->unload = 0;
4437
4438 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004439 * Initiate AEN (Asynchronous Event Notification)
4440 */
4441 if (megasas_start_aen(instance)) {
4442 printk(KERN_DEBUG "megasas: start aen failed\n");
4443 goto fail_start_aen;
4444 }
4445
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004446 return 0;
4447
4448 fail_start_aen:
4449 fail_io_attach:
4450 megasas_mgmt_info.count--;
4451 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4452 megasas_mgmt_info.max_index--;
4453
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304454 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07004455 if (instance->msix_vectors)
4456 for (i = 0 ; i < instance->msix_vectors; i++)
4457 free_irq(instance->msixentry[i].vector,
4458 &instance->irq_context[i]);
4459 else
4460 free_irq(instance->pdev->irq, &instance->irq_context[0]);
adam radfordeb1b1232011-02-24 20:55:56 -08004461fail_irq:
adam radford36807e62011-10-08 18:15:06 -07004462 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304463 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4464 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
adam radfordeb1b1232011-02-24 20:55:56 -08004465 megasas_release_fusion(instance);
4466 else
4467 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004468 fail_init_mfi:
adam radfordc8e858f2011-10-08 18:15:13 -07004469 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08004470 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004471 fail_alloc_dma_buf:
4472 if (instance->evt_detail)
4473 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4474 instance->evt_detail,
4475 instance->evt_detail_h);
4476
adam radfordeb1b1232011-02-24 20:55:56 -08004477 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004478 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4479 instance->producer_h);
4480 if (instance->consumer)
4481 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4482 instance->consumer_h);
4483 scsi_host_put(host);
4484
4485 fail_alloc_instance:
4486 fail_set_dma_mask:
4487 pci_disable_device(pdev);
4488
4489 return -ENODEV;
4490}
4491
4492/**
4493 * megasas_flush_cache - Requests FW to flush all its caches
4494 * @instance: Adapter soft state
4495 */
4496static void megasas_flush_cache(struct megasas_instance *instance)
4497{
4498 struct megasas_cmd *cmd;
4499 struct megasas_dcmd_frame *dcmd;
4500
bo yang39a98552010-09-22 22:36:29 -04004501 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4502 return;
4503
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004504 cmd = megasas_get_cmd(instance);
4505
4506 if (!cmd)
4507 return;
4508
4509 dcmd = &cmd->frame->dcmd;
4510
4511 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4512
4513 dcmd->cmd = MFI_CMD_DCMD;
4514 dcmd->cmd_status = 0x0;
4515 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304516 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004517 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004518 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004519 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304520 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004521 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4522
4523 megasas_issue_blocked_cmd(instance, cmd);
4524
4525 megasas_return_cmd(instance, cmd);
4526
4527 return;
4528}
4529
4530/**
4531 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4532 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004533 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004534 */
bo yang31ea7082007-11-07 12:09:50 -05004535static void megasas_shutdown_controller(struct megasas_instance *instance,
4536 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004537{
4538 struct megasas_cmd *cmd;
4539 struct megasas_dcmd_frame *dcmd;
4540
bo yang39a98552010-09-22 22:36:29 -04004541 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4542 return;
4543
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004544 cmd = megasas_get_cmd(instance);
4545
4546 if (!cmd)
4547 return;
4548
4549 if (instance->aen_cmd)
4550 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004551 if (instance->map_update_cmd)
4552 megasas_issue_blocked_abort_cmd(instance,
4553 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004554 dcmd = &cmd->frame->dcmd;
4555
4556 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4557
4558 dcmd->cmd = MFI_CMD_DCMD;
4559 dcmd->cmd_status = 0x0;
4560 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304561 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004562 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004563 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004564 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304565 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004566
4567 megasas_issue_blocked_cmd(instance, cmd);
4568
4569 megasas_return_cmd(instance, cmd);
4570
4571 return;
4572}
4573
Jiri Slaby33139b22008-05-01 17:56:02 +02004574#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004575/**
bo yangad84db22007-11-09 04:40:16 -05004576 * megasas_suspend - driver suspend entry point
4577 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004578 * @state: PCI power state to suspend routine
4579 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004580static int
bo yang31ea7082007-11-07 12:09:50 -05004581megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4582{
4583 struct Scsi_Host *host;
4584 struct megasas_instance *instance;
adam radfordc8e858f2011-10-08 18:15:13 -07004585 int i;
bo yang31ea7082007-11-07 12:09:50 -05004586
4587 instance = pci_get_drvdata(pdev);
4588 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004589 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004590
4591 megasas_flush_cache(instance);
4592 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004593
4594 /* cancel the delayed work if this work still in queue */
4595 if (instance->ev != NULL) {
4596 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08004597 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004598 instance->ev = NULL;
4599 }
4600
bo yang31ea7082007-11-07 12:09:50 -05004601 tasklet_kill(&instance->isr_tasklet);
4602
4603 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304604 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07004605
4606 if (instance->msix_vectors)
4607 for (i = 0 ; i < instance->msix_vectors; i++)
4608 free_irq(instance->msixentry[i].vector,
4609 &instance->irq_context[i]);
4610 else
4611 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4612 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004613 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004614
4615 pci_save_state(pdev);
4616 pci_disable_device(pdev);
4617
4618 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4619
4620 return 0;
4621}
4622
4623/**
4624 * megasas_resume- driver resume entry point
4625 * @pdev: PCI device structure
4626 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004627static int
bo yang31ea7082007-11-07 12:09:50 -05004628megasas_resume(struct pci_dev *pdev)
4629{
adam radfordc8e858f2011-10-08 18:15:13 -07004630 int rval, i, j;
bo yang31ea7082007-11-07 12:09:50 -05004631 struct Scsi_Host *host;
4632 struct megasas_instance *instance;
4633
4634 instance = pci_get_drvdata(pdev);
4635 host = instance->host;
4636 pci_set_power_state(pdev, PCI_D0);
4637 pci_enable_wake(pdev, PCI_D0, 0);
4638 pci_restore_state(pdev);
4639
4640 /*
4641 * PCI prepping: enable device set bus mastering and dma mask
4642 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004643 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004644
4645 if (rval) {
4646 printk(KERN_ERR "megasas: Enable device failed\n");
4647 return rval;
4648 }
4649
4650 pci_set_master(pdev);
4651
4652 if (megasas_set_dma_mask(pdev))
4653 goto fail_set_dma_mask;
4654
4655 /*
4656 * Initialize MFI Firmware
4657 */
4658
bo yang31ea7082007-11-07 12:09:50 -05004659 atomic_set(&instance->fw_outstanding, 0);
4660
4661 /*
4662 * We expect the FW state to be READY
4663 */
adam radford058a8fa2011-10-08 18:14:27 -07004664 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05004665 goto fail_ready_state;
4666
adam radford3f1abce2011-05-11 18:33:47 -07004667 /* Now re-enable MSI-X */
adam radfordc8e858f2011-10-08 18:15:13 -07004668 if (instance->msix_vectors)
4669 pci_enable_msix(instance->pdev, instance->msixentry,
4670 instance->msix_vectors);
adam radford3f1abce2011-05-11 18:33:47 -07004671
adam radford9c915a82010-12-21 13:34:31 -08004672 switch (instance->pdev->device) {
4673 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004674 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304675 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08004676 {
4677 megasas_reset_reply_desc(instance);
4678 if (megasas_ioc_init_fusion(instance)) {
4679 megasas_free_cmds(instance);
4680 megasas_free_cmds_fusion(instance);
4681 goto fail_init_mfi;
4682 }
4683 if (!megasas_get_map_info(instance))
4684 megasas_sync_map_info(instance);
4685 }
4686 break;
4687 default:
4688 *instance->producer = 0;
4689 *instance->consumer = 0;
4690 if (megasas_issue_init_mfi(instance))
4691 goto fail_init_mfi;
4692 break;
4693 }
bo yang31ea7082007-11-07 12:09:50 -05004694
adam radford9c915a82010-12-21 13:34:31 -08004695 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4696 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004697
4698 /*
4699 * Register IRQ
4700 */
adam radfordc8e858f2011-10-08 18:15:13 -07004701 if (instance->msix_vectors) {
4702 for (i = 0 ; i < instance->msix_vectors; i++) {
4703 instance->irq_context[i].instance = instance;
4704 instance->irq_context[i].MSIxIndex = i;
4705 if (request_irq(instance->msixentry[i].vector,
4706 instance->instancet->service_isr, 0,
4707 "megasas",
4708 &instance->irq_context[i])) {
4709 printk(KERN_DEBUG "megasas: Failed to "
4710 "register IRQ for vector %d.\n", i);
4711 for (j = 0 ; j < i ; j++)
4712 free_irq(
4713 instance->msixentry[j].vector,
4714 &instance->irq_context[j]);
4715 goto fail_irq;
4716 }
4717 }
4718 } else {
4719 instance->irq_context[0].instance = instance;
4720 instance->irq_context[0].MSIxIndex = 0;
4721 if (request_irq(pdev->irq, instance->instancet->service_isr,
4722 IRQF_SHARED, "megasas",
4723 &instance->irq_context[0])) {
4724 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4725 goto fail_irq;
4726 }
bo yang31ea7082007-11-07 12:09:50 -05004727 }
4728
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304729 instance->instancet->enable_intr(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06004730 instance->unload = 0;
4731
adam radford541f90b2011-05-11 18:34:29 -07004732 /*
4733 * Initiate AEN (Asynchronous Event Notification)
4734 */
4735 if (megasas_start_aen(instance))
4736 printk(KERN_ERR "megasas: Start AEN failed\n");
4737
bo yang31ea7082007-11-07 12:09:50 -05004738 return 0;
4739
4740fail_irq:
4741fail_init_mfi:
4742 if (instance->evt_detail)
4743 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4744 instance->evt_detail,
4745 instance->evt_detail_h);
4746
4747 if (instance->producer)
4748 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4749 instance->producer_h);
4750 if (instance->consumer)
4751 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4752 instance->consumer_h);
4753 scsi_host_put(host);
4754
4755fail_set_dma_mask:
4756fail_ready_state:
4757
4758 pci_disable_device(pdev);
4759
4760 return -ENODEV;
4761}
Jiri Slaby33139b22008-05-01 17:56:02 +02004762#else
4763#define megasas_suspend NULL
4764#define megasas_resume NULL
4765#endif
bo yang31ea7082007-11-07 12:09:50 -05004766
4767/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004768 * megasas_detach_one - PCI hot"un"plug entry point
4769 * @pdev: PCI device structure
4770 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004771static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004772{
4773 int i;
4774 struct Scsi_Host *host;
4775 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004776 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004777
4778 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004779 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004780 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004781 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004782
4783 scsi_remove_host(instance->host);
4784 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004785 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004786
4787 /* cancel the delayed work if this work still in queue*/
4788 if (instance->ev != NULL) {
4789 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08004790 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004791 instance->ev = NULL;
4792 }
4793
Sumant Patro5d018ad2006-10-03 13:13:18 -07004794 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004795
4796 /*
4797 * Take the instance off the instance array. Note that we will not
4798 * decrement the max_index. We let this array be sparse array
4799 */
4800 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4801 if (megasas_mgmt_info.instance[i] == instance) {
4802 megasas_mgmt_info.count--;
4803 megasas_mgmt_info.instance[i] = NULL;
4804
4805 break;
4806 }
4807 }
4808
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304809 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004810
adam radfordc8e858f2011-10-08 18:15:13 -07004811 if (instance->msix_vectors)
4812 for (i = 0 ; i < instance->msix_vectors; i++)
4813 free_irq(instance->msixentry[i].vector,
4814 &instance->irq_context[i]);
4815 else
4816 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4817 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004818 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004819
adam radford9c915a82010-12-21 13:34:31 -08004820 switch (instance->pdev->device) {
4821 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004822 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05304823 case PCI_DEVICE_ID_LSI_FURY:
adam radford9c915a82010-12-21 13:34:31 -08004824 megasas_release_fusion(instance);
4825 for (i = 0; i < 2 ; i++)
4826 if (fusion->ld_map[i])
4827 dma_free_coherent(&instance->pdev->dev,
4828 fusion->map_sz,
4829 fusion->ld_map[i],
4830 fusion->
4831 ld_map_phys[i]);
4832 kfree(instance->ctrl_context);
4833 break;
4834 default:
4835 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08004836 pci_free_consistent(pdev, sizeof(u32),
4837 instance->producer,
4838 instance->producer_h);
4839 pci_free_consistent(pdev, sizeof(u32),
4840 instance->consumer,
4841 instance->consumer_h);
4842 break;
4843 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004844
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05304845 if (instance->evt_detail)
4846 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4847 instance->evt_detail, instance->evt_detail_h);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004848 scsi_host_put(host);
4849
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004850 pci_disable_device(pdev);
4851
4852 return;
4853}
4854
4855/**
4856 * megasas_shutdown - Shutdown entry point
4857 * @device: Generic device structure
4858 */
4859static void megasas_shutdown(struct pci_dev *pdev)
4860{
adam radfordc8e858f2011-10-08 18:15:13 -07004861 int i;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004862 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07004863
Yang, Bo0c79e682009-10-06 14:47:35 -06004864 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004865 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004866 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304867 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07004868 if (instance->msix_vectors)
4869 for (i = 0 ; i < instance->msix_vectors; i++)
4870 free_irq(instance->msixentry[i].vector,
4871 &instance->irq_context[i]);
4872 else
4873 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4874 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07004875 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004876}
4877
4878/**
4879 * megasas_mgmt_open - char node "open" entry point
4880 */
4881static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4882{
4883 /*
4884 * Allow only those users with admin rights
4885 */
4886 if (!capable(CAP_SYS_ADMIN))
4887 return -EACCES;
4888
4889 return 0;
4890}
4891
4892/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004893 * megasas_mgmt_fasync - Async notifier registration from applications
4894 *
4895 * This function adds the calling process to a driver global queue. When an
4896 * event occurs, SIGIO will be sent to all processes in this queue.
4897 */
4898static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4899{
4900 int rc;
4901
Arjan van de Ven0b950672006-01-11 13:16:10 +01004902 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004903
4904 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4905
Arjan van de Ven0b950672006-01-11 13:16:10 +01004906 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004907
4908 if (rc >= 0) {
4909 /* For sanity check when we get ioctl */
4910 filep->private_data = filep;
4911 return 0;
4912 }
4913
4914 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4915
4916 return rc;
4917}
4918
4919/**
Yang, Boc3518832009-10-06 14:18:02 -06004920 * megasas_mgmt_poll - char node "poll" entry point
4921 * */
4922static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4923{
4924 unsigned int mask;
4925 unsigned long flags;
4926 poll_wait(file, &megasas_poll_wait, wait);
4927 spin_lock_irqsave(&poll_aen_lock, flags);
4928 if (megasas_poll_wait_aen)
4929 mask = (POLLIN | POLLRDNORM);
4930 else
4931 mask = 0;
4932 spin_unlock_irqrestore(&poll_aen_lock, flags);
4933 return mask;
4934}
4935
4936/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004937 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4938 * @instance: Adapter soft state
4939 * @argp: User's ioctl packet
4940 */
4941static int
4942megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4943 struct megasas_iocpacket __user * user_ioc,
4944 struct megasas_iocpacket *ioc)
4945{
4946 struct megasas_sge32 *kern_sge32;
4947 struct megasas_cmd *cmd;
4948 void *kbuff_arr[MAX_IOCTL_SGE];
4949 dma_addr_t buf_handle = 0;
4950 int error = 0, i;
4951 void *sense = NULL;
4952 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004953 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004954
4955 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4956
4957 if (ioc->sge_count > MAX_IOCTL_SGE) {
4958 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4959 ioc->sge_count, MAX_IOCTL_SGE);
4960 return -EINVAL;
4961 }
4962
4963 cmd = megasas_get_cmd(instance);
4964 if (!cmd) {
4965 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4966 return -ENOMEM;
4967 }
4968
4969 /*
4970 * User's IOCTL packet has 2 frames (maximum). Copy those two
4971 * frames into our cmd's frames. cmd->frame's context will get
4972 * overwritten when we copy from user's frames. So set that value
4973 * alone separately
4974 */
4975 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304976 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06004977 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304978 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
4979 MFI_FRAME_SGL64 |
4980 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004981
4982 /*
4983 * The management interface between applications and the fw uses
4984 * MFI frames. E.g, RAID configuration changes, LD property changes
4985 * etc are accomplishes through different kinds of MFI frames. The
4986 * driver needs to care only about substituting user buffers with
4987 * kernel buffers in SGLs. The location of SGL is embedded in the
4988 * struct iocpacket itself.
4989 */
4990 kern_sge32 = (struct megasas_sge32 *)
4991 ((unsigned long)cmd->frame + ioc->sgl_off);
4992
4993 /*
4994 * For each user buffer, create a mirror buffer and copy in
4995 */
4996 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004997 if (!ioc->sgl[i].iov_len)
4998 continue;
4999
Sumant Patro9f35fa82007-02-14 12:55:45 -08005000 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005001 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08005002 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005003 if (!kbuff_arr[i]) {
5004 printk(KERN_DEBUG "megasas: Failed to alloc "
5005 "kernel SGL buffer for IOCTL \n");
5006 error = -ENOMEM;
5007 goto out;
5008 }
5009
5010 /*
5011 * We don't change the dma_coherent_mask, so
5012 * pci_alloc_consistent only returns 32bit addresses
5013 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305014 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
5015 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005016
5017 /*
5018 * We created a kernel buffer corresponding to the
5019 * user buffer. Now copy in from the user buffer
5020 */
5021 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
5022 (u32) (ioc->sgl[i].iov_len))) {
5023 error = -EFAULT;
5024 goto out;
5025 }
5026 }
5027
5028 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08005029 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
5030 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005031 if (!sense) {
5032 error = -ENOMEM;
5033 goto out;
5034 }
5035
5036 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06005037 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305038 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005039 }
5040
5041 /*
5042 * Set the sync_cmd flag so that the ISR knows not to complete this
5043 * cmd to the SCSI mid-layer
5044 */
5045 cmd->sync_cmd = 1;
5046 megasas_issue_blocked_cmd(instance, cmd);
5047 cmd->sync_cmd = 0;
5048
5049 /*
5050 * copy out the kernel buffers to user buffers
5051 */
5052 for (i = 0; i < ioc->sge_count; i++) {
5053 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
5054 ioc->sgl[i].iov_len)) {
5055 error = -EFAULT;
5056 goto out;
5057 }
5058 }
5059
5060 /*
5061 * copy out the sense
5062 */
5063 if (ioc->sense_len) {
5064 /*
bo yangb70a41e2008-03-18 03:13:06 -04005065 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005066 * sense buffer address
5067 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06005068 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
5069 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005070
bo yangb70a41e2008-03-18 03:13:06 -04005071 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
5072 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05005073 printk(KERN_ERR "megasas: Failed to copy out to user "
5074 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005075 error = -EFAULT;
5076 goto out;
5077 }
5078 }
5079
5080 /*
5081 * copy the status codes returned by the fw
5082 */
5083 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
5084 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
5085 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
5086 error = -EFAULT;
5087 }
5088
5089 out:
5090 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08005091 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005092 sense, sense_handle);
5093 }
5094
Bjørn Mork7a6a7312012-11-21 09:54:48 +01005095 for (i = 0; i < ioc->sge_count; i++) {
5096 if (kbuff_arr[i])
5097 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305098 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01005099 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305100 le32_to_cpu(kern_sge32[i].phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005101 }
5102
5103 megasas_return_cmd(instance, cmd);
5104 return error;
5105}
5106
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005107static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
5108{
5109 struct megasas_iocpacket __user *user_ioc =
5110 (struct megasas_iocpacket __user *)arg;
5111 struct megasas_iocpacket *ioc;
5112 struct megasas_instance *instance;
5113 int error;
bo yang39a98552010-09-22 22:36:29 -04005114 int i;
5115 unsigned long flags;
5116 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005117
5118 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
5119 if (!ioc)
5120 return -ENOMEM;
5121
5122 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
5123 error = -EFAULT;
5124 goto out_kfree_ioc;
5125 }
5126
5127 instance = megasas_lookup_instance(ioc->host_no);
5128 if (!instance) {
5129 error = -ENODEV;
5130 goto out_kfree_ioc;
5131 }
5132
bo yang39a98552010-09-22 22:36:29 -04005133 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
5134 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06005135 error = -ENODEV;
5136 goto out_kfree_ioc;
5137 }
5138
5139 if (instance->unload == 1) {
5140 error = -ENODEV;
5141 goto out_kfree_ioc;
5142 }
5143
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005144 /*
5145 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
5146 */
5147 if (down_interruptible(&instance->ioctl_sem)) {
5148 error = -ERESTARTSYS;
5149 goto out_kfree_ioc;
5150 }
bo yang39a98552010-09-22 22:36:29 -04005151
5152 for (i = 0; i < wait_time; i++) {
5153
5154 spin_lock_irqsave(&instance->hba_lock, flags);
5155 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
5156 spin_unlock_irqrestore(&instance->hba_lock, flags);
5157 break;
5158 }
5159 spin_unlock_irqrestore(&instance->hba_lock, flags);
5160
5161 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5162 printk(KERN_NOTICE "megasas: waiting"
5163 "for controller reset to finish\n");
5164 }
5165
5166 msleep(1000);
5167 }
5168
5169 spin_lock_irqsave(&instance->hba_lock, flags);
5170 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5171 spin_unlock_irqrestore(&instance->hba_lock, flags);
5172
5173 printk(KERN_ERR "megaraid_sas: timed out while"
5174 "waiting for HBA to recover\n");
5175 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03005176 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04005177 }
5178 spin_unlock_irqrestore(&instance->hba_lock, flags);
5179
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005180 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Dan Carpenterc64e4832013-04-16 10:44:19 +03005181 out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005182 up(&instance->ioctl_sem);
5183
5184 out_kfree_ioc:
5185 kfree(ioc);
5186 return error;
5187}
5188
5189static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
5190{
5191 struct megasas_instance *instance;
5192 struct megasas_aen aen;
5193 int error;
bo yang39a98552010-09-22 22:36:29 -04005194 int i;
5195 unsigned long flags;
5196 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005197
5198 if (file->private_data != file) {
5199 printk(KERN_DEBUG "megasas: fasync_helper was not "
5200 "called first\n");
5201 return -EINVAL;
5202 }
5203
5204 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
5205 return -EFAULT;
5206
5207 instance = megasas_lookup_instance(aen.host_no);
5208
5209 if (!instance)
5210 return -ENODEV;
5211
bo yang39a98552010-09-22 22:36:29 -04005212 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
5213 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06005214 }
5215
5216 if (instance->unload == 1) {
5217 return -ENODEV;
5218 }
5219
bo yang39a98552010-09-22 22:36:29 -04005220 for (i = 0; i < wait_time; i++) {
5221
5222 spin_lock_irqsave(&instance->hba_lock, flags);
5223 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
5224 spin_unlock_irqrestore(&instance->hba_lock,
5225 flags);
5226 break;
5227 }
5228
5229 spin_unlock_irqrestore(&instance->hba_lock, flags);
5230
5231 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5232 printk(KERN_NOTICE "megasas: waiting for"
5233 "controller reset to finish\n");
5234 }
5235
5236 msleep(1000);
5237 }
5238
5239 spin_lock_irqsave(&instance->hba_lock, flags);
5240 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5241 spin_unlock_irqrestore(&instance->hba_lock, flags);
5242 printk(KERN_ERR "megaraid_sas: timed out while waiting"
5243 "for HBA to recover.\n");
5244 return -ENODEV;
5245 }
5246 spin_unlock_irqrestore(&instance->hba_lock, flags);
5247
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005248 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005249 error = megasas_register_aen(instance, aen.seq_num,
5250 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005251 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005252 return error;
5253}
5254
5255/**
5256 * megasas_mgmt_ioctl - char node ioctl entry point
5257 */
5258static long
5259megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5260{
5261 switch (cmd) {
5262 case MEGASAS_IOC_FIRMWARE:
5263 return megasas_mgmt_ioctl_fw(file, arg);
5264
5265 case MEGASAS_IOC_GET_AEN:
5266 return megasas_mgmt_ioctl_aen(file, arg);
5267 }
5268
5269 return -ENOTTY;
5270}
5271
5272#ifdef CONFIG_COMPAT
5273static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5274{
5275 struct compat_megasas_iocpacket __user *cioc =
5276 (struct compat_megasas_iocpacket __user *)arg;
5277 struct megasas_iocpacket __user *ioc =
5278 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5279 int i;
5280 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005281 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005282
Jeff Garzik83aabc12006-10-04 06:34:03 -04005283 if (clear_user(ioc, sizeof(*ioc)))
5284 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005285
5286 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5287 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5288 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5289 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5290 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5291 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5292 return -EFAULT;
5293
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005294 /*
5295 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5296 * sense_len is not null, so prepare the 64bit value under
5297 * the same condition.
5298 */
5299 if (ioc->sense_len) {
5300 void __user **sense_ioc_ptr =
5301 (void __user **)(ioc->frame.raw + ioc->sense_off);
5302 compat_uptr_t *sense_cioc_ptr =
5303 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5304 if (get_user(ptr, sense_cioc_ptr) ||
5305 put_user(compat_ptr(ptr), sense_ioc_ptr))
5306 return -EFAULT;
5307 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005308
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005309 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005310 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5311 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5312 copy_in_user(&ioc->sgl[i].iov_len,
5313 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5314 return -EFAULT;
5315 }
5316
5317 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5318
5319 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5320 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5321 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5322 return -EFAULT;
5323 }
5324 return error;
5325}
5326
5327static long
5328megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5329 unsigned long arg)
5330{
5331 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08005332 case MEGASAS_IOC_FIRMWARE32:
5333 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005334 case MEGASAS_IOC_GET_AEN:
5335 return megasas_mgmt_ioctl_aen(file, arg);
5336 }
5337
5338 return -ENOTTY;
5339}
5340#endif
5341
5342/*
5343 * File operations structure for management interface
5344 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08005345static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005346 .owner = THIS_MODULE,
5347 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005348 .fasync = megasas_mgmt_fasync,
5349 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005350 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005351#ifdef CONFIG_COMPAT
5352 .compat_ioctl = megasas_mgmt_compat_ioctl,
5353#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005354 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005355};
5356
5357/*
5358 * PCI hotplug support registration structure
5359 */
5360static struct pci_driver megasas_pci_driver = {
5361
5362 .name = "megaraid_sas",
5363 .id_table = megasas_pci_table,
5364 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005365 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05005366 .suspend = megasas_suspend,
5367 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005368 .shutdown = megasas_shutdown,
5369};
5370
5371/*
5372 * Sysfs driver attributes
5373 */
5374static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5375{
5376 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5377 MEGASAS_VERSION);
5378}
5379
5380static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5381
5382static ssize_t
5383megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5384{
5385 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5386 MEGASAS_RELDATE);
5387}
5388
5389static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5390 NULL);
5391
Sumant Patro658dced2006-10-03 13:09:14 -07005392static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005393megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5394{
5395 return sprintf(buf, "%u\n", support_poll_for_event);
5396}
5397
5398static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5399 megasas_sysfs_show_support_poll_for_event, NULL);
5400
Yang, Bo837f5fe2010-10-11 06:59:20 -06005401 static ssize_t
5402megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5403{
5404 return sprintf(buf, "%u\n", support_device_change);
5405}
5406
5407static DRIVER_ATTR(support_device_change, S_IRUGO,
5408 megasas_sysfs_show_support_device_change, NULL);
5409
Yang, Bo72c4fd32009-10-06 14:20:59 -06005410static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005411megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5412{
bo yangad84db22007-11-09 04:40:16 -05005413 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005414}
5415
5416static ssize_t
5417megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5418{
5419 int retval = count;
5420 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5421 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5422 retval = -EINVAL;
5423 }
5424 return retval;
5425}
5426
Joe Malicki66dca9b2008-08-14 17:14:48 -04005427static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005428 megasas_sysfs_set_dbg_lvl);
5429
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005430static void
5431megasas_aen_polling(struct work_struct *work)
5432{
5433 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005434 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005435 struct megasas_instance *instance = ev->instance;
5436 union megasas_evt_class_locale class_locale;
5437 struct Scsi_Host *host;
5438 struct scsi_device *sdev1;
5439 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005440 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005441 int i, j, doscan = 0;
5442 u32 seq_num;
5443 int error;
5444
5445 if (!instance) {
5446 printk(KERN_ERR "invalid instance!\n");
5447 kfree(ev);
5448 return;
5449 }
5450 instance->ev = NULL;
5451 host = instance->host;
5452 if (instance->evt_detail) {
5453
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305454 switch (le32_to_cpu(instance->evt_detail->code)) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005455 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005456 if (megasas_get_pd_list(instance) == 0) {
5457 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5458 for (j = 0;
5459 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5460 j++) {
5461
5462 pd_index =
5463 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5464
5465 sdev1 =
5466 scsi_device_lookup(host, i, j, 0);
5467
5468 if (instance->pd_list[pd_index].driveState
5469 == MR_PD_STATE_SYSTEM) {
5470 if (!sdev1) {
5471 scsi_add_device(host, i, j, 0);
5472 }
5473
5474 if (sdev1)
5475 scsi_device_put(sdev1);
5476 }
5477 }
5478 }
5479 }
5480 doscan = 0;
5481 break;
5482
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005483 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005484 if (megasas_get_pd_list(instance) == 0) {
Yang, Boc9786842009-12-06 08:39:25 -07005485 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5486 for (j = 0;
5487 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5488 j++) {
5489
5490 pd_index =
5491 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5492
5493 sdev1 =
5494 scsi_device_lookup(host, i, j, 0);
5495
5496 if (instance->pd_list[pd_index].driveState
5497 == MR_PD_STATE_SYSTEM) {
5498 if (sdev1) {
5499 scsi_device_put(sdev1);
5500 }
5501 } else {
5502 if (sdev1) {
5503 scsi_remove_device(sdev1);
5504 scsi_device_put(sdev1);
5505 }
5506 }
5507 }
5508 }
5509 }
5510 doscan = 0;
5511 break;
5512
5513 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005514 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005515 case MR_EVT_LD_DELETED:
adam radford21c9e162013-09-06 15:27:14 -07005516 if (megasas_ld_list_query(instance,
5517 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5518 megasas_get_ld_list(instance);
Yang, Boc9786842009-12-06 08:39:25 -07005519 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5520 for (j = 0;
5521 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5522 j++) {
5523
5524 ld_index =
5525 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5526
5527 sdev1 = scsi_device_lookup(host,
adam radford21c9e162013-09-06 15:27:14 -07005528 MEGASAS_MAX_PD_CHANNELS + i,
Yang, Boc9786842009-12-06 08:39:25 -07005529 j,
5530 0);
5531
5532 if (instance->ld_ids[ld_index] != 0xff) {
5533 if (sdev1) {
5534 scsi_device_put(sdev1);
5535 }
5536 } else {
5537 if (sdev1) {
5538 scsi_remove_device(sdev1);
5539 scsi_device_put(sdev1);
5540 }
5541 }
5542 }
5543 }
5544 doscan = 0;
5545 break;
5546 case MR_EVT_LD_CREATED:
adam radford21c9e162013-09-06 15:27:14 -07005547 if (megasas_ld_list_query(instance,
5548 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5549 megasas_get_ld_list(instance);
Yang, Boc9786842009-12-06 08:39:25 -07005550 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5551 for (j = 0;
5552 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5553 j++) {
5554 ld_index =
5555 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5556
5557 sdev1 = scsi_device_lookup(host,
adam radford21c9e162013-09-06 15:27:14 -07005558 MEGASAS_MAX_PD_CHANNELS + i,
Yang, Boc9786842009-12-06 08:39:25 -07005559 j, 0);
5560
5561 if (instance->ld_ids[ld_index] !=
5562 0xff) {
5563 if (!sdev1) {
5564 scsi_add_device(host,
adam radford21c9e162013-09-06 15:27:14 -07005565 MEGASAS_MAX_PD_CHANNELS + i,
Yang, Boc9786842009-12-06 08:39:25 -07005566 j, 0);
5567 }
5568 }
5569 if (sdev1) {
5570 scsi_device_put(sdev1);
5571 }
5572 }
5573 }
5574 doscan = 0;
5575 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005576 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005577 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005578 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005579 doscan = 1;
5580 break;
5581 default:
5582 doscan = 0;
5583 break;
5584 }
5585 } else {
5586 printk(KERN_ERR "invalid evt_detail!\n");
5587 kfree(ev);
5588 return;
5589 }
5590
5591 if (doscan) {
5592 printk(KERN_INFO "scanning ...\n");
5593 megasas_get_pd_list(instance);
5594 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5595 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5596 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5597 sdev1 = scsi_device_lookup(host, i, j, 0);
5598 if (instance->pd_list[pd_index].driveState ==
5599 MR_PD_STATE_SYSTEM) {
5600 if (!sdev1) {
5601 scsi_add_device(host, i, j, 0);
5602 }
5603 if (sdev1)
5604 scsi_device_put(sdev1);
5605 } else {
5606 if (sdev1) {
5607 scsi_remove_device(sdev1);
5608 scsi_device_put(sdev1);
5609 }
5610 }
5611 }
5612 }
Yang, Boc9786842009-12-06 08:39:25 -07005613
adam radford21c9e162013-09-06 15:27:14 -07005614 if (megasas_ld_list_query(instance,
5615 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5616 megasas_get_ld_list(instance);
Yang, Boc9786842009-12-06 08:39:25 -07005617 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5618 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5619 ld_index =
5620 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5621
5622 sdev1 = scsi_device_lookup(host,
adam radford21c9e162013-09-06 15:27:14 -07005623 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
Yang, Boc9786842009-12-06 08:39:25 -07005624 if (instance->ld_ids[ld_index] != 0xff) {
5625 if (!sdev1) {
5626 scsi_add_device(host,
adam radford21c9e162013-09-06 15:27:14 -07005627 MEGASAS_MAX_PD_CHANNELS + i,
Yang, Boc9786842009-12-06 08:39:25 -07005628 j, 0);
5629 } else {
5630 scsi_device_put(sdev1);
5631 }
5632 } else {
5633 if (sdev1) {
5634 scsi_remove_device(sdev1);
5635 scsi_device_put(sdev1);
5636 }
5637 }
5638 }
5639 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005640 }
5641
5642 if ( instance->aen_cmd != NULL ) {
5643 kfree(ev);
5644 return ;
5645 }
5646
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305647 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005648
5649 /* Register AEN with FW for latest sequence number plus 1 */
5650 class_locale.members.reserved = 0;
5651 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5652 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5653 mutex_lock(&instance->aen_mutex);
5654 error = megasas_register_aen(instance, seq_num,
5655 class_locale.word);
5656 mutex_unlock(&instance->aen_mutex);
5657
5658 if (error)
5659 printk(KERN_ERR "register aen failed error %x\n", error);
5660
5661 kfree(ev);
5662}
5663
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005664/**
5665 * megasas_init - Driver load entry point
5666 */
5667static int __init megasas_init(void)
5668{
5669 int rval;
5670
5671 /*
5672 * Announce driver version and other information
5673 */
5674 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5675 MEGASAS_EXT_VERSION);
5676
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07005677 spin_lock_init(&poll_aen_lock);
5678
Yang, Bo72c4fd32009-10-06 14:20:59 -06005679 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005680 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005681
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005682 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5683
5684 /*
5685 * Register character device node
5686 */
5687 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5688
5689 if (rval < 0) {
5690 printk(KERN_DEBUG "megasas: failed to open device node\n");
5691 return rval;
5692 }
5693
5694 megasas_mgmt_majorno = rval;
5695
5696 /*
5697 * Register ourselves as PCI hotplug module
5698 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005699 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005700
5701 if (rval) {
5702 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005703 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005704 }
5705
Jeff Garzik83aabc12006-10-04 06:34:03 -04005706 rval = driver_create_file(&megasas_pci_driver.driver,
5707 &driver_attr_version);
5708 if (rval)
5709 goto err_dcf_attr_ver;
5710 rval = driver_create_file(&megasas_pci_driver.driver,
5711 &driver_attr_release_date);
5712 if (rval)
5713 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005714
5715 rval = driver_create_file(&megasas_pci_driver.driver,
5716 &driver_attr_support_poll_for_event);
5717 if (rval)
5718 goto err_dcf_support_poll_for_event;
5719
Jeff Garzik83aabc12006-10-04 06:34:03 -04005720 rval = driver_create_file(&megasas_pci_driver.driver,
5721 &driver_attr_dbg_lvl);
5722 if (rval)
5723 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005724 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005725 &driver_attr_support_device_change);
5726 if (rval)
5727 goto err_dcf_support_device_change;
5728
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005729 return rval;
bo yangad84db22007-11-09 04:40:16 -05005730
Yang, Bo837f5fe2010-10-11 06:59:20 -06005731err_dcf_support_device_change:
5732 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005733 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005734err_dcf_dbg_lvl:
5735 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005736 &driver_attr_support_poll_for_event);
5737
5738err_dcf_support_poll_for_event:
5739 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005740 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005741
Jeff Garzik83aabc12006-10-04 06:34:03 -04005742err_dcf_rel_date:
5743 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5744err_dcf_attr_ver:
5745 pci_unregister_driver(&megasas_pci_driver);
5746err_pcidrv:
5747 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005748 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005749}
5750
5751/**
5752 * megasas_exit - Driver unload entry point
5753 */
5754static void __exit megasas_exit(void)
5755{
Sumant Patro658dced2006-10-03 13:09:14 -07005756 driver_remove_file(&megasas_pci_driver.driver,
5757 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005758 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005759 &driver_attr_support_poll_for_event);
5760 driver_remove_file(&megasas_pci_driver.driver,
5761 &driver_attr_support_device_change);
5762 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005763 &driver_attr_release_date);
5764 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005765
5766 pci_unregister_driver(&megasas_pci_driver);
5767 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5768}
5769
5770module_init(megasas_init);
5771module_exit(megasas_exit);