blob: d2c5366aff7fba67ffa0bdef6007d69ff8e37de2 [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 radfordae590572012-10-01 19:27:34 -070021 * Version : v06.504.01.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);
95static int megasas_issue_init_mfi(struct megasas_instance *instance);
96static int megasas_register_aen(struct megasas_instance *instance,
97 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040098/*
99 * PCI ID table for all supported controllers
100 */
101static struct pci_device_id megasas_pci_table[] = {
102
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
104 /* xscale IOP */
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
106 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
108 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
110 /* gen2*/
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
112 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
114 /* skinny*/
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
116 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
118 /* xscale IOP, vega */
119 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
120 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
122 /* Fusion */
adam radford36807e62011-10-08 18:15:06 -0700123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
124 /* Invader */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200125 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400126};
127
128MODULE_DEVICE_TABLE(pci, megasas_pci_table);
129
130static int megasas_mgmt_majorno;
131static struct megasas_mgmt_info megasas_mgmt_info;
132static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100133static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400134
Yang, Boc3518832009-10-06 14:18:02 -0600135static int megasas_poll_wait_aen;
136static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600137static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800138u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600139static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700140
Yang, Boc3518832009-10-06 14:18:02 -0600141/* define lock for aen poll */
142spinlock_t poll_aen_lock;
143
adam radford9c915a82010-12-21 13:34:31 -0800144void
bo yang7343eb62007-11-09 04:35:44 -0500145megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
146 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800147static u32
148megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
149static int
150megasas_adp_reset_gen2(struct megasas_instance *instance,
151 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800152static irqreturn_t megasas_isr(int irq, void *devp);
153static u32
154megasas_init_adapter_mfi(struct megasas_instance *instance);
155u32
156megasas_build_and_issue_cmd(struct megasas_instance *instance,
157 struct scsi_cmnd *scmd);
158static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800159void
160megasas_release_fusion(struct megasas_instance *instance);
161int
162megasas_ioc_init_fusion(struct megasas_instance *instance);
163void
164megasas_free_cmds_fusion(struct megasas_instance *instance);
165u8
166megasas_get_map_info(struct megasas_instance *instance);
167int
168megasas_sync_map_info(struct megasas_instance *instance);
169int
170wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
171void megasas_reset_reply_desc(struct megasas_instance *instance);
172u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
173 struct LD_LOAD_BALANCE_INFO *lbInfo);
174int megasas_reset_fusion(struct Scsi_Host *shost);
175void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800176
177void
178megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
179{
180 instance->instancet->fire_cmd(instance,
181 cmd->frame_phys_addr, 0, instance->reg_set);
182}
bo yang7343eb62007-11-09 04:35:44 -0500183
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400184/**
185 * megasas_get_cmd - Get a command from the free pool
186 * @instance: Adapter soft state
187 *
188 * Returns a free command from the pool
189 */
adam radford9c915a82010-12-21 13:34:31 -0800190struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400191 *instance)
192{
193 unsigned long flags;
194 struct megasas_cmd *cmd = NULL;
195
196 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
197
198 if (!list_empty(&instance->cmd_pool)) {
199 cmd = list_entry((&instance->cmd_pool)->next,
200 struct megasas_cmd, list);
201 list_del_init(&cmd->list);
202 } else {
203 printk(KERN_ERR "megasas: Command pool empty!\n");
204 }
205
206 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
207 return cmd;
208}
209
210/**
211 * megasas_return_cmd - Return a cmd to free command pool
212 * @instance: Adapter soft state
213 * @cmd: Command packet to be returned to free command pool
214 */
adam radford9c915a82010-12-21 13:34:31 -0800215inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400216megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
221
222 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800223 cmd->frame_count = 0;
adam radforde5f93a32011-10-08 18:15:19 -0700224 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
225 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
226 (reset_devices))
227 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400228 list_add_tail(&cmd->list, &instance->cmd_pool);
229
230 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
231}
232
Sumant Patro1341c932006-01-25 12:02:40 -0800233
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400234/**
adam radford0d490162010-12-14 19:17:17 -0800235* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800236* (deviceid : 1064R, PERC5) controllers
237*/
238
239/**
240 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400241 * @regs: MFI register set
242 */
243static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800244megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400245{
bo yang39a98552010-09-22 22:36:29 -0400246 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400247
248 /* Dummy readl to force pci flush */
249 readl(&regs->outbound_intr_mask);
250}
251
252/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700253 * megasas_disable_intr_xscale -Disables interrupt
254 * @regs: MFI register set
255 */
256static inline void
257megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
258{
259 u32 mask = 0x1f;
260 writel(mask, &regs->outbound_intr_mask);
261 /* Dummy readl to force pci flush */
262 readl(&regs->outbound_intr_mask);
263}
264
265/**
Sumant Patro1341c932006-01-25 12:02:40 -0800266 * megasas_read_fw_status_reg_xscale - returns the current FW status value
267 * @regs: MFI register set
268 */
269static u32
270megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
271{
272 return readl(&(regs)->outbound_msg_0);
273}
274/**
275 * megasas_clear_interrupt_xscale - Check & clear interrupt
276 * @regs: MFI register set
277 */
adam radford0d490162010-12-14 19:17:17 -0800278static int
Sumant Patro1341c932006-01-25 12:02:40 -0800279megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
280{
281 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400282 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800283 /*
284 * Check if it is our interrupt
285 */
286 status = readl(&regs->outbound_intr_status);
287
bo yang39a98552010-09-22 22:36:29 -0400288 if (status & MFI_OB_INTR_STATUS_MASK)
289 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
290 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
291 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800292
293 /*
294 * Clear the interrupt by writing back the same value
295 */
bo yang39a98552010-09-22 22:36:29 -0400296 if (mfiStatus)
297 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800298
Yang, Bo06f579d2008-08-10 12:42:37 -0700299 /* Dummy readl to force pci flush */
300 readl(&regs->outbound_intr_status);
301
bo yang39a98552010-09-22 22:36:29 -0400302 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800303}
304
305/**
306 * megasas_fire_cmd_xscale - Sends command to the FW
307 * @frame_phys_addr : Physical address of cmd
308 * @frame_count : Number of frames for the command
309 * @regs : MFI register set
310 */
adam radford0d490162010-12-14 19:17:17 -0800311static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600312megasas_fire_cmd_xscale(struct megasas_instance *instance,
313 dma_addr_t frame_phys_addr,
314 u32 frame_count,
315 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800316{
bo yang39a98552010-09-22 22:36:29 -0400317 unsigned long flags;
318 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800319 writel((frame_phys_addr >> 3)|(frame_count),
320 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400321 spin_unlock_irqrestore(&instance->hba_lock, flags);
322}
323
324/**
325 * megasas_adp_reset_xscale - For controller reset
326 * @regs: MFI register set
327 */
328static int
329megasas_adp_reset_xscale(struct megasas_instance *instance,
330 struct megasas_register_set __iomem *regs)
331{
332 u32 i;
333 u32 pcidata;
334 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
335
336 for (i = 0; i < 3; i++)
337 msleep(1000); /* sleep for 3 secs */
338 pcidata = 0;
339 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
340 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
341 if (pcidata & 0x2) {
342 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
343 pcidata &= ~0x2;
344 pci_write_config_dword(instance->pdev,
345 MFI_1068_PCSR_OFFSET, pcidata);
346
347 for (i = 0; i < 2; i++)
348 msleep(1000); /* need to wait 2 secs again */
349
350 pcidata = 0;
351 pci_read_config_dword(instance->pdev,
352 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
353 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
354 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
355 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
356 pcidata = 0;
357 pci_write_config_dword(instance->pdev,
358 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
359 }
360 }
361 return 0;
362}
363
364/**
365 * megasas_check_reset_xscale - For controller reset check
366 * @regs: MFI register set
367 */
368static int
369megasas_check_reset_xscale(struct megasas_instance *instance,
370 struct megasas_register_set __iomem *regs)
371{
372 u32 consumer;
373 consumer = *instance->consumer;
374
375 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
376 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
377 return 1;
378 }
379 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800380}
381
382static struct megasas_instance_template megasas_instance_template_xscale = {
383
384 .fire_cmd = megasas_fire_cmd_xscale,
385 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700386 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800387 .clear_intr = megasas_clear_intr_xscale,
388 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400389 .adp_reset = megasas_adp_reset_xscale,
390 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800391 .service_isr = megasas_isr,
392 .tasklet = megasas_complete_cmd_dpc,
393 .init_adapter = megasas_init_adapter_mfi,
394 .build_and_issue_cmd = megasas_build_and_issue_cmd,
395 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800396};
397
398/**
adam radford0d490162010-12-14 19:17:17 -0800399* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800400* to xscale (deviceid : 1064R, PERC5) controllers
401*/
402
403/**
adam radford0d490162010-12-14 19:17:17 -0800404* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800405* controllers
406*/
407
408/**
409 * megasas_enable_intr_ppc - Enables interrupts
410 * @regs: MFI register set
411 */
412static inline void
413megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
414{
415 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800416
bo yang39a98552010-09-22 22:36:29 -0400417 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800418
419 /* Dummy readl to force pci flush */
420 readl(&regs->outbound_intr_mask);
421}
422
423/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700424 * megasas_disable_intr_ppc - Disable interrupt
425 * @regs: MFI register set
426 */
427static inline void
428megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
429{
430 u32 mask = 0xFFFFFFFF;
431 writel(mask, &regs->outbound_intr_mask);
432 /* Dummy readl to force pci flush */
433 readl(&regs->outbound_intr_mask);
434}
435
436/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800437 * megasas_read_fw_status_reg_ppc - returns the current FW status value
438 * @regs: MFI register set
439 */
440static u32
441megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
442{
443 return readl(&(regs)->outbound_scratch_pad);
444}
445
446/**
447 * megasas_clear_interrupt_ppc - Check & clear interrupt
448 * @regs: MFI register set
449 */
adam radford0d490162010-12-14 19:17:17 -0800450static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800451megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
452{
adam radford3cc6851f92011-05-11 18:34:52 -0700453 u32 status, mfiStatus = 0;
454
Sumant Patrof9876f02006-02-03 15:34:35 -0800455 /*
456 * Check if it is our interrupt
457 */
458 status = readl(&regs->outbound_intr_status);
459
adam radford3cc6851f92011-05-11 18:34:52 -0700460 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
461 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
462
463 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
464 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800465
466 /*
467 * Clear the interrupt by writing back the same value
468 */
469 writel(status, &regs->outbound_doorbell_clear);
470
Yang, Bo06f579d2008-08-10 12:42:37 -0700471 /* Dummy readl to force pci flush */
472 readl(&regs->outbound_doorbell_clear);
473
adam radford3cc6851f92011-05-11 18:34:52 -0700474 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800475}
adam radford3cc6851f92011-05-11 18:34:52 -0700476
Sumant Patrof9876f02006-02-03 15:34:35 -0800477/**
478 * megasas_fire_cmd_ppc - Sends command to the FW
479 * @frame_phys_addr : Physical address of cmd
480 * @frame_count : Number of frames for the command
481 * @regs : MFI register set
482 */
adam radford0d490162010-12-14 19:17:17 -0800483static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600484megasas_fire_cmd_ppc(struct megasas_instance *instance,
485 dma_addr_t frame_phys_addr,
486 u32 frame_count,
487 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800488{
bo yang39a98552010-09-22 22:36:29 -0400489 unsigned long flags;
490 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800491 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800492 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400493 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800494}
495
bo yang39a98552010-09-22 22:36:29 -0400496/**
bo yang39a98552010-09-22 22:36:29 -0400497 * megasas_check_reset_ppc - For controller reset check
498 * @regs: MFI register set
499 */
500static int
501megasas_check_reset_ppc(struct megasas_instance *instance,
502 struct megasas_register_set __iomem *regs)
503{
adam radford3cc6851f92011-05-11 18:34:52 -0700504 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
505 return 1;
506
bo yang39a98552010-09-22 22:36:29 -0400507 return 0;
508}
adam radford3cc6851f92011-05-11 18:34:52 -0700509
Sumant Patrof9876f02006-02-03 15:34:35 -0800510static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800511
Sumant Patrof9876f02006-02-03 15:34:35 -0800512 .fire_cmd = megasas_fire_cmd_ppc,
513 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700514 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800515 .clear_intr = megasas_clear_intr_ppc,
516 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700517 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400518 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800519 .service_isr = megasas_isr,
520 .tasklet = megasas_complete_cmd_dpc,
521 .init_adapter = megasas_init_adapter_mfi,
522 .build_and_issue_cmd = megasas_build_and_issue_cmd,
523 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800524};
525
526/**
Yang, Bo87911122009-10-06 14:31:54 -0600527 * megasas_enable_intr_skinny - Enables interrupts
528 * @regs: MFI register set
529 */
530static inline void
531megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
532{
533 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
534
535 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
536
537 /* Dummy readl to force pci flush */
538 readl(&regs->outbound_intr_mask);
539}
540
541/**
542 * megasas_disable_intr_skinny - Disables interrupt
543 * @regs: MFI register set
544 */
545static inline void
546megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
547{
548 u32 mask = 0xFFFFFFFF;
549 writel(mask, &regs->outbound_intr_mask);
550 /* Dummy readl to force pci flush */
551 readl(&regs->outbound_intr_mask);
552}
553
554/**
555 * megasas_read_fw_status_reg_skinny - returns the current FW status value
556 * @regs: MFI register set
557 */
558static u32
559megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
560{
561 return readl(&(regs)->outbound_scratch_pad);
562}
563
564/**
565 * megasas_clear_interrupt_skinny - Check & clear interrupt
566 * @regs: MFI register set
567 */
568static int
569megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
570{
571 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800572 u32 mfiStatus = 0;
573
Yang, Bo87911122009-10-06 14:31:54 -0600574 /*
575 * Check if it is our interrupt
576 */
577 status = readl(&regs->outbound_intr_status);
578
579 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400580 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600581 }
582
583 /*
adam radfordebf054b2011-02-24 20:57:15 -0800584 * Check if it is our interrupt
585 */
586 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
587 MFI_STATE_FAULT) {
588 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
589 } else
590 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
591
592 /*
Yang, Bo87911122009-10-06 14:31:54 -0600593 * Clear the interrupt by writing back the same value
594 */
595 writel(status, &regs->outbound_intr_status);
596
597 /*
598 * dummy read to flush PCI
599 */
600 readl(&regs->outbound_intr_status);
601
adam radfordebf054b2011-02-24 20:57:15 -0800602 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600603}
604
605/**
606 * megasas_fire_cmd_skinny - Sends command to the FW
607 * @frame_phys_addr : Physical address of cmd
608 * @frame_count : Number of frames for the command
609 * @regs : MFI register set
610 */
611static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600612megasas_fire_cmd_skinny(struct megasas_instance *instance,
613 dma_addr_t frame_phys_addr,
614 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600615 struct megasas_register_set __iomem *regs)
616{
Yang, Bo0c79e682009-10-06 14:47:35 -0600617 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400618 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600619 writel(0, &(regs)->inbound_high_queue_port);
620 writel((frame_phys_addr | (frame_count<<1))|1,
621 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400622 spin_unlock_irqrestore(&instance->hba_lock, flags);
623}
624
625/**
bo yang39a98552010-09-22 22:36:29 -0400626 * megasas_check_reset_skinny - For controller reset check
627 * @regs: MFI register set
628 */
629static int
630megasas_check_reset_skinny(struct megasas_instance *instance,
631 struct megasas_register_set __iomem *regs)
632{
adam radford3cc6851f92011-05-11 18:34:52 -0700633 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
634 return 1;
635
bo yang39a98552010-09-22 22:36:29 -0400636 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600637}
638
639static struct megasas_instance_template megasas_instance_template_skinny = {
640
641 .fire_cmd = megasas_fire_cmd_skinny,
642 .enable_intr = megasas_enable_intr_skinny,
643 .disable_intr = megasas_disable_intr_skinny,
644 .clear_intr = megasas_clear_intr_skinny,
645 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800646 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400647 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800648 .service_isr = megasas_isr,
649 .tasklet = megasas_complete_cmd_dpc,
650 .init_adapter = megasas_init_adapter_mfi,
651 .build_and_issue_cmd = megasas_build_and_issue_cmd,
652 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600653};
654
655
656/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700657* The following functions are defined for gen2 (deviceid : 0x78 0x79)
658* controllers
659*/
660
661/**
662 * megasas_enable_intr_gen2 - Enables interrupts
663 * @regs: MFI register set
664 */
665static inline void
666megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
667{
668 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
669
670 /* write ~0x00000005 (4 & 1) to the intr mask*/
671 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
672
673 /* Dummy readl to force pci flush */
674 readl(&regs->outbound_intr_mask);
675}
676
677/**
678 * megasas_disable_intr_gen2 - Disables interrupt
679 * @regs: MFI register set
680 */
681static inline void
682megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
683{
684 u32 mask = 0xFFFFFFFF;
685 writel(mask, &regs->outbound_intr_mask);
686 /* Dummy readl to force pci flush */
687 readl(&regs->outbound_intr_mask);
688}
689
690/**
691 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
692 * @regs: MFI register set
693 */
694static u32
695megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
696{
697 return readl(&(regs)->outbound_scratch_pad);
698}
699
700/**
701 * megasas_clear_interrupt_gen2 - Check & clear interrupt
702 * @regs: MFI register set
703 */
704static int
705megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
706{
707 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400708 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700709 /*
710 * Check if it is our interrupt
711 */
712 status = readl(&regs->outbound_intr_status);
713
bo yang39a98552010-09-22 22:36:29 -0400714 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
715 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
716 }
717 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
718 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
719 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700720
721 /*
722 * Clear the interrupt by writing back the same value
723 */
bo yang39a98552010-09-22 22:36:29 -0400724 if (mfiStatus)
725 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700726
727 /* Dummy readl to force pci flush */
728 readl(&regs->outbound_intr_status);
729
bo yang39a98552010-09-22 22:36:29 -0400730 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700731}
732/**
733 * megasas_fire_cmd_gen2 - Sends command to the FW
734 * @frame_phys_addr : Physical address of cmd
735 * @frame_count : Number of frames for the command
736 * @regs : MFI register set
737 */
738static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600739megasas_fire_cmd_gen2(struct megasas_instance *instance,
740 dma_addr_t frame_phys_addr,
741 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700742 struct megasas_register_set __iomem *regs)
743{
bo yang39a98552010-09-22 22:36:29 -0400744 unsigned long flags;
745 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700746 writel((frame_phys_addr | (frame_count<<1))|1,
747 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400748 spin_unlock_irqrestore(&instance->hba_lock, flags);
749}
750
751/**
752 * megasas_adp_reset_gen2 - For controller reset
753 * @regs: MFI register set
754 */
755static int
756megasas_adp_reset_gen2(struct megasas_instance *instance,
757 struct megasas_register_set __iomem *reg_set)
758{
759 u32 retry = 0 ;
760 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800761 u32 *seq_offset = &reg_set->seq_offset;
762 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400763
adam radfordebf054b2011-02-24 20:57:15 -0800764 if (instance->instancet == &megasas_instance_template_skinny) {
765 seq_offset = &reg_set->fusion_seq_offset;
766 hostdiag_offset = &reg_set->fusion_host_diag;
767 }
768
769 writel(0, seq_offset);
770 writel(4, seq_offset);
771 writel(0xb, seq_offset);
772 writel(2, seq_offset);
773 writel(7, seq_offset);
774 writel(0xd, seq_offset);
775
bo yang39a98552010-09-22 22:36:29 -0400776 msleep(1000);
777
adam radfordebf054b2011-02-24 20:57:15 -0800778 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400779
780 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
781 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800782 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400783 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
784 retry, HostDiag);
785
786 if (retry++ >= 100)
787 return 1;
788
789 }
790
791 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
792
adam radfordebf054b2011-02-24 20:57:15 -0800793 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400794
795 ssleep(10);
796
adam radfordebf054b2011-02-24 20:57:15 -0800797 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400798 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
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 "RESET_GEN2: retry=%x, hostdiag=%x\n",
802 retry, HostDiag);
803
804 if (retry++ >= 1000)
805 return 1;
806
807 }
808 return 0;
809}
810
811/**
812 * megasas_check_reset_gen2 - For controller reset check
813 * @regs: MFI register set
814 */
815static int
816megasas_check_reset_gen2(struct megasas_instance *instance,
817 struct megasas_register_set __iomem *regs)
818{
Yang, Bo707e09b2010-10-12 07:20:27 -0600819 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
820 return 1;
821 }
822
bo yang39a98552010-09-22 22:36:29 -0400823 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700824}
825
826static struct megasas_instance_template megasas_instance_template_gen2 = {
827
828 .fire_cmd = megasas_fire_cmd_gen2,
829 .enable_intr = megasas_enable_intr_gen2,
830 .disable_intr = megasas_disable_intr_gen2,
831 .clear_intr = megasas_clear_intr_gen2,
832 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400833 .adp_reset = megasas_adp_reset_gen2,
834 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800835 .service_isr = megasas_isr,
836 .tasklet = megasas_complete_cmd_dpc,
837 .init_adapter = megasas_init_adapter_mfi,
838 .build_and_issue_cmd = megasas_build_and_issue_cmd,
839 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700840};
841
842/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800843* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400844* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800845*/
846
adam radford9c915a82010-12-21 13:34:31 -0800847/*
848 * Template added for TB (Fusion)
849 */
850extern struct megasas_instance_template megasas_instance_template_fusion;
851
Sumant Patrof9876f02006-02-03 15:34:35 -0800852/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400853 * megasas_issue_polled - Issues a polling command
854 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800855 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400856 *
857 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
858 */
adam radford9c915a82010-12-21 13:34:31 -0800859int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400860megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
861{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400862
863 struct megasas_header *frame_hdr = &cmd->frame->hdr;
864
865 frame_hdr->cmd_status = 0xFF;
866 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
867
868 /*
869 * Issue the frame using inbound queue port
870 */
adam radford9c915a82010-12-21 13:34:31 -0800871 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400872
873 /*
874 * Wait for cmd_status to change
875 */
adam radford9c915a82010-12-21 13:34:31 -0800876 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400877}
878
879/**
880 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
881 * @instance: Adapter soft state
882 * @cmd: Command to be issued
883 *
884 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700885 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400886 * Used to issue ioctl commands.
887 */
888static int
889megasas_issue_blocked_cmd(struct megasas_instance *instance,
890 struct megasas_cmd *cmd)
891{
892 cmd->cmd_status = ENODATA;
893
adam radford9c915a82010-12-21 13:34:31 -0800894 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400895
bo yang39a98552010-09-22 22:36:29 -0400896 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400897
898 return 0;
899}
900
901/**
902 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
903 * @instance: Adapter soft state
904 * @cmd_to_abort: Previously issued cmd to be aborted
905 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100906 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400907 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700908 * cmd and waits for return status.
909 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400910 */
911static int
912megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
913 struct megasas_cmd *cmd_to_abort)
914{
915 struct megasas_cmd *cmd;
916 struct megasas_abort_frame *abort_fr;
917
918 cmd = megasas_get_cmd(instance);
919
920 if (!cmd)
921 return -1;
922
923 abort_fr = &cmd->frame->abort;
924
925 /*
926 * Prepare and issue the abort frame
927 */
928 abort_fr->cmd = MFI_CMD_ABORT;
929 abort_fr->cmd_status = 0xFF;
930 abort_fr->flags = 0;
931 abort_fr->abort_context = cmd_to_abort->index;
932 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
933 abort_fr->abort_mfi_phys_addr_hi = 0;
934
935 cmd->sync_cmd = 1;
936 cmd->cmd_status = 0xFF;
937
adam radford9c915a82010-12-21 13:34:31 -0800938 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400939
940 /*
941 * Wait for this cmd to complete
942 */
bo yang39a98552010-09-22 22:36:29 -0400943 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
944 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400945
946 megasas_return_cmd(instance, cmd);
947 return 0;
948}
949
950/**
951 * megasas_make_sgl32 - Prepares 32-bit SGL
952 * @instance: Adapter soft state
953 * @scp: SCSI command from the mid-layer
954 * @mfi_sgl: SGL to be filled in
955 *
956 * If successful, this function returns the number of SG elements. Otherwise,
957 * it returnes -1.
958 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800959static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400960megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
961 union megasas_sgl *mfi_sgl)
962{
963 int i;
964 int sge_count;
965 struct scatterlist *os_sgl;
966
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900967 sge_count = scsi_dma_map(scp);
968 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400969
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900970 if (sge_count) {
971 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
972 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
973 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
974 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400975 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400976 return sge_count;
977}
978
979/**
980 * megasas_make_sgl64 - Prepares 64-bit SGL
981 * @instance: Adapter soft state
982 * @scp: SCSI command from the mid-layer
983 * @mfi_sgl: SGL to be filled in
984 *
985 * If successful, this function returns the number of SG elements. Otherwise,
986 * it returnes -1.
987 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800988static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
990 union megasas_sgl *mfi_sgl)
991{
992 int i;
993 int sge_count;
994 struct scatterlist *os_sgl;
995
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900996 sge_count = scsi_dma_map(scp);
997 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900999 if (sge_count) {
1000 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1001 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
1002 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1003 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001004 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001005 return sge_count;
1006}
1007
Yang, Bof4c9a132009-10-06 14:43:28 -06001008/**
1009 * megasas_make_sgl_skinny - Prepares IEEE SGL
1010 * @instance: Adapter soft state
1011 * @scp: SCSI command from the mid-layer
1012 * @mfi_sgl: SGL to be filled in
1013 *
1014 * If successful, this function returns the number of SG elements. Otherwise,
1015 * it returnes -1.
1016 */
1017static int
1018megasas_make_sgl_skinny(struct megasas_instance *instance,
1019 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1020{
1021 int i;
1022 int sge_count;
1023 struct scatterlist *os_sgl;
1024
1025 sge_count = scsi_dma_map(scp);
1026
1027 if (sge_count) {
1028 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1029 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1030 mfi_sgl->sge_skinny[i].phys_addr =
1031 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001032 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001033 }
1034 }
1035 return sge_count;
1036}
1037
Sumant Patrob1df99d2006-10-03 12:40:47 -07001038 /**
1039 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001040 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001041 * @sge_count : number of sg elements
1042 *
1043 * Returns the number of frames required for numnber of sge's (sge_count)
1044 */
1045
Yang, Bof4c9a132009-10-06 14:43:28 -06001046static u32 megasas_get_frame_count(struct megasas_instance *instance,
1047 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001048{
1049 int num_cnt;
1050 int sge_bytes;
1051 u32 sge_sz;
1052 u32 frame_count=0;
1053
1054 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1055 sizeof(struct megasas_sge32);
1056
Yang, Bof4c9a132009-10-06 14:43:28 -06001057 if (instance->flag_ieee) {
1058 sge_sz = sizeof(struct megasas_sge_skinny);
1059 }
1060
Sumant Patrob1df99d2006-10-03 12:40:47 -07001061 /*
bo yangd532dbe2008-03-17 03:36:43 -04001062 * Main frame can contain 2 SGEs for 64-bit SGLs and
1063 * 3 SGEs for 32-bit SGLs for ldio &
1064 * 1 SGEs for 64-bit SGLs and
1065 * 2 SGEs for 32-bit SGLs for pthru frame
1066 */
1067 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001068 if (instance->flag_ieee == 1) {
1069 num_cnt = sge_count - 1;
1070 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001071 num_cnt = sge_count - 1;
1072 else
1073 num_cnt = sge_count - 2;
1074 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001075 if (instance->flag_ieee == 1) {
1076 num_cnt = sge_count - 1;
1077 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001078 num_cnt = sge_count - 2;
1079 else
1080 num_cnt = sge_count - 3;
1081 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001082
1083 if(num_cnt>0){
1084 sge_bytes = sge_sz * num_cnt;
1085
1086 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1087 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1088 }
1089 /* Main frame */
1090 frame_count +=1;
1091
1092 if (frame_count > 7)
1093 frame_count = 8;
1094 return frame_count;
1095}
1096
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001097/**
1098 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1099 * @instance: Adapter soft state
1100 * @scp: SCSI command
1101 * @cmd: Command to be prepared in
1102 *
1103 * This function prepares CDB commands. These are typcially pass-through
1104 * commands to the devices.
1105 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001106static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001107megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1108 struct megasas_cmd *cmd)
1109{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001110 u32 is_logical;
1111 u32 device_id;
1112 u16 flags = 0;
1113 struct megasas_pthru_frame *pthru;
1114
1115 is_logical = MEGASAS_IS_LOGICAL(scp);
1116 device_id = MEGASAS_DEV_INDEX(instance, scp);
1117 pthru = (struct megasas_pthru_frame *)cmd->frame;
1118
1119 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1120 flags = MFI_FRAME_DIR_WRITE;
1121 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1122 flags = MFI_FRAME_DIR_READ;
1123 else if (scp->sc_data_direction == PCI_DMA_NONE)
1124 flags = MFI_FRAME_DIR_NONE;
1125
Yang, Bof4c9a132009-10-06 14:43:28 -06001126 if (instance->flag_ieee == 1) {
1127 flags |= MFI_FRAME_IEEE;
1128 }
1129
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001130 /*
1131 * Prepare the DCDB frame
1132 */
1133 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1134 pthru->cmd_status = 0x0;
1135 pthru->scsi_status = 0x0;
1136 pthru->target_id = device_id;
1137 pthru->lun = scp->device->lun;
1138 pthru->cdb_len = scp->cmd_len;
1139 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001140 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001141 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001142 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001143
1144 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1145
1146 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001147 * If the command is for the tape device, set the
1148 * pthru timeout to the os layer timeout value.
1149 */
1150 if (scp->device->type == TYPE_TAPE) {
1151 if ((scp->request->timeout / HZ) > 0xFFFF)
1152 pthru->timeout = 0xFFFF;
1153 else
1154 pthru->timeout = scp->request->timeout / HZ;
1155 }
1156
1157 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001158 * Construct SGL
1159 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001160 if (instance->flag_ieee == 1) {
1161 pthru->flags |= MFI_FRAME_SGL64;
1162 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1163 &pthru->sgl);
1164 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165 pthru->flags |= MFI_FRAME_SGL64;
1166 pthru->sge_count = megasas_make_sgl64(instance, scp,
1167 &pthru->sgl);
1168 } else
1169 pthru->sge_count = megasas_make_sgl32(instance, scp,
1170 &pthru->sgl);
1171
Yang, Bobdc6fb82009-12-06 08:30:19 -07001172 if (pthru->sge_count > instance->max_num_sge) {
1173 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1174 pthru->sge_count);
1175 return 0;
1176 }
1177
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001178 /*
1179 * Sense info specific
1180 */
1181 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1182 pthru->sense_buf_phys_addr_hi = 0;
1183 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1184
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001185 /*
1186 * Compute the total number of frames this command consumes. FW uses
1187 * this number to pull sufficient number of frames from host memory.
1188 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001189 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001190 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001191
1192 return cmd->frame_count;
1193}
1194
1195/**
1196 * megasas_build_ldio - Prepares IOs to logical devices
1197 * @instance: Adapter soft state
1198 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001199 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001200 *
1201 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1202 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001203static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001204megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1205 struct megasas_cmd *cmd)
1206{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001207 u32 device_id;
1208 u8 sc = scp->cmnd[0];
1209 u16 flags = 0;
1210 struct megasas_io_frame *ldio;
1211
1212 device_id = MEGASAS_DEV_INDEX(instance, scp);
1213 ldio = (struct megasas_io_frame *)cmd->frame;
1214
1215 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1216 flags = MFI_FRAME_DIR_WRITE;
1217 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1218 flags = MFI_FRAME_DIR_READ;
1219
Yang, Bof4c9a132009-10-06 14:43:28 -06001220 if (instance->flag_ieee == 1) {
1221 flags |= MFI_FRAME_IEEE;
1222 }
1223
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001224 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001225 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001226 */
1227 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1228 ldio->cmd_status = 0x0;
1229 ldio->scsi_status = 0x0;
1230 ldio->target_id = device_id;
1231 ldio->timeout = 0;
1232 ldio->reserved_0 = 0;
1233 ldio->pad_0 = 0;
1234 ldio->flags = flags;
1235 ldio->start_lba_hi = 0;
1236 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1237
1238 /*
1239 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1240 */
1241 if (scp->cmd_len == 6) {
1242 ldio->lba_count = (u32) scp->cmnd[4];
1243 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1244 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1245
1246 ldio->start_lba_lo &= 0x1FFFFF;
1247 }
1248
1249 /*
1250 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1251 */
1252 else if (scp->cmd_len == 10) {
1253 ldio->lba_count = (u32) scp->cmnd[8] |
1254 ((u32) scp->cmnd[7] << 8);
1255 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1256 ((u32) scp->cmnd[3] << 16) |
1257 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1258 }
1259
1260 /*
1261 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1262 */
1263 else if (scp->cmd_len == 12) {
1264 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1265 ((u32) scp->cmnd[7] << 16) |
1266 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1267
1268 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1269 ((u32) scp->cmnd[3] << 16) |
1270 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1271 }
1272
1273 /*
1274 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1275 */
1276 else if (scp->cmd_len == 16) {
1277 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1278 ((u32) scp->cmnd[11] << 16) |
1279 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1280
1281 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1282 ((u32) scp->cmnd[7] << 16) |
1283 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1284
1285 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1286 ((u32) scp->cmnd[3] << 16) |
1287 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1288
1289 }
1290
1291 /*
1292 * Construct SGL
1293 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001294 if (instance->flag_ieee) {
1295 ldio->flags |= MFI_FRAME_SGL64;
1296 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1297 &ldio->sgl);
1298 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001299 ldio->flags |= MFI_FRAME_SGL64;
1300 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1301 } else
1302 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1303
Yang, Bobdc6fb82009-12-06 08:30:19 -07001304 if (ldio->sge_count > instance->max_num_sge) {
1305 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1306 ldio->sge_count);
1307 return 0;
1308 }
1309
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001310 /*
1311 * Sense info specific
1312 */
1313 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1314 ldio->sense_buf_phys_addr_hi = 0;
1315 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1316
Sumant Patrob1df99d2006-10-03 12:40:47 -07001317 /*
1318 * Compute the total number of frames this command consumes. FW uses
1319 * this number to pull sufficient number of frames from host memory.
1320 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001321 cmd->frame_count = megasas_get_frame_count(instance,
1322 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001323
1324 return cmd->frame_count;
1325}
1326
1327/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001328 * megasas_is_ldio - Checks if the cmd is for logical drive
1329 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001330 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001331 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001332 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001333 */
adam radford9c915a82010-12-21 13:34:31 -08001334inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001335{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001336 if (!MEGASAS_IS_LOGICAL(cmd))
1337 return 0;
1338 switch (cmd->cmnd[0]) {
1339 case READ_10:
1340 case WRITE_10:
1341 case READ_12:
1342 case WRITE_12:
1343 case READ_6:
1344 case WRITE_6:
1345 case READ_16:
1346 case WRITE_16:
1347 return 1;
1348 default:
1349 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001350 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001351}
1352
Sumant Patro658dced2006-10-03 13:09:14 -07001353 /**
1354 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1355 * in FW
1356 * @instance: Adapter soft state
1357 */
1358static inline void
1359megasas_dump_pending_frames(struct megasas_instance *instance)
1360{
1361 struct megasas_cmd *cmd;
1362 int i,n;
1363 union megasas_sgl *mfi_sgl;
1364 struct megasas_io_frame *ldio;
1365 struct megasas_pthru_frame *pthru;
1366 u32 sgcount;
1367 u32 max_cmd = instance->max_fw_cmds;
1368
1369 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1370 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1371 if (IS_DMA64)
1372 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1373 else
1374 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1375
1376 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1377 for (i = 0; i < max_cmd; i++) {
1378 cmd = instance->cmd_list[i];
1379 if(!cmd->scmd)
1380 continue;
1381 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1382 if (megasas_is_ldio(cmd->scmd)){
1383 ldio = (struct megasas_io_frame *)cmd->frame;
1384 mfi_sgl = &ldio->sgl;
1385 sgcount = ldio->sge_count;
1386 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
1387 }
1388 else {
1389 pthru = (struct megasas_pthru_frame *) cmd->frame;
1390 mfi_sgl = &pthru->sgl;
1391 sgcount = pthru->sge_count;
1392 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
1393 }
1394 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1395 for (n = 0; n < sgcount; n++){
1396 if (IS_DMA64)
1397 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
1398 else
1399 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1400 }
1401 }
1402 printk(KERN_ERR "\n");
1403 } /*for max_cmd*/
1404 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1405 for (i = 0; i < max_cmd; i++) {
1406
1407 cmd = instance->cmd_list[i];
1408
1409 if(cmd->sync_cmd == 1){
1410 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1411 }
1412 }
1413 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1414}
1415
adam radfordcd50ba82010-12-21 10:23:23 -08001416u32
1417megasas_build_and_issue_cmd(struct megasas_instance *instance,
1418 struct scsi_cmnd *scmd)
1419{
1420 struct megasas_cmd *cmd;
1421 u32 frame_count;
1422
1423 cmd = megasas_get_cmd(instance);
1424 if (!cmd)
1425 return SCSI_MLQUEUE_HOST_BUSY;
1426
1427 /*
1428 * Logical drive command
1429 */
1430 if (megasas_is_ldio(scmd))
1431 frame_count = megasas_build_ldio(instance, scmd, cmd);
1432 else
1433 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1434
1435 if (!frame_count)
1436 goto out_return_cmd;
1437
1438 cmd->scmd = scmd;
1439 scmd->SCp.ptr = (char *)cmd;
1440
1441 /*
1442 * Issue the command to the FW
1443 */
1444 atomic_inc(&instance->fw_outstanding);
1445
1446 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1447 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001448
1449 return 0;
1450out_return_cmd:
1451 megasas_return_cmd(instance, cmd);
1452 return 1;
1453}
1454
1455
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001456/**
1457 * megasas_queue_command - Queue entry point
1458 * @scmd: SCSI command to be queued
1459 * @done: Callback entry point
1460 */
1461static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001462megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001463{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001465 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001466
1467 instance = (struct megasas_instance *)
1468 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001469
bo yang39a98552010-09-22 22:36:29 -04001470 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001471 return SCSI_MLQUEUE_HOST_BUSY;
1472
bo yang39a98552010-09-22 22:36:29 -04001473 spin_lock_irqsave(&instance->hba_lock, flags);
1474 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1475 spin_unlock_irqrestore(&instance->hba_lock, flags);
1476 return SCSI_MLQUEUE_HOST_BUSY;
1477 }
1478
1479 spin_unlock_irqrestore(&instance->hba_lock, flags);
1480
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001481 scmd->scsi_done = done;
1482 scmd->result = 0;
1483
Sumant Patrocb59aa62006-01-25 11:53:25 -08001484 if (MEGASAS_IS_LOGICAL(scmd) &&
1485 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1486 scmd->result = DID_BAD_TARGET << 16;
1487 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001488 }
1489
Sumant Patro02b01e02007-02-14 13:00:55 -08001490 switch (scmd->cmnd[0]) {
1491 case SYNCHRONIZE_CACHE:
1492 /*
1493 * FW takes care of flush cache on its own
1494 * No need to send it down
1495 */
1496 scmd->result = DID_OK << 16;
1497 goto out_done;
1498 default:
1499 break;
1500 }
1501
adam radfordcd50ba82010-12-21 10:23:23 -08001502 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1503 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001504 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001505 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001506
1507 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001508
Sumant Patrocb59aa62006-01-25 11:53:25 -08001509 out_done:
1510 done(scmd);
1511 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001512}
1513
Jeff Garzikf2812332010-11-16 02:10:29 -05001514static DEF_SCSI_QCMD(megasas_queue_command)
1515
Yang, Bo044833b2009-10-06 14:33:06 -06001516static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1517{
1518 int i;
1519
1520 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1521
1522 if ((megasas_mgmt_info.instance[i]) &&
1523 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1524 return megasas_mgmt_info.instance[i];
1525 }
1526
1527 return NULL;
1528}
1529
Christoph Hellwig147aab62006-02-17 12:13:48 +01001530static int megasas_slave_configure(struct scsi_device *sdev)
1531{
Yang, Bo044833b2009-10-06 14:33:06 -06001532 u16 pd_index = 0;
1533 struct megasas_instance *instance ;
1534
1535 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001536
1537 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001538 * Don't export physical disk devices to the disk driver.
1539 *
1540 * FIXME: Currently we don't export them to the midlayer at all.
1541 * That will be fixed once LSI engineers have audited the
1542 * firmware for possible issues.
1543 */
1544 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1545 sdev->type == TYPE_DISK) {
1546 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1547 sdev->id;
1548 if (instance->pd_list[pd_index].driveState ==
1549 MR_PD_STATE_SYSTEM) {
1550 blk_queue_rq_timeout(sdev->request_queue,
1551 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1552 return 0;
1553 }
1554 return -ENXIO;
1555 }
1556
1557 /*
1558 * The RAID firmware may require extended timeouts.
1559 */
1560 blk_queue_rq_timeout(sdev->request_queue,
1561 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1562 return 0;
1563}
1564
1565static int megasas_slave_alloc(struct scsi_device *sdev)
1566{
1567 u16 pd_index = 0;
1568 struct megasas_instance *instance ;
1569 instance = megasas_lookup_instance(sdev->host->host_no);
1570 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1571 (sdev->type == TYPE_DISK)) {
1572 /*
1573 * Open the OS scan to the SYSTEM PD
1574 */
1575 pd_index =
1576 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1577 sdev->id;
1578 if ((instance->pd_list[pd_index].driveState ==
1579 MR_PD_STATE_SYSTEM) &&
1580 (instance->pd_list[pd_index].driveType ==
1581 TYPE_DISK)) {
1582 return 0;
1583 }
1584 return -ENXIO;
1585 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001586 return 0;
1587}
1588
adam radford9c915a82010-12-21 13:34:31 -08001589void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001590{
1591 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001592 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
adam radford36807e62011-10-08 18:15:06 -07001593 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1594 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08001595 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001596 } else {
adam radford9c915a82010-12-21 13:34:31 -08001597 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1598 }
1599}
1600
1601 /**
1602 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1603 * restored to max value
1604 * @instance: Adapter soft state
1605 *
1606 */
1607void
1608megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1609{
1610 unsigned long flags;
1611 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001612 && time_after(jiffies, instance->last_time + 5 * HZ)
1613 && atomic_read(&instance->fw_outstanding) <
1614 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001615
1616 spin_lock_irqsave(instance->host->host_lock, flags);
1617 instance->flag &= ~MEGASAS_FW_BUSY;
1618 if ((instance->pdev->device ==
1619 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1620 (instance->pdev->device ==
1621 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1622 instance->host->can_queue =
1623 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1624 } else
1625 instance->host->can_queue =
1626 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1627
1628 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001629 }
1630}
1631
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001632/**
bo yang7343eb62007-11-09 04:35:44 -05001633 * megasas_complete_cmd_dpc - Returns FW's controller structure
1634 * @instance_addr: Address of adapter soft state
1635 *
1636 * Tasklet to complete cmds
1637 */
1638static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1639{
1640 u32 producer;
1641 u32 consumer;
1642 u32 context;
1643 struct megasas_cmd *cmd;
1644 struct megasas_instance *instance =
1645 (struct megasas_instance *)instance_addr;
1646 unsigned long flags;
1647
1648 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001649 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001650 return;
1651
1652 spin_lock_irqsave(&instance->completion_lock, flags);
1653
1654 producer = *instance->producer;
1655 consumer = *instance->consumer;
1656
1657 while (consumer != producer) {
1658 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001659 if (context >= instance->max_fw_cmds) {
1660 printk(KERN_ERR "Unexpected context value %x\n",
1661 context);
1662 BUG();
1663 }
bo yang7343eb62007-11-09 04:35:44 -05001664
1665 cmd = instance->cmd_list[context];
1666
1667 megasas_complete_cmd(instance, cmd, DID_OK);
1668
1669 consumer++;
1670 if (consumer == (instance->max_fw_cmds + 1)) {
1671 consumer = 0;
1672 }
1673 }
1674
1675 *instance->consumer = producer;
1676
1677 spin_unlock_irqrestore(&instance->completion_lock, flags);
1678
1679 /*
1680 * Check if we can restore can_queue
1681 */
adam radford9c915a82010-12-21 13:34:31 -08001682 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001683}
1684
Yang, Bo707e09b2010-10-12 07:20:27 -06001685static void
1686megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1687
1688static void
1689process_fw_state_change_wq(struct work_struct *work);
1690
1691void megasas_do_ocr(struct megasas_instance *instance)
1692{
1693 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1694 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1695 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1696 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1697 }
1698 instance->instancet->disable_intr(instance->reg_set);
1699 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1700 instance->issuepend_done = 0;
1701
1702 atomic_set(&instance->fw_outstanding, 0);
1703 megasas_internal_reset_defer_cmds(instance);
1704 process_fw_state_change_wq(&instance->work_init);
1705}
1706
bo yang7343eb62007-11-09 04:35:44 -05001707/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001708 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1709 * @instance: Adapter soft state
1710 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001711 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001712 * complete all its outstanding commands. Returns error if one or more IOs
1713 * are pending after this time period. It also marks the controller dead.
1714 */
1715static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1716{
1717 int i;
bo yang39a98552010-09-22 22:36:29 -04001718 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001719 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001720 u8 adprecovery;
1721 unsigned long flags;
1722 struct list_head clist_local;
1723 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001724 u32 fw_state;
1725 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001726
1727 spin_lock_irqsave(&instance->hba_lock, flags);
1728 adprecovery = instance->adprecovery;
1729 spin_unlock_irqrestore(&instance->hba_lock, flags);
1730
1731 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1732
1733 INIT_LIST_HEAD(&clist_local);
1734 spin_lock_irqsave(&instance->hba_lock, flags);
1735 list_splice_init(&instance->internal_reset_pending_q,
1736 &clist_local);
1737 spin_unlock_irqrestore(&instance->hba_lock, flags);
1738
1739 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1740 for (i = 0; i < wait_time; i++) {
1741 msleep(1000);
1742 spin_lock_irqsave(&instance->hba_lock, flags);
1743 adprecovery = instance->adprecovery;
1744 spin_unlock_irqrestore(&instance->hba_lock, flags);
1745 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1746 break;
1747 }
1748
1749 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1750 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1751 spin_lock_irqsave(&instance->hba_lock, flags);
1752 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1753 spin_unlock_irqrestore(&instance->hba_lock, flags);
1754 return FAILED;
1755 }
1756
1757 reset_index = 0;
1758 while (!list_empty(&clist_local)) {
1759 reset_cmd = list_entry((&clist_local)->next,
1760 struct megasas_cmd, list);
1761 list_del_init(&reset_cmd->list);
1762 if (reset_cmd->scmd) {
1763 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001764 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001765 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001766 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001767
1768 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1769 megasas_return_cmd(instance, reset_cmd);
1770 } else if (reset_cmd->sync_cmd) {
1771 printk(KERN_NOTICE "megasas:%p synch cmds"
1772 "reset queue\n",
1773 reset_cmd);
1774
1775 reset_cmd->cmd_status = ENODATA;
1776 instance->instancet->fire_cmd(instance,
1777 reset_cmd->frame_phys_addr,
1778 0, instance->reg_set);
1779 } else {
1780 printk(KERN_NOTICE "megasas: %p unexpected"
1781 "cmds lst\n",
1782 reset_cmd);
1783 }
1784 reset_index++;
1785 }
1786
1787 return SUCCESS;
1788 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001789
adam radfordc007b8b2012-07-17 18:20:24 -07001790 for (i = 0; i < resetwaittime; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001791
Sumant Patroe4a082c2006-05-30 12:03:37 -07001792 int outstanding = atomic_read(&instance->fw_outstanding);
1793
1794 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001795 break;
1796
1797 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1798 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001799 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001800 /*
1801 * Call cmd completion routine. Cmd to be
1802 * be completed directly without depending on isr.
1803 */
1804 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001805 }
1806
1807 msleep(1000);
1808 }
1809
Yang, Bo707e09b2010-10-12 07:20:27 -06001810 i = 0;
1811 kill_adapter_flag = 0;
1812 do {
1813 fw_state = instance->instancet->read_fw_status_reg(
1814 instance->reg_set) & MFI_STATE_MASK;
1815 if ((fw_state == MFI_STATE_FAULT) &&
1816 (instance->disableOnlineCtrlReset == 0)) {
1817 if (i == 3) {
1818 kill_adapter_flag = 2;
1819 break;
1820 }
1821 megasas_do_ocr(instance);
1822 kill_adapter_flag = 1;
1823
1824 /* wait for 1 secs to let FW finish the pending cmds */
1825 msleep(1000);
1826 }
1827 i++;
1828 } while (i <= 3);
1829
1830 if (atomic_read(&instance->fw_outstanding) &&
1831 !kill_adapter_flag) {
1832 if (instance->disableOnlineCtrlReset == 0) {
1833
1834 megasas_do_ocr(instance);
1835
1836 /* wait for 5 secs to let FW finish the pending cmds */
1837 for (i = 0; i < wait_time; i++) {
1838 int outstanding =
1839 atomic_read(&instance->fw_outstanding);
1840 if (!outstanding)
1841 return SUCCESS;
1842 msleep(1000);
1843 }
1844 }
1845 }
1846
1847 if (atomic_read(&instance->fw_outstanding) ||
1848 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001849 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001850 /*
1851 * Send signal to FW to stop processing any pending cmds.
1852 * The controller will be taken offline by the OS now.
1853 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001854 if ((instance->pdev->device ==
1855 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1856 (instance->pdev->device ==
1857 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1858 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001859 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001860 } else {
1861 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001862 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001863 }
Sumant Patro658dced2006-10-03 13:09:14 -07001864 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001865 spin_lock_irqsave(&instance->hba_lock, flags);
1866 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1867 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001868 return FAILED;
1869 }
1870
bo yang39a98552010-09-22 22:36:29 -04001871 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1872
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001873 return SUCCESS;
1874}
1875
1876/**
1877 * megasas_generic_reset - Generic reset routine
1878 * @scmd: Mid-layer SCSI command
1879 *
1880 * This routine implements a generic reset handler for device, bus and host
1881 * reset requests. Device, bus and host specific reset handlers can use this
1882 * function after they do their specific tasks.
1883 */
1884static int megasas_generic_reset(struct scsi_cmnd *scmd)
1885{
1886 int ret_val;
1887 struct megasas_instance *instance;
1888
1889 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1890
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001891 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1892 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001893
bo yang39a98552010-09-22 22:36:29 -04001894 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001895 printk(KERN_ERR "megasas: cannot recover from previous reset "
1896 "failures\n");
1897 return FAILED;
1898 }
1899
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001900 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001901 if (ret_val == SUCCESS)
1902 printk(KERN_NOTICE "megasas: reset successful \n");
1903 else
1904 printk(KERN_ERR "megasas: failed to do reset\n");
1905
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001906 return ret_val;
1907}
1908
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001909/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001910 * megasas_reset_timer - quiesce the adapter if required
1911 * @scmd: scsi cmnd
1912 *
1913 * Sets the FW busy flag and reduces the host->can_queue if the
1914 * cmd has not been completed within the timeout period.
1915 */
1916static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001917blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001918{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001919 struct megasas_instance *instance;
1920 unsigned long flags;
1921
1922 if (time_after(jiffies, scmd->jiffies_at_alloc +
1923 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001924 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001925 }
1926
adam radfordf575c5d2011-10-13 16:01:12 -07001927 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001928 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1929 /* FW is busy, throttle IO */
1930 spin_lock_irqsave(instance->host->host_lock, flags);
1931
adam radfordc5daa6a2012-07-17 18:20:03 -07001932 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001933 instance->last_time = jiffies;
1934 instance->flag |= MEGASAS_FW_BUSY;
1935
1936 spin_unlock_irqrestore(instance->host->host_lock, flags);
1937 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001938 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001939}
1940
1941/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001942 * megasas_reset_device - Device reset handler entry point
1943 */
1944static int megasas_reset_device(struct scsi_cmnd *scmd)
1945{
1946 int ret;
1947
1948 /*
1949 * First wait for all commands to complete
1950 */
1951 ret = megasas_generic_reset(scmd);
1952
1953 return ret;
1954}
1955
1956/**
1957 * megasas_reset_bus_host - Bus & host reset handler entry point
1958 */
1959static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1960{
1961 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001962 struct megasas_instance *instance;
1963 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001964
1965 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001966 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001967 */
adam radford36807e62011-10-08 18:15:06 -07001968 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1969 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08001970 ret = megasas_reset_fusion(scmd->device->host);
1971 else
1972 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001973
1974 return ret;
1975}
1976
1977/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001978 * megasas_bios_param - Returns disk geometry for a disk
1979 * @sdev: device handle
1980 * @bdev: block device
1981 * @capacity: drive capacity
1982 * @geom: geometry parameters
1983 */
1984static int
1985megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1986 sector_t capacity, int geom[])
1987{
1988 int heads;
1989 int sectors;
1990 sector_t cylinders;
1991 unsigned long tmp;
1992 /* Default heads (64) & sectors (32) */
1993 heads = 64;
1994 sectors = 32;
1995
1996 tmp = heads * sectors;
1997 cylinders = capacity;
1998
1999 sector_div(cylinders, tmp);
2000
2001 /*
2002 * Handle extended translation size for logical drives > 1Gb
2003 */
2004
2005 if (capacity >= 0x200000) {
2006 heads = 255;
2007 sectors = 63;
2008 tmp = heads*sectors;
2009 cylinders = capacity;
2010 sector_div(cylinders, tmp);
2011 }
2012
2013 geom[0] = heads;
2014 geom[1] = sectors;
2015 geom[2] = cylinders;
2016
2017 return 0;
2018}
2019
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002020static void megasas_aen_polling(struct work_struct *work);
2021
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002022/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002023 * megasas_service_aen - Processes an event notification
2024 * @instance: Adapter soft state
2025 * @cmd: AEN command completed by the ISR
2026 *
2027 * For AEN, driver sends a command down to FW that is held by the FW till an
2028 * event occurs. When an event of interest occurs, FW completes the command
2029 * that it was previously holding.
2030 *
2031 * This routines sends SIGIO signal to processes that have registered with the
2032 * driver for AEN.
2033 */
2034static void
2035megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2036{
Yang, Boc3518832009-10-06 14:18:02 -06002037 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002038 /*
2039 * Don't signal app if it is just an aborted previously registered aen
2040 */
Yang, Boc3518832009-10-06 14:18:02 -06002041 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2042 spin_lock_irqsave(&poll_aen_lock, flags);
2043 megasas_poll_wait_aen = 1;
2044 spin_unlock_irqrestore(&poll_aen_lock, flags);
2045 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002046 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002047 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002048 else
2049 cmd->abort_aen = 0;
2050
2051 instance->aen_cmd = NULL;
2052 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002053
bo yang39a98552010-09-22 22:36:29 -04002054 if ((instance->unload == 0) &&
2055 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002056 struct megasas_aen_event *ev;
2057 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2058 if (!ev) {
2059 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2060 } else {
2061 ev->instance = instance;
2062 instance->ev = ev;
2063 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2064 schedule_delayed_work(
2065 (struct delayed_work *)&ev->hotplug_work, 0);
2066 }
2067 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002068}
2069
adam radford4bcde502011-07-26 15:42:52 -07002070static int megasas_change_queue_depth(struct scsi_device *sdev,
2071 int queue_depth, int reason)
2072{
2073 if (reason != SCSI_QDEPTH_DEFAULT)
2074 return -EOPNOTSUPP;
2075
2076 if (queue_depth > sdev->host->can_queue)
2077 queue_depth = sdev->host->can_queue;
2078 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2079 queue_depth);
2080
2081 return queue_depth;
2082}
2083
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002084/*
2085 * Scsi host template for megaraid_sas driver
2086 */
2087static struct scsi_host_template megasas_template = {
2088
2089 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002090 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002091 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002092 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002093 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002094 .queuecommand = megasas_queue_command,
2095 .eh_device_reset_handler = megasas_reset_device,
2096 .eh_bus_reset_handler = megasas_reset_bus_host,
2097 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002098 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002099 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002100 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002101 .change_queue_depth = megasas_change_queue_depth,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002102};
2103
2104/**
2105 * megasas_complete_int_cmd - Completes an internal command
2106 * @instance: Adapter soft state
2107 * @cmd: Command to be completed
2108 *
2109 * The megasas_issue_blocked_cmd() function waits for a command to complete
2110 * after it issues a command. This function wakes up that waiting routine by
2111 * calling wake_up() on the wait queue.
2112 */
2113static void
2114megasas_complete_int_cmd(struct megasas_instance *instance,
2115 struct megasas_cmd *cmd)
2116{
2117 cmd->cmd_status = cmd->frame->io.cmd_status;
2118
2119 if (cmd->cmd_status == ENODATA) {
2120 cmd->cmd_status = 0;
2121 }
2122 wake_up(&instance->int_cmd_wait_q);
2123}
2124
2125/**
2126 * megasas_complete_abort - Completes aborting a command
2127 * @instance: Adapter soft state
2128 * @cmd: Cmd that was issued to abort another cmd
2129 *
adam radford0d490162010-12-14 19:17:17 -08002130 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2131 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002132 * wakes up all functions waiting on the same wait queue.
2133 */
2134static void
2135megasas_complete_abort(struct megasas_instance *instance,
2136 struct megasas_cmd *cmd)
2137{
2138 if (cmd->sync_cmd) {
2139 cmd->sync_cmd = 0;
2140 cmd->cmd_status = 0;
2141 wake_up(&instance->abort_cmd_wait_q);
2142 }
2143
2144 return;
2145}
2146
2147/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002148 * megasas_complete_cmd - Completes a command
2149 * @instance: Adapter soft state
2150 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002151 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002152 * SCSI mid-layer instead of the value returned
2153 * by the FW. This should be used if caller wants
2154 * an alternate status (as in the case of aborted
2155 * commands)
2156 */
adam radford9c915a82010-12-21 13:34:31 -08002157void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002158megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2159 u8 alt_status)
2160{
2161 int exception = 0;
2162 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002163 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002164 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002165
bo yang39a98552010-09-22 22:36:29 -04002166 /* flag for the retry reset */
2167 cmd->retry_for_fw_reset = 0;
2168
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002169 if (cmd->scmd)
2170 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002171
2172 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002173 case MFI_CMD_INVALID:
2174 /* Some older 1068 controller FW may keep a pended
2175 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2176 when booting the kdump kernel. Ignore this command to
2177 prevent a kernel panic on shutdown of the kdump kernel. */
2178 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2179 "completed.\n");
2180 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2181 "other than PERC5, please upgrade your firmware.\n");
2182 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002183 case MFI_CMD_PD_SCSI_IO:
2184 case MFI_CMD_LD_SCSI_IO:
2185
2186 /*
2187 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2188 * issued either through an IO path or an IOCTL path. If it
2189 * was via IOCTL, we will send it to internal completion.
2190 */
2191 if (cmd->sync_cmd) {
2192 cmd->sync_cmd = 0;
2193 megasas_complete_int_cmd(instance, cmd);
2194 break;
2195 }
2196
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002197 case MFI_CMD_LD_READ:
2198 case MFI_CMD_LD_WRITE:
2199
2200 if (alt_status) {
2201 cmd->scmd->result = alt_status << 16;
2202 exception = 1;
2203 }
2204
2205 if (exception) {
2206
Sumant Patroe4a082c2006-05-30 12:03:37 -07002207 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002208
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002209 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002210 cmd->scmd->scsi_done(cmd->scmd);
2211 megasas_return_cmd(instance, cmd);
2212
2213 break;
2214 }
2215
2216 switch (hdr->cmd_status) {
2217
2218 case MFI_STAT_OK:
2219 cmd->scmd->result = DID_OK << 16;
2220 break;
2221
2222 case MFI_STAT_SCSI_IO_FAILED:
2223 case MFI_STAT_LD_INIT_IN_PROGRESS:
2224 cmd->scmd->result =
2225 (DID_ERROR << 16) | hdr->scsi_status;
2226 break;
2227
2228 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2229
2230 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2231
2232 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2233 memset(cmd->scmd->sense_buffer, 0,
2234 SCSI_SENSE_BUFFERSIZE);
2235 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2236 hdr->sense_len);
2237
2238 cmd->scmd->result |= DRIVER_SENSE << 24;
2239 }
2240
2241 break;
2242
2243 case MFI_STAT_LD_OFFLINE:
2244 case MFI_STAT_DEVICE_NOT_FOUND:
2245 cmd->scmd->result = DID_BAD_TARGET << 16;
2246 break;
2247
2248 default:
2249 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2250 hdr->cmd_status);
2251 cmd->scmd->result = DID_ERROR << 16;
2252 break;
2253 }
2254
Sumant Patroe4a082c2006-05-30 12:03:37 -07002255 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002256
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002257 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002258 cmd->scmd->scsi_done(cmd->scmd);
2259 megasas_return_cmd(instance, cmd);
2260
2261 break;
2262
2263 case MFI_CMD_SMP:
2264 case MFI_CMD_STP:
2265 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002266 /* Check for LD map update */
2267 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2268 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2269 spin_lock_irqsave(instance->host->host_lock, flags);
2270 if (cmd->frame->hdr.cmd_status != 0) {
2271 if (cmd->frame->hdr.cmd_status !=
2272 MFI_STAT_NOT_FOUND)
2273 printk(KERN_WARNING "megasas: map sync"
2274 "failed, status = 0x%x.\n",
2275 cmd->frame->hdr.cmd_status);
2276 else {
2277 megasas_return_cmd(instance, cmd);
2278 spin_unlock_irqrestore(
2279 instance->host->host_lock,
2280 flags);
2281 break;
2282 }
2283 } else
2284 instance->map_id++;
2285 megasas_return_cmd(instance, cmd);
2286 if (MR_ValidateMapInfo(
2287 fusion->ld_map[(instance->map_id & 1)],
2288 fusion->load_balance_info))
2289 fusion->fast_path_io = 1;
2290 else
2291 fusion->fast_path_io = 0;
2292 megasas_sync_map_info(instance);
2293 spin_unlock_irqrestore(instance->host->host_lock,
2294 flags);
2295 break;
2296 }
Yang, Boc3518832009-10-06 14:18:02 -06002297 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2298 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2299 spin_lock_irqsave(&poll_aen_lock, flags);
2300 megasas_poll_wait_aen = 0;
2301 spin_unlock_irqrestore(&poll_aen_lock, flags);
2302 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002303
2304 /*
2305 * See if got an event notification
2306 */
2307 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2308 megasas_service_aen(instance, cmd);
2309 else
2310 megasas_complete_int_cmd(instance, cmd);
2311
2312 break;
2313
2314 case MFI_CMD_ABORT:
2315 /*
2316 * Cmd issued to abort another cmd returned
2317 */
2318 megasas_complete_abort(instance, cmd);
2319 break;
2320
2321 default:
2322 printk("megasas: Unknown command completed! [0x%X]\n",
2323 hdr->cmd);
2324 break;
2325 }
2326}
2327
2328/**
bo yang39a98552010-09-22 22:36:29 -04002329 * megasas_issue_pending_cmds_again - issue all pending cmds
2330 * in FW again because of the fw reset
2331 * @instance: Adapter soft state
2332 */
2333static inline void
2334megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2335{
2336 struct megasas_cmd *cmd;
2337 struct list_head clist_local;
2338 union megasas_evt_class_locale class_locale;
2339 unsigned long flags;
2340 u32 seq_num;
2341
2342 INIT_LIST_HEAD(&clist_local);
2343 spin_lock_irqsave(&instance->hba_lock, flags);
2344 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2345 spin_unlock_irqrestore(&instance->hba_lock, flags);
2346
2347 while (!list_empty(&clist_local)) {
2348 cmd = list_entry((&clist_local)->next,
2349 struct megasas_cmd, list);
2350 list_del_init(&cmd->list);
2351
2352 if (cmd->sync_cmd || cmd->scmd) {
2353 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2354 "detected to be pending while HBA reset.\n",
2355 cmd, cmd->scmd, cmd->sync_cmd);
2356
2357 cmd->retry_for_fw_reset++;
2358
2359 if (cmd->retry_for_fw_reset == 3) {
2360 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2361 "was tried multiple times during reset."
2362 "Shutting down the HBA\n",
2363 cmd, cmd->scmd, cmd->sync_cmd);
2364 megaraid_sas_kill_hba(instance);
2365
2366 instance->adprecovery =
2367 MEGASAS_HW_CRITICAL_ERROR;
2368 return;
2369 }
2370 }
2371
2372 if (cmd->sync_cmd == 1) {
2373 if (cmd->scmd) {
2374 printk(KERN_NOTICE "megaraid_sas: unexpected"
2375 "cmd attached to internal command!\n");
2376 }
2377 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2378 "on the internal reset queue,"
2379 "issue it again.\n", cmd);
2380 cmd->cmd_status = ENODATA;
2381 instance->instancet->fire_cmd(instance,
2382 cmd->frame_phys_addr ,
2383 0, instance->reg_set);
2384 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002385 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002386 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002387 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002388
2389 atomic_inc(&instance->fw_outstanding);
2390 instance->instancet->fire_cmd(instance,
2391 cmd->frame_phys_addr,
2392 cmd->frame_count-1, instance->reg_set);
2393 } else {
2394 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2395 "internal reset defer list while re-issue!!\n",
2396 cmd);
2397 }
2398 }
2399
2400 if (instance->aen_cmd) {
2401 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2402 megasas_return_cmd(instance, instance->aen_cmd);
2403
2404 instance->aen_cmd = NULL;
2405 }
2406
2407 /*
2408 * Initiate AEN (Asynchronous Event Notification)
2409 */
2410 seq_num = instance->last_seq_num;
2411 class_locale.members.reserved = 0;
2412 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2413 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2414
2415 megasas_register_aen(instance, seq_num, class_locale.word);
2416}
2417
2418/**
2419 * Move the internal reset pending commands to a deferred queue.
2420 *
2421 * We move the commands pending at internal reset time to a
2422 * pending queue. This queue would be flushed after successful
2423 * completion of the internal reset sequence. if the internal reset
2424 * did not complete in time, the kernel reset handler would flush
2425 * these commands.
2426 **/
2427static void
2428megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2429{
2430 struct megasas_cmd *cmd;
2431 int i;
2432 u32 max_cmd = instance->max_fw_cmds;
2433 u32 defer_index;
2434 unsigned long flags;
2435
2436 defer_index = 0;
2437 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2438 for (i = 0; i < max_cmd; i++) {
2439 cmd = instance->cmd_list[i];
2440 if (cmd->sync_cmd == 1 || cmd->scmd) {
2441 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2442 "on the defer queue as internal\n",
2443 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2444
2445 if (!list_empty(&cmd->list)) {
2446 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2447 " moving this cmd:%p, %d %p, it was"
2448 "discovered on some list?\n",
2449 cmd, cmd->sync_cmd, cmd->scmd);
2450
2451 list_del_init(&cmd->list);
2452 }
2453 defer_index++;
2454 list_add_tail(&cmd->list,
2455 &instance->internal_reset_pending_q);
2456 }
2457 }
2458 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2459}
2460
2461
2462static void
2463process_fw_state_change_wq(struct work_struct *work)
2464{
2465 struct megasas_instance *instance =
2466 container_of(work, struct megasas_instance, work_init);
2467 u32 wait;
2468 unsigned long flags;
2469
2470 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2471 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2472 instance->adprecovery);
2473 return ;
2474 }
2475
2476 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2477 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2478 "state, restarting it...\n");
2479
2480 instance->instancet->disable_intr(instance->reg_set);
2481 atomic_set(&instance->fw_outstanding, 0);
2482
2483 atomic_set(&instance->fw_reset_no_pci_access, 1);
2484 instance->instancet->adp_reset(instance, instance->reg_set);
2485 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2486
2487 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2488 "initiating next stage...\n");
2489
2490 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2491 "state 2 starting...\n");
2492
2493 /*waitting for about 20 second before start the second init*/
2494 for (wait = 0; wait < 30; wait++) {
2495 msleep(1000);
2496 }
2497
adam radford058a8fa2011-10-08 18:14:27 -07002498 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04002499 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2500
2501 megaraid_sas_kill_hba(instance);
2502 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2503 return ;
2504 }
2505
2506 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2507 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2508 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2509 ) {
2510 *instance->consumer = *instance->producer;
2511 } else {
2512 *instance->consumer = 0;
2513 *instance->producer = 0;
2514 }
2515
2516 megasas_issue_init_mfi(instance);
2517
2518 spin_lock_irqsave(&instance->hba_lock, flags);
2519 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2520 spin_unlock_irqrestore(&instance->hba_lock, flags);
2521 instance->instancet->enable_intr(instance->reg_set);
2522
2523 megasas_issue_pending_cmds_again(instance);
2524 instance->issuepend_done = 1;
2525 }
2526 return ;
2527}
2528
2529/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002530 * megasas_deplete_reply_queue - Processes all completed commands
2531 * @instance: Adapter soft state
2532 * @alt_status: Alternate status to be returned to
2533 * SCSI mid-layer instead of the status
2534 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002535 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002536 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002537static int
bo yang39a98552010-09-22 22:36:29 -04002538megasas_deplete_reply_queue(struct megasas_instance *instance,
2539 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002540{
bo yang39a98552010-09-22 22:36:29 -04002541 u32 mfiStatus;
2542 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002543
bo yang39a98552010-09-22 22:36:29 -04002544 if ((mfiStatus = instance->instancet->check_reset(instance,
2545 instance->reg_set)) == 1) {
2546 return IRQ_HANDLED;
2547 }
2548
2549 if ((mfiStatus = instance->instancet->clear_intr(
2550 instance->reg_set)
2551 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002552 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07002553 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08002554 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002555 }
2556
2557 instance->mfiStatus = mfiStatus;
2558
2559 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2560 fw_state = instance->instancet->read_fw_status_reg(
2561 instance->reg_set) & MFI_STATE_MASK;
2562
2563 if (fw_state != MFI_STATE_FAULT) {
2564 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2565 fw_state);
2566 }
2567
2568 if ((fw_state == MFI_STATE_FAULT) &&
2569 (instance->disableOnlineCtrlReset == 0)) {
2570 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2571
2572 if ((instance->pdev->device ==
2573 PCI_DEVICE_ID_LSI_SAS1064R) ||
2574 (instance->pdev->device ==
2575 PCI_DEVICE_ID_DELL_PERC5) ||
2576 (instance->pdev->device ==
2577 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2578
2579 *instance->consumer =
2580 MEGASAS_ADPRESET_INPROG_SIGN;
2581 }
2582
2583
2584 instance->instancet->disable_intr(instance->reg_set);
2585 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2586 instance->issuepend_done = 0;
2587
2588 atomic_set(&instance->fw_outstanding, 0);
2589 megasas_internal_reset_defer_cmds(instance);
2590
2591 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2592 fw_state, instance->adprecovery);
2593
2594 schedule_work(&instance->work_init);
2595 return IRQ_HANDLED;
2596
2597 } else {
2598 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2599 fw_state, instance->disableOnlineCtrlReset);
2600 }
2601 }
2602
Sumant Patro5d018ad2006-10-03 13:13:18 -07002603 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002604 return IRQ_HANDLED;
2605}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002606/**
2607 * megasas_isr - isr entry point
2608 */
David Howells7d12e782006-10-05 14:55:46 +01002609static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002610{
adam radfordc8e858f2011-10-08 18:15:13 -07002611 struct megasas_irq_context *irq_context = devp;
2612 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04002613 unsigned long flags;
2614 irqreturn_t rc;
2615
adam radfordc8e858f2011-10-08 18:15:13 -07002616 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04002617 return IRQ_HANDLED;
2618
bo yang39a98552010-09-22 22:36:29 -04002619 spin_lock_irqsave(&instance->hba_lock, flags);
2620 rc = megasas_deplete_reply_queue(instance, DID_OK);
2621 spin_unlock_irqrestore(&instance->hba_lock, flags);
2622
2623 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002624}
2625
2626/**
2627 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002628 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002629 *
2630 * During the initialization, FW passes can potentially be in any one of
2631 * several possible states. If the FW in operational, waiting-for-handshake
2632 * states, driver must take steps to bring it to ready state. Otherwise, it
2633 * has to wait for the ready state.
2634 */
adam radford9c915a82010-12-21 13:34:31 -08002635int
adam radford058a8fa2011-10-08 18:14:27 -07002636megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002637{
2638 int i;
2639 u8 max_wait;
2640 u32 fw_state;
2641 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002642 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002643
Sumant Patro1341c932006-01-25 12:02:40 -08002644 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002645
Sumant Patroe3bbff92006-10-03 12:28:49 -07002646 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002647 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2648 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002649
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002650 while (fw_state != MFI_STATE_READY) {
2651
Yang, Bo7218df62009-10-06 14:52:20 -06002652 abs_state =
2653 instance->instancet->read_fw_status_reg(instance->reg_set);
2654
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002655 switch (fw_state) {
2656
2657 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002658 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07002659 if (ocr) {
2660 max_wait = MEGASAS_RESET_WAIT_TIME;
2661 cur_state = MFI_STATE_FAULT;
2662 break;
2663 } else
2664 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002665
2666 case MFI_STATE_WAIT_HANDSHAKE:
2667 /*
2668 * Set the CLR bit in inbound doorbell
2669 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002670 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002671 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2672 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002673 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2674 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002675 PCI_DEVICE_ID_LSI_FUSION) ||
2676 (instance->pdev->device ==
2677 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002678 writel(
2679 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002680 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002681 } else {
2682 writel(
2683 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2684 &instance->reg_set->inbound_doorbell);
2685 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002686
Yang, Bo7218df62009-10-06 14:52:20 -06002687 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002688 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2689 break;
2690
Sumant Patroe3bbff92006-10-03 12:28:49 -07002691 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002692 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002693 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2694 (instance->pdev->device ==
2695 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2696 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002697 PCI_DEVICE_ID_LSI_FUSION) ||
2698 (instance->pdev->device ==
2699 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002700 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002701 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002702 } else
2703 writel(MFI_INIT_HOTPLUG,
2704 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002705
Yang, Bo7218df62009-10-06 14:52:20 -06002706 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002707 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2708 break;
2709
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002710 case MFI_STATE_OPERATIONAL:
2711 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002712 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002713 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002714 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002715 if ((instance->pdev->device ==
2716 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2717 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002718 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2719 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07002720 == PCI_DEVICE_ID_LSI_FUSION) ||
2721 (instance->pdev->device
2722 == PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002723 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002724 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07002725 if ((instance->pdev->device ==
2726 PCI_DEVICE_ID_LSI_FUSION) ||
2727 (instance->pdev->device ==
2728 PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08002729 for (i = 0; i < (10 * 1000); i += 20) {
2730 if (readl(
2731 &instance->
2732 reg_set->
2733 doorbell) & 1)
2734 msleep(20);
2735 else
2736 break;
2737 }
2738 }
Yang, Bo87911122009-10-06 14:31:54 -06002739 } else
2740 writel(MFI_RESET_FLAGS,
2741 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002742
Yang, Bo7218df62009-10-06 14:52:20 -06002743 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002744 cur_state = MFI_STATE_OPERATIONAL;
2745 break;
2746
2747 case MFI_STATE_UNDEFINED:
2748 /*
2749 * This state should not last for more than 2 seconds
2750 */
Yang, Bo7218df62009-10-06 14:52:20 -06002751 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002752 cur_state = MFI_STATE_UNDEFINED;
2753 break;
2754
2755 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002756 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002757 cur_state = MFI_STATE_BB_INIT;
2758 break;
2759
2760 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002761 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002762 cur_state = MFI_STATE_FW_INIT;
2763 break;
2764
2765 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002766 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002767 cur_state = MFI_STATE_FW_INIT_2;
2768 break;
2769
2770 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002771 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002772 cur_state = MFI_STATE_DEVICE_SCAN;
2773 break;
2774
2775 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002776 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002777 cur_state = MFI_STATE_FLUSH_CACHE;
2778 break;
2779
2780 default:
2781 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2782 fw_state);
2783 return -ENODEV;
2784 }
2785
2786 /*
2787 * The cur_state should not last for more than max_wait secs
2788 */
2789 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002790 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002791 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002792 curr_abs_state =
2793 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002794
Yang, Bo7218df62009-10-06 14:52:20 -06002795 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002796 msleep(1);
2797 } else
2798 break;
2799 }
2800
2801 /*
2802 * Return error if fw_state hasn't changed after max_wait
2803 */
Yang, Bo7218df62009-10-06 14:52:20 -06002804 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002805 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2806 "in %d secs\n", fw_state, max_wait);
2807 return -ENODEV;
2808 }
bo yang39a98552010-09-22 22:36:29 -04002809 }
adam radford0d490162010-12-14 19:17:17 -08002810 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002811
2812 return 0;
2813}
2814
2815/**
2816 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2817 * @instance: Adapter soft state
2818 */
2819static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2820{
2821 int i;
adam radford9c915a82010-12-21 13:34:31 -08002822 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002823 struct megasas_cmd *cmd;
2824
2825 if (!instance->frame_dma_pool)
2826 return;
2827
2828 /*
2829 * Return all frames to pool
2830 */
2831 for (i = 0; i < max_cmd; i++) {
2832
2833 cmd = instance->cmd_list[i];
2834
2835 if (cmd->frame)
2836 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2837 cmd->frame_phys_addr);
2838
2839 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002840 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002841 cmd->sense_phys_addr);
2842 }
2843
2844 /*
2845 * Now destroy the pool itself
2846 */
2847 pci_pool_destroy(instance->frame_dma_pool);
2848 pci_pool_destroy(instance->sense_dma_pool);
2849
2850 instance->frame_dma_pool = NULL;
2851 instance->sense_dma_pool = NULL;
2852}
2853
2854/**
2855 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2856 * @instance: Adapter soft state
2857 *
2858 * Each command packet has an embedded DMA memory buffer that is used for
2859 * filling MFI frame and the SG list that immediately follows the frame. This
2860 * function creates those DMA memory buffers for each command packet by using
2861 * PCI pool facility.
2862 */
2863static int megasas_create_frame_pool(struct megasas_instance *instance)
2864{
2865 int i;
2866 u32 max_cmd;
2867 u32 sge_sz;
2868 u32 sgl_sz;
2869 u32 total_sz;
2870 u32 frame_count;
2871 struct megasas_cmd *cmd;
2872
adam radford9c915a82010-12-21 13:34:31 -08002873 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002874
2875 /*
2876 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2877 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2878 */
2879 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2880 sizeof(struct megasas_sge32);
2881
Yang, Bof4c9a132009-10-06 14:43:28 -06002882 if (instance->flag_ieee) {
2883 sge_sz = sizeof(struct megasas_sge_skinny);
2884 }
2885
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002886 /*
2887 * Calculated the number of 64byte frames required for SGL
2888 */
2889 sgl_sz = sge_sz * instance->max_num_sge;
2890 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002891 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002892
2893 /*
2894 * We need one extra frame for the MFI command
2895 */
2896 frame_count++;
2897
2898 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2899 /*
2900 * Use DMA pool facility provided by PCI layer
2901 */
2902 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2903 instance->pdev, total_sz, 64,
2904 0);
2905
2906 if (!instance->frame_dma_pool) {
2907 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2908 return -ENOMEM;
2909 }
2910
2911 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2912 instance->pdev, 128, 4, 0);
2913
2914 if (!instance->sense_dma_pool) {
2915 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2916
2917 pci_pool_destroy(instance->frame_dma_pool);
2918 instance->frame_dma_pool = NULL;
2919
2920 return -ENOMEM;
2921 }
2922
2923 /*
2924 * Allocate and attach a frame to each of the commands in cmd_list.
2925 * By making cmd->index as the context instead of the &cmd, we can
2926 * always use 32bit context regardless of the architecture
2927 */
2928 for (i = 0; i < max_cmd; i++) {
2929
2930 cmd = instance->cmd_list[i];
2931
2932 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2933 GFP_KERNEL, &cmd->frame_phys_addr);
2934
2935 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2936 GFP_KERNEL, &cmd->sense_phys_addr);
2937
2938 /*
2939 * megasas_teardown_frame_pool() takes care of freeing
2940 * whatever has been allocated
2941 */
2942 if (!cmd->frame || !cmd->sense) {
2943 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2944 megasas_teardown_frame_pool(instance);
2945 return -ENOMEM;
2946 }
2947
Yang, Bo707e09b2010-10-12 07:20:27 -06002948 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002949 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002950 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07002951 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2952 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2953 (reset_devices))
2954 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002955 }
2956
2957 return 0;
2958}
2959
2960/**
2961 * megasas_free_cmds - Free all the cmds in the free cmd pool
2962 * @instance: Adapter soft state
2963 */
adam radford9c915a82010-12-21 13:34:31 -08002964void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002965{
2966 int i;
2967 /* First free the MFI frame pool */
2968 megasas_teardown_frame_pool(instance);
2969
2970 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002971 for (i = 0; i < instance->max_mfi_cmds; i++)
2972
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002973 kfree(instance->cmd_list[i]);
2974
2975 /* Free the cmd_list buffer itself */
2976 kfree(instance->cmd_list);
2977 instance->cmd_list = NULL;
2978
2979 INIT_LIST_HEAD(&instance->cmd_pool);
2980}
2981
2982/**
2983 * megasas_alloc_cmds - Allocates the command packets
2984 * @instance: Adapter soft state
2985 *
2986 * Each command that is issued to the FW, whether IO commands from the OS or
2987 * internal commands like IOCTLs, are wrapped in local data structure called
2988 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2989 * the FW.
2990 *
2991 * Each frame has a 32-bit field called context (tag). This context is used
2992 * to get back the megasas_cmd from the frame when a frame gets completed in
2993 * the ISR. Typically the address of the megasas_cmd itself would be used as
2994 * the context. But we wanted to keep the differences between 32 and 64 bit
2995 * systems to the mininum. We always use 32 bit integers for the context. In
2996 * this driver, the 32 bit values are the indices into an array cmd_list.
2997 * This array is used only to look up the megasas_cmd given the context. The
2998 * free commands themselves are maintained in a linked list called cmd_pool.
2999 */
adam radford9c915a82010-12-21 13:34:31 -08003000int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003001{
3002 int i;
3003 int j;
3004 u32 max_cmd;
3005 struct megasas_cmd *cmd;
3006
adam radford9c915a82010-12-21 13:34:31 -08003007 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003008
3009 /*
3010 * instance->cmd_list is an array of struct megasas_cmd pointers.
3011 * Allocate the dynamic array first and then allocate individual
3012 * commands.
3013 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003014 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003015
3016 if (!instance->cmd_list) {
3017 printk(KERN_DEBUG "megasas: out of memory\n");
3018 return -ENOMEM;
3019 }
3020
adam radford9c915a82010-12-21 13:34:31 -08003021 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003022
3023 for (i = 0; i < max_cmd; i++) {
3024 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3025 GFP_KERNEL);
3026
3027 if (!instance->cmd_list[i]) {
3028
3029 for (j = 0; j < i; j++)
3030 kfree(instance->cmd_list[j]);
3031
3032 kfree(instance->cmd_list);
3033 instance->cmd_list = NULL;
3034
3035 return -ENOMEM;
3036 }
3037 }
3038
3039 /*
3040 * Add all the commands to command pool (instance->cmd_pool)
3041 */
3042 for (i = 0; i < max_cmd; i++) {
3043 cmd = instance->cmd_list[i];
3044 memset(cmd, 0, sizeof(struct megasas_cmd));
3045 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003046 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003047 cmd->instance = instance;
3048
3049 list_add_tail(&cmd->list, &instance->cmd_pool);
3050 }
3051
3052 /*
3053 * Create a frame pool and assign one frame to each cmd
3054 */
3055 if (megasas_create_frame_pool(instance)) {
3056 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3057 megasas_free_cmds(instance);
3058 }
3059
3060 return 0;
3061}
3062
Yang, Bo81e403c2009-10-06 14:27:54 -06003063/*
3064 * megasas_get_pd_list_info - Returns FW's pd_list structure
3065 * @instance: Adapter soft state
3066 * @pd_list: pd_list structure
3067 *
3068 * Issues an internal command (DCMD) to get the FW's controller PD
3069 * list structure. This information is mainly used to find out SYSTEM
3070 * supported by the FW.
3071 */
3072static int
3073megasas_get_pd_list(struct megasas_instance *instance)
3074{
3075 int ret = 0, pd_index = 0;
3076 struct megasas_cmd *cmd;
3077 struct megasas_dcmd_frame *dcmd;
3078 struct MR_PD_LIST *ci;
3079 struct MR_PD_ADDRESS *pd_addr;
3080 dma_addr_t ci_h = 0;
3081
3082 cmd = megasas_get_cmd(instance);
3083
3084 if (!cmd) {
3085 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3086 return -ENOMEM;
3087 }
3088
3089 dcmd = &cmd->frame->dcmd;
3090
3091 ci = pci_alloc_consistent(instance->pdev,
3092 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3093
3094 if (!ci) {
3095 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3096 megasas_return_cmd(instance, cmd);
3097 return -ENOMEM;
3098 }
3099
3100 memset(ci, 0, sizeof(*ci));
3101 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3102
3103 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3104 dcmd->mbox.b[1] = 0;
3105 dcmd->cmd = MFI_CMD_DCMD;
3106 dcmd->cmd_status = 0xFF;
3107 dcmd->sge_count = 1;
3108 dcmd->flags = MFI_FRAME_DIR_READ;
3109 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003110 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003111 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3112 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3113 dcmd->sgl.sge32[0].phys_addr = ci_h;
3114 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3115
3116 if (!megasas_issue_polled(instance, cmd)) {
3117 ret = 0;
3118 } else {
3119 ret = -1;
3120 }
3121
3122 /*
3123 * the following function will get the instance PD LIST.
3124 */
3125
3126 pd_addr = ci->addr;
3127
3128 if ( ret == 0 &&
3129 (ci->count <
3130 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3131
3132 memset(instance->pd_list, 0,
3133 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3134
3135 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3136
3137 instance->pd_list[pd_addr->deviceId].tid =
3138 pd_addr->deviceId;
3139 instance->pd_list[pd_addr->deviceId].driveType =
3140 pd_addr->scsiDevType;
3141 instance->pd_list[pd_addr->deviceId].driveState =
3142 MR_PD_STATE_SYSTEM;
3143 pd_addr++;
3144 }
3145 }
3146
3147 pci_free_consistent(instance->pdev,
3148 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3149 ci, ci_h);
3150 megasas_return_cmd(instance, cmd);
3151
3152 return ret;
3153}
3154
Yang, Bobdc6fb82009-12-06 08:30:19 -07003155/*
3156 * megasas_get_ld_list_info - Returns FW's ld_list structure
3157 * @instance: Adapter soft state
3158 * @ld_list: ld_list structure
3159 *
3160 * Issues an internal command (DCMD) to get the FW's controller PD
3161 * list structure. This information is mainly used to find out SYSTEM
3162 * supported by the FW.
3163 */
3164static int
3165megasas_get_ld_list(struct megasas_instance *instance)
3166{
3167 int ret = 0, ld_index = 0, ids = 0;
3168 struct megasas_cmd *cmd;
3169 struct megasas_dcmd_frame *dcmd;
3170 struct MR_LD_LIST *ci;
3171 dma_addr_t ci_h = 0;
3172
3173 cmd = megasas_get_cmd(instance);
3174
3175 if (!cmd) {
3176 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3177 return -ENOMEM;
3178 }
3179
3180 dcmd = &cmd->frame->dcmd;
3181
3182 ci = pci_alloc_consistent(instance->pdev,
3183 sizeof(struct MR_LD_LIST),
3184 &ci_h);
3185
3186 if (!ci) {
3187 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3188 megasas_return_cmd(instance, cmd);
3189 return -ENOMEM;
3190 }
3191
3192 memset(ci, 0, sizeof(*ci));
3193 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3194
3195 dcmd->cmd = MFI_CMD_DCMD;
3196 dcmd->cmd_status = 0xFF;
3197 dcmd->sge_count = 1;
3198 dcmd->flags = MFI_FRAME_DIR_READ;
3199 dcmd->timeout = 0;
3200 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3201 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3202 dcmd->sgl.sge32[0].phys_addr = ci_h;
3203 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3204 dcmd->pad_0 = 0;
3205
3206 if (!megasas_issue_polled(instance, cmd)) {
3207 ret = 0;
3208 } else {
3209 ret = -1;
3210 }
3211
3212 /* the following function will get the instance PD LIST */
3213
bo yang39a98552010-09-22 22:36:29 -04003214 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003215 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3216
3217 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3218 if (ci->ldList[ld_index].state != 0) {
3219 ids = ci->ldList[ld_index].ref.targetId;
3220 instance->ld_ids[ids] =
3221 ci->ldList[ld_index].ref.targetId;
3222 }
3223 }
3224 }
3225
3226 pci_free_consistent(instance->pdev,
3227 sizeof(struct MR_LD_LIST),
3228 ci,
3229 ci_h);
3230
3231 megasas_return_cmd(instance, cmd);
3232 return ret;
3233}
3234
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003235/**
3236 * megasas_get_controller_info - Returns FW's controller structure
3237 * @instance: Adapter soft state
3238 * @ctrl_info: Controller information structure
3239 *
3240 * Issues an internal command (DCMD) to get the FW's controller structure.
3241 * This information is mainly used to find out the maximum IO transfer per
3242 * command supported by the FW.
3243 */
3244static int
3245megasas_get_ctrl_info(struct megasas_instance *instance,
3246 struct megasas_ctrl_info *ctrl_info)
3247{
3248 int ret = 0;
3249 struct megasas_cmd *cmd;
3250 struct megasas_dcmd_frame *dcmd;
3251 struct megasas_ctrl_info *ci;
3252 dma_addr_t ci_h = 0;
3253
3254 cmd = megasas_get_cmd(instance);
3255
3256 if (!cmd) {
3257 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3258 return -ENOMEM;
3259 }
3260
3261 dcmd = &cmd->frame->dcmd;
3262
3263 ci = pci_alloc_consistent(instance->pdev,
3264 sizeof(struct megasas_ctrl_info), &ci_h);
3265
3266 if (!ci) {
3267 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3268 megasas_return_cmd(instance, cmd);
3269 return -ENOMEM;
3270 }
3271
3272 memset(ci, 0, sizeof(*ci));
3273 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3274
3275 dcmd->cmd = MFI_CMD_DCMD;
3276 dcmd->cmd_status = 0xFF;
3277 dcmd->sge_count = 1;
3278 dcmd->flags = MFI_FRAME_DIR_READ;
3279 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003280 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003281 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3282 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3283 dcmd->sgl.sge32[0].phys_addr = ci_h;
3284 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3285
3286 if (!megasas_issue_polled(instance, cmd)) {
3287 ret = 0;
3288 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3289 } else {
3290 ret = -1;
3291 }
3292
3293 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3294 ci, ci_h);
3295
3296 megasas_return_cmd(instance, cmd);
3297 return ret;
3298}
3299
3300/**
bo yang31ea7082007-11-07 12:09:50 -05003301 * megasas_issue_init_mfi - Initializes the FW
3302 * @instance: Adapter soft state
3303 *
3304 * Issues the INIT MFI cmd
3305 */
3306static int
3307megasas_issue_init_mfi(struct megasas_instance *instance)
3308{
3309 u32 context;
3310
3311 struct megasas_cmd *cmd;
3312
3313 struct megasas_init_frame *init_frame;
3314 struct megasas_init_queue_info *initq_info;
3315 dma_addr_t init_frame_h;
3316 dma_addr_t initq_info_h;
3317
3318 /*
3319 * Prepare a init frame. Note the init frame points to queue info
3320 * structure. Each frame has SGL allocated after first 64 bytes. For
3321 * this frame - since we don't need any SGL - we use SGL's space as
3322 * queue info structure
3323 *
3324 * We will not get a NULL command below. We just created the pool.
3325 */
3326 cmd = megasas_get_cmd(instance);
3327
3328 init_frame = (struct megasas_init_frame *)cmd->frame;
3329 initq_info = (struct megasas_init_queue_info *)
3330 ((unsigned long)init_frame + 64);
3331
3332 init_frame_h = cmd->frame_phys_addr;
3333 initq_info_h = init_frame_h + 64;
3334
3335 context = init_frame->context;
3336 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3337 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3338 init_frame->context = context;
3339
3340 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3341 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3342
3343 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3344 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3345
3346 init_frame->cmd = MFI_CMD_INIT;
3347 init_frame->cmd_status = 0xFF;
3348 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3349
3350 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3351
3352 /*
3353 * disable the intr before firing the init frame to FW
3354 */
3355 instance->instancet->disable_intr(instance->reg_set);
3356
3357 /*
3358 * Issue the init frame in polled mode
3359 */
3360
3361 if (megasas_issue_polled(instance, cmd)) {
3362 printk(KERN_ERR "megasas: Failed to init firmware\n");
3363 megasas_return_cmd(instance, cmd);
3364 goto fail_fw_init;
3365 }
3366
3367 megasas_return_cmd(instance, cmd);
3368
3369 return 0;
3370
3371fail_fw_init:
3372 return -EINVAL;
3373}
3374
adam radfordcd50ba82010-12-21 10:23:23 -08003375static u32
3376megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003377{
adam radfordcd50ba82010-12-21 10:23:23 -08003378 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003379 u32 context_sz;
3380 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003381
3382 reg_set = instance->reg_set;
3383
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003384 /*
3385 * Get various operational parameters from status register
3386 */
Sumant Patro1341c932006-01-25 12:02:40 -08003387 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003388 /*
3389 * Reduce the max supported cmds by 1. This is to ensure that the
3390 * reply_q_sz (1 more than the max cmd that driver may send)
3391 * does not exceed max cmds that the FW can support
3392 */
3393 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003394 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003395 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003396 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003397 /*
3398 * Create a pool of commands
3399 */
3400 if (megasas_alloc_cmds(instance))
3401 goto fail_alloc_cmds;
3402
3403 /*
3404 * Allocate memory for reply queue. Length of reply queue should
3405 * be _one_ more than the maximum commands handled by the firmware.
3406 *
3407 * Note: When FW completes commands, it places corresponding contex
3408 * values in this circular reply queue. This circular queue is a fairly
3409 * typical producer-consumer queue. FW is the producer (of completed
3410 * commands) and the driver is the consumer.
3411 */
3412 context_sz = sizeof(u32);
3413 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3414
3415 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3416 reply_q_sz,
3417 &instance->reply_queue_h);
3418
3419 if (!instance->reply_queue) {
3420 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3421 goto fail_reply_queue;
3422 }
3423
bo yang31ea7082007-11-07 12:09:50 -05003424 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003425 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003426
bo yang39a98552010-09-22 22:36:29 -04003427 instance->fw_support_ieee = 0;
3428 instance->fw_support_ieee =
3429 (instance->instancet->read_fw_status_reg(reg_set) &
3430 0x04000000);
3431
3432 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3433 instance->fw_support_ieee);
3434
3435 if (instance->fw_support_ieee)
3436 instance->flag_ieee = 1;
3437
adam radfordcd50ba82010-12-21 10:23:23 -08003438 return 0;
3439
3440fail_fw_init:
3441
3442 pci_free_consistent(instance->pdev, reply_q_sz,
3443 instance->reply_queue, instance->reply_queue_h);
3444fail_reply_queue:
3445 megasas_free_cmds(instance);
3446
3447fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003448 return 1;
3449}
3450
3451/**
3452 * megasas_init_fw - Initializes the FW
3453 * @instance: Adapter soft state
3454 *
3455 * This is the main function for initializing firmware
3456 */
3457
3458static int megasas_init_fw(struct megasas_instance *instance)
3459{
3460 u32 max_sectors_1;
3461 u32 max_sectors_2;
adam radford3f1abce2011-05-11 18:33:47 -07003462 u32 tmp_sectors, msix_enable;
adam radfordcd50ba82010-12-21 10:23:23 -08003463 struct megasas_register_set __iomem *reg_set;
3464 struct megasas_ctrl_info *ctrl_info;
3465 unsigned long bar_list;
adam radfordc8e858f2011-10-08 18:15:13 -07003466 int i;
adam radfordcd50ba82010-12-21 10:23:23 -08003467
3468 /* Find first memory bar */
3469 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3470 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3471 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3472 if (pci_request_selected_regions(instance->pdev, instance->bar,
3473 "megasas: LSI")) {
3474 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3475 return -EBUSY;
3476 }
3477
3478 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3479
3480 if (!instance->reg_set) {
3481 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3482 goto fail_ioremap;
3483 }
3484
3485 reg_set = instance->reg_set;
3486
3487 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003488 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07003489 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08003490 instance->instancet = &megasas_instance_template_fusion;
3491 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003492 case PCI_DEVICE_ID_LSI_SAS1078R:
3493 case PCI_DEVICE_ID_LSI_SAS1078DE:
3494 instance->instancet = &megasas_instance_template_ppc;
3495 break;
3496 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3497 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3498 instance->instancet = &megasas_instance_template_gen2;
3499 break;
3500 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3501 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3502 instance->instancet = &megasas_instance_template_skinny;
3503 break;
3504 case PCI_DEVICE_ID_LSI_SAS1064R:
3505 case PCI_DEVICE_ID_DELL_PERC5:
3506 default:
3507 instance->instancet = &megasas_instance_template_xscale;
3508 break;
3509 }
3510
3511 /*
3512 * We expect the FW state to be READY
3513 */
adam radford058a8fa2011-10-08 18:14:27 -07003514 if (megasas_transition_to_ready(instance, 0))
adam radfordcd50ba82010-12-21 10:23:23 -08003515 goto fail_ready_state;
3516
adam radford3f1abce2011-05-11 18:33:47 -07003517 /* Check if MSI-X is supported while in ready state */
3518 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3519 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07003520 if (msix_enable && !msix_disable) {
3521 /* Check max MSI-X vectors */
3522 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3523 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3524 instance->msix_vectors = (readl(&instance->reg_set->
3525 outbound_scratch_pad_2
3526 ) & 0x1F) + 1;
adam radford079eadd2012-10-01 19:26:59 -07003527 if (msix_vectors)
3528 instance->msix_vectors =
3529 min(msix_vectors,
3530 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07003531 } else
3532 instance->msix_vectors = 1;
3533 /* Don't bother allocating more MSI-X vectors than cpus */
3534 instance->msix_vectors = min(instance->msix_vectors,
3535 (unsigned int)num_online_cpus());
3536 for (i = 0; i < instance->msix_vectors; i++)
3537 instance->msixentry[i].entry = i;
3538 i = pci_enable_msix(instance->pdev, instance->msixentry,
3539 instance->msix_vectors);
3540 if (i >= 0) {
3541 if (i) {
3542 if (!pci_enable_msix(instance->pdev,
3543 instance->msixentry, i))
3544 instance->msix_vectors = i;
3545 else
3546 instance->msix_vectors = 0;
3547 }
3548 } else
3549 instance->msix_vectors = 0;
3550 }
adam radford3f1abce2011-05-11 18:33:47 -07003551
adam radfordcd50ba82010-12-21 10:23:23 -08003552 /* Get operational params, sge flags, send init cmd to controller */
3553 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003554 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003555
3556 printk(KERN_ERR "megasas: INIT adapter done\n");
3557
bo yang39a98552010-09-22 22:36:29 -04003558 /** for passthrough
3559 * the following function will get the PD LIST.
3560 */
3561
Yang, Bo81e403c2009-10-06 14:27:54 -06003562 memset(instance->pd_list, 0 ,
3563 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3564 megasas_get_pd_list(instance);
3565
Yang, Bobdc6fb82009-12-06 08:30:19 -07003566 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3567 megasas_get_ld_list(instance);
3568
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003569 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3570
3571 /*
3572 * Compute the max allowed sectors per IO: The controller info has two
3573 * limits on max sectors. Driver should use the minimum of these two.
3574 *
3575 * 1 << stripe_sz_ops.min = max sectors per strip
3576 *
3577 * Note that older firmwares ( < FW ver 30) didn't report information
3578 * to calculate max_sectors_1. So the number ended up as zero always.
3579 */
bo yang14faea92007-11-09 04:14:00 -05003580 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003581 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3582
3583 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3584 ctrl_info->max_strips_per_io;
3585 max_sectors_2 = ctrl_info->max_request_size;
3586
bo yang14faea92007-11-09 04:14:00 -05003587 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003588 instance->disableOnlineCtrlReset =
3589 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003590 }
3591
3592 instance->max_sectors_per_req = instance->max_num_sge *
3593 PAGE_SIZE / 512;
3594 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3595 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003596
3597 kfree(ctrl_info);
3598
adam radfordc5daa6a2012-07-17 18:20:03 -07003599 /* Check for valid throttlequeuedepth module parameter */
3600 if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3601 instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3602 if (throttlequeuedepth > (instance->max_fw_cmds -
3603 MEGASAS_SKINNY_INT_CMDS))
3604 instance->throttlequeuedepth =
3605 MEGASAS_THROTTLE_QUEUE_DEPTH;
3606 else
3607 instance->throttlequeuedepth = throttlequeuedepth;
3608 } else {
3609 if (throttlequeuedepth > (instance->max_fw_cmds -
3610 MEGASAS_INT_CMDS))
3611 instance->throttlequeuedepth =
3612 MEGASAS_THROTTLE_QUEUE_DEPTH;
3613 else
3614 instance->throttlequeuedepth = throttlequeuedepth;
3615 }
3616
Sumant Patro5d018ad2006-10-03 13:13:18 -07003617 /*
3618 * Setup tasklet for cmd completion
3619 */
3620
adam radfordf86c5422011-02-24 20:57:00 -08003621 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003622 (unsigned long)instance);
3623
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003624 return 0;
3625
adam radfordeb1b1232011-02-24 20:55:56 -08003626fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003627fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003628 iounmap(instance->reg_set);
3629
3630 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003631 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003632
3633 return -EINVAL;
3634}
3635
3636/**
3637 * megasas_release_mfi - Reverses the FW initialization
3638 * @intance: Adapter soft state
3639 */
3640static void megasas_release_mfi(struct megasas_instance *instance)
3641{
adam radford9c915a82010-12-21 13:34:31 -08003642 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003643
adam radford9c915a82010-12-21 13:34:31 -08003644 if (instance->reply_queue)
3645 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003646 instance->reply_queue, instance->reply_queue_h);
3647
3648 megasas_free_cmds(instance);
3649
3650 iounmap(instance->reg_set);
3651
adam radfordb6d5d882010-12-14 18:56:07 -08003652 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003653}
3654
3655/**
3656 * megasas_get_seq_num - Gets latest event sequence numbers
3657 * @instance: Adapter soft state
3658 * @eli: FW event log sequence numbers information
3659 *
3660 * FW maintains a log of all events in a non-volatile area. Upper layers would
3661 * usually find out the latest sequence number of the events, the seq number at
3662 * the boot etc. They would "read" all the events below the latest seq number
3663 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3664 * number), they would subsribe to AEN (asynchronous event notification) and
3665 * wait for the events to happen.
3666 */
3667static int
3668megasas_get_seq_num(struct megasas_instance *instance,
3669 struct megasas_evt_log_info *eli)
3670{
3671 struct megasas_cmd *cmd;
3672 struct megasas_dcmd_frame *dcmd;
3673 struct megasas_evt_log_info *el_info;
3674 dma_addr_t el_info_h = 0;
3675
3676 cmd = megasas_get_cmd(instance);
3677
3678 if (!cmd) {
3679 return -ENOMEM;
3680 }
3681
3682 dcmd = &cmd->frame->dcmd;
3683 el_info = pci_alloc_consistent(instance->pdev,
3684 sizeof(struct megasas_evt_log_info),
3685 &el_info_h);
3686
3687 if (!el_info) {
3688 megasas_return_cmd(instance, cmd);
3689 return -ENOMEM;
3690 }
3691
3692 memset(el_info, 0, sizeof(*el_info));
3693 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3694
3695 dcmd->cmd = MFI_CMD_DCMD;
3696 dcmd->cmd_status = 0x0;
3697 dcmd->sge_count = 1;
3698 dcmd->flags = MFI_FRAME_DIR_READ;
3699 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003700 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003701 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3702 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3703 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3704 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3705
3706 megasas_issue_blocked_cmd(instance, cmd);
3707
3708 /*
3709 * Copy the data back into callers buffer
3710 */
3711 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3712
3713 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3714 el_info, el_info_h);
3715
3716 megasas_return_cmd(instance, cmd);
3717
3718 return 0;
3719}
3720
3721/**
3722 * megasas_register_aen - Registers for asynchronous event notification
3723 * @instance: Adapter soft state
3724 * @seq_num: The starting sequence number
3725 * @class_locale: Class of the event
3726 *
3727 * This function subscribes for AEN for events beyond the @seq_num. It requests
3728 * to be notified if and only if the event is of type @class_locale
3729 */
3730static int
3731megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3732 u32 class_locale_word)
3733{
3734 int ret_val;
3735 struct megasas_cmd *cmd;
3736 struct megasas_dcmd_frame *dcmd;
3737 union megasas_evt_class_locale curr_aen;
3738 union megasas_evt_class_locale prev_aen;
3739
3740 /*
3741 * If there an AEN pending already (aen_cmd), check if the
3742 * class_locale of that pending AEN is inclusive of the new
3743 * AEN request we currently have. If it is, then we don't have
3744 * to do anything. In other words, whichever events the current
3745 * AEN request is subscribing to, have already been subscribed
3746 * to.
3747 *
3748 * If the old_cmd is _not_ inclusive, then we have to abort
3749 * that command, form a class_locale that is superset of both
3750 * old and current and re-issue to the FW
3751 */
3752
3753 curr_aen.word = class_locale_word;
3754
3755 if (instance->aen_cmd) {
3756
3757 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3758
3759 /*
3760 * A class whose enum value is smaller is inclusive of all
3761 * higher values. If a PROGRESS (= -1) was previously
3762 * registered, then a new registration requests for higher
3763 * classes need not be sent to FW. They are automatically
3764 * included.
3765 *
3766 * Locale numbers don't have such hierarchy. They are bitmap
3767 * values
3768 */
3769 if ((prev_aen.members.class <= curr_aen.members.class) &&
3770 !((prev_aen.members.locale & curr_aen.members.locale) ^
3771 curr_aen.members.locale)) {
3772 /*
3773 * Previously issued event registration includes
3774 * current request. Nothing to do.
3775 */
3776 return 0;
3777 } else {
3778 curr_aen.members.locale |= prev_aen.members.locale;
3779
3780 if (prev_aen.members.class < curr_aen.members.class)
3781 curr_aen.members.class = prev_aen.members.class;
3782
3783 instance->aen_cmd->abort_aen = 1;
3784 ret_val = megasas_issue_blocked_abort_cmd(instance,
3785 instance->
3786 aen_cmd);
3787
3788 if (ret_val) {
3789 printk(KERN_DEBUG "megasas: Failed to abort "
3790 "previous AEN command\n");
3791 return ret_val;
3792 }
3793 }
3794 }
3795
3796 cmd = megasas_get_cmd(instance);
3797
3798 if (!cmd)
3799 return -ENOMEM;
3800
3801 dcmd = &cmd->frame->dcmd;
3802
3803 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3804
3805 /*
3806 * Prepare DCMD for aen registration
3807 */
3808 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3809
3810 dcmd->cmd = MFI_CMD_DCMD;
3811 dcmd->cmd_status = 0x0;
3812 dcmd->sge_count = 1;
3813 dcmd->flags = MFI_FRAME_DIR_READ;
3814 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003815 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003816 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003817 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3818 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3819 dcmd->mbox.w[0] = seq_num;
3820 dcmd->mbox.w[1] = curr_aen.word;
3821 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3822 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3823
Yang, Bof4c9a132009-10-06 14:43:28 -06003824 if (instance->aen_cmd != NULL) {
3825 megasas_return_cmd(instance, cmd);
3826 return 0;
3827 }
3828
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003829 /*
3830 * Store reference to the cmd used to register for AEN. When an
3831 * application wants us to register for AEN, we have to abort this
3832 * cmd and re-register with a new EVENT LOCALE supplied by that app
3833 */
3834 instance->aen_cmd = cmd;
3835
3836 /*
3837 * Issue the aen registration frame
3838 */
adam radford9c915a82010-12-21 13:34:31 -08003839 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003840
3841 return 0;
3842}
3843
3844/**
3845 * megasas_start_aen - Subscribes to AEN during driver load time
3846 * @instance: Adapter soft state
3847 */
3848static int megasas_start_aen(struct megasas_instance *instance)
3849{
3850 struct megasas_evt_log_info eli;
3851 union megasas_evt_class_locale class_locale;
3852
3853 /*
3854 * Get the latest sequence number from FW
3855 */
3856 memset(&eli, 0, sizeof(eli));
3857
3858 if (megasas_get_seq_num(instance, &eli))
3859 return -1;
3860
3861 /*
3862 * Register AEN with FW for latest sequence number plus 1
3863 */
3864 class_locale.members.reserved = 0;
3865 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3866 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3867
3868 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3869 class_locale.word);
3870}
3871
3872/**
3873 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3874 * @instance: Adapter soft state
3875 */
3876static int megasas_io_attach(struct megasas_instance *instance)
3877{
3878 struct Scsi_Host *host = instance->host;
3879
3880 /*
3881 * Export parameters required by SCSI mid-layer
3882 */
3883 host->irq = instance->pdev->irq;
3884 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003885 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3886 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3887 host->can_queue =
3888 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3889 } else
3890 host->can_queue =
3891 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003892 host->this_id = instance->init_id;
3893 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003894
3895 if (instance->fw_support_ieee)
3896 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3897
Yang, Bo1fd10682010-10-12 07:18:50 -06003898 /*
3899 * Check if the module parameter value for max_sectors can be used
3900 */
3901 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3902 instance->max_sectors_per_req = max_sectors;
3903 else {
3904 if (max_sectors) {
3905 if (((instance->pdev->device ==
3906 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3907 (instance->pdev->device ==
3908 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3909 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3910 instance->max_sectors_per_req = max_sectors;
3911 } else {
3912 printk(KERN_INFO "megasas: max_sectors should be > 0"
3913 "and <= %d (or < 1MB for GEN2 controller)\n",
3914 instance->max_sectors_per_req);
3915 }
3916 }
3917 }
3918
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003919 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003920 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003921 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3922 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3923 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003924 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003925
adam radford9c915a82010-12-21 13:34:31 -08003926 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07003927 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3928 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08003929 host->hostt->eh_device_reset_handler = NULL;
3930 host->hostt->eh_bus_reset_handler = NULL;
3931 }
3932
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003933 /*
3934 * Notify the mid-layer about the new controller
3935 */
3936 if (scsi_add_host(host, &instance->pdev->dev)) {
3937 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3938 return -ENODEV;
3939 }
3940
3941 /*
3942 * Trigger SCSI to scan our drives
3943 */
3944 scsi_scan_host(host);
3945 return 0;
3946}
3947
bo yang31ea7082007-11-07 12:09:50 -05003948static int
3949megasas_set_dma_mask(struct pci_dev *pdev)
3950{
3951 /*
3952 * All our contollers are capable of performing 64-bit DMA
3953 */
3954 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003955 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003956
Yang Hongyang284901a2009-04-06 19:01:15 -07003957 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003958 goto fail_set_dma_mask;
3959 }
3960 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003961 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003962 goto fail_set_dma_mask;
3963 }
3964 return 0;
3965
3966fail_set_dma_mask:
3967 return 1;
3968}
3969
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003970/**
3971 * megasas_probe_one - PCI hotplug entry point
3972 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003973 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003974 */
3975static int __devinit
3976megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3977{
adam radfordc8e858f2011-10-08 18:15:13 -07003978 int rval, pos, i, j;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003979 struct Scsi_Host *host;
3980 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08003981 u16 control = 0;
3982
3983 /* Reset MSI-X in the kdump kernel */
3984 if (reset_devices) {
3985 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3986 if (pos) {
3987 pci_read_config_word(pdev, msi_control_reg(pos),
3988 &control);
3989 if (control & PCI_MSIX_FLAGS_ENABLE) {
3990 dev_info(&pdev->dev, "resetting MSI-X\n");
3991 pci_write_config_word(pdev,
3992 msi_control_reg(pos),
3993 control &
3994 ~PCI_MSIX_FLAGS_ENABLE);
3995 }
3996 }
3997 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003998
3999 /*
4000 * Announce PCI information
4001 */
4002 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4003 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4004 pdev->subsystem_device);
4005
4006 printk("bus %d:slot %d:func %d\n",
4007 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4008
4009 /*
4010 * PCI prepping: enable device set bus mastering and dma mask
4011 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004012 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004013
4014 if (rval) {
4015 return rval;
4016 }
4017
4018 pci_set_master(pdev);
4019
bo yang31ea7082007-11-07 12:09:50 -05004020 if (megasas_set_dma_mask(pdev))
4021 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004022
4023 host = scsi_host_alloc(&megasas_template,
4024 sizeof(struct megasas_instance));
4025
4026 if (!host) {
4027 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4028 goto fail_alloc_instance;
4029 }
4030
4031 instance = (struct megasas_instance *)host->hostdata;
4032 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04004033 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08004034 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004035
adam radford9c915a82010-12-21 13:34:31 -08004036 switch (instance->pdev->device) {
4037 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004038 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004039 {
4040 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004041
adam radford9c915a82010-12-21 13:34:31 -08004042 instance->ctrl_context =
4043 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4044 if (!instance->ctrl_context) {
4045 printk(KERN_DEBUG "megasas: Failed to allocate "
4046 "memory for Fusion context info\n");
4047 goto fail_alloc_dma_buf;
4048 }
4049 fusion = instance->ctrl_context;
4050 INIT_LIST_HEAD(&fusion->cmd_pool);
4051 spin_lock_init(&fusion->cmd_pool_lock);
4052 }
4053 break;
4054 default: /* For all other supported controllers */
4055
4056 instance->producer =
4057 pci_alloc_consistent(pdev, sizeof(u32),
4058 &instance->producer_h);
4059 instance->consumer =
4060 pci_alloc_consistent(pdev, sizeof(u32),
4061 &instance->consumer_h);
4062
4063 if (!instance->producer || !instance->consumer) {
4064 printk(KERN_DEBUG "megasas: Failed to allocate"
4065 "memory for producer, consumer\n");
4066 goto fail_alloc_dma_buf;
4067 }
4068
4069 *instance->producer = 0;
4070 *instance->consumer = 0;
4071 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004072 }
4073
Yang, Boc3518832009-10-06 14:18:02 -06004074 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004075 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004076 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004077 instance->issuepend_done = 1;
4078 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4079 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004080
4081 instance->evt_detail = pci_alloc_consistent(pdev,
4082 sizeof(struct
4083 megasas_evt_detail),
4084 &instance->evt_detail_h);
4085
4086 if (!instance->evt_detail) {
4087 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4088 "event detail structure\n");
4089 goto fail_alloc_dma_buf;
4090 }
4091
4092 /*
4093 * Initialize locks and queues
4094 */
4095 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004096 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004097
Sumant Patroe4a082c2006-05-30 12:03:37 -07004098 atomic_set(&instance->fw_outstanding,0);
4099
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004100 init_waitqueue_head(&instance->int_cmd_wait_q);
4101 init_waitqueue_head(&instance->abort_cmd_wait_q);
4102
4103 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004104 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004105 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004106
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004107 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004108 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004109
4110 /*
4111 * Initialize PCI related and misc parameters
4112 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004113 instance->host = host;
4114 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4115 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4116
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004117 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4118 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004119 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004120 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4121 } else
4122 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4123
Sumant Patro658dced2006-10-03 13:09:14 -07004124 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004125 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004126 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004127 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004128 instance->disableOnlineCtrlReset = 1;
4129
adam radford36807e62011-10-08 18:15:06 -07004130 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4131 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08004132 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4133 else
4134 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004135
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004136 /*
adam radford0a770662011-02-24 20:56:12 -08004137 * Initialize MFI Firmware
4138 */
4139 if (megasas_init_fw(instance))
4140 goto fail_init_mfi;
4141
4142 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004143 * Register IRQ
4144 */
adam radfordc8e858f2011-10-08 18:15:13 -07004145 if (instance->msix_vectors) {
4146 for (i = 0 ; i < instance->msix_vectors; i++) {
4147 instance->irq_context[i].instance = instance;
4148 instance->irq_context[i].MSIxIndex = i;
4149 if (request_irq(instance->msixentry[i].vector,
4150 instance->instancet->service_isr, 0,
4151 "megasas",
4152 &instance->irq_context[i])) {
4153 printk(KERN_DEBUG "megasas: Failed to "
4154 "register IRQ for vector %d.\n", i);
4155 for (j = 0 ; j < i ; j++)
4156 free_irq(
4157 instance->msixentry[j].vector,
4158 &instance->irq_context[j]);
4159 goto fail_irq;
4160 }
4161 }
4162 } else {
4163 instance->irq_context[0].instance = instance;
4164 instance->irq_context[0].MSIxIndex = 0;
4165 if (request_irq(pdev->irq, instance->instancet->service_isr,
4166 IRQF_SHARED, "megasas",
4167 &instance->irq_context[0])) {
4168 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4169 goto fail_irq;
4170 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004171 }
4172
Sumant Patro1341c932006-01-25 12:02:40 -08004173 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004174
4175 /*
4176 * Store instance in PCI softstate
4177 */
4178 pci_set_drvdata(pdev, instance);
4179
4180 /*
4181 * Add this controller to megasas_mgmt_info structure so that it
4182 * can be exported to management applications
4183 */
4184 megasas_mgmt_info.count++;
4185 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4186 megasas_mgmt_info.max_index++;
4187
4188 /*
adam radford541f90b2011-05-11 18:34:29 -07004189 * Register with SCSI mid-layer
4190 */
4191 if (megasas_io_attach(instance))
4192 goto fail_io_attach;
4193
4194 instance->unload = 0;
4195
4196 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004197 * Initiate AEN (Asynchronous Event Notification)
4198 */
4199 if (megasas_start_aen(instance)) {
4200 printk(KERN_DEBUG "megasas: start aen failed\n");
4201 goto fail_start_aen;
4202 }
4203
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004204 return 0;
4205
4206 fail_start_aen:
4207 fail_io_attach:
4208 megasas_mgmt_info.count--;
4209 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4210 megasas_mgmt_info.max_index--;
4211
4212 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004213 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004214 if (instance->msix_vectors)
4215 for (i = 0 ; i < instance->msix_vectors; i++)
4216 free_irq(instance->msixentry[i].vector,
4217 &instance->irq_context[i]);
4218 else
4219 free_irq(instance->pdev->irq, &instance->irq_context[0]);
adam radfordeb1b1232011-02-24 20:55:56 -08004220fail_irq:
adam radford36807e62011-10-08 18:15:06 -07004221 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4222 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radfordeb1b1232011-02-24 20:55:56 -08004223 megasas_release_fusion(instance);
4224 else
4225 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004226 fail_init_mfi:
adam radfordc8e858f2011-10-08 18:15:13 -07004227 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08004228 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004229 fail_alloc_dma_buf:
4230 if (instance->evt_detail)
4231 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4232 instance->evt_detail,
4233 instance->evt_detail_h);
4234
adam radfordeb1b1232011-02-24 20:55:56 -08004235 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004236 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4237 instance->producer_h);
4238 if (instance->consumer)
4239 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4240 instance->consumer_h);
4241 scsi_host_put(host);
4242
4243 fail_alloc_instance:
4244 fail_set_dma_mask:
4245 pci_disable_device(pdev);
4246
4247 return -ENODEV;
4248}
4249
4250/**
4251 * megasas_flush_cache - Requests FW to flush all its caches
4252 * @instance: Adapter soft state
4253 */
4254static void megasas_flush_cache(struct megasas_instance *instance)
4255{
4256 struct megasas_cmd *cmd;
4257 struct megasas_dcmd_frame *dcmd;
4258
bo yang39a98552010-09-22 22:36:29 -04004259 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4260 return;
4261
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004262 cmd = megasas_get_cmd(instance);
4263
4264 if (!cmd)
4265 return;
4266
4267 dcmd = &cmd->frame->dcmd;
4268
4269 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4270
4271 dcmd->cmd = MFI_CMD_DCMD;
4272 dcmd->cmd_status = 0x0;
4273 dcmd->sge_count = 0;
4274 dcmd->flags = MFI_FRAME_DIR_NONE;
4275 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004276 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004277 dcmd->data_xfer_len = 0;
4278 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4279 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4280
4281 megasas_issue_blocked_cmd(instance, cmd);
4282
4283 megasas_return_cmd(instance, cmd);
4284
4285 return;
4286}
4287
4288/**
4289 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4290 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004291 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004292 */
bo yang31ea7082007-11-07 12:09:50 -05004293static void megasas_shutdown_controller(struct megasas_instance *instance,
4294 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004295{
4296 struct megasas_cmd *cmd;
4297 struct megasas_dcmd_frame *dcmd;
4298
bo yang39a98552010-09-22 22:36:29 -04004299 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4300 return;
4301
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004302 cmd = megasas_get_cmd(instance);
4303
4304 if (!cmd)
4305 return;
4306
4307 if (instance->aen_cmd)
4308 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004309 if (instance->map_update_cmd)
4310 megasas_issue_blocked_abort_cmd(instance,
4311 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004312 dcmd = &cmd->frame->dcmd;
4313
4314 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4315
4316 dcmd->cmd = MFI_CMD_DCMD;
4317 dcmd->cmd_status = 0x0;
4318 dcmd->sge_count = 0;
4319 dcmd->flags = MFI_FRAME_DIR_NONE;
4320 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004321 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004322 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004323 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004324
4325 megasas_issue_blocked_cmd(instance, cmd);
4326
4327 megasas_return_cmd(instance, cmd);
4328
4329 return;
4330}
4331
Jiri Slaby33139b22008-05-01 17:56:02 +02004332#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004333/**
bo yangad84db22007-11-09 04:40:16 -05004334 * megasas_suspend - driver suspend entry point
4335 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004336 * @state: PCI power state to suspend routine
4337 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004338static int
bo yang31ea7082007-11-07 12:09:50 -05004339megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4340{
4341 struct Scsi_Host *host;
4342 struct megasas_instance *instance;
adam radfordc8e858f2011-10-08 18:15:13 -07004343 int i;
bo yang31ea7082007-11-07 12:09:50 -05004344
4345 instance = pci_get_drvdata(pdev);
4346 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004347 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004348
4349 megasas_flush_cache(instance);
4350 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004351
4352 /* cancel the delayed work if this work still in queue */
4353 if (instance->ev != NULL) {
4354 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004355 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004356 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004357 instance->ev = NULL;
4358 }
4359
bo yang31ea7082007-11-07 12:09:50 -05004360 tasklet_kill(&instance->isr_tasklet);
4361
4362 pci_set_drvdata(instance->pdev, instance);
4363 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004364
4365 if (instance->msix_vectors)
4366 for (i = 0 ; i < instance->msix_vectors; i++)
4367 free_irq(instance->msixentry[i].vector,
4368 &instance->irq_context[i]);
4369 else
4370 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4371 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004372 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004373
4374 pci_save_state(pdev);
4375 pci_disable_device(pdev);
4376
4377 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4378
4379 return 0;
4380}
4381
4382/**
4383 * megasas_resume- driver resume entry point
4384 * @pdev: PCI device structure
4385 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004386static int
bo yang31ea7082007-11-07 12:09:50 -05004387megasas_resume(struct pci_dev *pdev)
4388{
adam radfordc8e858f2011-10-08 18:15:13 -07004389 int rval, i, j;
bo yang31ea7082007-11-07 12:09:50 -05004390 struct Scsi_Host *host;
4391 struct megasas_instance *instance;
4392
4393 instance = pci_get_drvdata(pdev);
4394 host = instance->host;
4395 pci_set_power_state(pdev, PCI_D0);
4396 pci_enable_wake(pdev, PCI_D0, 0);
4397 pci_restore_state(pdev);
4398
4399 /*
4400 * PCI prepping: enable device set bus mastering and dma mask
4401 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004402 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004403
4404 if (rval) {
4405 printk(KERN_ERR "megasas: Enable device failed\n");
4406 return rval;
4407 }
4408
4409 pci_set_master(pdev);
4410
4411 if (megasas_set_dma_mask(pdev))
4412 goto fail_set_dma_mask;
4413
4414 /*
4415 * Initialize MFI Firmware
4416 */
4417
bo yang31ea7082007-11-07 12:09:50 -05004418 atomic_set(&instance->fw_outstanding, 0);
4419
4420 /*
4421 * We expect the FW state to be READY
4422 */
adam radford058a8fa2011-10-08 18:14:27 -07004423 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05004424 goto fail_ready_state;
4425
adam radford3f1abce2011-05-11 18:33:47 -07004426 /* Now re-enable MSI-X */
adam radfordc8e858f2011-10-08 18:15:13 -07004427 if (instance->msix_vectors)
4428 pci_enable_msix(instance->pdev, instance->msixentry,
4429 instance->msix_vectors);
adam radford3f1abce2011-05-11 18:33:47 -07004430
adam radford9c915a82010-12-21 13:34:31 -08004431 switch (instance->pdev->device) {
4432 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004433 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004434 {
4435 megasas_reset_reply_desc(instance);
4436 if (megasas_ioc_init_fusion(instance)) {
4437 megasas_free_cmds(instance);
4438 megasas_free_cmds_fusion(instance);
4439 goto fail_init_mfi;
4440 }
4441 if (!megasas_get_map_info(instance))
4442 megasas_sync_map_info(instance);
4443 }
4444 break;
4445 default:
4446 *instance->producer = 0;
4447 *instance->consumer = 0;
4448 if (megasas_issue_init_mfi(instance))
4449 goto fail_init_mfi;
4450 break;
4451 }
bo yang31ea7082007-11-07 12:09:50 -05004452
adam radford9c915a82010-12-21 13:34:31 -08004453 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4454 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004455
4456 /*
4457 * Register IRQ
4458 */
adam radfordc8e858f2011-10-08 18:15:13 -07004459 if (instance->msix_vectors) {
4460 for (i = 0 ; i < instance->msix_vectors; i++) {
4461 instance->irq_context[i].instance = instance;
4462 instance->irq_context[i].MSIxIndex = i;
4463 if (request_irq(instance->msixentry[i].vector,
4464 instance->instancet->service_isr, 0,
4465 "megasas",
4466 &instance->irq_context[i])) {
4467 printk(KERN_DEBUG "megasas: Failed to "
4468 "register IRQ for vector %d.\n", i);
4469 for (j = 0 ; j < i ; j++)
4470 free_irq(
4471 instance->msixentry[j].vector,
4472 &instance->irq_context[j]);
4473 goto fail_irq;
4474 }
4475 }
4476 } else {
4477 instance->irq_context[0].instance = instance;
4478 instance->irq_context[0].MSIxIndex = 0;
4479 if (request_irq(pdev->irq, instance->instancet->service_isr,
4480 IRQF_SHARED, "megasas",
4481 &instance->irq_context[0])) {
4482 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4483 goto fail_irq;
4484 }
bo yang31ea7082007-11-07 12:09:50 -05004485 }
4486
4487 instance->instancet->enable_intr(instance->reg_set);
Yang, Bo0c79e682009-10-06 14:47:35 -06004488 instance->unload = 0;
4489
adam radford541f90b2011-05-11 18:34:29 -07004490 /*
4491 * Initiate AEN (Asynchronous Event Notification)
4492 */
4493 if (megasas_start_aen(instance))
4494 printk(KERN_ERR "megasas: Start AEN failed\n");
4495
bo yang31ea7082007-11-07 12:09:50 -05004496 return 0;
4497
4498fail_irq:
4499fail_init_mfi:
4500 if (instance->evt_detail)
4501 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4502 instance->evt_detail,
4503 instance->evt_detail_h);
4504
4505 if (instance->producer)
4506 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4507 instance->producer_h);
4508 if (instance->consumer)
4509 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4510 instance->consumer_h);
4511 scsi_host_put(host);
4512
4513fail_set_dma_mask:
4514fail_ready_state:
4515
4516 pci_disable_device(pdev);
4517
4518 return -ENODEV;
4519}
Jiri Slaby33139b22008-05-01 17:56:02 +02004520#else
4521#define megasas_suspend NULL
4522#define megasas_resume NULL
4523#endif
bo yang31ea7082007-11-07 12:09:50 -05004524
4525/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004526 * megasas_detach_one - PCI hot"un"plug entry point
4527 * @pdev: PCI device structure
4528 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004529static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004530{
4531 int i;
4532 struct Scsi_Host *host;
4533 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004534 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004535
4536 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004537 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004538 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004539 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004540
4541 scsi_remove_host(instance->host);
4542 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004543 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004544
4545 /* cancel the delayed work if this work still in queue*/
4546 if (instance->ev != NULL) {
4547 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004548 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004549 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004550 instance->ev = NULL;
4551 }
4552
Sumant Patro5d018ad2006-10-03 13:13:18 -07004553 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004554
4555 /*
4556 * Take the instance off the instance array. Note that we will not
4557 * decrement the max_index. We let this array be sparse array
4558 */
4559 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4560 if (megasas_mgmt_info.instance[i] == instance) {
4561 megasas_mgmt_info.count--;
4562 megasas_mgmt_info.instance[i] = NULL;
4563
4564 break;
4565 }
4566 }
4567
4568 pci_set_drvdata(instance->pdev, NULL);
4569
Sumant Patrob274cab2006-10-03 12:52:12 -07004570 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004571
adam radfordc8e858f2011-10-08 18:15:13 -07004572 if (instance->msix_vectors)
4573 for (i = 0 ; i < instance->msix_vectors; i++)
4574 free_irq(instance->msixentry[i].vector,
4575 &instance->irq_context[i]);
4576 else
4577 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4578 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004579 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004580
adam radford9c915a82010-12-21 13:34:31 -08004581 switch (instance->pdev->device) {
4582 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004583 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004584 megasas_release_fusion(instance);
4585 for (i = 0; i < 2 ; i++)
4586 if (fusion->ld_map[i])
4587 dma_free_coherent(&instance->pdev->dev,
4588 fusion->map_sz,
4589 fusion->ld_map[i],
4590 fusion->
4591 ld_map_phys[i]);
4592 kfree(instance->ctrl_context);
4593 break;
4594 default:
4595 megasas_release_mfi(instance);
4596 pci_free_consistent(pdev,
4597 sizeof(struct megasas_evt_detail),
4598 instance->evt_detail,
4599 instance->evt_detail_h);
4600 pci_free_consistent(pdev, sizeof(u32),
4601 instance->producer,
4602 instance->producer_h);
4603 pci_free_consistent(pdev, sizeof(u32),
4604 instance->consumer,
4605 instance->consumer_h);
4606 break;
4607 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004608
4609 scsi_host_put(host);
4610
4611 pci_set_drvdata(pdev, NULL);
4612
4613 pci_disable_device(pdev);
4614
4615 return;
4616}
4617
4618/**
4619 * megasas_shutdown - Shutdown entry point
4620 * @device: Generic device structure
4621 */
4622static void megasas_shutdown(struct pci_dev *pdev)
4623{
adam radfordc8e858f2011-10-08 18:15:13 -07004624 int i;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004625 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07004626
Yang, Bo0c79e682009-10-06 14:47:35 -06004627 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004628 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004629 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
adam radford46fd2562011-05-11 18:34:17 -07004630 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004631 if (instance->msix_vectors)
4632 for (i = 0 ; i < instance->msix_vectors; i++)
4633 free_irq(instance->msixentry[i].vector,
4634 &instance->irq_context[i]);
4635 else
4636 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4637 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07004638 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004639}
4640
4641/**
4642 * megasas_mgmt_open - char node "open" entry point
4643 */
4644static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4645{
4646 /*
4647 * Allow only those users with admin rights
4648 */
4649 if (!capable(CAP_SYS_ADMIN))
4650 return -EACCES;
4651
4652 return 0;
4653}
4654
4655/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004656 * megasas_mgmt_fasync - Async notifier registration from applications
4657 *
4658 * This function adds the calling process to a driver global queue. When an
4659 * event occurs, SIGIO will be sent to all processes in this queue.
4660 */
4661static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4662{
4663 int rc;
4664
Arjan van de Ven0b950672006-01-11 13:16:10 +01004665 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004666
4667 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4668
Arjan van de Ven0b950672006-01-11 13:16:10 +01004669 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004670
4671 if (rc >= 0) {
4672 /* For sanity check when we get ioctl */
4673 filep->private_data = filep;
4674 return 0;
4675 }
4676
4677 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4678
4679 return rc;
4680}
4681
4682/**
Yang, Boc3518832009-10-06 14:18:02 -06004683 * megasas_mgmt_poll - char node "poll" entry point
4684 * */
4685static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4686{
4687 unsigned int mask;
4688 unsigned long flags;
4689 poll_wait(file, &megasas_poll_wait, wait);
4690 spin_lock_irqsave(&poll_aen_lock, flags);
4691 if (megasas_poll_wait_aen)
4692 mask = (POLLIN | POLLRDNORM);
4693 else
4694 mask = 0;
4695 spin_unlock_irqrestore(&poll_aen_lock, flags);
4696 return mask;
4697}
4698
4699/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004700 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4701 * @instance: Adapter soft state
4702 * @argp: User's ioctl packet
4703 */
4704static int
4705megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4706 struct megasas_iocpacket __user * user_ioc,
4707 struct megasas_iocpacket *ioc)
4708{
4709 struct megasas_sge32 *kern_sge32;
4710 struct megasas_cmd *cmd;
4711 void *kbuff_arr[MAX_IOCTL_SGE];
4712 dma_addr_t buf_handle = 0;
4713 int error = 0, i;
4714 void *sense = NULL;
4715 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004716 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004717
4718 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4719
4720 if (ioc->sge_count > MAX_IOCTL_SGE) {
4721 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4722 ioc->sge_count, MAX_IOCTL_SGE);
4723 return -EINVAL;
4724 }
4725
4726 cmd = megasas_get_cmd(instance);
4727 if (!cmd) {
4728 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4729 return -ENOMEM;
4730 }
4731
4732 /*
4733 * User's IOCTL packet has 2 frames (maximum). Copy those two
4734 * frames into our cmd's frames. cmd->frame's context will get
4735 * overwritten when we copy from user's frames. So set that value
4736 * alone separately
4737 */
4738 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4739 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004740 cmd->frame->hdr.pad_0 = 0;
adam radford882be7c32012-01-06 17:02:34 -08004741 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4742 MFI_FRAME_SENSE64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004743
4744 /*
4745 * The management interface between applications and the fw uses
4746 * MFI frames. E.g, RAID configuration changes, LD property changes
4747 * etc are accomplishes through different kinds of MFI frames. The
4748 * driver needs to care only about substituting user buffers with
4749 * kernel buffers in SGLs. The location of SGL is embedded in the
4750 * struct iocpacket itself.
4751 */
4752 kern_sge32 = (struct megasas_sge32 *)
4753 ((unsigned long)cmd->frame + ioc->sgl_off);
4754
4755 /*
4756 * For each user buffer, create a mirror buffer and copy in
4757 */
4758 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004759 if (!ioc->sgl[i].iov_len)
4760 continue;
4761
Sumant Patro9f35fa82007-02-14 12:55:45 -08004762 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004763 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004764 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004765 if (!kbuff_arr[i]) {
4766 printk(KERN_DEBUG "megasas: Failed to alloc "
4767 "kernel SGL buffer for IOCTL \n");
4768 error = -ENOMEM;
4769 goto out;
4770 }
4771
4772 /*
4773 * We don't change the dma_coherent_mask, so
4774 * pci_alloc_consistent only returns 32bit addresses
4775 */
4776 kern_sge32[i].phys_addr = (u32) buf_handle;
4777 kern_sge32[i].length = ioc->sgl[i].iov_len;
4778
4779 /*
4780 * We created a kernel buffer corresponding to the
4781 * user buffer. Now copy in from the user buffer
4782 */
4783 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4784 (u32) (ioc->sgl[i].iov_len))) {
4785 error = -EFAULT;
4786 goto out;
4787 }
4788 }
4789
4790 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004791 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4792 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004793 if (!sense) {
4794 error = -ENOMEM;
4795 goto out;
4796 }
4797
4798 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004799 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004800 *sense_ptr = sense_handle;
4801 }
4802
4803 /*
4804 * Set the sync_cmd flag so that the ISR knows not to complete this
4805 * cmd to the SCSI mid-layer
4806 */
4807 cmd->sync_cmd = 1;
4808 megasas_issue_blocked_cmd(instance, cmd);
4809 cmd->sync_cmd = 0;
4810
4811 /*
4812 * copy out the kernel buffers to user buffers
4813 */
4814 for (i = 0; i < ioc->sge_count; i++) {
4815 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4816 ioc->sgl[i].iov_len)) {
4817 error = -EFAULT;
4818 goto out;
4819 }
4820 }
4821
4822 /*
4823 * copy out the sense
4824 */
4825 if (ioc->sense_len) {
4826 /*
bo yangb70a41e2008-03-18 03:13:06 -04004827 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004828 * sense buffer address
4829 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004830 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4831 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004832
bo yangb70a41e2008-03-18 03:13:06 -04004833 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4834 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004835 printk(KERN_ERR "megasas: Failed to copy out to user "
4836 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004837 error = -EFAULT;
4838 goto out;
4839 }
4840 }
4841
4842 /*
4843 * copy the status codes returned by the fw
4844 */
4845 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4846 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4847 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4848 error = -EFAULT;
4849 }
4850
4851 out:
4852 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004853 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004854 sense, sense_handle);
4855 }
4856
4857 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004858 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004859 kern_sge32[i].length,
4860 kbuff_arr[i], kern_sge32[i].phys_addr);
4861 }
4862
4863 megasas_return_cmd(instance, cmd);
4864 return error;
4865}
4866
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004867static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4868{
4869 struct megasas_iocpacket __user *user_ioc =
4870 (struct megasas_iocpacket __user *)arg;
4871 struct megasas_iocpacket *ioc;
4872 struct megasas_instance *instance;
4873 int error;
bo yang39a98552010-09-22 22:36:29 -04004874 int i;
4875 unsigned long flags;
4876 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004877
4878 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4879 if (!ioc)
4880 return -ENOMEM;
4881
4882 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4883 error = -EFAULT;
4884 goto out_kfree_ioc;
4885 }
4886
4887 instance = megasas_lookup_instance(ioc->host_no);
4888 if (!instance) {
4889 error = -ENODEV;
4890 goto out_kfree_ioc;
4891 }
4892
bo yang39a98552010-09-22 22:36:29 -04004893 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4894 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004895 error = -ENODEV;
4896 goto out_kfree_ioc;
4897 }
4898
4899 if (instance->unload == 1) {
4900 error = -ENODEV;
4901 goto out_kfree_ioc;
4902 }
4903
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004904 /*
4905 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4906 */
4907 if (down_interruptible(&instance->ioctl_sem)) {
4908 error = -ERESTARTSYS;
4909 goto out_kfree_ioc;
4910 }
bo yang39a98552010-09-22 22:36:29 -04004911
4912 for (i = 0; i < wait_time; i++) {
4913
4914 spin_lock_irqsave(&instance->hba_lock, flags);
4915 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4916 spin_unlock_irqrestore(&instance->hba_lock, flags);
4917 break;
4918 }
4919 spin_unlock_irqrestore(&instance->hba_lock, flags);
4920
4921 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4922 printk(KERN_NOTICE "megasas: waiting"
4923 "for controller reset to finish\n");
4924 }
4925
4926 msleep(1000);
4927 }
4928
4929 spin_lock_irqsave(&instance->hba_lock, flags);
4930 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4931 spin_unlock_irqrestore(&instance->hba_lock, flags);
4932
4933 printk(KERN_ERR "megaraid_sas: timed out while"
4934 "waiting for HBA to recover\n");
4935 error = -ENODEV;
4936 goto out_kfree_ioc;
4937 }
4938 spin_unlock_irqrestore(&instance->hba_lock, flags);
4939
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004940 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4941 up(&instance->ioctl_sem);
4942
4943 out_kfree_ioc:
4944 kfree(ioc);
4945 return error;
4946}
4947
4948static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4949{
4950 struct megasas_instance *instance;
4951 struct megasas_aen aen;
4952 int error;
bo yang39a98552010-09-22 22:36:29 -04004953 int i;
4954 unsigned long flags;
4955 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004956
4957 if (file->private_data != file) {
4958 printk(KERN_DEBUG "megasas: fasync_helper was not "
4959 "called first\n");
4960 return -EINVAL;
4961 }
4962
4963 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4964 return -EFAULT;
4965
4966 instance = megasas_lookup_instance(aen.host_no);
4967
4968 if (!instance)
4969 return -ENODEV;
4970
bo yang39a98552010-09-22 22:36:29 -04004971 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4972 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004973 }
4974
4975 if (instance->unload == 1) {
4976 return -ENODEV;
4977 }
4978
bo yang39a98552010-09-22 22:36:29 -04004979 for (i = 0; i < wait_time; i++) {
4980
4981 spin_lock_irqsave(&instance->hba_lock, flags);
4982 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4983 spin_unlock_irqrestore(&instance->hba_lock,
4984 flags);
4985 break;
4986 }
4987
4988 spin_unlock_irqrestore(&instance->hba_lock, flags);
4989
4990 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4991 printk(KERN_NOTICE "megasas: waiting for"
4992 "controller reset to finish\n");
4993 }
4994
4995 msleep(1000);
4996 }
4997
4998 spin_lock_irqsave(&instance->hba_lock, flags);
4999 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5000 spin_unlock_irqrestore(&instance->hba_lock, flags);
5001 printk(KERN_ERR "megaraid_sas: timed out while waiting"
5002 "for HBA to recover.\n");
5003 return -ENODEV;
5004 }
5005 spin_unlock_irqrestore(&instance->hba_lock, flags);
5006
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005007 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005008 error = megasas_register_aen(instance, aen.seq_num,
5009 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005010 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005011 return error;
5012}
5013
5014/**
5015 * megasas_mgmt_ioctl - char node ioctl entry point
5016 */
5017static long
5018megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5019{
5020 switch (cmd) {
5021 case MEGASAS_IOC_FIRMWARE:
5022 return megasas_mgmt_ioctl_fw(file, arg);
5023
5024 case MEGASAS_IOC_GET_AEN:
5025 return megasas_mgmt_ioctl_aen(file, arg);
5026 }
5027
5028 return -ENOTTY;
5029}
5030
5031#ifdef CONFIG_COMPAT
5032static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5033{
5034 struct compat_megasas_iocpacket __user *cioc =
5035 (struct compat_megasas_iocpacket __user *)arg;
5036 struct megasas_iocpacket __user *ioc =
5037 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5038 int i;
5039 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005040 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005041
Jeff Garzik83aabc12006-10-04 06:34:03 -04005042 if (clear_user(ioc, sizeof(*ioc)))
5043 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005044
5045 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5046 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5047 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5048 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5049 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5050 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5051 return -EFAULT;
5052
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005053 /*
5054 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5055 * sense_len is not null, so prepare the 64bit value under
5056 * the same condition.
5057 */
5058 if (ioc->sense_len) {
5059 void __user **sense_ioc_ptr =
5060 (void __user **)(ioc->frame.raw + ioc->sense_off);
5061 compat_uptr_t *sense_cioc_ptr =
5062 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5063 if (get_user(ptr, sense_cioc_ptr) ||
5064 put_user(compat_ptr(ptr), sense_ioc_ptr))
5065 return -EFAULT;
5066 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005067
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005068 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005069 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5070 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5071 copy_in_user(&ioc->sgl[i].iov_len,
5072 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5073 return -EFAULT;
5074 }
5075
5076 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5077
5078 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5079 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5080 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5081 return -EFAULT;
5082 }
5083 return error;
5084}
5085
5086static long
5087megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5088 unsigned long arg)
5089{
5090 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08005091 case MEGASAS_IOC_FIRMWARE32:
5092 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005093 case MEGASAS_IOC_GET_AEN:
5094 return megasas_mgmt_ioctl_aen(file, arg);
5095 }
5096
5097 return -ENOTTY;
5098}
5099#endif
5100
5101/*
5102 * File operations structure for management interface
5103 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08005104static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005105 .owner = THIS_MODULE,
5106 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005107 .fasync = megasas_mgmt_fasync,
5108 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005109 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005110#ifdef CONFIG_COMPAT
5111 .compat_ioctl = megasas_mgmt_compat_ioctl,
5112#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005113 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005114};
5115
5116/*
5117 * PCI hotplug support registration structure
5118 */
5119static struct pci_driver megasas_pci_driver = {
5120
5121 .name = "megaraid_sas",
5122 .id_table = megasas_pci_table,
5123 .probe = megasas_probe_one,
5124 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05005125 .suspend = megasas_suspend,
5126 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005127 .shutdown = megasas_shutdown,
5128};
5129
5130/*
5131 * Sysfs driver attributes
5132 */
5133static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5134{
5135 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5136 MEGASAS_VERSION);
5137}
5138
5139static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5140
5141static ssize_t
5142megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5143{
5144 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5145 MEGASAS_RELDATE);
5146}
5147
5148static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5149 NULL);
5150
Sumant Patro658dced2006-10-03 13:09:14 -07005151static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005152megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5153{
5154 return sprintf(buf, "%u\n", support_poll_for_event);
5155}
5156
5157static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5158 megasas_sysfs_show_support_poll_for_event, NULL);
5159
Yang, Bo837f5fe2010-10-11 06:59:20 -06005160 static ssize_t
5161megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5162{
5163 return sprintf(buf, "%u\n", support_device_change);
5164}
5165
5166static DRIVER_ATTR(support_device_change, S_IRUGO,
5167 megasas_sysfs_show_support_device_change, NULL);
5168
Yang, Bo72c4fd32009-10-06 14:20:59 -06005169static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005170megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5171{
bo yangad84db22007-11-09 04:40:16 -05005172 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005173}
5174
5175static ssize_t
5176megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5177{
5178 int retval = count;
5179 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5180 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5181 retval = -EINVAL;
5182 }
5183 return retval;
5184}
5185
Joe Malicki66dca9b2008-08-14 17:14:48 -04005186static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005187 megasas_sysfs_set_dbg_lvl);
5188
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005189static void
5190megasas_aen_polling(struct work_struct *work)
5191{
5192 struct megasas_aen_event *ev =
5193 container_of(work, struct megasas_aen_event, hotplug_work);
5194 struct megasas_instance *instance = ev->instance;
5195 union megasas_evt_class_locale class_locale;
5196 struct Scsi_Host *host;
5197 struct scsi_device *sdev1;
5198 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005199 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005200 int i, j, doscan = 0;
5201 u32 seq_num;
5202 int error;
5203
5204 if (!instance) {
5205 printk(KERN_ERR "invalid instance!\n");
5206 kfree(ev);
5207 return;
5208 }
5209 instance->ev = NULL;
5210 host = instance->host;
5211 if (instance->evt_detail) {
5212
5213 switch (instance->evt_detail->code) {
5214 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005215 if (megasas_get_pd_list(instance) == 0) {
5216 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5217 for (j = 0;
5218 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5219 j++) {
5220
5221 pd_index =
5222 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5223
5224 sdev1 =
5225 scsi_device_lookup(host, i, j, 0);
5226
5227 if (instance->pd_list[pd_index].driveState
5228 == MR_PD_STATE_SYSTEM) {
5229 if (!sdev1) {
5230 scsi_add_device(host, i, j, 0);
5231 }
5232
5233 if (sdev1)
5234 scsi_device_put(sdev1);
5235 }
5236 }
5237 }
5238 }
5239 doscan = 0;
5240 break;
5241
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005242 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005243 if (megasas_get_pd_list(instance) == 0) {
Yang, Boc9786842009-12-06 08:39:25 -07005244 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5245 for (j = 0;
5246 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5247 j++) {
5248
5249 pd_index =
5250 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5251
5252 sdev1 =
5253 scsi_device_lookup(host, i, j, 0);
5254
5255 if (instance->pd_list[pd_index].driveState
5256 == MR_PD_STATE_SYSTEM) {
5257 if (sdev1) {
5258 scsi_device_put(sdev1);
5259 }
5260 } else {
5261 if (sdev1) {
5262 scsi_remove_device(sdev1);
5263 scsi_device_put(sdev1);
5264 }
5265 }
5266 }
5267 }
5268 }
5269 doscan = 0;
5270 break;
5271
5272 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005273 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005274 case MR_EVT_LD_DELETED:
5275 megasas_get_ld_list(instance);
5276 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5277 for (j = 0;
5278 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5279 j++) {
5280
5281 ld_index =
5282 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5283
5284 sdev1 = scsi_device_lookup(host,
5285 i + MEGASAS_MAX_LD_CHANNELS,
5286 j,
5287 0);
5288
5289 if (instance->ld_ids[ld_index] != 0xff) {
5290 if (sdev1) {
5291 scsi_device_put(sdev1);
5292 }
5293 } else {
5294 if (sdev1) {
5295 scsi_remove_device(sdev1);
5296 scsi_device_put(sdev1);
5297 }
5298 }
5299 }
5300 }
5301 doscan = 0;
5302 break;
5303 case MR_EVT_LD_CREATED:
5304 megasas_get_ld_list(instance);
5305 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5306 for (j = 0;
5307 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5308 j++) {
5309 ld_index =
5310 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5311
5312 sdev1 = scsi_device_lookup(host,
5313 i+MEGASAS_MAX_LD_CHANNELS,
5314 j, 0);
5315
5316 if (instance->ld_ids[ld_index] !=
5317 0xff) {
5318 if (!sdev1) {
5319 scsi_add_device(host,
5320 i + 2,
5321 j, 0);
5322 }
5323 }
5324 if (sdev1) {
5325 scsi_device_put(sdev1);
5326 }
5327 }
5328 }
5329 doscan = 0;
5330 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005331 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005332 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005333 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005334 doscan = 1;
5335 break;
5336 default:
5337 doscan = 0;
5338 break;
5339 }
5340 } else {
5341 printk(KERN_ERR "invalid evt_detail!\n");
5342 kfree(ev);
5343 return;
5344 }
5345
5346 if (doscan) {
5347 printk(KERN_INFO "scanning ...\n");
5348 megasas_get_pd_list(instance);
5349 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5350 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5351 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5352 sdev1 = scsi_device_lookup(host, i, j, 0);
5353 if (instance->pd_list[pd_index].driveState ==
5354 MR_PD_STATE_SYSTEM) {
5355 if (!sdev1) {
5356 scsi_add_device(host, i, j, 0);
5357 }
5358 if (sdev1)
5359 scsi_device_put(sdev1);
5360 } else {
5361 if (sdev1) {
5362 scsi_remove_device(sdev1);
5363 scsi_device_put(sdev1);
5364 }
5365 }
5366 }
5367 }
Yang, Boc9786842009-12-06 08:39:25 -07005368
5369 megasas_get_ld_list(instance);
5370 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5371 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5372 ld_index =
5373 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5374
5375 sdev1 = scsi_device_lookup(host,
5376 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5377 if (instance->ld_ids[ld_index] != 0xff) {
5378 if (!sdev1) {
5379 scsi_add_device(host,
5380 i+2,
5381 j, 0);
5382 } else {
5383 scsi_device_put(sdev1);
5384 }
5385 } else {
5386 if (sdev1) {
5387 scsi_remove_device(sdev1);
5388 scsi_device_put(sdev1);
5389 }
5390 }
5391 }
5392 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005393 }
5394
5395 if ( instance->aen_cmd != NULL ) {
5396 kfree(ev);
5397 return ;
5398 }
5399
5400 seq_num = instance->evt_detail->seq_num + 1;
5401
5402 /* Register AEN with FW for latest sequence number plus 1 */
5403 class_locale.members.reserved = 0;
5404 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5405 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5406 mutex_lock(&instance->aen_mutex);
5407 error = megasas_register_aen(instance, seq_num,
5408 class_locale.word);
5409 mutex_unlock(&instance->aen_mutex);
5410
5411 if (error)
5412 printk(KERN_ERR "register aen failed error %x\n", error);
5413
5414 kfree(ev);
5415}
5416
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005417/**
5418 * megasas_init - Driver load entry point
5419 */
5420static int __init megasas_init(void)
5421{
5422 int rval;
5423
5424 /*
5425 * Announce driver version and other information
5426 */
5427 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5428 MEGASAS_EXT_VERSION);
5429
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07005430 spin_lock_init(&poll_aen_lock);
5431
Yang, Bo72c4fd32009-10-06 14:20:59 -06005432 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005433 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005434
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005435 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5436
5437 /*
5438 * Register character device node
5439 */
5440 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5441
5442 if (rval < 0) {
5443 printk(KERN_DEBUG "megasas: failed to open device node\n");
5444 return rval;
5445 }
5446
5447 megasas_mgmt_majorno = rval;
5448
5449 /*
5450 * Register ourselves as PCI hotplug module
5451 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005452 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005453
5454 if (rval) {
5455 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005456 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005457 }
5458
Jeff Garzik83aabc12006-10-04 06:34:03 -04005459 rval = driver_create_file(&megasas_pci_driver.driver,
5460 &driver_attr_version);
5461 if (rval)
5462 goto err_dcf_attr_ver;
5463 rval = driver_create_file(&megasas_pci_driver.driver,
5464 &driver_attr_release_date);
5465 if (rval)
5466 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005467
5468 rval = driver_create_file(&megasas_pci_driver.driver,
5469 &driver_attr_support_poll_for_event);
5470 if (rval)
5471 goto err_dcf_support_poll_for_event;
5472
Jeff Garzik83aabc12006-10-04 06:34:03 -04005473 rval = driver_create_file(&megasas_pci_driver.driver,
5474 &driver_attr_dbg_lvl);
5475 if (rval)
5476 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005477 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005478 &driver_attr_support_device_change);
5479 if (rval)
5480 goto err_dcf_support_device_change;
5481
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005482 return rval;
bo yangad84db22007-11-09 04:40:16 -05005483
Yang, Bo837f5fe2010-10-11 06:59:20 -06005484err_dcf_support_device_change:
5485 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005486 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005487err_dcf_dbg_lvl:
5488 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005489 &driver_attr_support_poll_for_event);
5490
5491err_dcf_support_poll_for_event:
5492 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005493 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005494
Jeff Garzik83aabc12006-10-04 06:34:03 -04005495err_dcf_rel_date:
5496 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5497err_dcf_attr_ver:
5498 pci_unregister_driver(&megasas_pci_driver);
5499err_pcidrv:
5500 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005501 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005502}
5503
5504/**
5505 * megasas_exit - Driver unload entry point
5506 */
5507static void __exit megasas_exit(void)
5508{
Sumant Patro658dced2006-10-03 13:09:14 -07005509 driver_remove_file(&megasas_pci_driver.driver,
5510 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005511 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005512 &driver_attr_support_poll_for_event);
5513 driver_remove_file(&megasas_pci_driver.driver,
5514 &driver_attr_support_device_change);
5515 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005516 &driver_attr_release_date);
5517 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005518
5519 pci_unregister_driver(&megasas_pci_driver);
5520 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5521}
5522
5523module_init(megasas_init);
5524module_exit(megasas_exit);