blob: f8f0a165dfb678f14e2cc84090f87a22b3fa7357 [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 radford3f1530c2010-12-14 18:51:48 -08004 * Copyright (c) 2009-2011 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 radforde38a8132012-03-19 19:50:06 -070021 * Version : v00.00.06.15-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 radfordc5daa6a2012-07-17 18:20:03 -070074static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
75module_param(throttlequeuedepth, int, S_IRUGO);
76MODULE_PARM_DESC(throttlequeuedepth,
77 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
78
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040079MODULE_LICENSE("GPL");
80MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080081MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050082MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040083
adam radford058a8fa2011-10-08 18:14:27 -070084int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -040085static int megasas_get_pd_list(struct megasas_instance *instance);
86static int megasas_issue_init_mfi(struct megasas_instance *instance);
87static int megasas_register_aen(struct megasas_instance *instance,
88 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040089/*
90 * PCI ID table for all supported controllers
91 */
92static struct pci_device_id megasas_pci_table[] = {
93
Henrik Kretzschmarf3d7271c2006-08-15 11:17:21 +020094 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
95 /* xscale IOP */
96 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
97 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -040098 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
99 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700100 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
101 /* gen2*/
102 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
103 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600104 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
105 /* skinny*/
106 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
107 /* skinny*/
Henrik Kretzschmarf3d7271c2006-08-15 11:17:21 +0200108 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
109 /* xscale IOP, vega */
110 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
111 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800112 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
113 /* Fusion */
adam radford36807e62011-10-08 18:15:06 -0700114 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
115 /* Invader */
Henrik Kretzschmarf3d7271c2006-08-15 11:17:21 +0200116 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400117};
118
119MODULE_DEVICE_TABLE(pci, megasas_pci_table);
120
121static int megasas_mgmt_majorno;
122static struct megasas_mgmt_info megasas_mgmt_info;
123static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100124static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400125
Yang, Boc3518832009-10-06 14:18:02 -0600126static int megasas_poll_wait_aen;
127static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600128static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800129u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600130static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700131
Yang, Boc3518832009-10-06 14:18:02 -0600132/* define lock for aen poll */
133spinlock_t poll_aen_lock;
134
adam radford9c915a82010-12-21 13:34:31 -0800135void
bo yang7343eb62007-11-09 04:35:44 -0500136megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
137 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800138static u32
139megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
140static int
141megasas_adp_reset_gen2(struct megasas_instance *instance,
142 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800143static irqreturn_t megasas_isr(int irq, void *devp);
144static u32
145megasas_init_adapter_mfi(struct megasas_instance *instance);
146u32
147megasas_build_and_issue_cmd(struct megasas_instance *instance,
148 struct scsi_cmnd *scmd);
149static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800150void
151megasas_release_fusion(struct megasas_instance *instance);
152int
153megasas_ioc_init_fusion(struct megasas_instance *instance);
154void
155megasas_free_cmds_fusion(struct megasas_instance *instance);
156u8
157megasas_get_map_info(struct megasas_instance *instance);
158int
159megasas_sync_map_info(struct megasas_instance *instance);
160int
161wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
162void megasas_reset_reply_desc(struct megasas_instance *instance);
163u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
164 struct LD_LOAD_BALANCE_INFO *lbInfo);
165int megasas_reset_fusion(struct Scsi_Host *shost);
166void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800167
168void
169megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
170{
171 instance->instancet->fire_cmd(instance,
172 cmd->frame_phys_addr, 0, instance->reg_set);
173}
bo yang7343eb62007-11-09 04:35:44 -0500174
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400175/**
176 * megasas_get_cmd - Get a command from the free pool
177 * @instance: Adapter soft state
178 *
179 * Returns a free command from the pool
180 */
adam radford9c915a82010-12-21 13:34:31 -0800181struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400182 *instance)
183{
184 unsigned long flags;
185 struct megasas_cmd *cmd = NULL;
186
187 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
188
189 if (!list_empty(&instance->cmd_pool)) {
190 cmd = list_entry((&instance->cmd_pool)->next,
191 struct megasas_cmd, list);
192 list_del_init(&cmd->list);
193 } else {
194 printk(KERN_ERR "megasas: Command pool empty!\n");
195 }
196
197 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
198 return cmd;
199}
200
201/**
202 * megasas_return_cmd - Return a cmd to free command pool
203 * @instance: Adapter soft state
204 * @cmd: Command packet to be returned to free command pool
205 */
adam radford9c915a82010-12-21 13:34:31 -0800206inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400207megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
208{
209 unsigned long flags;
210
211 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
212
213 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800214 cmd->frame_count = 0;
adam radforde5f93a32011-10-08 18:15:19 -0700215 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
216 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
217 (reset_devices))
218 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400219 list_add_tail(&cmd->list, &instance->cmd_pool);
220
221 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
222}
223
Sumant Patro1341c932006-01-25 12:02:40 -0800224
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400225/**
adam radford0d490162010-12-14 19:17:17 -0800226* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800227* (deviceid : 1064R, PERC5) controllers
228*/
229
230/**
231 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400232 * @regs: MFI register set
233 */
234static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800235megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400236{
bo yang39a98552010-09-22 22:36:29 -0400237 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400238
239 /* Dummy readl to force pci flush */
240 readl(&regs->outbound_intr_mask);
241}
242
243/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700244 * megasas_disable_intr_xscale -Disables interrupt
245 * @regs: MFI register set
246 */
247static inline void
248megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
249{
250 u32 mask = 0x1f;
251 writel(mask, &regs->outbound_intr_mask);
252 /* Dummy readl to force pci flush */
253 readl(&regs->outbound_intr_mask);
254}
255
256/**
Sumant Patro1341c932006-01-25 12:02:40 -0800257 * megasas_read_fw_status_reg_xscale - returns the current FW status value
258 * @regs: MFI register set
259 */
260static u32
261megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
262{
263 return readl(&(regs)->outbound_msg_0);
264}
265/**
266 * megasas_clear_interrupt_xscale - Check & clear interrupt
267 * @regs: MFI register set
268 */
adam radford0d490162010-12-14 19:17:17 -0800269static int
Sumant Patro1341c932006-01-25 12:02:40 -0800270megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
271{
272 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400273 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800274 /*
275 * Check if it is our interrupt
276 */
277 status = readl(&regs->outbound_intr_status);
278
bo yang39a98552010-09-22 22:36:29 -0400279 if (status & MFI_OB_INTR_STATUS_MASK)
280 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
281 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
282 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800283
284 /*
285 * Clear the interrupt by writing back the same value
286 */
bo yang39a98552010-09-22 22:36:29 -0400287 if (mfiStatus)
288 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800289
Yang, Bo06f579d2008-08-10 12:42:37 -0700290 /* Dummy readl to force pci flush */
291 readl(&regs->outbound_intr_status);
292
bo yang39a98552010-09-22 22:36:29 -0400293 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800294}
295
296/**
297 * megasas_fire_cmd_xscale - Sends command to the FW
298 * @frame_phys_addr : Physical address of cmd
299 * @frame_count : Number of frames for the command
300 * @regs : MFI register set
301 */
adam radford0d490162010-12-14 19:17:17 -0800302static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600303megasas_fire_cmd_xscale(struct megasas_instance *instance,
304 dma_addr_t frame_phys_addr,
305 u32 frame_count,
306 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800307{
bo yang39a98552010-09-22 22:36:29 -0400308 unsigned long flags;
309 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800310 writel((frame_phys_addr >> 3)|(frame_count),
311 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400312 spin_unlock_irqrestore(&instance->hba_lock, flags);
313}
314
315/**
316 * megasas_adp_reset_xscale - For controller reset
317 * @regs: MFI register set
318 */
319static int
320megasas_adp_reset_xscale(struct megasas_instance *instance,
321 struct megasas_register_set __iomem *regs)
322{
323 u32 i;
324 u32 pcidata;
325 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
326
327 for (i = 0; i < 3; i++)
328 msleep(1000); /* sleep for 3 secs */
329 pcidata = 0;
330 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
331 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
332 if (pcidata & 0x2) {
333 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
334 pcidata &= ~0x2;
335 pci_write_config_dword(instance->pdev,
336 MFI_1068_PCSR_OFFSET, pcidata);
337
338 for (i = 0; i < 2; i++)
339 msleep(1000); /* need to wait 2 secs again */
340
341 pcidata = 0;
342 pci_read_config_dword(instance->pdev,
343 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
344 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
345 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
346 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
347 pcidata = 0;
348 pci_write_config_dword(instance->pdev,
349 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
350 }
351 }
352 return 0;
353}
354
355/**
356 * megasas_check_reset_xscale - For controller reset check
357 * @regs: MFI register set
358 */
359static int
360megasas_check_reset_xscale(struct megasas_instance *instance,
361 struct megasas_register_set __iomem *regs)
362{
363 u32 consumer;
364 consumer = *instance->consumer;
365
366 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
367 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
368 return 1;
369 }
370 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800371}
372
373static struct megasas_instance_template megasas_instance_template_xscale = {
374
375 .fire_cmd = megasas_fire_cmd_xscale,
376 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700377 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800378 .clear_intr = megasas_clear_intr_xscale,
379 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400380 .adp_reset = megasas_adp_reset_xscale,
381 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800382 .service_isr = megasas_isr,
383 .tasklet = megasas_complete_cmd_dpc,
384 .init_adapter = megasas_init_adapter_mfi,
385 .build_and_issue_cmd = megasas_build_and_issue_cmd,
386 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800387};
388
389/**
adam radford0d490162010-12-14 19:17:17 -0800390* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800391* to xscale (deviceid : 1064R, PERC5) controllers
392*/
393
394/**
adam radford0d490162010-12-14 19:17:17 -0800395* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800396* controllers
397*/
398
399/**
400 * megasas_enable_intr_ppc - Enables interrupts
401 * @regs: MFI register set
402 */
403static inline void
404megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
405{
406 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800407
bo yang39a98552010-09-22 22:36:29 -0400408 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800409
410 /* Dummy readl to force pci flush */
411 readl(&regs->outbound_intr_mask);
412}
413
414/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700415 * megasas_disable_intr_ppc - Disable interrupt
416 * @regs: MFI register set
417 */
418static inline void
419megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
420{
421 u32 mask = 0xFFFFFFFF;
422 writel(mask, &regs->outbound_intr_mask);
423 /* Dummy readl to force pci flush */
424 readl(&regs->outbound_intr_mask);
425}
426
427/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800428 * megasas_read_fw_status_reg_ppc - returns the current FW status value
429 * @regs: MFI register set
430 */
431static u32
432megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
433{
434 return readl(&(regs)->outbound_scratch_pad);
435}
436
437/**
438 * megasas_clear_interrupt_ppc - Check & clear interrupt
439 * @regs: MFI register set
440 */
adam radford0d490162010-12-14 19:17:17 -0800441static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800442megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
443{
adam radford3cc6851f92011-05-11 18:34:52 -0700444 u32 status, mfiStatus = 0;
445
Sumant Patrof9876f02006-02-03 15:34:35 -0800446 /*
447 * Check if it is our interrupt
448 */
449 status = readl(&regs->outbound_intr_status);
450
adam radford3cc6851f92011-05-11 18:34:52 -0700451 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
452 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
453
454 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
455 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800456
457 /*
458 * Clear the interrupt by writing back the same value
459 */
460 writel(status, &regs->outbound_doorbell_clear);
461
Yang, Bo06f579d2008-08-10 12:42:37 -0700462 /* Dummy readl to force pci flush */
463 readl(&regs->outbound_doorbell_clear);
464
adam radford3cc6851f92011-05-11 18:34:52 -0700465 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800466}
adam radford3cc6851f92011-05-11 18:34:52 -0700467
Sumant Patrof9876f02006-02-03 15:34:35 -0800468/**
469 * megasas_fire_cmd_ppc - Sends command to the FW
470 * @frame_phys_addr : Physical address of cmd
471 * @frame_count : Number of frames for the command
472 * @regs : MFI register set
473 */
adam radford0d490162010-12-14 19:17:17 -0800474static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600475megasas_fire_cmd_ppc(struct megasas_instance *instance,
476 dma_addr_t frame_phys_addr,
477 u32 frame_count,
478 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800479{
bo yang39a98552010-09-22 22:36:29 -0400480 unsigned long flags;
481 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800482 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800483 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400484 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800485}
486
bo yang39a98552010-09-22 22:36:29 -0400487/**
bo yang39a98552010-09-22 22:36:29 -0400488 * megasas_check_reset_ppc - For controller reset check
489 * @regs: MFI register set
490 */
491static int
492megasas_check_reset_ppc(struct megasas_instance *instance,
493 struct megasas_register_set __iomem *regs)
494{
adam radford3cc6851f92011-05-11 18:34:52 -0700495 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
496 return 1;
497
bo yang39a98552010-09-22 22:36:29 -0400498 return 0;
499}
adam radford3cc6851f92011-05-11 18:34:52 -0700500
Sumant Patrof9876f02006-02-03 15:34:35 -0800501static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800502
Sumant Patrof9876f02006-02-03 15:34:35 -0800503 .fire_cmd = megasas_fire_cmd_ppc,
504 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700505 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800506 .clear_intr = megasas_clear_intr_ppc,
507 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700508 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400509 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800510 .service_isr = megasas_isr,
511 .tasklet = megasas_complete_cmd_dpc,
512 .init_adapter = megasas_init_adapter_mfi,
513 .build_and_issue_cmd = megasas_build_and_issue_cmd,
514 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800515};
516
517/**
Yang, Bo87911122009-10-06 14:31:54 -0600518 * megasas_enable_intr_skinny - Enables interrupts
519 * @regs: MFI register set
520 */
521static inline void
522megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
523{
524 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
525
526 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
527
528 /* Dummy readl to force pci flush */
529 readl(&regs->outbound_intr_mask);
530}
531
532/**
533 * megasas_disable_intr_skinny - Disables interrupt
534 * @regs: MFI register set
535 */
536static inline void
537megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
538{
539 u32 mask = 0xFFFFFFFF;
540 writel(mask, &regs->outbound_intr_mask);
541 /* Dummy readl to force pci flush */
542 readl(&regs->outbound_intr_mask);
543}
544
545/**
546 * megasas_read_fw_status_reg_skinny - returns the current FW status value
547 * @regs: MFI register set
548 */
549static u32
550megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
551{
552 return readl(&(regs)->outbound_scratch_pad);
553}
554
555/**
556 * megasas_clear_interrupt_skinny - Check & clear interrupt
557 * @regs: MFI register set
558 */
559static int
560megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
561{
562 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800563 u32 mfiStatus = 0;
564
Yang, Bo87911122009-10-06 14:31:54 -0600565 /*
566 * Check if it is our interrupt
567 */
568 status = readl(&regs->outbound_intr_status);
569
570 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400571 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600572 }
573
574 /*
adam radfordebf054b2011-02-24 20:57:15 -0800575 * Check if it is our interrupt
576 */
577 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
578 MFI_STATE_FAULT) {
579 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
580 } else
581 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
582
583 /*
Yang, Bo87911122009-10-06 14:31:54 -0600584 * Clear the interrupt by writing back the same value
585 */
586 writel(status, &regs->outbound_intr_status);
587
588 /*
589 * dummy read to flush PCI
590 */
591 readl(&regs->outbound_intr_status);
592
adam radfordebf054b2011-02-24 20:57:15 -0800593 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600594}
595
596/**
597 * megasas_fire_cmd_skinny - Sends command to the FW
598 * @frame_phys_addr : Physical address of cmd
599 * @frame_count : Number of frames for the command
600 * @regs : MFI register set
601 */
602static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600603megasas_fire_cmd_skinny(struct megasas_instance *instance,
604 dma_addr_t frame_phys_addr,
605 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600606 struct megasas_register_set __iomem *regs)
607{
Yang, Bo0c79e682009-10-06 14:47:35 -0600608 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400609 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600610 writel(0, &(regs)->inbound_high_queue_port);
611 writel((frame_phys_addr | (frame_count<<1))|1,
612 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400613 spin_unlock_irqrestore(&instance->hba_lock, flags);
614}
615
616/**
bo yang39a98552010-09-22 22:36:29 -0400617 * megasas_check_reset_skinny - For controller reset check
618 * @regs: MFI register set
619 */
620static int
621megasas_check_reset_skinny(struct megasas_instance *instance,
622 struct megasas_register_set __iomem *regs)
623{
adam radford3cc6851f92011-05-11 18:34:52 -0700624 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
625 return 1;
626
bo yang39a98552010-09-22 22:36:29 -0400627 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600628}
629
630static struct megasas_instance_template megasas_instance_template_skinny = {
631
632 .fire_cmd = megasas_fire_cmd_skinny,
633 .enable_intr = megasas_enable_intr_skinny,
634 .disable_intr = megasas_disable_intr_skinny,
635 .clear_intr = megasas_clear_intr_skinny,
636 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800637 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400638 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800639 .service_isr = megasas_isr,
640 .tasklet = megasas_complete_cmd_dpc,
641 .init_adapter = megasas_init_adapter_mfi,
642 .build_and_issue_cmd = megasas_build_and_issue_cmd,
643 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600644};
645
646
647/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700648* The following functions are defined for gen2 (deviceid : 0x78 0x79)
649* controllers
650*/
651
652/**
653 * megasas_enable_intr_gen2 - Enables interrupts
654 * @regs: MFI register set
655 */
656static inline void
657megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
658{
659 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
660
661 /* write ~0x00000005 (4 & 1) to the intr mask*/
662 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
663
664 /* Dummy readl to force pci flush */
665 readl(&regs->outbound_intr_mask);
666}
667
668/**
669 * megasas_disable_intr_gen2 - Disables interrupt
670 * @regs: MFI register set
671 */
672static inline void
673megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
674{
675 u32 mask = 0xFFFFFFFF;
676 writel(mask, &regs->outbound_intr_mask);
677 /* Dummy readl to force pci flush */
678 readl(&regs->outbound_intr_mask);
679}
680
681/**
682 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
683 * @regs: MFI register set
684 */
685static u32
686megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
687{
688 return readl(&(regs)->outbound_scratch_pad);
689}
690
691/**
692 * megasas_clear_interrupt_gen2 - Check & clear interrupt
693 * @regs: MFI register set
694 */
695static int
696megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
697{
698 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400699 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700700 /*
701 * Check if it is our interrupt
702 */
703 status = readl(&regs->outbound_intr_status);
704
bo yang39a98552010-09-22 22:36:29 -0400705 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
706 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
707 }
708 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
709 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
710 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700711
712 /*
713 * Clear the interrupt by writing back the same value
714 */
bo yang39a98552010-09-22 22:36:29 -0400715 if (mfiStatus)
716 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700717
718 /* Dummy readl to force pci flush */
719 readl(&regs->outbound_intr_status);
720
bo yang39a98552010-09-22 22:36:29 -0400721 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700722}
723/**
724 * megasas_fire_cmd_gen2 - Sends command to the FW
725 * @frame_phys_addr : Physical address of cmd
726 * @frame_count : Number of frames for the command
727 * @regs : MFI register set
728 */
729static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600730megasas_fire_cmd_gen2(struct megasas_instance *instance,
731 dma_addr_t frame_phys_addr,
732 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700733 struct megasas_register_set __iomem *regs)
734{
bo yang39a98552010-09-22 22:36:29 -0400735 unsigned long flags;
736 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700737 writel((frame_phys_addr | (frame_count<<1))|1,
738 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400739 spin_unlock_irqrestore(&instance->hba_lock, flags);
740}
741
742/**
743 * megasas_adp_reset_gen2 - For controller reset
744 * @regs: MFI register set
745 */
746static int
747megasas_adp_reset_gen2(struct megasas_instance *instance,
748 struct megasas_register_set __iomem *reg_set)
749{
750 u32 retry = 0 ;
751 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800752 u32 *seq_offset = &reg_set->seq_offset;
753 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400754
adam radfordebf054b2011-02-24 20:57:15 -0800755 if (instance->instancet == &megasas_instance_template_skinny) {
756 seq_offset = &reg_set->fusion_seq_offset;
757 hostdiag_offset = &reg_set->fusion_host_diag;
758 }
759
760 writel(0, seq_offset);
761 writel(4, seq_offset);
762 writel(0xb, seq_offset);
763 writel(2, seq_offset);
764 writel(7, seq_offset);
765 writel(0xd, seq_offset);
766
bo yang39a98552010-09-22 22:36:29 -0400767 msleep(1000);
768
adam radfordebf054b2011-02-24 20:57:15 -0800769 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400770
771 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
772 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800773 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400774 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
775 retry, HostDiag);
776
777 if (retry++ >= 100)
778 return 1;
779
780 }
781
782 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
783
adam radfordebf054b2011-02-24 20:57:15 -0800784 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400785
786 ssleep(10);
787
adam radfordebf054b2011-02-24 20:57:15 -0800788 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400789 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
790 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800791 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400792 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
793 retry, HostDiag);
794
795 if (retry++ >= 1000)
796 return 1;
797
798 }
799 return 0;
800}
801
802/**
803 * megasas_check_reset_gen2 - For controller reset check
804 * @regs: MFI register set
805 */
806static int
807megasas_check_reset_gen2(struct megasas_instance *instance,
808 struct megasas_register_set __iomem *regs)
809{
Yang, Bo707e09b2010-10-12 07:20:27 -0600810 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
811 return 1;
812 }
813
bo yang39a98552010-09-22 22:36:29 -0400814 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700815}
816
817static struct megasas_instance_template megasas_instance_template_gen2 = {
818
819 .fire_cmd = megasas_fire_cmd_gen2,
820 .enable_intr = megasas_enable_intr_gen2,
821 .disable_intr = megasas_disable_intr_gen2,
822 .clear_intr = megasas_clear_intr_gen2,
823 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400824 .adp_reset = megasas_adp_reset_gen2,
825 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800826 .service_isr = megasas_isr,
827 .tasklet = megasas_complete_cmd_dpc,
828 .init_adapter = megasas_init_adapter_mfi,
829 .build_and_issue_cmd = megasas_build_and_issue_cmd,
830 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700831};
832
833/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800834* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400835* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800836*/
837
adam radford9c915a82010-12-21 13:34:31 -0800838/*
839 * Template added for TB (Fusion)
840 */
841extern struct megasas_instance_template megasas_instance_template_fusion;
842
Sumant Patrof9876f02006-02-03 15:34:35 -0800843/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400844 * megasas_issue_polled - Issues a polling command
845 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800846 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400847 *
848 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
849 */
adam radford9c915a82010-12-21 13:34:31 -0800850int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400851megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
852{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400853
854 struct megasas_header *frame_hdr = &cmd->frame->hdr;
855
856 frame_hdr->cmd_status = 0xFF;
857 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
858
859 /*
860 * Issue the frame using inbound queue port
861 */
adam radford9c915a82010-12-21 13:34:31 -0800862 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400863
864 /*
865 * Wait for cmd_status to change
866 */
adam radford9c915a82010-12-21 13:34:31 -0800867 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400868}
869
870/**
871 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
872 * @instance: Adapter soft state
873 * @cmd: Command to be issued
874 *
875 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700876 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400877 * Used to issue ioctl commands.
878 */
879static int
880megasas_issue_blocked_cmd(struct megasas_instance *instance,
881 struct megasas_cmd *cmd)
882{
883 cmd->cmd_status = ENODATA;
884
adam radford9c915a82010-12-21 13:34:31 -0800885 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400886
bo yang39a98552010-09-22 22:36:29 -0400887 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400888
889 return 0;
890}
891
892/**
893 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
894 * @instance: Adapter soft state
895 * @cmd_to_abort: Previously issued cmd to be aborted
896 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100897 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400898 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700899 * cmd and waits for return status.
900 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400901 */
902static int
903megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
904 struct megasas_cmd *cmd_to_abort)
905{
906 struct megasas_cmd *cmd;
907 struct megasas_abort_frame *abort_fr;
908
909 cmd = megasas_get_cmd(instance);
910
911 if (!cmd)
912 return -1;
913
914 abort_fr = &cmd->frame->abort;
915
916 /*
917 * Prepare and issue the abort frame
918 */
919 abort_fr->cmd = MFI_CMD_ABORT;
920 abort_fr->cmd_status = 0xFF;
921 abort_fr->flags = 0;
922 abort_fr->abort_context = cmd_to_abort->index;
923 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
924 abort_fr->abort_mfi_phys_addr_hi = 0;
925
926 cmd->sync_cmd = 1;
927 cmd->cmd_status = 0xFF;
928
adam radford9c915a82010-12-21 13:34:31 -0800929 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400930
931 /*
932 * Wait for this cmd to complete
933 */
bo yang39a98552010-09-22 22:36:29 -0400934 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
935 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400936
937 megasas_return_cmd(instance, cmd);
938 return 0;
939}
940
941/**
942 * megasas_make_sgl32 - Prepares 32-bit SGL
943 * @instance: Adapter soft state
944 * @scp: SCSI command from the mid-layer
945 * @mfi_sgl: SGL to be filled in
946 *
947 * If successful, this function returns the number of SG elements. Otherwise,
948 * it returnes -1.
949 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800950static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400951megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
952 union megasas_sgl *mfi_sgl)
953{
954 int i;
955 int sge_count;
956 struct scatterlist *os_sgl;
957
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900958 sge_count = scsi_dma_map(scp);
959 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400960
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900961 if (sge_count) {
962 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
963 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
964 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
965 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400966 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400967 return sge_count;
968}
969
970/**
971 * megasas_make_sgl64 - Prepares 64-bit SGL
972 * @instance: Adapter soft state
973 * @scp: SCSI command from the mid-layer
974 * @mfi_sgl: SGL to be filled in
975 *
976 * If successful, this function returns the number of SG elements. Otherwise,
977 * it returnes -1.
978 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800979static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400980megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
981 union megasas_sgl *mfi_sgl)
982{
983 int i;
984 int sge_count;
985 struct scatterlist *os_sgl;
986
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900987 sge_count = scsi_dma_map(scp);
988 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900990 if (sge_count) {
991 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
992 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
993 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
994 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400995 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996 return sge_count;
997}
998
Yang, Bof4c9a132009-10-06 14:43:28 -0600999/**
1000 * megasas_make_sgl_skinny - Prepares IEEE SGL
1001 * @instance: Adapter soft state
1002 * @scp: SCSI command from the mid-layer
1003 * @mfi_sgl: SGL to be filled in
1004 *
1005 * If successful, this function returns the number of SG elements. Otherwise,
1006 * it returnes -1.
1007 */
1008static int
1009megasas_make_sgl_skinny(struct megasas_instance *instance,
1010 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1011{
1012 int i;
1013 int sge_count;
1014 struct scatterlist *os_sgl;
1015
1016 sge_count = scsi_dma_map(scp);
1017
1018 if (sge_count) {
1019 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1020 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1021 mfi_sgl->sge_skinny[i].phys_addr =
1022 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001023 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001024 }
1025 }
1026 return sge_count;
1027}
1028
Sumant Patrob1df99d2006-10-03 12:40:47 -07001029 /**
1030 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001031 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001032 * @sge_count : number of sg elements
1033 *
1034 * Returns the number of frames required for numnber of sge's (sge_count)
1035 */
1036
Yang, Bof4c9a132009-10-06 14:43:28 -06001037static u32 megasas_get_frame_count(struct megasas_instance *instance,
1038 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001039{
1040 int num_cnt;
1041 int sge_bytes;
1042 u32 sge_sz;
1043 u32 frame_count=0;
1044
1045 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1046 sizeof(struct megasas_sge32);
1047
Yang, Bof4c9a132009-10-06 14:43:28 -06001048 if (instance->flag_ieee) {
1049 sge_sz = sizeof(struct megasas_sge_skinny);
1050 }
1051
Sumant Patrob1df99d2006-10-03 12:40:47 -07001052 /*
bo yangd532dbe2008-03-17 03:36:43 -04001053 * Main frame can contain 2 SGEs for 64-bit SGLs and
1054 * 3 SGEs for 32-bit SGLs for ldio &
1055 * 1 SGEs for 64-bit SGLs and
1056 * 2 SGEs for 32-bit SGLs for pthru frame
1057 */
1058 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001059 if (instance->flag_ieee == 1) {
1060 num_cnt = sge_count - 1;
1061 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001062 num_cnt = sge_count - 1;
1063 else
1064 num_cnt = sge_count - 2;
1065 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001066 if (instance->flag_ieee == 1) {
1067 num_cnt = sge_count - 1;
1068 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001069 num_cnt = sge_count - 2;
1070 else
1071 num_cnt = sge_count - 3;
1072 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001073
1074 if(num_cnt>0){
1075 sge_bytes = sge_sz * num_cnt;
1076
1077 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1078 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1079 }
1080 /* Main frame */
1081 frame_count +=1;
1082
1083 if (frame_count > 7)
1084 frame_count = 8;
1085 return frame_count;
1086}
1087
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001088/**
1089 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1090 * @instance: Adapter soft state
1091 * @scp: SCSI command
1092 * @cmd: Command to be prepared in
1093 *
1094 * This function prepares CDB commands. These are typcially pass-through
1095 * commands to the devices.
1096 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001097static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001098megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1099 struct megasas_cmd *cmd)
1100{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001101 u32 is_logical;
1102 u32 device_id;
1103 u16 flags = 0;
1104 struct megasas_pthru_frame *pthru;
1105
1106 is_logical = MEGASAS_IS_LOGICAL(scp);
1107 device_id = MEGASAS_DEV_INDEX(instance, scp);
1108 pthru = (struct megasas_pthru_frame *)cmd->frame;
1109
1110 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1111 flags = MFI_FRAME_DIR_WRITE;
1112 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1113 flags = MFI_FRAME_DIR_READ;
1114 else if (scp->sc_data_direction == PCI_DMA_NONE)
1115 flags = MFI_FRAME_DIR_NONE;
1116
Yang, Bof4c9a132009-10-06 14:43:28 -06001117 if (instance->flag_ieee == 1) {
1118 flags |= MFI_FRAME_IEEE;
1119 }
1120
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001121 /*
1122 * Prepare the DCDB frame
1123 */
1124 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1125 pthru->cmd_status = 0x0;
1126 pthru->scsi_status = 0x0;
1127 pthru->target_id = device_id;
1128 pthru->lun = scp->device->lun;
1129 pthru->cdb_len = scp->cmd_len;
1130 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001131 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001132 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001133 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001134
1135 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1136
1137 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001138 * If the command is for the tape device, set the
1139 * pthru timeout to the os layer timeout value.
1140 */
1141 if (scp->device->type == TYPE_TAPE) {
1142 if ((scp->request->timeout / HZ) > 0xFFFF)
1143 pthru->timeout = 0xFFFF;
1144 else
1145 pthru->timeout = scp->request->timeout / HZ;
1146 }
1147
1148 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001149 * Construct SGL
1150 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001151 if (instance->flag_ieee == 1) {
1152 pthru->flags |= MFI_FRAME_SGL64;
1153 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1154 &pthru->sgl);
1155 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001156 pthru->flags |= MFI_FRAME_SGL64;
1157 pthru->sge_count = megasas_make_sgl64(instance, scp,
1158 &pthru->sgl);
1159 } else
1160 pthru->sge_count = megasas_make_sgl32(instance, scp,
1161 &pthru->sgl);
1162
Yang, Bobdc6fb82009-12-06 08:30:19 -07001163 if (pthru->sge_count > instance->max_num_sge) {
1164 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1165 pthru->sge_count);
1166 return 0;
1167 }
1168
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001169 /*
1170 * Sense info specific
1171 */
1172 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1173 pthru->sense_buf_phys_addr_hi = 0;
1174 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1175
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001176 /*
1177 * Compute the total number of frames this command consumes. FW uses
1178 * this number to pull sufficient number of frames from host memory.
1179 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001180 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001181 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001182
1183 return cmd->frame_count;
1184}
1185
1186/**
1187 * megasas_build_ldio - Prepares IOs to logical devices
1188 * @instance: Adapter soft state
1189 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001190 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001191 *
1192 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1193 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001194static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001195megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1196 struct megasas_cmd *cmd)
1197{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001198 u32 device_id;
1199 u8 sc = scp->cmnd[0];
1200 u16 flags = 0;
1201 struct megasas_io_frame *ldio;
1202
1203 device_id = MEGASAS_DEV_INDEX(instance, scp);
1204 ldio = (struct megasas_io_frame *)cmd->frame;
1205
1206 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1207 flags = MFI_FRAME_DIR_WRITE;
1208 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1209 flags = MFI_FRAME_DIR_READ;
1210
Yang, Bof4c9a132009-10-06 14:43:28 -06001211 if (instance->flag_ieee == 1) {
1212 flags |= MFI_FRAME_IEEE;
1213 }
1214
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001215 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001216 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001217 */
1218 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1219 ldio->cmd_status = 0x0;
1220 ldio->scsi_status = 0x0;
1221 ldio->target_id = device_id;
1222 ldio->timeout = 0;
1223 ldio->reserved_0 = 0;
1224 ldio->pad_0 = 0;
1225 ldio->flags = flags;
1226 ldio->start_lba_hi = 0;
1227 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1228
1229 /*
1230 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1231 */
1232 if (scp->cmd_len == 6) {
1233 ldio->lba_count = (u32) scp->cmnd[4];
1234 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1235 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1236
1237 ldio->start_lba_lo &= 0x1FFFFF;
1238 }
1239
1240 /*
1241 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1242 */
1243 else if (scp->cmd_len == 10) {
1244 ldio->lba_count = (u32) scp->cmnd[8] |
1245 ((u32) scp->cmnd[7] << 8);
1246 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1247 ((u32) scp->cmnd[3] << 16) |
1248 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1249 }
1250
1251 /*
1252 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1253 */
1254 else if (scp->cmd_len == 12) {
1255 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1256 ((u32) scp->cmnd[7] << 16) |
1257 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1258
1259 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1260 ((u32) scp->cmnd[3] << 16) |
1261 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1262 }
1263
1264 /*
1265 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1266 */
1267 else if (scp->cmd_len == 16) {
1268 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1269 ((u32) scp->cmnd[11] << 16) |
1270 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1271
1272 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1273 ((u32) scp->cmnd[7] << 16) |
1274 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1275
1276 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1277 ((u32) scp->cmnd[3] << 16) |
1278 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1279
1280 }
1281
1282 /*
1283 * Construct SGL
1284 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001285 if (instance->flag_ieee) {
1286 ldio->flags |= MFI_FRAME_SGL64;
1287 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1288 &ldio->sgl);
1289 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001290 ldio->flags |= MFI_FRAME_SGL64;
1291 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1292 } else
1293 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1294
Yang, Bobdc6fb82009-12-06 08:30:19 -07001295 if (ldio->sge_count > instance->max_num_sge) {
1296 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1297 ldio->sge_count);
1298 return 0;
1299 }
1300
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001301 /*
1302 * Sense info specific
1303 */
1304 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1305 ldio->sense_buf_phys_addr_hi = 0;
1306 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1307
Sumant Patrob1df99d2006-10-03 12:40:47 -07001308 /*
1309 * Compute the total number of frames this command consumes. FW uses
1310 * this number to pull sufficient number of frames from host memory.
1311 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001312 cmd->frame_count = megasas_get_frame_count(instance,
1313 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001314
1315 return cmd->frame_count;
1316}
1317
1318/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001319 * megasas_is_ldio - Checks if the cmd is for logical drive
1320 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001321 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001322 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001323 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001324 */
adam radford9c915a82010-12-21 13:34:31 -08001325inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001326{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001327 if (!MEGASAS_IS_LOGICAL(cmd))
1328 return 0;
1329 switch (cmd->cmnd[0]) {
1330 case READ_10:
1331 case WRITE_10:
1332 case READ_12:
1333 case WRITE_12:
1334 case READ_6:
1335 case WRITE_6:
1336 case READ_16:
1337 case WRITE_16:
1338 return 1;
1339 default:
1340 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001341 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001342}
1343
Sumant Patro658dced2006-10-03 13:09:14 -07001344 /**
1345 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1346 * in FW
1347 * @instance: Adapter soft state
1348 */
1349static inline void
1350megasas_dump_pending_frames(struct megasas_instance *instance)
1351{
1352 struct megasas_cmd *cmd;
1353 int i,n;
1354 union megasas_sgl *mfi_sgl;
1355 struct megasas_io_frame *ldio;
1356 struct megasas_pthru_frame *pthru;
1357 u32 sgcount;
1358 u32 max_cmd = instance->max_fw_cmds;
1359
1360 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1361 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1362 if (IS_DMA64)
1363 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1364 else
1365 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1366
1367 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1368 for (i = 0; i < max_cmd; i++) {
1369 cmd = instance->cmd_list[i];
1370 if(!cmd->scmd)
1371 continue;
1372 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1373 if (megasas_is_ldio(cmd->scmd)){
1374 ldio = (struct megasas_io_frame *)cmd->frame;
1375 mfi_sgl = &ldio->sgl;
1376 sgcount = ldio->sge_count;
1377 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);
1378 }
1379 else {
1380 pthru = (struct megasas_pthru_frame *) cmd->frame;
1381 mfi_sgl = &pthru->sgl;
1382 sgcount = pthru->sge_count;
1383 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);
1384 }
1385 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1386 for (n = 0; n < sgcount; n++){
1387 if (IS_DMA64)
1388 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) ;
1389 else
1390 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1391 }
1392 }
1393 printk(KERN_ERR "\n");
1394 } /*for max_cmd*/
1395 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1396 for (i = 0; i < max_cmd; i++) {
1397
1398 cmd = instance->cmd_list[i];
1399
1400 if(cmd->sync_cmd == 1){
1401 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1402 }
1403 }
1404 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1405}
1406
adam radfordcd50ba82010-12-21 10:23:23 -08001407u32
1408megasas_build_and_issue_cmd(struct megasas_instance *instance,
1409 struct scsi_cmnd *scmd)
1410{
1411 struct megasas_cmd *cmd;
1412 u32 frame_count;
1413
1414 cmd = megasas_get_cmd(instance);
1415 if (!cmd)
1416 return SCSI_MLQUEUE_HOST_BUSY;
1417
1418 /*
1419 * Logical drive command
1420 */
1421 if (megasas_is_ldio(scmd))
1422 frame_count = megasas_build_ldio(instance, scmd, cmd);
1423 else
1424 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1425
1426 if (!frame_count)
1427 goto out_return_cmd;
1428
1429 cmd->scmd = scmd;
1430 scmd->SCp.ptr = (char *)cmd;
1431
1432 /*
1433 * Issue the command to the FW
1434 */
1435 atomic_inc(&instance->fw_outstanding);
1436
1437 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1438 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001439
1440 return 0;
1441out_return_cmd:
1442 megasas_return_cmd(instance, cmd);
1443 return 1;
1444}
1445
1446
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001447/**
1448 * megasas_queue_command - Queue entry point
1449 * @scmd: SCSI command to be queued
1450 * @done: Callback entry point
1451 */
1452static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001453megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001454{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001455 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001456 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001457
1458 instance = (struct megasas_instance *)
1459 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001460
bo yang39a98552010-09-22 22:36:29 -04001461 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001462 return SCSI_MLQUEUE_HOST_BUSY;
1463
bo yang39a98552010-09-22 22:36:29 -04001464 spin_lock_irqsave(&instance->hba_lock, flags);
1465 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1466 spin_unlock_irqrestore(&instance->hba_lock, flags);
1467 return SCSI_MLQUEUE_HOST_BUSY;
1468 }
1469
1470 spin_unlock_irqrestore(&instance->hba_lock, flags);
1471
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001472 scmd->scsi_done = done;
1473 scmd->result = 0;
1474
Sumant Patrocb59aa62006-01-25 11:53:25 -08001475 if (MEGASAS_IS_LOGICAL(scmd) &&
1476 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1477 scmd->result = DID_BAD_TARGET << 16;
1478 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001479 }
1480
Sumant Patro02b01e02007-02-14 13:00:55 -08001481 switch (scmd->cmnd[0]) {
1482 case SYNCHRONIZE_CACHE:
1483 /*
1484 * FW takes care of flush cache on its own
1485 * No need to send it down
1486 */
1487 scmd->result = DID_OK << 16;
1488 goto out_done;
1489 default:
1490 break;
1491 }
1492
adam radfordcd50ba82010-12-21 10:23:23 -08001493 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1494 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001495 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001496 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001497
1498 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001499
Sumant Patrocb59aa62006-01-25 11:53:25 -08001500 out_done:
1501 done(scmd);
1502 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001503}
1504
Jeff Garzikf2812332010-11-16 02:10:29 -05001505static DEF_SCSI_QCMD(megasas_queue_command)
1506
Yang, Bo044833b2009-10-06 14:33:06 -06001507static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1508{
1509 int i;
1510
1511 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1512
1513 if ((megasas_mgmt_info.instance[i]) &&
1514 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1515 return megasas_mgmt_info.instance[i];
1516 }
1517
1518 return NULL;
1519}
1520
Christoph Hellwig147aab62006-02-17 12:13:48 +01001521static int megasas_slave_configure(struct scsi_device *sdev)
1522{
Yang, Bo044833b2009-10-06 14:33:06 -06001523 u16 pd_index = 0;
1524 struct megasas_instance *instance ;
1525
1526 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001527
1528 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001529 * Don't export physical disk devices to the disk driver.
1530 *
1531 * FIXME: Currently we don't export them to the midlayer at all.
1532 * That will be fixed once LSI engineers have audited the
1533 * firmware for possible issues.
1534 */
1535 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1536 sdev->type == TYPE_DISK) {
1537 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1538 sdev->id;
1539 if (instance->pd_list[pd_index].driveState ==
1540 MR_PD_STATE_SYSTEM) {
1541 blk_queue_rq_timeout(sdev->request_queue,
1542 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1543 return 0;
1544 }
1545 return -ENXIO;
1546 }
1547
1548 /*
1549 * The RAID firmware may require extended timeouts.
1550 */
1551 blk_queue_rq_timeout(sdev->request_queue,
1552 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1553 return 0;
1554}
1555
1556static int megasas_slave_alloc(struct scsi_device *sdev)
1557{
1558 u16 pd_index = 0;
1559 struct megasas_instance *instance ;
1560 instance = megasas_lookup_instance(sdev->host->host_no);
1561 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1562 (sdev->type == TYPE_DISK)) {
1563 /*
1564 * Open the OS scan to the SYSTEM PD
1565 */
1566 pd_index =
1567 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1568 sdev->id;
1569 if ((instance->pd_list[pd_index].driveState ==
1570 MR_PD_STATE_SYSTEM) &&
1571 (instance->pd_list[pd_index].driveType ==
1572 TYPE_DISK)) {
1573 return 0;
1574 }
1575 return -ENXIO;
1576 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001577 return 0;
1578}
1579
adam radford9c915a82010-12-21 13:34:31 -08001580void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001581{
1582 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001583 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
adam radford36807e62011-10-08 18:15:06 -07001584 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1585 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08001586 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001587 } else {
adam radford9c915a82010-12-21 13:34:31 -08001588 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1589 }
1590}
1591
1592 /**
1593 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1594 * restored to max value
1595 * @instance: Adapter soft state
1596 *
1597 */
1598void
1599megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1600{
1601 unsigned long flags;
1602 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001603 && time_after(jiffies, instance->last_time + 5 * HZ)
1604 && atomic_read(&instance->fw_outstanding) <
1605 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001606
1607 spin_lock_irqsave(instance->host->host_lock, flags);
1608 instance->flag &= ~MEGASAS_FW_BUSY;
1609 if ((instance->pdev->device ==
1610 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1611 (instance->pdev->device ==
1612 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1613 instance->host->can_queue =
1614 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1615 } else
1616 instance->host->can_queue =
1617 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1618
1619 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001620 }
1621}
1622
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001623/**
bo yang7343eb62007-11-09 04:35:44 -05001624 * megasas_complete_cmd_dpc - Returns FW's controller structure
1625 * @instance_addr: Address of adapter soft state
1626 *
1627 * Tasklet to complete cmds
1628 */
1629static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1630{
1631 u32 producer;
1632 u32 consumer;
1633 u32 context;
1634 struct megasas_cmd *cmd;
1635 struct megasas_instance *instance =
1636 (struct megasas_instance *)instance_addr;
1637 unsigned long flags;
1638
1639 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001640 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001641 return;
1642
1643 spin_lock_irqsave(&instance->completion_lock, flags);
1644
1645 producer = *instance->producer;
1646 consumer = *instance->consumer;
1647
1648 while (consumer != producer) {
1649 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001650 if (context >= instance->max_fw_cmds) {
1651 printk(KERN_ERR "Unexpected context value %x\n",
1652 context);
1653 BUG();
1654 }
bo yang7343eb62007-11-09 04:35:44 -05001655
1656 cmd = instance->cmd_list[context];
1657
1658 megasas_complete_cmd(instance, cmd, DID_OK);
1659
1660 consumer++;
1661 if (consumer == (instance->max_fw_cmds + 1)) {
1662 consumer = 0;
1663 }
1664 }
1665
1666 *instance->consumer = producer;
1667
1668 spin_unlock_irqrestore(&instance->completion_lock, flags);
1669
1670 /*
1671 * Check if we can restore can_queue
1672 */
adam radford9c915a82010-12-21 13:34:31 -08001673 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001674}
1675
Yang, Bo707e09b2010-10-12 07:20:27 -06001676static void
1677megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1678
1679static void
1680process_fw_state_change_wq(struct work_struct *work);
1681
1682void megasas_do_ocr(struct megasas_instance *instance)
1683{
1684 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1685 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1686 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1687 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1688 }
1689 instance->instancet->disable_intr(instance->reg_set);
1690 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1691 instance->issuepend_done = 0;
1692
1693 atomic_set(&instance->fw_outstanding, 0);
1694 megasas_internal_reset_defer_cmds(instance);
1695 process_fw_state_change_wq(&instance->work_init);
1696}
1697
bo yang7343eb62007-11-09 04:35:44 -05001698/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001699 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1700 * @instance: Adapter soft state
1701 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001702 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001703 * complete all its outstanding commands. Returns error if one or more IOs
1704 * are pending after this time period. It also marks the controller dead.
1705 */
1706static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1707{
1708 int i;
bo yang39a98552010-09-22 22:36:29 -04001709 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001710 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001711 u8 adprecovery;
1712 unsigned long flags;
1713 struct list_head clist_local;
1714 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001715 u32 fw_state;
1716 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001717
1718 spin_lock_irqsave(&instance->hba_lock, flags);
1719 adprecovery = instance->adprecovery;
1720 spin_unlock_irqrestore(&instance->hba_lock, flags);
1721
1722 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1723
1724 INIT_LIST_HEAD(&clist_local);
1725 spin_lock_irqsave(&instance->hba_lock, flags);
1726 list_splice_init(&instance->internal_reset_pending_q,
1727 &clist_local);
1728 spin_unlock_irqrestore(&instance->hba_lock, flags);
1729
1730 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1731 for (i = 0; i < wait_time; i++) {
1732 msleep(1000);
1733 spin_lock_irqsave(&instance->hba_lock, flags);
1734 adprecovery = instance->adprecovery;
1735 spin_unlock_irqrestore(&instance->hba_lock, flags);
1736 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1737 break;
1738 }
1739
1740 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1741 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1742 spin_lock_irqsave(&instance->hba_lock, flags);
1743 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1744 spin_unlock_irqrestore(&instance->hba_lock, flags);
1745 return FAILED;
1746 }
1747
1748 reset_index = 0;
1749 while (!list_empty(&clist_local)) {
1750 reset_cmd = list_entry((&clist_local)->next,
1751 struct megasas_cmd, list);
1752 list_del_init(&reset_cmd->list);
1753 if (reset_cmd->scmd) {
1754 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001755 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001756 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001757 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001758
1759 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1760 megasas_return_cmd(instance, reset_cmd);
1761 } else if (reset_cmd->sync_cmd) {
1762 printk(KERN_NOTICE "megasas:%p synch cmds"
1763 "reset queue\n",
1764 reset_cmd);
1765
1766 reset_cmd->cmd_status = ENODATA;
1767 instance->instancet->fire_cmd(instance,
1768 reset_cmd->frame_phys_addr,
1769 0, instance->reg_set);
1770 } else {
1771 printk(KERN_NOTICE "megasas: %p unexpected"
1772 "cmds lst\n",
1773 reset_cmd);
1774 }
1775 reset_index++;
1776 }
1777
1778 return SUCCESS;
1779 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001780
1781 for (i = 0; i < wait_time; i++) {
1782
Sumant Patroe4a082c2006-05-30 12:03:37 -07001783 int outstanding = atomic_read(&instance->fw_outstanding);
1784
1785 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001786 break;
1787
1788 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1789 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001790 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001791 /*
1792 * Call cmd completion routine. Cmd to be
1793 * be completed directly without depending on isr.
1794 */
1795 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001796 }
1797
1798 msleep(1000);
1799 }
1800
Yang, Bo707e09b2010-10-12 07:20:27 -06001801 i = 0;
1802 kill_adapter_flag = 0;
1803 do {
1804 fw_state = instance->instancet->read_fw_status_reg(
1805 instance->reg_set) & MFI_STATE_MASK;
1806 if ((fw_state == MFI_STATE_FAULT) &&
1807 (instance->disableOnlineCtrlReset == 0)) {
1808 if (i == 3) {
1809 kill_adapter_flag = 2;
1810 break;
1811 }
1812 megasas_do_ocr(instance);
1813 kill_adapter_flag = 1;
1814
1815 /* wait for 1 secs to let FW finish the pending cmds */
1816 msleep(1000);
1817 }
1818 i++;
1819 } while (i <= 3);
1820
1821 if (atomic_read(&instance->fw_outstanding) &&
1822 !kill_adapter_flag) {
1823 if (instance->disableOnlineCtrlReset == 0) {
1824
1825 megasas_do_ocr(instance);
1826
1827 /* wait for 5 secs to let FW finish the pending cmds */
1828 for (i = 0; i < wait_time; i++) {
1829 int outstanding =
1830 atomic_read(&instance->fw_outstanding);
1831 if (!outstanding)
1832 return SUCCESS;
1833 msleep(1000);
1834 }
1835 }
1836 }
1837
1838 if (atomic_read(&instance->fw_outstanding) ||
1839 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001840 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001841 /*
1842 * Send signal to FW to stop processing any pending cmds.
1843 * The controller will be taken offline by the OS now.
1844 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001845 if ((instance->pdev->device ==
1846 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1847 (instance->pdev->device ==
1848 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1849 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001850 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001851 } else {
1852 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001853 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001854 }
Sumant Patro658dced2006-10-03 13:09:14 -07001855 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001856 spin_lock_irqsave(&instance->hba_lock, flags);
1857 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1858 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001859 return FAILED;
1860 }
1861
bo yang39a98552010-09-22 22:36:29 -04001862 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1863
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001864 return SUCCESS;
1865}
1866
1867/**
1868 * megasas_generic_reset - Generic reset routine
1869 * @scmd: Mid-layer SCSI command
1870 *
1871 * This routine implements a generic reset handler for device, bus and host
1872 * reset requests. Device, bus and host specific reset handlers can use this
1873 * function after they do their specific tasks.
1874 */
1875static int megasas_generic_reset(struct scsi_cmnd *scmd)
1876{
1877 int ret_val;
1878 struct megasas_instance *instance;
1879
1880 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1881
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001882 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1883 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001884
bo yang39a98552010-09-22 22:36:29 -04001885 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001886 printk(KERN_ERR "megasas: cannot recover from previous reset "
1887 "failures\n");
1888 return FAILED;
1889 }
1890
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001891 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001892 if (ret_val == SUCCESS)
1893 printk(KERN_NOTICE "megasas: reset successful \n");
1894 else
1895 printk(KERN_ERR "megasas: failed to do reset\n");
1896
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001897 return ret_val;
1898}
1899
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001900/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001901 * megasas_reset_timer - quiesce the adapter if required
1902 * @scmd: scsi cmnd
1903 *
1904 * Sets the FW busy flag and reduces the host->can_queue if the
1905 * cmd has not been completed within the timeout period.
1906 */
1907static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001908blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001909{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001910 struct megasas_instance *instance;
1911 unsigned long flags;
1912
1913 if (time_after(jiffies, scmd->jiffies_at_alloc +
1914 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001915 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001916 }
1917
adam radfordf575c5d2011-10-13 16:01:12 -07001918 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001919 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1920 /* FW is busy, throttle IO */
1921 spin_lock_irqsave(instance->host->host_lock, flags);
1922
adam radfordc5daa6a2012-07-17 18:20:03 -07001923 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001924 instance->last_time = jiffies;
1925 instance->flag |= MEGASAS_FW_BUSY;
1926
1927 spin_unlock_irqrestore(instance->host->host_lock, flags);
1928 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001929 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001930}
1931
1932/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001933 * megasas_reset_device - Device reset handler entry point
1934 */
1935static int megasas_reset_device(struct scsi_cmnd *scmd)
1936{
1937 int ret;
1938
1939 /*
1940 * First wait for all commands to complete
1941 */
1942 ret = megasas_generic_reset(scmd);
1943
1944 return ret;
1945}
1946
1947/**
1948 * megasas_reset_bus_host - Bus & host reset handler entry point
1949 */
1950static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1951{
1952 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001953 struct megasas_instance *instance;
1954 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001955
1956 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001957 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001958 */
adam radford36807e62011-10-08 18:15:06 -07001959 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1960 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08001961 ret = megasas_reset_fusion(scmd->device->host);
1962 else
1963 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001964
1965 return ret;
1966}
1967
1968/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001969 * megasas_bios_param - Returns disk geometry for a disk
1970 * @sdev: device handle
1971 * @bdev: block device
1972 * @capacity: drive capacity
1973 * @geom: geometry parameters
1974 */
1975static int
1976megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1977 sector_t capacity, int geom[])
1978{
1979 int heads;
1980 int sectors;
1981 sector_t cylinders;
1982 unsigned long tmp;
1983 /* Default heads (64) & sectors (32) */
1984 heads = 64;
1985 sectors = 32;
1986
1987 tmp = heads * sectors;
1988 cylinders = capacity;
1989
1990 sector_div(cylinders, tmp);
1991
1992 /*
1993 * Handle extended translation size for logical drives > 1Gb
1994 */
1995
1996 if (capacity >= 0x200000) {
1997 heads = 255;
1998 sectors = 63;
1999 tmp = heads*sectors;
2000 cylinders = capacity;
2001 sector_div(cylinders, tmp);
2002 }
2003
2004 geom[0] = heads;
2005 geom[1] = sectors;
2006 geom[2] = cylinders;
2007
2008 return 0;
2009}
2010
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002011static void megasas_aen_polling(struct work_struct *work);
2012
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002013/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002014 * megasas_service_aen - Processes an event notification
2015 * @instance: Adapter soft state
2016 * @cmd: AEN command completed by the ISR
2017 *
2018 * For AEN, driver sends a command down to FW that is held by the FW till an
2019 * event occurs. When an event of interest occurs, FW completes the command
2020 * that it was previously holding.
2021 *
2022 * This routines sends SIGIO signal to processes that have registered with the
2023 * driver for AEN.
2024 */
2025static void
2026megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2027{
Yang, Boc3518832009-10-06 14:18:02 -06002028 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002029 /*
2030 * Don't signal app if it is just an aborted previously registered aen
2031 */
Yang, Boc3518832009-10-06 14:18:02 -06002032 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2033 spin_lock_irqsave(&poll_aen_lock, flags);
2034 megasas_poll_wait_aen = 1;
2035 spin_unlock_irqrestore(&poll_aen_lock, flags);
2036 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002037 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002038 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002039 else
2040 cmd->abort_aen = 0;
2041
2042 instance->aen_cmd = NULL;
2043 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002044
bo yang39a98552010-09-22 22:36:29 -04002045 if ((instance->unload == 0) &&
2046 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002047 struct megasas_aen_event *ev;
2048 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2049 if (!ev) {
2050 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2051 } else {
2052 ev->instance = instance;
2053 instance->ev = ev;
2054 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2055 schedule_delayed_work(
2056 (struct delayed_work *)&ev->hotplug_work, 0);
2057 }
2058 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002059}
2060
adam radford4bcde502011-07-26 15:42:52 -07002061static int megasas_change_queue_depth(struct scsi_device *sdev,
2062 int queue_depth, int reason)
2063{
2064 if (reason != SCSI_QDEPTH_DEFAULT)
2065 return -EOPNOTSUPP;
2066
2067 if (queue_depth > sdev->host->can_queue)
2068 queue_depth = sdev->host->can_queue;
2069 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2070 queue_depth);
2071
2072 return queue_depth;
2073}
2074
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002075/*
2076 * Scsi host template for megaraid_sas driver
2077 */
2078static struct scsi_host_template megasas_template = {
2079
2080 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002081 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002082 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002083 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002084 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002085 .queuecommand = megasas_queue_command,
2086 .eh_device_reset_handler = megasas_reset_device,
2087 .eh_bus_reset_handler = megasas_reset_bus_host,
2088 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002089 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002090 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002091 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002092 .change_queue_depth = megasas_change_queue_depth,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002093};
2094
2095/**
2096 * megasas_complete_int_cmd - Completes an internal command
2097 * @instance: Adapter soft state
2098 * @cmd: Command to be completed
2099 *
2100 * The megasas_issue_blocked_cmd() function waits for a command to complete
2101 * after it issues a command. This function wakes up that waiting routine by
2102 * calling wake_up() on the wait queue.
2103 */
2104static void
2105megasas_complete_int_cmd(struct megasas_instance *instance,
2106 struct megasas_cmd *cmd)
2107{
2108 cmd->cmd_status = cmd->frame->io.cmd_status;
2109
2110 if (cmd->cmd_status == ENODATA) {
2111 cmd->cmd_status = 0;
2112 }
2113 wake_up(&instance->int_cmd_wait_q);
2114}
2115
2116/**
2117 * megasas_complete_abort - Completes aborting a command
2118 * @instance: Adapter soft state
2119 * @cmd: Cmd that was issued to abort another cmd
2120 *
adam radford0d490162010-12-14 19:17:17 -08002121 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2122 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002123 * wakes up all functions waiting on the same wait queue.
2124 */
2125static void
2126megasas_complete_abort(struct megasas_instance *instance,
2127 struct megasas_cmd *cmd)
2128{
2129 if (cmd->sync_cmd) {
2130 cmd->sync_cmd = 0;
2131 cmd->cmd_status = 0;
2132 wake_up(&instance->abort_cmd_wait_q);
2133 }
2134
2135 return;
2136}
2137
2138/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002139 * megasas_complete_cmd - Completes a command
2140 * @instance: Adapter soft state
2141 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002142 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002143 * SCSI mid-layer instead of the value returned
2144 * by the FW. This should be used if caller wants
2145 * an alternate status (as in the case of aborted
2146 * commands)
2147 */
adam radford9c915a82010-12-21 13:34:31 -08002148void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002149megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2150 u8 alt_status)
2151{
2152 int exception = 0;
2153 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002154 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002155 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002156
bo yang39a98552010-09-22 22:36:29 -04002157 /* flag for the retry reset */
2158 cmd->retry_for_fw_reset = 0;
2159
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002160 if (cmd->scmd)
2161 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002162
2163 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002164 case MFI_CMD_INVALID:
2165 /* Some older 1068 controller FW may keep a pended
2166 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2167 when booting the kdump kernel. Ignore this command to
2168 prevent a kernel panic on shutdown of the kdump kernel. */
2169 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2170 "completed.\n");
2171 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2172 "other than PERC5, please upgrade your firmware.\n");
2173 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002174 case MFI_CMD_PD_SCSI_IO:
2175 case MFI_CMD_LD_SCSI_IO:
2176
2177 /*
2178 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2179 * issued either through an IO path or an IOCTL path. If it
2180 * was via IOCTL, we will send it to internal completion.
2181 */
2182 if (cmd->sync_cmd) {
2183 cmd->sync_cmd = 0;
2184 megasas_complete_int_cmd(instance, cmd);
2185 break;
2186 }
2187
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002188 case MFI_CMD_LD_READ:
2189 case MFI_CMD_LD_WRITE:
2190
2191 if (alt_status) {
2192 cmd->scmd->result = alt_status << 16;
2193 exception = 1;
2194 }
2195
2196 if (exception) {
2197
Sumant Patroe4a082c2006-05-30 12:03:37 -07002198 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002199
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002200 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002201 cmd->scmd->scsi_done(cmd->scmd);
2202 megasas_return_cmd(instance, cmd);
2203
2204 break;
2205 }
2206
2207 switch (hdr->cmd_status) {
2208
2209 case MFI_STAT_OK:
2210 cmd->scmd->result = DID_OK << 16;
2211 break;
2212
2213 case MFI_STAT_SCSI_IO_FAILED:
2214 case MFI_STAT_LD_INIT_IN_PROGRESS:
2215 cmd->scmd->result =
2216 (DID_ERROR << 16) | hdr->scsi_status;
2217 break;
2218
2219 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2220
2221 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2222
2223 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2224 memset(cmd->scmd->sense_buffer, 0,
2225 SCSI_SENSE_BUFFERSIZE);
2226 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2227 hdr->sense_len);
2228
2229 cmd->scmd->result |= DRIVER_SENSE << 24;
2230 }
2231
2232 break;
2233
2234 case MFI_STAT_LD_OFFLINE:
2235 case MFI_STAT_DEVICE_NOT_FOUND:
2236 cmd->scmd->result = DID_BAD_TARGET << 16;
2237 break;
2238
2239 default:
2240 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2241 hdr->cmd_status);
2242 cmd->scmd->result = DID_ERROR << 16;
2243 break;
2244 }
2245
Sumant Patroe4a082c2006-05-30 12:03:37 -07002246 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002247
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002248 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002249 cmd->scmd->scsi_done(cmd->scmd);
2250 megasas_return_cmd(instance, cmd);
2251
2252 break;
2253
2254 case MFI_CMD_SMP:
2255 case MFI_CMD_STP:
2256 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002257 /* Check for LD map update */
2258 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2259 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2260 spin_lock_irqsave(instance->host->host_lock, flags);
2261 if (cmd->frame->hdr.cmd_status != 0) {
2262 if (cmd->frame->hdr.cmd_status !=
2263 MFI_STAT_NOT_FOUND)
2264 printk(KERN_WARNING "megasas: map sync"
2265 "failed, status = 0x%x.\n",
2266 cmd->frame->hdr.cmd_status);
2267 else {
2268 megasas_return_cmd(instance, cmd);
2269 spin_unlock_irqrestore(
2270 instance->host->host_lock,
2271 flags);
2272 break;
2273 }
2274 } else
2275 instance->map_id++;
2276 megasas_return_cmd(instance, cmd);
2277 if (MR_ValidateMapInfo(
2278 fusion->ld_map[(instance->map_id & 1)],
2279 fusion->load_balance_info))
2280 fusion->fast_path_io = 1;
2281 else
2282 fusion->fast_path_io = 0;
2283 megasas_sync_map_info(instance);
2284 spin_unlock_irqrestore(instance->host->host_lock,
2285 flags);
2286 break;
2287 }
Yang, Boc3518832009-10-06 14:18:02 -06002288 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2289 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2290 spin_lock_irqsave(&poll_aen_lock, flags);
2291 megasas_poll_wait_aen = 0;
2292 spin_unlock_irqrestore(&poll_aen_lock, flags);
2293 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002294
2295 /*
2296 * See if got an event notification
2297 */
2298 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2299 megasas_service_aen(instance, cmd);
2300 else
2301 megasas_complete_int_cmd(instance, cmd);
2302
2303 break;
2304
2305 case MFI_CMD_ABORT:
2306 /*
2307 * Cmd issued to abort another cmd returned
2308 */
2309 megasas_complete_abort(instance, cmd);
2310 break;
2311
2312 default:
2313 printk("megasas: Unknown command completed! [0x%X]\n",
2314 hdr->cmd);
2315 break;
2316 }
2317}
2318
2319/**
bo yang39a98552010-09-22 22:36:29 -04002320 * megasas_issue_pending_cmds_again - issue all pending cmds
2321 * in FW again because of the fw reset
2322 * @instance: Adapter soft state
2323 */
2324static inline void
2325megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2326{
2327 struct megasas_cmd *cmd;
2328 struct list_head clist_local;
2329 union megasas_evt_class_locale class_locale;
2330 unsigned long flags;
2331 u32 seq_num;
2332
2333 INIT_LIST_HEAD(&clist_local);
2334 spin_lock_irqsave(&instance->hba_lock, flags);
2335 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2336 spin_unlock_irqrestore(&instance->hba_lock, flags);
2337
2338 while (!list_empty(&clist_local)) {
2339 cmd = list_entry((&clist_local)->next,
2340 struct megasas_cmd, list);
2341 list_del_init(&cmd->list);
2342
2343 if (cmd->sync_cmd || cmd->scmd) {
2344 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2345 "detected to be pending while HBA reset.\n",
2346 cmd, cmd->scmd, cmd->sync_cmd);
2347
2348 cmd->retry_for_fw_reset++;
2349
2350 if (cmd->retry_for_fw_reset == 3) {
2351 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2352 "was tried multiple times during reset."
2353 "Shutting down the HBA\n",
2354 cmd, cmd->scmd, cmd->sync_cmd);
2355 megaraid_sas_kill_hba(instance);
2356
2357 instance->adprecovery =
2358 MEGASAS_HW_CRITICAL_ERROR;
2359 return;
2360 }
2361 }
2362
2363 if (cmd->sync_cmd == 1) {
2364 if (cmd->scmd) {
2365 printk(KERN_NOTICE "megaraid_sas: unexpected"
2366 "cmd attached to internal command!\n");
2367 }
2368 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2369 "on the internal reset queue,"
2370 "issue it again.\n", cmd);
2371 cmd->cmd_status = ENODATA;
2372 instance->instancet->fire_cmd(instance,
2373 cmd->frame_phys_addr ,
2374 0, instance->reg_set);
2375 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002376 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002377 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002378 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002379
2380 atomic_inc(&instance->fw_outstanding);
2381 instance->instancet->fire_cmd(instance,
2382 cmd->frame_phys_addr,
2383 cmd->frame_count-1, instance->reg_set);
2384 } else {
2385 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2386 "internal reset defer list while re-issue!!\n",
2387 cmd);
2388 }
2389 }
2390
2391 if (instance->aen_cmd) {
2392 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2393 megasas_return_cmd(instance, instance->aen_cmd);
2394
2395 instance->aen_cmd = NULL;
2396 }
2397
2398 /*
2399 * Initiate AEN (Asynchronous Event Notification)
2400 */
2401 seq_num = instance->last_seq_num;
2402 class_locale.members.reserved = 0;
2403 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2404 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2405
2406 megasas_register_aen(instance, seq_num, class_locale.word);
2407}
2408
2409/**
2410 * Move the internal reset pending commands to a deferred queue.
2411 *
2412 * We move the commands pending at internal reset time to a
2413 * pending queue. This queue would be flushed after successful
2414 * completion of the internal reset sequence. if the internal reset
2415 * did not complete in time, the kernel reset handler would flush
2416 * these commands.
2417 **/
2418static void
2419megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2420{
2421 struct megasas_cmd *cmd;
2422 int i;
2423 u32 max_cmd = instance->max_fw_cmds;
2424 u32 defer_index;
2425 unsigned long flags;
2426
2427 defer_index = 0;
2428 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2429 for (i = 0; i < max_cmd; i++) {
2430 cmd = instance->cmd_list[i];
2431 if (cmd->sync_cmd == 1 || cmd->scmd) {
2432 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2433 "on the defer queue as internal\n",
2434 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2435
2436 if (!list_empty(&cmd->list)) {
2437 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2438 " moving this cmd:%p, %d %p, it was"
2439 "discovered on some list?\n",
2440 cmd, cmd->sync_cmd, cmd->scmd);
2441
2442 list_del_init(&cmd->list);
2443 }
2444 defer_index++;
2445 list_add_tail(&cmd->list,
2446 &instance->internal_reset_pending_q);
2447 }
2448 }
2449 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2450}
2451
2452
2453static void
2454process_fw_state_change_wq(struct work_struct *work)
2455{
2456 struct megasas_instance *instance =
2457 container_of(work, struct megasas_instance, work_init);
2458 u32 wait;
2459 unsigned long flags;
2460
2461 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2462 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2463 instance->adprecovery);
2464 return ;
2465 }
2466
2467 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2468 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2469 "state, restarting it...\n");
2470
2471 instance->instancet->disable_intr(instance->reg_set);
2472 atomic_set(&instance->fw_outstanding, 0);
2473
2474 atomic_set(&instance->fw_reset_no_pci_access, 1);
2475 instance->instancet->adp_reset(instance, instance->reg_set);
2476 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2477
2478 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2479 "initiating next stage...\n");
2480
2481 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2482 "state 2 starting...\n");
2483
2484 /*waitting for about 20 second before start the second init*/
2485 for (wait = 0; wait < 30; wait++) {
2486 msleep(1000);
2487 }
2488
adam radford058a8fa2011-10-08 18:14:27 -07002489 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04002490 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2491
2492 megaraid_sas_kill_hba(instance);
2493 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2494 return ;
2495 }
2496
2497 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2498 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2499 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2500 ) {
2501 *instance->consumer = *instance->producer;
2502 } else {
2503 *instance->consumer = 0;
2504 *instance->producer = 0;
2505 }
2506
2507 megasas_issue_init_mfi(instance);
2508
2509 spin_lock_irqsave(&instance->hba_lock, flags);
2510 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2511 spin_unlock_irqrestore(&instance->hba_lock, flags);
2512 instance->instancet->enable_intr(instance->reg_set);
2513
2514 megasas_issue_pending_cmds_again(instance);
2515 instance->issuepend_done = 1;
2516 }
2517 return ;
2518}
2519
2520/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002521 * megasas_deplete_reply_queue - Processes all completed commands
2522 * @instance: Adapter soft state
2523 * @alt_status: Alternate status to be returned to
2524 * SCSI mid-layer instead of the status
2525 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002526 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002527 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002528static int
bo yang39a98552010-09-22 22:36:29 -04002529megasas_deplete_reply_queue(struct megasas_instance *instance,
2530 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002531{
bo yang39a98552010-09-22 22:36:29 -04002532 u32 mfiStatus;
2533 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002534
bo yang39a98552010-09-22 22:36:29 -04002535 if ((mfiStatus = instance->instancet->check_reset(instance,
2536 instance->reg_set)) == 1) {
2537 return IRQ_HANDLED;
2538 }
2539
2540 if ((mfiStatus = instance->instancet->clear_intr(
2541 instance->reg_set)
2542 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002543 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07002544 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08002545 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002546 }
2547
2548 instance->mfiStatus = mfiStatus;
2549
2550 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2551 fw_state = instance->instancet->read_fw_status_reg(
2552 instance->reg_set) & MFI_STATE_MASK;
2553
2554 if (fw_state != MFI_STATE_FAULT) {
2555 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2556 fw_state);
2557 }
2558
2559 if ((fw_state == MFI_STATE_FAULT) &&
2560 (instance->disableOnlineCtrlReset == 0)) {
2561 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2562
2563 if ((instance->pdev->device ==
2564 PCI_DEVICE_ID_LSI_SAS1064R) ||
2565 (instance->pdev->device ==
2566 PCI_DEVICE_ID_DELL_PERC5) ||
2567 (instance->pdev->device ==
2568 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2569
2570 *instance->consumer =
2571 MEGASAS_ADPRESET_INPROG_SIGN;
2572 }
2573
2574
2575 instance->instancet->disable_intr(instance->reg_set);
2576 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2577 instance->issuepend_done = 0;
2578
2579 atomic_set(&instance->fw_outstanding, 0);
2580 megasas_internal_reset_defer_cmds(instance);
2581
2582 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2583 fw_state, instance->adprecovery);
2584
2585 schedule_work(&instance->work_init);
2586 return IRQ_HANDLED;
2587
2588 } else {
2589 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2590 fw_state, instance->disableOnlineCtrlReset);
2591 }
2592 }
2593
Sumant Patro5d018ad2006-10-03 13:13:18 -07002594 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002595 return IRQ_HANDLED;
2596}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002597/**
2598 * megasas_isr - isr entry point
2599 */
David Howells7d12e782006-10-05 14:55:46 +01002600static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002601{
adam radfordc8e858f2011-10-08 18:15:13 -07002602 struct megasas_irq_context *irq_context = devp;
2603 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04002604 unsigned long flags;
2605 irqreturn_t rc;
2606
adam radfordc8e858f2011-10-08 18:15:13 -07002607 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04002608 return IRQ_HANDLED;
2609
bo yang39a98552010-09-22 22:36:29 -04002610 spin_lock_irqsave(&instance->hba_lock, flags);
2611 rc = megasas_deplete_reply_queue(instance, DID_OK);
2612 spin_unlock_irqrestore(&instance->hba_lock, flags);
2613
2614 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002615}
2616
2617/**
2618 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002619 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002620 *
2621 * During the initialization, FW passes can potentially be in any one of
2622 * several possible states. If the FW in operational, waiting-for-handshake
2623 * states, driver must take steps to bring it to ready state. Otherwise, it
2624 * has to wait for the ready state.
2625 */
adam radford9c915a82010-12-21 13:34:31 -08002626int
adam radford058a8fa2011-10-08 18:14:27 -07002627megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002628{
2629 int i;
2630 u8 max_wait;
2631 u32 fw_state;
2632 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002633 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002634
Sumant Patro1341c932006-01-25 12:02:40 -08002635 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002636
Sumant Patroe3bbff92006-10-03 12:28:49 -07002637 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002638 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2639 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002640
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002641 while (fw_state != MFI_STATE_READY) {
2642
Yang, Bo7218df62009-10-06 14:52:20 -06002643 abs_state =
2644 instance->instancet->read_fw_status_reg(instance->reg_set);
2645
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002646 switch (fw_state) {
2647
2648 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002649 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07002650 if (ocr) {
2651 max_wait = MEGASAS_RESET_WAIT_TIME;
2652 cur_state = MFI_STATE_FAULT;
2653 break;
2654 } else
2655 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002656
2657 case MFI_STATE_WAIT_HANDSHAKE:
2658 /*
2659 * Set the CLR bit in inbound doorbell
2660 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002661 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002662 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2663 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002664 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2665 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002666 PCI_DEVICE_ID_LSI_FUSION) ||
2667 (instance->pdev->device ==
2668 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002669 writel(
2670 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002671 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002672 } else {
2673 writel(
2674 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2675 &instance->reg_set->inbound_doorbell);
2676 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002677
Yang, Bo7218df62009-10-06 14:52:20 -06002678 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002679 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2680 break;
2681
Sumant Patroe3bbff92006-10-03 12:28:49 -07002682 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002683 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002684 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2685 (instance->pdev->device ==
2686 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2687 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002688 PCI_DEVICE_ID_LSI_FUSION) ||
2689 (instance->pdev->device ==
2690 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002691 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002692 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002693 } else
2694 writel(MFI_INIT_HOTPLUG,
2695 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002696
Yang, Bo7218df62009-10-06 14:52:20 -06002697 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002698 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2699 break;
2700
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002701 case MFI_STATE_OPERATIONAL:
2702 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002703 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002704 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002705 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002706 if ((instance->pdev->device ==
2707 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2708 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002709 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2710 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07002711 == PCI_DEVICE_ID_LSI_FUSION) ||
2712 (instance->pdev->device
2713 == PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002714 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002715 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07002716 if ((instance->pdev->device ==
2717 PCI_DEVICE_ID_LSI_FUSION) ||
2718 (instance->pdev->device ==
2719 PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08002720 for (i = 0; i < (10 * 1000); i += 20) {
2721 if (readl(
2722 &instance->
2723 reg_set->
2724 doorbell) & 1)
2725 msleep(20);
2726 else
2727 break;
2728 }
2729 }
Yang, Bo87911122009-10-06 14:31:54 -06002730 } else
2731 writel(MFI_RESET_FLAGS,
2732 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002733
Yang, Bo7218df62009-10-06 14:52:20 -06002734 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002735 cur_state = MFI_STATE_OPERATIONAL;
2736 break;
2737
2738 case MFI_STATE_UNDEFINED:
2739 /*
2740 * This state should not last for more than 2 seconds
2741 */
Yang, Bo7218df62009-10-06 14:52:20 -06002742 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002743 cur_state = MFI_STATE_UNDEFINED;
2744 break;
2745
2746 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002747 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002748 cur_state = MFI_STATE_BB_INIT;
2749 break;
2750
2751 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002752 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002753 cur_state = MFI_STATE_FW_INIT;
2754 break;
2755
2756 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002757 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002758 cur_state = MFI_STATE_FW_INIT_2;
2759 break;
2760
2761 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002762 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002763 cur_state = MFI_STATE_DEVICE_SCAN;
2764 break;
2765
2766 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002767 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002768 cur_state = MFI_STATE_FLUSH_CACHE;
2769 break;
2770
2771 default:
2772 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2773 fw_state);
2774 return -ENODEV;
2775 }
2776
2777 /*
2778 * The cur_state should not last for more than max_wait secs
2779 */
2780 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002781 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002782 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002783 curr_abs_state =
2784 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002785
Yang, Bo7218df62009-10-06 14:52:20 -06002786 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002787 msleep(1);
2788 } else
2789 break;
2790 }
2791
2792 /*
2793 * Return error if fw_state hasn't changed after max_wait
2794 */
Yang, Bo7218df62009-10-06 14:52:20 -06002795 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002796 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2797 "in %d secs\n", fw_state, max_wait);
2798 return -ENODEV;
2799 }
bo yang39a98552010-09-22 22:36:29 -04002800 }
adam radford0d490162010-12-14 19:17:17 -08002801 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002802
2803 return 0;
2804}
2805
2806/**
2807 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2808 * @instance: Adapter soft state
2809 */
2810static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2811{
2812 int i;
adam radford9c915a82010-12-21 13:34:31 -08002813 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002814 struct megasas_cmd *cmd;
2815
2816 if (!instance->frame_dma_pool)
2817 return;
2818
2819 /*
2820 * Return all frames to pool
2821 */
2822 for (i = 0; i < max_cmd; i++) {
2823
2824 cmd = instance->cmd_list[i];
2825
2826 if (cmd->frame)
2827 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2828 cmd->frame_phys_addr);
2829
2830 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002831 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002832 cmd->sense_phys_addr);
2833 }
2834
2835 /*
2836 * Now destroy the pool itself
2837 */
2838 pci_pool_destroy(instance->frame_dma_pool);
2839 pci_pool_destroy(instance->sense_dma_pool);
2840
2841 instance->frame_dma_pool = NULL;
2842 instance->sense_dma_pool = NULL;
2843}
2844
2845/**
2846 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2847 * @instance: Adapter soft state
2848 *
2849 * Each command packet has an embedded DMA memory buffer that is used for
2850 * filling MFI frame and the SG list that immediately follows the frame. This
2851 * function creates those DMA memory buffers for each command packet by using
2852 * PCI pool facility.
2853 */
2854static int megasas_create_frame_pool(struct megasas_instance *instance)
2855{
2856 int i;
2857 u32 max_cmd;
2858 u32 sge_sz;
2859 u32 sgl_sz;
2860 u32 total_sz;
2861 u32 frame_count;
2862 struct megasas_cmd *cmd;
2863
adam radford9c915a82010-12-21 13:34:31 -08002864 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002865
2866 /*
2867 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2868 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2869 */
2870 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2871 sizeof(struct megasas_sge32);
2872
Yang, Bof4c9a132009-10-06 14:43:28 -06002873 if (instance->flag_ieee) {
2874 sge_sz = sizeof(struct megasas_sge_skinny);
2875 }
2876
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002877 /*
2878 * Calculated the number of 64byte frames required for SGL
2879 */
2880 sgl_sz = sge_sz * instance->max_num_sge;
2881 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002882 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002883
2884 /*
2885 * We need one extra frame for the MFI command
2886 */
2887 frame_count++;
2888
2889 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2890 /*
2891 * Use DMA pool facility provided by PCI layer
2892 */
2893 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2894 instance->pdev, total_sz, 64,
2895 0);
2896
2897 if (!instance->frame_dma_pool) {
2898 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2899 return -ENOMEM;
2900 }
2901
2902 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2903 instance->pdev, 128, 4, 0);
2904
2905 if (!instance->sense_dma_pool) {
2906 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2907
2908 pci_pool_destroy(instance->frame_dma_pool);
2909 instance->frame_dma_pool = NULL;
2910
2911 return -ENOMEM;
2912 }
2913
2914 /*
2915 * Allocate and attach a frame to each of the commands in cmd_list.
2916 * By making cmd->index as the context instead of the &cmd, we can
2917 * always use 32bit context regardless of the architecture
2918 */
2919 for (i = 0; i < max_cmd; i++) {
2920
2921 cmd = instance->cmd_list[i];
2922
2923 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2924 GFP_KERNEL, &cmd->frame_phys_addr);
2925
2926 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2927 GFP_KERNEL, &cmd->sense_phys_addr);
2928
2929 /*
2930 * megasas_teardown_frame_pool() takes care of freeing
2931 * whatever has been allocated
2932 */
2933 if (!cmd->frame || !cmd->sense) {
2934 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2935 megasas_teardown_frame_pool(instance);
2936 return -ENOMEM;
2937 }
2938
Yang, Bo707e09b2010-10-12 07:20:27 -06002939 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002940 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002941 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07002942 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2943 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2944 (reset_devices))
2945 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002946 }
2947
2948 return 0;
2949}
2950
2951/**
2952 * megasas_free_cmds - Free all the cmds in the free cmd pool
2953 * @instance: Adapter soft state
2954 */
adam radford9c915a82010-12-21 13:34:31 -08002955void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002956{
2957 int i;
2958 /* First free the MFI frame pool */
2959 megasas_teardown_frame_pool(instance);
2960
2961 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002962 for (i = 0; i < instance->max_mfi_cmds; i++)
2963
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002964 kfree(instance->cmd_list[i]);
2965
2966 /* Free the cmd_list buffer itself */
2967 kfree(instance->cmd_list);
2968 instance->cmd_list = NULL;
2969
2970 INIT_LIST_HEAD(&instance->cmd_pool);
2971}
2972
2973/**
2974 * megasas_alloc_cmds - Allocates the command packets
2975 * @instance: Adapter soft state
2976 *
2977 * Each command that is issued to the FW, whether IO commands from the OS or
2978 * internal commands like IOCTLs, are wrapped in local data structure called
2979 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2980 * the FW.
2981 *
2982 * Each frame has a 32-bit field called context (tag). This context is used
2983 * to get back the megasas_cmd from the frame when a frame gets completed in
2984 * the ISR. Typically the address of the megasas_cmd itself would be used as
2985 * the context. But we wanted to keep the differences between 32 and 64 bit
2986 * systems to the mininum. We always use 32 bit integers for the context. In
2987 * this driver, the 32 bit values are the indices into an array cmd_list.
2988 * This array is used only to look up the megasas_cmd given the context. The
2989 * free commands themselves are maintained in a linked list called cmd_pool.
2990 */
adam radford9c915a82010-12-21 13:34:31 -08002991int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002992{
2993 int i;
2994 int j;
2995 u32 max_cmd;
2996 struct megasas_cmd *cmd;
2997
adam radford9c915a82010-12-21 13:34:31 -08002998 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002999
3000 /*
3001 * instance->cmd_list is an array of struct megasas_cmd pointers.
3002 * Allocate the dynamic array first and then allocate individual
3003 * commands.
3004 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003005 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003006
3007 if (!instance->cmd_list) {
3008 printk(KERN_DEBUG "megasas: out of memory\n");
3009 return -ENOMEM;
3010 }
3011
adam radford9c915a82010-12-21 13:34:31 -08003012 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003013
3014 for (i = 0; i < max_cmd; i++) {
3015 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3016 GFP_KERNEL);
3017
3018 if (!instance->cmd_list[i]) {
3019
3020 for (j = 0; j < i; j++)
3021 kfree(instance->cmd_list[j]);
3022
3023 kfree(instance->cmd_list);
3024 instance->cmd_list = NULL;
3025
3026 return -ENOMEM;
3027 }
3028 }
3029
3030 /*
3031 * Add all the commands to command pool (instance->cmd_pool)
3032 */
3033 for (i = 0; i < max_cmd; i++) {
3034 cmd = instance->cmd_list[i];
3035 memset(cmd, 0, sizeof(struct megasas_cmd));
3036 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003037 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003038 cmd->instance = instance;
3039
3040 list_add_tail(&cmd->list, &instance->cmd_pool);
3041 }
3042
3043 /*
3044 * Create a frame pool and assign one frame to each cmd
3045 */
3046 if (megasas_create_frame_pool(instance)) {
3047 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3048 megasas_free_cmds(instance);
3049 }
3050
3051 return 0;
3052}
3053
Yang, Bo81e403c2009-10-06 14:27:54 -06003054/*
3055 * megasas_get_pd_list_info - Returns FW's pd_list structure
3056 * @instance: Adapter soft state
3057 * @pd_list: pd_list structure
3058 *
3059 * Issues an internal command (DCMD) to get the FW's controller PD
3060 * list structure. This information is mainly used to find out SYSTEM
3061 * supported by the FW.
3062 */
3063static int
3064megasas_get_pd_list(struct megasas_instance *instance)
3065{
3066 int ret = 0, pd_index = 0;
3067 struct megasas_cmd *cmd;
3068 struct megasas_dcmd_frame *dcmd;
3069 struct MR_PD_LIST *ci;
3070 struct MR_PD_ADDRESS *pd_addr;
3071 dma_addr_t ci_h = 0;
3072
3073 cmd = megasas_get_cmd(instance);
3074
3075 if (!cmd) {
3076 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3077 return -ENOMEM;
3078 }
3079
3080 dcmd = &cmd->frame->dcmd;
3081
3082 ci = pci_alloc_consistent(instance->pdev,
3083 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3084
3085 if (!ci) {
3086 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3087 megasas_return_cmd(instance, cmd);
3088 return -ENOMEM;
3089 }
3090
3091 memset(ci, 0, sizeof(*ci));
3092 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3093
3094 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3095 dcmd->mbox.b[1] = 0;
3096 dcmd->cmd = MFI_CMD_DCMD;
3097 dcmd->cmd_status = 0xFF;
3098 dcmd->sge_count = 1;
3099 dcmd->flags = MFI_FRAME_DIR_READ;
3100 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003101 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003102 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3103 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3104 dcmd->sgl.sge32[0].phys_addr = ci_h;
3105 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3106
3107 if (!megasas_issue_polled(instance, cmd)) {
3108 ret = 0;
3109 } else {
3110 ret = -1;
3111 }
3112
3113 /*
3114 * the following function will get the instance PD LIST.
3115 */
3116
3117 pd_addr = ci->addr;
3118
3119 if ( ret == 0 &&
3120 (ci->count <
3121 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3122
3123 memset(instance->pd_list, 0,
3124 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3125
3126 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3127
3128 instance->pd_list[pd_addr->deviceId].tid =
3129 pd_addr->deviceId;
3130 instance->pd_list[pd_addr->deviceId].driveType =
3131 pd_addr->scsiDevType;
3132 instance->pd_list[pd_addr->deviceId].driveState =
3133 MR_PD_STATE_SYSTEM;
3134 pd_addr++;
3135 }
3136 }
3137
3138 pci_free_consistent(instance->pdev,
3139 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3140 ci, ci_h);
3141 megasas_return_cmd(instance, cmd);
3142
3143 return ret;
3144}
3145
Yang, Bobdc6fb82009-12-06 08:30:19 -07003146/*
3147 * megasas_get_ld_list_info - Returns FW's ld_list structure
3148 * @instance: Adapter soft state
3149 * @ld_list: ld_list structure
3150 *
3151 * Issues an internal command (DCMD) to get the FW's controller PD
3152 * list structure. This information is mainly used to find out SYSTEM
3153 * supported by the FW.
3154 */
3155static int
3156megasas_get_ld_list(struct megasas_instance *instance)
3157{
3158 int ret = 0, ld_index = 0, ids = 0;
3159 struct megasas_cmd *cmd;
3160 struct megasas_dcmd_frame *dcmd;
3161 struct MR_LD_LIST *ci;
3162 dma_addr_t ci_h = 0;
3163
3164 cmd = megasas_get_cmd(instance);
3165
3166 if (!cmd) {
3167 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3168 return -ENOMEM;
3169 }
3170
3171 dcmd = &cmd->frame->dcmd;
3172
3173 ci = pci_alloc_consistent(instance->pdev,
3174 sizeof(struct MR_LD_LIST),
3175 &ci_h);
3176
3177 if (!ci) {
3178 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3179 megasas_return_cmd(instance, cmd);
3180 return -ENOMEM;
3181 }
3182
3183 memset(ci, 0, sizeof(*ci));
3184 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3185
3186 dcmd->cmd = MFI_CMD_DCMD;
3187 dcmd->cmd_status = 0xFF;
3188 dcmd->sge_count = 1;
3189 dcmd->flags = MFI_FRAME_DIR_READ;
3190 dcmd->timeout = 0;
3191 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3192 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3193 dcmd->sgl.sge32[0].phys_addr = ci_h;
3194 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3195 dcmd->pad_0 = 0;
3196
3197 if (!megasas_issue_polled(instance, cmd)) {
3198 ret = 0;
3199 } else {
3200 ret = -1;
3201 }
3202
3203 /* the following function will get the instance PD LIST */
3204
bo yang39a98552010-09-22 22:36:29 -04003205 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003206 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3207
3208 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3209 if (ci->ldList[ld_index].state != 0) {
3210 ids = ci->ldList[ld_index].ref.targetId;
3211 instance->ld_ids[ids] =
3212 ci->ldList[ld_index].ref.targetId;
3213 }
3214 }
3215 }
3216
3217 pci_free_consistent(instance->pdev,
3218 sizeof(struct MR_LD_LIST),
3219 ci,
3220 ci_h);
3221
3222 megasas_return_cmd(instance, cmd);
3223 return ret;
3224}
3225
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003226/**
3227 * megasas_get_controller_info - Returns FW's controller structure
3228 * @instance: Adapter soft state
3229 * @ctrl_info: Controller information structure
3230 *
3231 * Issues an internal command (DCMD) to get the FW's controller structure.
3232 * This information is mainly used to find out the maximum IO transfer per
3233 * command supported by the FW.
3234 */
3235static int
3236megasas_get_ctrl_info(struct megasas_instance *instance,
3237 struct megasas_ctrl_info *ctrl_info)
3238{
3239 int ret = 0;
3240 struct megasas_cmd *cmd;
3241 struct megasas_dcmd_frame *dcmd;
3242 struct megasas_ctrl_info *ci;
3243 dma_addr_t ci_h = 0;
3244
3245 cmd = megasas_get_cmd(instance);
3246
3247 if (!cmd) {
3248 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3249 return -ENOMEM;
3250 }
3251
3252 dcmd = &cmd->frame->dcmd;
3253
3254 ci = pci_alloc_consistent(instance->pdev,
3255 sizeof(struct megasas_ctrl_info), &ci_h);
3256
3257 if (!ci) {
3258 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3259 megasas_return_cmd(instance, cmd);
3260 return -ENOMEM;
3261 }
3262
3263 memset(ci, 0, sizeof(*ci));
3264 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3265
3266 dcmd->cmd = MFI_CMD_DCMD;
3267 dcmd->cmd_status = 0xFF;
3268 dcmd->sge_count = 1;
3269 dcmd->flags = MFI_FRAME_DIR_READ;
3270 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003271 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003272 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3273 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3274 dcmd->sgl.sge32[0].phys_addr = ci_h;
3275 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3276
3277 if (!megasas_issue_polled(instance, cmd)) {
3278 ret = 0;
3279 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3280 } else {
3281 ret = -1;
3282 }
3283
3284 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3285 ci, ci_h);
3286
3287 megasas_return_cmd(instance, cmd);
3288 return ret;
3289}
3290
3291/**
bo yang31ea7082007-11-07 12:09:50 -05003292 * megasas_issue_init_mfi - Initializes the FW
3293 * @instance: Adapter soft state
3294 *
3295 * Issues the INIT MFI cmd
3296 */
3297static int
3298megasas_issue_init_mfi(struct megasas_instance *instance)
3299{
3300 u32 context;
3301
3302 struct megasas_cmd *cmd;
3303
3304 struct megasas_init_frame *init_frame;
3305 struct megasas_init_queue_info *initq_info;
3306 dma_addr_t init_frame_h;
3307 dma_addr_t initq_info_h;
3308
3309 /*
3310 * Prepare a init frame. Note the init frame points to queue info
3311 * structure. Each frame has SGL allocated after first 64 bytes. For
3312 * this frame - since we don't need any SGL - we use SGL's space as
3313 * queue info structure
3314 *
3315 * We will not get a NULL command below. We just created the pool.
3316 */
3317 cmd = megasas_get_cmd(instance);
3318
3319 init_frame = (struct megasas_init_frame *)cmd->frame;
3320 initq_info = (struct megasas_init_queue_info *)
3321 ((unsigned long)init_frame + 64);
3322
3323 init_frame_h = cmd->frame_phys_addr;
3324 initq_info_h = init_frame_h + 64;
3325
3326 context = init_frame->context;
3327 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3328 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3329 init_frame->context = context;
3330
3331 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3332 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3333
3334 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3335 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3336
3337 init_frame->cmd = MFI_CMD_INIT;
3338 init_frame->cmd_status = 0xFF;
3339 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3340
3341 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3342
3343 /*
3344 * disable the intr before firing the init frame to FW
3345 */
3346 instance->instancet->disable_intr(instance->reg_set);
3347
3348 /*
3349 * Issue the init frame in polled mode
3350 */
3351
3352 if (megasas_issue_polled(instance, cmd)) {
3353 printk(KERN_ERR "megasas: Failed to init firmware\n");
3354 megasas_return_cmd(instance, cmd);
3355 goto fail_fw_init;
3356 }
3357
3358 megasas_return_cmd(instance, cmd);
3359
3360 return 0;
3361
3362fail_fw_init:
3363 return -EINVAL;
3364}
3365
adam radfordcd50ba82010-12-21 10:23:23 -08003366static u32
3367megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003368{
adam radfordcd50ba82010-12-21 10:23:23 -08003369 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003370 u32 context_sz;
3371 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003372
3373 reg_set = instance->reg_set;
3374
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003375 /*
3376 * Get various operational parameters from status register
3377 */
Sumant Patro1341c932006-01-25 12:02:40 -08003378 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003379 /*
3380 * Reduce the max supported cmds by 1. This is to ensure that the
3381 * reply_q_sz (1 more than the max cmd that driver may send)
3382 * does not exceed max cmds that the FW can support
3383 */
3384 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003385 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003386 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003387 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003388 /*
3389 * Create a pool of commands
3390 */
3391 if (megasas_alloc_cmds(instance))
3392 goto fail_alloc_cmds;
3393
3394 /*
3395 * Allocate memory for reply queue. Length of reply queue should
3396 * be _one_ more than the maximum commands handled by the firmware.
3397 *
3398 * Note: When FW completes commands, it places corresponding contex
3399 * values in this circular reply queue. This circular queue is a fairly
3400 * typical producer-consumer queue. FW is the producer (of completed
3401 * commands) and the driver is the consumer.
3402 */
3403 context_sz = sizeof(u32);
3404 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3405
3406 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3407 reply_q_sz,
3408 &instance->reply_queue_h);
3409
3410 if (!instance->reply_queue) {
3411 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3412 goto fail_reply_queue;
3413 }
3414
bo yang31ea7082007-11-07 12:09:50 -05003415 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003416 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003417
bo yang39a98552010-09-22 22:36:29 -04003418 instance->fw_support_ieee = 0;
3419 instance->fw_support_ieee =
3420 (instance->instancet->read_fw_status_reg(reg_set) &
3421 0x04000000);
3422
3423 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3424 instance->fw_support_ieee);
3425
3426 if (instance->fw_support_ieee)
3427 instance->flag_ieee = 1;
3428
adam radfordcd50ba82010-12-21 10:23:23 -08003429 return 0;
3430
3431fail_fw_init:
3432
3433 pci_free_consistent(instance->pdev, reply_q_sz,
3434 instance->reply_queue, instance->reply_queue_h);
3435fail_reply_queue:
3436 megasas_free_cmds(instance);
3437
3438fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003439 return 1;
3440}
3441
3442/**
3443 * megasas_init_fw - Initializes the FW
3444 * @instance: Adapter soft state
3445 *
3446 * This is the main function for initializing firmware
3447 */
3448
3449static int megasas_init_fw(struct megasas_instance *instance)
3450{
3451 u32 max_sectors_1;
3452 u32 max_sectors_2;
adam radford3f1abce2011-05-11 18:33:47 -07003453 u32 tmp_sectors, msix_enable;
adam radfordcd50ba82010-12-21 10:23:23 -08003454 struct megasas_register_set __iomem *reg_set;
3455 struct megasas_ctrl_info *ctrl_info;
3456 unsigned long bar_list;
adam radfordc8e858f2011-10-08 18:15:13 -07003457 int i;
adam radfordcd50ba82010-12-21 10:23:23 -08003458
3459 /* Find first memory bar */
3460 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3461 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3462 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3463 if (pci_request_selected_regions(instance->pdev, instance->bar,
3464 "megasas: LSI")) {
3465 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3466 return -EBUSY;
3467 }
3468
3469 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3470
3471 if (!instance->reg_set) {
3472 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3473 goto fail_ioremap;
3474 }
3475
3476 reg_set = instance->reg_set;
3477
3478 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003479 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07003480 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08003481 instance->instancet = &megasas_instance_template_fusion;
3482 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003483 case PCI_DEVICE_ID_LSI_SAS1078R:
3484 case PCI_DEVICE_ID_LSI_SAS1078DE:
3485 instance->instancet = &megasas_instance_template_ppc;
3486 break;
3487 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3488 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3489 instance->instancet = &megasas_instance_template_gen2;
3490 break;
3491 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3492 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3493 instance->instancet = &megasas_instance_template_skinny;
3494 break;
3495 case PCI_DEVICE_ID_LSI_SAS1064R:
3496 case PCI_DEVICE_ID_DELL_PERC5:
3497 default:
3498 instance->instancet = &megasas_instance_template_xscale;
3499 break;
3500 }
3501
3502 /*
3503 * We expect the FW state to be READY
3504 */
adam radford058a8fa2011-10-08 18:14:27 -07003505 if (megasas_transition_to_ready(instance, 0))
adam radfordcd50ba82010-12-21 10:23:23 -08003506 goto fail_ready_state;
3507
adam radford3f1abce2011-05-11 18:33:47 -07003508 /* Check if MSI-X is supported while in ready state */
3509 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3510 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07003511 if (msix_enable && !msix_disable) {
3512 /* Check max MSI-X vectors */
3513 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3514 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3515 instance->msix_vectors = (readl(&instance->reg_set->
3516 outbound_scratch_pad_2
3517 ) & 0x1F) + 1;
3518 } else
3519 instance->msix_vectors = 1;
3520 /* Don't bother allocating more MSI-X vectors than cpus */
3521 instance->msix_vectors = min(instance->msix_vectors,
3522 (unsigned int)num_online_cpus());
3523 for (i = 0; i < instance->msix_vectors; i++)
3524 instance->msixentry[i].entry = i;
3525 i = pci_enable_msix(instance->pdev, instance->msixentry,
3526 instance->msix_vectors);
3527 if (i >= 0) {
3528 if (i) {
3529 if (!pci_enable_msix(instance->pdev,
3530 instance->msixentry, i))
3531 instance->msix_vectors = i;
3532 else
3533 instance->msix_vectors = 0;
3534 }
3535 } else
3536 instance->msix_vectors = 0;
3537 }
adam radford3f1abce2011-05-11 18:33:47 -07003538
adam radfordcd50ba82010-12-21 10:23:23 -08003539 /* Get operational params, sge flags, send init cmd to controller */
3540 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003541 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003542
3543 printk(KERN_ERR "megasas: INIT adapter done\n");
3544
bo yang39a98552010-09-22 22:36:29 -04003545 /** for passthrough
3546 * the following function will get the PD LIST.
3547 */
3548
Yang, Bo81e403c2009-10-06 14:27:54 -06003549 memset(instance->pd_list, 0 ,
3550 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3551 megasas_get_pd_list(instance);
3552
Yang, Bobdc6fb82009-12-06 08:30:19 -07003553 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3554 megasas_get_ld_list(instance);
3555
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003556 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3557
3558 /*
3559 * Compute the max allowed sectors per IO: The controller info has two
3560 * limits on max sectors. Driver should use the minimum of these two.
3561 *
3562 * 1 << stripe_sz_ops.min = max sectors per strip
3563 *
3564 * Note that older firmwares ( < FW ver 30) didn't report information
3565 * to calculate max_sectors_1. So the number ended up as zero always.
3566 */
bo yang14faea92007-11-09 04:14:00 -05003567 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003568 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3569
3570 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3571 ctrl_info->max_strips_per_io;
3572 max_sectors_2 = ctrl_info->max_request_size;
3573
bo yang14faea92007-11-09 04:14:00 -05003574 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003575 instance->disableOnlineCtrlReset =
3576 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003577 }
3578
3579 instance->max_sectors_per_req = instance->max_num_sge *
3580 PAGE_SIZE / 512;
3581 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3582 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003583
3584 kfree(ctrl_info);
3585
adam radfordc5daa6a2012-07-17 18:20:03 -07003586 /* Check for valid throttlequeuedepth module parameter */
3587 if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3588 instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3589 if (throttlequeuedepth > (instance->max_fw_cmds -
3590 MEGASAS_SKINNY_INT_CMDS))
3591 instance->throttlequeuedepth =
3592 MEGASAS_THROTTLE_QUEUE_DEPTH;
3593 else
3594 instance->throttlequeuedepth = throttlequeuedepth;
3595 } else {
3596 if (throttlequeuedepth > (instance->max_fw_cmds -
3597 MEGASAS_INT_CMDS))
3598 instance->throttlequeuedepth =
3599 MEGASAS_THROTTLE_QUEUE_DEPTH;
3600 else
3601 instance->throttlequeuedepth = throttlequeuedepth;
3602 }
3603
Sumant Patro5d018ad2006-10-03 13:13:18 -07003604 /*
3605 * Setup tasklet for cmd completion
3606 */
3607
adam radfordf86c5422011-02-24 20:57:00 -08003608 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003609 (unsigned long)instance);
3610
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003611 return 0;
3612
adam radfordeb1b1232011-02-24 20:55:56 -08003613fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003614fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003615 iounmap(instance->reg_set);
3616
3617 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003618 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003619
3620 return -EINVAL;
3621}
3622
3623/**
3624 * megasas_release_mfi - Reverses the FW initialization
3625 * @intance: Adapter soft state
3626 */
3627static void megasas_release_mfi(struct megasas_instance *instance)
3628{
adam radford9c915a82010-12-21 13:34:31 -08003629 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003630
adam radford9c915a82010-12-21 13:34:31 -08003631 if (instance->reply_queue)
3632 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003633 instance->reply_queue, instance->reply_queue_h);
3634
3635 megasas_free_cmds(instance);
3636
3637 iounmap(instance->reg_set);
3638
adam radfordb6d5d882010-12-14 18:56:07 -08003639 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003640}
3641
3642/**
3643 * megasas_get_seq_num - Gets latest event sequence numbers
3644 * @instance: Adapter soft state
3645 * @eli: FW event log sequence numbers information
3646 *
3647 * FW maintains a log of all events in a non-volatile area. Upper layers would
3648 * usually find out the latest sequence number of the events, the seq number at
3649 * the boot etc. They would "read" all the events below the latest seq number
3650 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3651 * number), they would subsribe to AEN (asynchronous event notification) and
3652 * wait for the events to happen.
3653 */
3654static int
3655megasas_get_seq_num(struct megasas_instance *instance,
3656 struct megasas_evt_log_info *eli)
3657{
3658 struct megasas_cmd *cmd;
3659 struct megasas_dcmd_frame *dcmd;
3660 struct megasas_evt_log_info *el_info;
3661 dma_addr_t el_info_h = 0;
3662
3663 cmd = megasas_get_cmd(instance);
3664
3665 if (!cmd) {
3666 return -ENOMEM;
3667 }
3668
3669 dcmd = &cmd->frame->dcmd;
3670 el_info = pci_alloc_consistent(instance->pdev,
3671 sizeof(struct megasas_evt_log_info),
3672 &el_info_h);
3673
3674 if (!el_info) {
3675 megasas_return_cmd(instance, cmd);
3676 return -ENOMEM;
3677 }
3678
3679 memset(el_info, 0, sizeof(*el_info));
3680 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3681
3682 dcmd->cmd = MFI_CMD_DCMD;
3683 dcmd->cmd_status = 0x0;
3684 dcmd->sge_count = 1;
3685 dcmd->flags = MFI_FRAME_DIR_READ;
3686 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003687 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003688 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3689 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3690 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3691 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3692
3693 megasas_issue_blocked_cmd(instance, cmd);
3694
3695 /*
3696 * Copy the data back into callers buffer
3697 */
3698 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3699
3700 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3701 el_info, el_info_h);
3702
3703 megasas_return_cmd(instance, cmd);
3704
3705 return 0;
3706}
3707
3708/**
3709 * megasas_register_aen - Registers for asynchronous event notification
3710 * @instance: Adapter soft state
3711 * @seq_num: The starting sequence number
3712 * @class_locale: Class of the event
3713 *
3714 * This function subscribes for AEN for events beyond the @seq_num. It requests
3715 * to be notified if and only if the event is of type @class_locale
3716 */
3717static int
3718megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3719 u32 class_locale_word)
3720{
3721 int ret_val;
3722 struct megasas_cmd *cmd;
3723 struct megasas_dcmd_frame *dcmd;
3724 union megasas_evt_class_locale curr_aen;
3725 union megasas_evt_class_locale prev_aen;
3726
3727 /*
3728 * If there an AEN pending already (aen_cmd), check if the
3729 * class_locale of that pending AEN is inclusive of the new
3730 * AEN request we currently have. If it is, then we don't have
3731 * to do anything. In other words, whichever events the current
3732 * AEN request is subscribing to, have already been subscribed
3733 * to.
3734 *
3735 * If the old_cmd is _not_ inclusive, then we have to abort
3736 * that command, form a class_locale that is superset of both
3737 * old and current and re-issue to the FW
3738 */
3739
3740 curr_aen.word = class_locale_word;
3741
3742 if (instance->aen_cmd) {
3743
3744 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3745
3746 /*
3747 * A class whose enum value is smaller is inclusive of all
3748 * higher values. If a PROGRESS (= -1) was previously
3749 * registered, then a new registration requests for higher
3750 * classes need not be sent to FW. They are automatically
3751 * included.
3752 *
3753 * Locale numbers don't have such hierarchy. They are bitmap
3754 * values
3755 */
3756 if ((prev_aen.members.class <= curr_aen.members.class) &&
3757 !((prev_aen.members.locale & curr_aen.members.locale) ^
3758 curr_aen.members.locale)) {
3759 /*
3760 * Previously issued event registration includes
3761 * current request. Nothing to do.
3762 */
3763 return 0;
3764 } else {
3765 curr_aen.members.locale |= prev_aen.members.locale;
3766
3767 if (prev_aen.members.class < curr_aen.members.class)
3768 curr_aen.members.class = prev_aen.members.class;
3769
3770 instance->aen_cmd->abort_aen = 1;
3771 ret_val = megasas_issue_blocked_abort_cmd(instance,
3772 instance->
3773 aen_cmd);
3774
3775 if (ret_val) {
3776 printk(KERN_DEBUG "megasas: Failed to abort "
3777 "previous AEN command\n");
3778 return ret_val;
3779 }
3780 }
3781 }
3782
3783 cmd = megasas_get_cmd(instance);
3784
3785 if (!cmd)
3786 return -ENOMEM;
3787
3788 dcmd = &cmd->frame->dcmd;
3789
3790 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3791
3792 /*
3793 * Prepare DCMD for aen registration
3794 */
3795 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3796
3797 dcmd->cmd = MFI_CMD_DCMD;
3798 dcmd->cmd_status = 0x0;
3799 dcmd->sge_count = 1;
3800 dcmd->flags = MFI_FRAME_DIR_READ;
3801 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003802 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003803 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003804 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3805 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3806 dcmd->mbox.w[0] = seq_num;
3807 dcmd->mbox.w[1] = curr_aen.word;
3808 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3809 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3810
Yang, Bof4c9a132009-10-06 14:43:28 -06003811 if (instance->aen_cmd != NULL) {
3812 megasas_return_cmd(instance, cmd);
3813 return 0;
3814 }
3815
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003816 /*
3817 * Store reference to the cmd used to register for AEN. When an
3818 * application wants us to register for AEN, we have to abort this
3819 * cmd and re-register with a new EVENT LOCALE supplied by that app
3820 */
3821 instance->aen_cmd = cmd;
3822
3823 /*
3824 * Issue the aen registration frame
3825 */
adam radford9c915a82010-12-21 13:34:31 -08003826 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003827
3828 return 0;
3829}
3830
3831/**
3832 * megasas_start_aen - Subscribes to AEN during driver load time
3833 * @instance: Adapter soft state
3834 */
3835static int megasas_start_aen(struct megasas_instance *instance)
3836{
3837 struct megasas_evt_log_info eli;
3838 union megasas_evt_class_locale class_locale;
3839
3840 /*
3841 * Get the latest sequence number from FW
3842 */
3843 memset(&eli, 0, sizeof(eli));
3844
3845 if (megasas_get_seq_num(instance, &eli))
3846 return -1;
3847
3848 /*
3849 * Register AEN with FW for latest sequence number plus 1
3850 */
3851 class_locale.members.reserved = 0;
3852 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3853 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3854
3855 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3856 class_locale.word);
3857}
3858
3859/**
3860 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3861 * @instance: Adapter soft state
3862 */
3863static int megasas_io_attach(struct megasas_instance *instance)
3864{
3865 struct Scsi_Host *host = instance->host;
3866
3867 /*
3868 * Export parameters required by SCSI mid-layer
3869 */
3870 host->irq = instance->pdev->irq;
3871 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003872 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3873 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3874 host->can_queue =
3875 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3876 } else
3877 host->can_queue =
3878 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003879 host->this_id = instance->init_id;
3880 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003881
3882 if (instance->fw_support_ieee)
3883 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3884
Yang, Bo1fd10682010-10-12 07:18:50 -06003885 /*
3886 * Check if the module parameter value for max_sectors can be used
3887 */
3888 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3889 instance->max_sectors_per_req = max_sectors;
3890 else {
3891 if (max_sectors) {
3892 if (((instance->pdev->device ==
3893 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3894 (instance->pdev->device ==
3895 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3896 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3897 instance->max_sectors_per_req = max_sectors;
3898 } else {
3899 printk(KERN_INFO "megasas: max_sectors should be > 0"
3900 "and <= %d (or < 1MB for GEN2 controller)\n",
3901 instance->max_sectors_per_req);
3902 }
3903 }
3904 }
3905
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003906 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003907 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003908 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3909 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3910 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003911 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003912
adam radford9c915a82010-12-21 13:34:31 -08003913 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07003914 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3915 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08003916 host->hostt->eh_device_reset_handler = NULL;
3917 host->hostt->eh_bus_reset_handler = NULL;
3918 }
3919
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003920 /*
3921 * Notify the mid-layer about the new controller
3922 */
3923 if (scsi_add_host(host, &instance->pdev->dev)) {
3924 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3925 return -ENODEV;
3926 }
3927
3928 /*
3929 * Trigger SCSI to scan our drives
3930 */
3931 scsi_scan_host(host);
3932 return 0;
3933}
3934
bo yang31ea7082007-11-07 12:09:50 -05003935static int
3936megasas_set_dma_mask(struct pci_dev *pdev)
3937{
3938 /*
3939 * All our contollers are capable of performing 64-bit DMA
3940 */
3941 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003942 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003943
Yang Hongyang284901a2009-04-06 19:01:15 -07003944 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003945 goto fail_set_dma_mask;
3946 }
3947 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003948 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003949 goto fail_set_dma_mask;
3950 }
3951 return 0;
3952
3953fail_set_dma_mask:
3954 return 1;
3955}
3956
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003957/**
3958 * megasas_probe_one - PCI hotplug entry point
3959 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003960 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003961 */
3962static int __devinit
3963megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3964{
adam radfordc8e858f2011-10-08 18:15:13 -07003965 int rval, pos, i, j;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003966 struct Scsi_Host *host;
3967 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08003968 u16 control = 0;
3969
3970 /* Reset MSI-X in the kdump kernel */
3971 if (reset_devices) {
3972 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3973 if (pos) {
3974 pci_read_config_word(pdev, msi_control_reg(pos),
3975 &control);
3976 if (control & PCI_MSIX_FLAGS_ENABLE) {
3977 dev_info(&pdev->dev, "resetting MSI-X\n");
3978 pci_write_config_word(pdev,
3979 msi_control_reg(pos),
3980 control &
3981 ~PCI_MSIX_FLAGS_ENABLE);
3982 }
3983 }
3984 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003985
3986 /*
3987 * Announce PCI information
3988 */
3989 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3990 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3991 pdev->subsystem_device);
3992
3993 printk("bus %d:slot %d:func %d\n",
3994 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3995
3996 /*
3997 * PCI prepping: enable device set bus mastering and dma mask
3998 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003999 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004000
4001 if (rval) {
4002 return rval;
4003 }
4004
4005 pci_set_master(pdev);
4006
bo yang31ea7082007-11-07 12:09:50 -05004007 if (megasas_set_dma_mask(pdev))
4008 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004009
4010 host = scsi_host_alloc(&megasas_template,
4011 sizeof(struct megasas_instance));
4012
4013 if (!host) {
4014 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4015 goto fail_alloc_instance;
4016 }
4017
4018 instance = (struct megasas_instance *)host->hostdata;
4019 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04004020 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08004021 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004022
adam radford9c915a82010-12-21 13:34:31 -08004023 switch (instance->pdev->device) {
4024 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004025 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004026 {
4027 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004028
adam radford9c915a82010-12-21 13:34:31 -08004029 instance->ctrl_context =
4030 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4031 if (!instance->ctrl_context) {
4032 printk(KERN_DEBUG "megasas: Failed to allocate "
4033 "memory for Fusion context info\n");
4034 goto fail_alloc_dma_buf;
4035 }
4036 fusion = instance->ctrl_context;
4037 INIT_LIST_HEAD(&fusion->cmd_pool);
4038 spin_lock_init(&fusion->cmd_pool_lock);
4039 }
4040 break;
4041 default: /* For all other supported controllers */
4042
4043 instance->producer =
4044 pci_alloc_consistent(pdev, sizeof(u32),
4045 &instance->producer_h);
4046 instance->consumer =
4047 pci_alloc_consistent(pdev, sizeof(u32),
4048 &instance->consumer_h);
4049
4050 if (!instance->producer || !instance->consumer) {
4051 printk(KERN_DEBUG "megasas: Failed to allocate"
4052 "memory for producer, consumer\n");
4053 goto fail_alloc_dma_buf;
4054 }
4055
4056 *instance->producer = 0;
4057 *instance->consumer = 0;
4058 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004059 }
4060
Yang, Boc3518832009-10-06 14:18:02 -06004061 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004062 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004063 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004064 instance->issuepend_done = 1;
4065 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4066 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004067
4068 instance->evt_detail = pci_alloc_consistent(pdev,
4069 sizeof(struct
4070 megasas_evt_detail),
4071 &instance->evt_detail_h);
4072
4073 if (!instance->evt_detail) {
4074 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4075 "event detail structure\n");
4076 goto fail_alloc_dma_buf;
4077 }
4078
4079 /*
4080 * Initialize locks and queues
4081 */
4082 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004083 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004084
Sumant Patroe4a082c2006-05-30 12:03:37 -07004085 atomic_set(&instance->fw_outstanding,0);
4086
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004087 init_waitqueue_head(&instance->int_cmd_wait_q);
4088 init_waitqueue_head(&instance->abort_cmd_wait_q);
4089
4090 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004091 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004092 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06004093 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004094
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004095 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004096 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004097
4098 /*
4099 * Initialize PCI related and misc parameters
4100 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004101 instance->host = host;
4102 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4103 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4104
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004105 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4106 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004107 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004108 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4109 } else
4110 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4111
Sumant Patro658dced2006-10-03 13:09:14 -07004112 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004113 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004114 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004115 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004116 instance->disableOnlineCtrlReset = 1;
4117
adam radford36807e62011-10-08 18:15:06 -07004118 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4119 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08004120 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4121 else
4122 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004123
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004124 /*
adam radford0a770662011-02-24 20:56:12 -08004125 * Initialize MFI Firmware
4126 */
4127 if (megasas_init_fw(instance))
4128 goto fail_init_mfi;
4129
4130 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004131 * Register IRQ
4132 */
adam radfordc8e858f2011-10-08 18:15:13 -07004133 if (instance->msix_vectors) {
4134 for (i = 0 ; i < instance->msix_vectors; i++) {
4135 instance->irq_context[i].instance = instance;
4136 instance->irq_context[i].MSIxIndex = i;
4137 if (request_irq(instance->msixentry[i].vector,
4138 instance->instancet->service_isr, 0,
4139 "megasas",
4140 &instance->irq_context[i])) {
4141 printk(KERN_DEBUG "megasas: Failed to "
4142 "register IRQ for vector %d.\n", i);
4143 for (j = 0 ; j < i ; j++)
4144 free_irq(
4145 instance->msixentry[j].vector,
4146 &instance->irq_context[j]);
4147 goto fail_irq;
4148 }
4149 }
4150 } else {
4151 instance->irq_context[0].instance = instance;
4152 instance->irq_context[0].MSIxIndex = 0;
4153 if (request_irq(pdev->irq, instance->instancet->service_isr,
4154 IRQF_SHARED, "megasas",
4155 &instance->irq_context[0])) {
4156 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4157 goto fail_irq;
4158 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004159 }
4160
Sumant Patro1341c932006-01-25 12:02:40 -08004161 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004162
4163 /*
4164 * Store instance in PCI softstate
4165 */
4166 pci_set_drvdata(pdev, instance);
4167
4168 /*
4169 * Add this controller to megasas_mgmt_info structure so that it
4170 * can be exported to management applications
4171 */
4172 megasas_mgmt_info.count++;
4173 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4174 megasas_mgmt_info.max_index++;
4175
4176 /*
adam radford541f90b2011-05-11 18:34:29 -07004177 * Register with SCSI mid-layer
4178 */
4179 if (megasas_io_attach(instance))
4180 goto fail_io_attach;
4181
4182 instance->unload = 0;
4183
4184 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004185 * Initiate AEN (Asynchronous Event Notification)
4186 */
4187 if (megasas_start_aen(instance)) {
4188 printk(KERN_DEBUG "megasas: start aen failed\n");
4189 goto fail_start_aen;
4190 }
4191
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004192 return 0;
4193
4194 fail_start_aen:
4195 fail_io_attach:
4196 megasas_mgmt_info.count--;
4197 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4198 megasas_mgmt_info.max_index--;
4199
4200 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004201 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004202 if (instance->msix_vectors)
4203 for (i = 0 ; i < instance->msix_vectors; i++)
4204 free_irq(instance->msixentry[i].vector,
4205 &instance->irq_context[i]);
4206 else
4207 free_irq(instance->pdev->irq, &instance->irq_context[0]);
adam radfordeb1b1232011-02-24 20:55:56 -08004208fail_irq:
adam radford36807e62011-10-08 18:15:06 -07004209 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4210 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radfordeb1b1232011-02-24 20:55:56 -08004211 megasas_release_fusion(instance);
4212 else
4213 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004214 fail_init_mfi:
adam radfordc8e858f2011-10-08 18:15:13 -07004215 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08004216 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004217 fail_alloc_dma_buf:
4218 if (instance->evt_detail)
4219 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4220 instance->evt_detail,
4221 instance->evt_detail_h);
4222
adam radfordeb1b1232011-02-24 20:55:56 -08004223 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004224 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4225 instance->producer_h);
4226 if (instance->consumer)
4227 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4228 instance->consumer_h);
4229 scsi_host_put(host);
4230
4231 fail_alloc_instance:
4232 fail_set_dma_mask:
4233 pci_disable_device(pdev);
4234
4235 return -ENODEV;
4236}
4237
4238/**
4239 * megasas_flush_cache - Requests FW to flush all its caches
4240 * @instance: Adapter soft state
4241 */
4242static void megasas_flush_cache(struct megasas_instance *instance)
4243{
4244 struct megasas_cmd *cmd;
4245 struct megasas_dcmd_frame *dcmd;
4246
bo yang39a98552010-09-22 22:36:29 -04004247 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4248 return;
4249
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004250 cmd = megasas_get_cmd(instance);
4251
4252 if (!cmd)
4253 return;
4254
4255 dcmd = &cmd->frame->dcmd;
4256
4257 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4258
4259 dcmd->cmd = MFI_CMD_DCMD;
4260 dcmd->cmd_status = 0x0;
4261 dcmd->sge_count = 0;
4262 dcmd->flags = MFI_FRAME_DIR_NONE;
4263 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004264 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004265 dcmd->data_xfer_len = 0;
4266 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4267 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4268
4269 megasas_issue_blocked_cmd(instance, cmd);
4270
4271 megasas_return_cmd(instance, cmd);
4272
4273 return;
4274}
4275
4276/**
4277 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4278 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004279 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004280 */
bo yang31ea7082007-11-07 12:09:50 -05004281static void megasas_shutdown_controller(struct megasas_instance *instance,
4282 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004283{
4284 struct megasas_cmd *cmd;
4285 struct megasas_dcmd_frame *dcmd;
4286
bo yang39a98552010-09-22 22:36:29 -04004287 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4288 return;
4289
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004290 cmd = megasas_get_cmd(instance);
4291
4292 if (!cmd)
4293 return;
4294
4295 if (instance->aen_cmd)
4296 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004297 if (instance->map_update_cmd)
4298 megasas_issue_blocked_abort_cmd(instance,
4299 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004300 dcmd = &cmd->frame->dcmd;
4301
4302 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4303
4304 dcmd->cmd = MFI_CMD_DCMD;
4305 dcmd->cmd_status = 0x0;
4306 dcmd->sge_count = 0;
4307 dcmd->flags = MFI_FRAME_DIR_NONE;
4308 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004309 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004310 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004311 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004312
4313 megasas_issue_blocked_cmd(instance, cmd);
4314
4315 megasas_return_cmd(instance, cmd);
4316
4317 return;
4318}
4319
Jiri Slaby33139b22008-05-01 17:56:02 +02004320#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004321/**
bo yangad84db22007-11-09 04:40:16 -05004322 * megasas_suspend - driver suspend entry point
4323 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004324 * @state: PCI power state to suspend routine
4325 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004326static int
bo yang31ea7082007-11-07 12:09:50 -05004327megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4328{
4329 struct Scsi_Host *host;
4330 struct megasas_instance *instance;
adam radfordc8e858f2011-10-08 18:15:13 -07004331 int i;
bo yang31ea7082007-11-07 12:09:50 -05004332
4333 instance = pci_get_drvdata(pdev);
4334 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004335 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004336
4337 megasas_flush_cache(instance);
4338 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004339
4340 /* cancel the delayed work if this work still in queue */
4341 if (instance->ev != NULL) {
4342 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004343 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004344 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004345 instance->ev = NULL;
4346 }
4347
bo yang31ea7082007-11-07 12:09:50 -05004348 tasklet_kill(&instance->isr_tasklet);
4349
4350 pci_set_drvdata(instance->pdev, instance);
4351 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004352
4353 if (instance->msix_vectors)
4354 for (i = 0 ; i < instance->msix_vectors; i++)
4355 free_irq(instance->msixentry[i].vector,
4356 &instance->irq_context[i]);
4357 else
4358 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4359 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004360 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004361
4362 pci_save_state(pdev);
4363 pci_disable_device(pdev);
4364
4365 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4366
4367 return 0;
4368}
4369
4370/**
4371 * megasas_resume- driver resume entry point
4372 * @pdev: PCI device structure
4373 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004374static int
bo yang31ea7082007-11-07 12:09:50 -05004375megasas_resume(struct pci_dev *pdev)
4376{
adam radfordc8e858f2011-10-08 18:15:13 -07004377 int rval, i, j;
bo yang31ea7082007-11-07 12:09:50 -05004378 struct Scsi_Host *host;
4379 struct megasas_instance *instance;
4380
4381 instance = pci_get_drvdata(pdev);
4382 host = instance->host;
4383 pci_set_power_state(pdev, PCI_D0);
4384 pci_enable_wake(pdev, PCI_D0, 0);
4385 pci_restore_state(pdev);
4386
4387 /*
4388 * PCI prepping: enable device set bus mastering and dma mask
4389 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004390 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004391
4392 if (rval) {
4393 printk(KERN_ERR "megasas: Enable device failed\n");
4394 return rval;
4395 }
4396
4397 pci_set_master(pdev);
4398
4399 if (megasas_set_dma_mask(pdev))
4400 goto fail_set_dma_mask;
4401
4402 /*
4403 * Initialize MFI Firmware
4404 */
4405
bo yang31ea7082007-11-07 12:09:50 -05004406 atomic_set(&instance->fw_outstanding, 0);
4407
4408 /*
4409 * We expect the FW state to be READY
4410 */
adam radford058a8fa2011-10-08 18:14:27 -07004411 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05004412 goto fail_ready_state;
4413
adam radford3f1abce2011-05-11 18:33:47 -07004414 /* Now re-enable MSI-X */
adam radfordc8e858f2011-10-08 18:15:13 -07004415 if (instance->msix_vectors)
4416 pci_enable_msix(instance->pdev, instance->msixentry,
4417 instance->msix_vectors);
adam radford3f1abce2011-05-11 18:33:47 -07004418
adam radford9c915a82010-12-21 13:34:31 -08004419 switch (instance->pdev->device) {
4420 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004421 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004422 {
4423 megasas_reset_reply_desc(instance);
4424 if (megasas_ioc_init_fusion(instance)) {
4425 megasas_free_cmds(instance);
4426 megasas_free_cmds_fusion(instance);
4427 goto fail_init_mfi;
4428 }
4429 if (!megasas_get_map_info(instance))
4430 megasas_sync_map_info(instance);
4431 }
4432 break;
4433 default:
4434 *instance->producer = 0;
4435 *instance->consumer = 0;
4436 if (megasas_issue_init_mfi(instance))
4437 goto fail_init_mfi;
4438 break;
4439 }
bo yang31ea7082007-11-07 12:09:50 -05004440
adam radford9c915a82010-12-21 13:34:31 -08004441 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4442 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004443
4444 /*
4445 * Register IRQ
4446 */
adam radfordc8e858f2011-10-08 18:15:13 -07004447 if (instance->msix_vectors) {
4448 for (i = 0 ; i < instance->msix_vectors; i++) {
4449 instance->irq_context[i].instance = instance;
4450 instance->irq_context[i].MSIxIndex = i;
4451 if (request_irq(instance->msixentry[i].vector,
4452 instance->instancet->service_isr, 0,
4453 "megasas",
4454 &instance->irq_context[i])) {
4455 printk(KERN_DEBUG "megasas: Failed to "
4456 "register IRQ for vector %d.\n", i);
4457 for (j = 0 ; j < i ; j++)
4458 free_irq(
4459 instance->msixentry[j].vector,
4460 &instance->irq_context[j]);
4461 goto fail_irq;
4462 }
4463 }
4464 } else {
4465 instance->irq_context[0].instance = instance;
4466 instance->irq_context[0].MSIxIndex = 0;
4467 if (request_irq(pdev->irq, instance->instancet->service_isr,
4468 IRQF_SHARED, "megasas",
4469 &instance->irq_context[0])) {
4470 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4471 goto fail_irq;
4472 }
bo yang31ea7082007-11-07 12:09:50 -05004473 }
4474
4475 instance->instancet->enable_intr(instance->reg_set);
Yang, Bo0c79e682009-10-06 14:47:35 -06004476 instance->unload = 0;
4477
adam radford541f90b2011-05-11 18:34:29 -07004478 /*
4479 * Initiate AEN (Asynchronous Event Notification)
4480 */
4481 if (megasas_start_aen(instance))
4482 printk(KERN_ERR "megasas: Start AEN failed\n");
4483
bo yang31ea7082007-11-07 12:09:50 -05004484 return 0;
4485
4486fail_irq:
4487fail_init_mfi:
4488 if (instance->evt_detail)
4489 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4490 instance->evt_detail,
4491 instance->evt_detail_h);
4492
4493 if (instance->producer)
4494 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4495 instance->producer_h);
4496 if (instance->consumer)
4497 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4498 instance->consumer_h);
4499 scsi_host_put(host);
4500
4501fail_set_dma_mask:
4502fail_ready_state:
4503
4504 pci_disable_device(pdev);
4505
4506 return -ENODEV;
4507}
Jiri Slaby33139b22008-05-01 17:56:02 +02004508#else
4509#define megasas_suspend NULL
4510#define megasas_resume NULL
4511#endif
bo yang31ea7082007-11-07 12:09:50 -05004512
4513/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004514 * megasas_detach_one - PCI hot"un"plug entry point
4515 * @pdev: PCI device structure
4516 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004517static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004518{
4519 int i;
4520 struct Scsi_Host *host;
4521 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004522 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004523
4524 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004525 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004526 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004527 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004528
4529 scsi_remove_host(instance->host);
4530 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004531 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004532
4533 /* cancel the delayed work if this work still in queue*/
4534 if (instance->ev != NULL) {
4535 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004536 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004537 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004538 instance->ev = NULL;
4539 }
4540
Sumant Patro5d018ad2006-10-03 13:13:18 -07004541 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004542
4543 /*
4544 * Take the instance off the instance array. Note that we will not
4545 * decrement the max_index. We let this array be sparse array
4546 */
4547 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4548 if (megasas_mgmt_info.instance[i] == instance) {
4549 megasas_mgmt_info.count--;
4550 megasas_mgmt_info.instance[i] = NULL;
4551
4552 break;
4553 }
4554 }
4555
4556 pci_set_drvdata(instance->pdev, NULL);
4557
Sumant Patrob274cab2006-10-03 12:52:12 -07004558 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004559
adam radfordc8e858f2011-10-08 18:15:13 -07004560 if (instance->msix_vectors)
4561 for (i = 0 ; i < instance->msix_vectors; i++)
4562 free_irq(instance->msixentry[i].vector,
4563 &instance->irq_context[i]);
4564 else
4565 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4566 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004567 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004568
adam radford9c915a82010-12-21 13:34:31 -08004569 switch (instance->pdev->device) {
4570 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004571 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004572 megasas_release_fusion(instance);
4573 for (i = 0; i < 2 ; i++)
4574 if (fusion->ld_map[i])
4575 dma_free_coherent(&instance->pdev->dev,
4576 fusion->map_sz,
4577 fusion->ld_map[i],
4578 fusion->
4579 ld_map_phys[i]);
4580 kfree(instance->ctrl_context);
4581 break;
4582 default:
4583 megasas_release_mfi(instance);
4584 pci_free_consistent(pdev,
4585 sizeof(struct megasas_evt_detail),
4586 instance->evt_detail,
4587 instance->evt_detail_h);
4588 pci_free_consistent(pdev, sizeof(u32),
4589 instance->producer,
4590 instance->producer_h);
4591 pci_free_consistent(pdev, sizeof(u32),
4592 instance->consumer,
4593 instance->consumer_h);
4594 break;
4595 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004596
4597 scsi_host_put(host);
4598
4599 pci_set_drvdata(pdev, NULL);
4600
4601 pci_disable_device(pdev);
4602
4603 return;
4604}
4605
4606/**
4607 * megasas_shutdown - Shutdown entry point
4608 * @device: Generic device structure
4609 */
4610static void megasas_shutdown(struct pci_dev *pdev)
4611{
adam radfordc8e858f2011-10-08 18:15:13 -07004612 int i;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004613 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07004614
Yang, Bo0c79e682009-10-06 14:47:35 -06004615 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004616 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004617 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
adam radford46fd2562011-05-11 18:34:17 -07004618 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004619 if (instance->msix_vectors)
4620 for (i = 0 ; i < instance->msix_vectors; i++)
4621 free_irq(instance->msixentry[i].vector,
4622 &instance->irq_context[i]);
4623 else
4624 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4625 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07004626 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004627}
4628
4629/**
4630 * megasas_mgmt_open - char node "open" entry point
4631 */
4632static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4633{
4634 /*
4635 * Allow only those users with admin rights
4636 */
4637 if (!capable(CAP_SYS_ADMIN))
4638 return -EACCES;
4639
4640 return 0;
4641}
4642
4643/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004644 * megasas_mgmt_fasync - Async notifier registration from applications
4645 *
4646 * This function adds the calling process to a driver global queue. When an
4647 * event occurs, SIGIO will be sent to all processes in this queue.
4648 */
4649static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4650{
4651 int rc;
4652
Arjan van de Ven0b950672006-01-11 13:16:10 +01004653 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004654
4655 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4656
Arjan van de Ven0b950672006-01-11 13:16:10 +01004657 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004658
4659 if (rc >= 0) {
4660 /* For sanity check when we get ioctl */
4661 filep->private_data = filep;
4662 return 0;
4663 }
4664
4665 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4666
4667 return rc;
4668}
4669
4670/**
Yang, Boc3518832009-10-06 14:18:02 -06004671 * megasas_mgmt_poll - char node "poll" entry point
4672 * */
4673static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4674{
4675 unsigned int mask;
4676 unsigned long flags;
4677 poll_wait(file, &megasas_poll_wait, wait);
4678 spin_lock_irqsave(&poll_aen_lock, flags);
4679 if (megasas_poll_wait_aen)
4680 mask = (POLLIN | POLLRDNORM);
4681 else
4682 mask = 0;
4683 spin_unlock_irqrestore(&poll_aen_lock, flags);
4684 return mask;
4685}
4686
4687/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004688 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4689 * @instance: Adapter soft state
4690 * @argp: User's ioctl packet
4691 */
4692static int
4693megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4694 struct megasas_iocpacket __user * user_ioc,
4695 struct megasas_iocpacket *ioc)
4696{
4697 struct megasas_sge32 *kern_sge32;
4698 struct megasas_cmd *cmd;
4699 void *kbuff_arr[MAX_IOCTL_SGE];
4700 dma_addr_t buf_handle = 0;
4701 int error = 0, i;
4702 void *sense = NULL;
4703 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004704 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004705
4706 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4707
4708 if (ioc->sge_count > MAX_IOCTL_SGE) {
4709 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4710 ioc->sge_count, MAX_IOCTL_SGE);
4711 return -EINVAL;
4712 }
4713
4714 cmd = megasas_get_cmd(instance);
4715 if (!cmd) {
4716 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4717 return -ENOMEM;
4718 }
4719
4720 /*
4721 * User's IOCTL packet has 2 frames (maximum). Copy those two
4722 * frames into our cmd's frames. cmd->frame's context will get
4723 * overwritten when we copy from user's frames. So set that value
4724 * alone separately
4725 */
4726 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4727 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004728 cmd->frame->hdr.pad_0 = 0;
adam radford882be7c32012-01-06 17:02:34 -08004729 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4730 MFI_FRAME_SENSE64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004731
4732 /*
4733 * The management interface between applications and the fw uses
4734 * MFI frames. E.g, RAID configuration changes, LD property changes
4735 * etc are accomplishes through different kinds of MFI frames. The
4736 * driver needs to care only about substituting user buffers with
4737 * kernel buffers in SGLs. The location of SGL is embedded in the
4738 * struct iocpacket itself.
4739 */
4740 kern_sge32 = (struct megasas_sge32 *)
4741 ((unsigned long)cmd->frame + ioc->sgl_off);
4742
4743 /*
4744 * For each user buffer, create a mirror buffer and copy in
4745 */
4746 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004747 if (!ioc->sgl[i].iov_len)
4748 continue;
4749
Sumant Patro9f35fa82007-02-14 12:55:45 -08004750 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004751 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004752 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004753 if (!kbuff_arr[i]) {
4754 printk(KERN_DEBUG "megasas: Failed to alloc "
4755 "kernel SGL buffer for IOCTL \n");
4756 error = -ENOMEM;
4757 goto out;
4758 }
4759
4760 /*
4761 * We don't change the dma_coherent_mask, so
4762 * pci_alloc_consistent only returns 32bit addresses
4763 */
4764 kern_sge32[i].phys_addr = (u32) buf_handle;
4765 kern_sge32[i].length = ioc->sgl[i].iov_len;
4766
4767 /*
4768 * We created a kernel buffer corresponding to the
4769 * user buffer. Now copy in from the user buffer
4770 */
4771 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4772 (u32) (ioc->sgl[i].iov_len))) {
4773 error = -EFAULT;
4774 goto out;
4775 }
4776 }
4777
4778 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004779 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4780 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004781 if (!sense) {
4782 error = -ENOMEM;
4783 goto out;
4784 }
4785
4786 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004787 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004788 *sense_ptr = sense_handle;
4789 }
4790
4791 /*
4792 * Set the sync_cmd flag so that the ISR knows not to complete this
4793 * cmd to the SCSI mid-layer
4794 */
4795 cmd->sync_cmd = 1;
4796 megasas_issue_blocked_cmd(instance, cmd);
4797 cmd->sync_cmd = 0;
4798
4799 /*
4800 * copy out the kernel buffers to user buffers
4801 */
4802 for (i = 0; i < ioc->sge_count; i++) {
4803 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4804 ioc->sgl[i].iov_len)) {
4805 error = -EFAULT;
4806 goto out;
4807 }
4808 }
4809
4810 /*
4811 * copy out the sense
4812 */
4813 if (ioc->sense_len) {
4814 /*
bo yangb70a41e2008-03-18 03:13:06 -04004815 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004816 * sense buffer address
4817 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004818 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4819 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004820
bo yangb70a41e2008-03-18 03:13:06 -04004821 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4822 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004823 printk(KERN_ERR "megasas: Failed to copy out to user "
4824 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004825 error = -EFAULT;
4826 goto out;
4827 }
4828 }
4829
4830 /*
4831 * copy the status codes returned by the fw
4832 */
4833 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4834 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4835 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4836 error = -EFAULT;
4837 }
4838
4839 out:
4840 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004841 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004842 sense, sense_handle);
4843 }
4844
4845 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004846 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004847 kern_sge32[i].length,
4848 kbuff_arr[i], kern_sge32[i].phys_addr);
4849 }
4850
4851 megasas_return_cmd(instance, cmd);
4852 return error;
4853}
4854
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004855static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4856{
4857 struct megasas_iocpacket __user *user_ioc =
4858 (struct megasas_iocpacket __user *)arg;
4859 struct megasas_iocpacket *ioc;
4860 struct megasas_instance *instance;
4861 int error;
bo yang39a98552010-09-22 22:36:29 -04004862 int i;
4863 unsigned long flags;
4864 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004865
4866 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4867 if (!ioc)
4868 return -ENOMEM;
4869
4870 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4871 error = -EFAULT;
4872 goto out_kfree_ioc;
4873 }
4874
4875 instance = megasas_lookup_instance(ioc->host_no);
4876 if (!instance) {
4877 error = -ENODEV;
4878 goto out_kfree_ioc;
4879 }
4880
bo yang39a98552010-09-22 22:36:29 -04004881 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4882 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004883 error = -ENODEV;
4884 goto out_kfree_ioc;
4885 }
4886
4887 if (instance->unload == 1) {
4888 error = -ENODEV;
4889 goto out_kfree_ioc;
4890 }
4891
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004892 /*
4893 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4894 */
4895 if (down_interruptible(&instance->ioctl_sem)) {
4896 error = -ERESTARTSYS;
4897 goto out_kfree_ioc;
4898 }
bo yang39a98552010-09-22 22:36:29 -04004899
4900 for (i = 0; i < wait_time; i++) {
4901
4902 spin_lock_irqsave(&instance->hba_lock, flags);
4903 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4904 spin_unlock_irqrestore(&instance->hba_lock, flags);
4905 break;
4906 }
4907 spin_unlock_irqrestore(&instance->hba_lock, flags);
4908
4909 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4910 printk(KERN_NOTICE "megasas: waiting"
4911 "for controller reset to finish\n");
4912 }
4913
4914 msleep(1000);
4915 }
4916
4917 spin_lock_irqsave(&instance->hba_lock, flags);
4918 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4919 spin_unlock_irqrestore(&instance->hba_lock, flags);
4920
4921 printk(KERN_ERR "megaraid_sas: timed out while"
4922 "waiting for HBA to recover\n");
4923 error = -ENODEV;
4924 goto out_kfree_ioc;
4925 }
4926 spin_unlock_irqrestore(&instance->hba_lock, flags);
4927
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004928 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4929 up(&instance->ioctl_sem);
4930
4931 out_kfree_ioc:
4932 kfree(ioc);
4933 return error;
4934}
4935
4936static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4937{
4938 struct megasas_instance *instance;
4939 struct megasas_aen aen;
4940 int error;
bo yang39a98552010-09-22 22:36:29 -04004941 int i;
4942 unsigned long flags;
4943 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004944
4945 if (file->private_data != file) {
4946 printk(KERN_DEBUG "megasas: fasync_helper was not "
4947 "called first\n");
4948 return -EINVAL;
4949 }
4950
4951 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4952 return -EFAULT;
4953
4954 instance = megasas_lookup_instance(aen.host_no);
4955
4956 if (!instance)
4957 return -ENODEV;
4958
bo yang39a98552010-09-22 22:36:29 -04004959 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4960 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004961 }
4962
4963 if (instance->unload == 1) {
4964 return -ENODEV;
4965 }
4966
bo yang39a98552010-09-22 22:36:29 -04004967 for (i = 0; i < wait_time; i++) {
4968
4969 spin_lock_irqsave(&instance->hba_lock, flags);
4970 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4971 spin_unlock_irqrestore(&instance->hba_lock,
4972 flags);
4973 break;
4974 }
4975
4976 spin_unlock_irqrestore(&instance->hba_lock, flags);
4977
4978 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4979 printk(KERN_NOTICE "megasas: waiting for"
4980 "controller reset to finish\n");
4981 }
4982
4983 msleep(1000);
4984 }
4985
4986 spin_lock_irqsave(&instance->hba_lock, flags);
4987 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4988 spin_unlock_irqrestore(&instance->hba_lock, flags);
4989 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4990 "for HBA to recover.\n");
4991 return -ENODEV;
4992 }
4993 spin_unlock_irqrestore(&instance->hba_lock, flags);
4994
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004995 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004996 error = megasas_register_aen(instance, aen.seq_num,
4997 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004998 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004999 return error;
5000}
5001
5002/**
5003 * megasas_mgmt_ioctl - char node ioctl entry point
5004 */
5005static long
5006megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5007{
5008 switch (cmd) {
5009 case MEGASAS_IOC_FIRMWARE:
5010 return megasas_mgmt_ioctl_fw(file, arg);
5011
5012 case MEGASAS_IOC_GET_AEN:
5013 return megasas_mgmt_ioctl_aen(file, arg);
5014 }
5015
5016 return -ENOTTY;
5017}
5018
5019#ifdef CONFIG_COMPAT
5020static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5021{
5022 struct compat_megasas_iocpacket __user *cioc =
5023 (struct compat_megasas_iocpacket __user *)arg;
5024 struct megasas_iocpacket __user *ioc =
5025 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5026 int i;
5027 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005028 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005029
Jeff Garzik83aabc12006-10-04 06:34:03 -04005030 if (clear_user(ioc, sizeof(*ioc)))
5031 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005032
5033 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5034 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5035 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5036 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5037 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5038 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5039 return -EFAULT;
5040
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005041 /*
5042 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5043 * sense_len is not null, so prepare the 64bit value under
5044 * the same condition.
5045 */
5046 if (ioc->sense_len) {
5047 void __user **sense_ioc_ptr =
5048 (void __user **)(ioc->frame.raw + ioc->sense_off);
5049 compat_uptr_t *sense_cioc_ptr =
5050 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5051 if (get_user(ptr, sense_cioc_ptr) ||
5052 put_user(compat_ptr(ptr), sense_ioc_ptr))
5053 return -EFAULT;
5054 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005055
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005056 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005057 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5058 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5059 copy_in_user(&ioc->sgl[i].iov_len,
5060 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5061 return -EFAULT;
5062 }
5063
5064 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5065
5066 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5067 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5068 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5069 return -EFAULT;
5070 }
5071 return error;
5072}
5073
5074static long
5075megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5076 unsigned long arg)
5077{
5078 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08005079 case MEGASAS_IOC_FIRMWARE32:
5080 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005081 case MEGASAS_IOC_GET_AEN:
5082 return megasas_mgmt_ioctl_aen(file, arg);
5083 }
5084
5085 return -ENOTTY;
5086}
5087#endif
5088
5089/*
5090 * File operations structure for management interface
5091 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08005092static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005093 .owner = THIS_MODULE,
5094 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005095 .fasync = megasas_mgmt_fasync,
5096 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005097 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005098#ifdef CONFIG_COMPAT
5099 .compat_ioctl = megasas_mgmt_compat_ioctl,
5100#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005101 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005102};
5103
5104/*
5105 * PCI hotplug support registration structure
5106 */
5107static struct pci_driver megasas_pci_driver = {
5108
5109 .name = "megaraid_sas",
5110 .id_table = megasas_pci_table,
5111 .probe = megasas_probe_one,
5112 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05005113 .suspend = megasas_suspend,
5114 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005115 .shutdown = megasas_shutdown,
5116};
5117
5118/*
5119 * Sysfs driver attributes
5120 */
5121static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5122{
5123 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5124 MEGASAS_VERSION);
5125}
5126
5127static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5128
5129static ssize_t
5130megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5131{
5132 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5133 MEGASAS_RELDATE);
5134}
5135
5136static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5137 NULL);
5138
Sumant Patro658dced2006-10-03 13:09:14 -07005139static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005140megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5141{
5142 return sprintf(buf, "%u\n", support_poll_for_event);
5143}
5144
5145static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5146 megasas_sysfs_show_support_poll_for_event, NULL);
5147
Yang, Bo837f5fe2010-10-11 06:59:20 -06005148 static ssize_t
5149megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5150{
5151 return sprintf(buf, "%u\n", support_device_change);
5152}
5153
5154static DRIVER_ATTR(support_device_change, S_IRUGO,
5155 megasas_sysfs_show_support_device_change, NULL);
5156
Yang, Bo72c4fd32009-10-06 14:20:59 -06005157static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005158megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5159{
bo yangad84db22007-11-09 04:40:16 -05005160 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005161}
5162
5163static ssize_t
5164megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5165{
5166 int retval = count;
5167 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5168 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5169 retval = -EINVAL;
5170 }
5171 return retval;
5172}
5173
Joe Malicki66dca9b2008-08-14 17:14:48 -04005174static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005175 megasas_sysfs_set_dbg_lvl);
5176
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005177static void
5178megasas_aen_polling(struct work_struct *work)
5179{
5180 struct megasas_aen_event *ev =
5181 container_of(work, struct megasas_aen_event, hotplug_work);
5182 struct megasas_instance *instance = ev->instance;
5183 union megasas_evt_class_locale class_locale;
5184 struct Scsi_Host *host;
5185 struct scsi_device *sdev1;
5186 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005187 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005188 int i, j, doscan = 0;
5189 u32 seq_num;
5190 int error;
5191
5192 if (!instance) {
5193 printk(KERN_ERR "invalid instance!\n");
5194 kfree(ev);
5195 return;
5196 }
5197 instance->ev = NULL;
5198 host = instance->host;
5199 if (instance->evt_detail) {
5200
5201 switch (instance->evt_detail->code) {
5202 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005203 if (megasas_get_pd_list(instance) == 0) {
5204 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5205 for (j = 0;
5206 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5207 j++) {
5208
5209 pd_index =
5210 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5211
5212 sdev1 =
5213 scsi_device_lookup(host, i, j, 0);
5214
5215 if (instance->pd_list[pd_index].driveState
5216 == MR_PD_STATE_SYSTEM) {
5217 if (!sdev1) {
5218 scsi_add_device(host, i, j, 0);
5219 }
5220
5221 if (sdev1)
5222 scsi_device_put(sdev1);
5223 }
5224 }
5225 }
5226 }
5227 doscan = 0;
5228 break;
5229
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005230 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005231 if (megasas_get_pd_list(instance) == 0) {
5232 megasas_get_pd_list(instance);
5233 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5234 for (j = 0;
5235 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5236 j++) {
5237
5238 pd_index =
5239 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5240
5241 sdev1 =
5242 scsi_device_lookup(host, i, j, 0);
5243
5244 if (instance->pd_list[pd_index].driveState
5245 == MR_PD_STATE_SYSTEM) {
5246 if (sdev1) {
5247 scsi_device_put(sdev1);
5248 }
5249 } else {
5250 if (sdev1) {
5251 scsi_remove_device(sdev1);
5252 scsi_device_put(sdev1);
5253 }
5254 }
5255 }
5256 }
5257 }
5258 doscan = 0;
5259 break;
5260
5261 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005262 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005263 case MR_EVT_LD_DELETED:
5264 megasas_get_ld_list(instance);
5265 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5266 for (j = 0;
5267 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5268 j++) {
5269
5270 ld_index =
5271 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5272
5273 sdev1 = scsi_device_lookup(host,
5274 i + MEGASAS_MAX_LD_CHANNELS,
5275 j,
5276 0);
5277
5278 if (instance->ld_ids[ld_index] != 0xff) {
5279 if (sdev1) {
5280 scsi_device_put(sdev1);
5281 }
5282 } else {
5283 if (sdev1) {
5284 scsi_remove_device(sdev1);
5285 scsi_device_put(sdev1);
5286 }
5287 }
5288 }
5289 }
5290 doscan = 0;
5291 break;
5292 case MR_EVT_LD_CREATED:
5293 megasas_get_ld_list(instance);
5294 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5295 for (j = 0;
5296 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5297 j++) {
5298 ld_index =
5299 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5300
5301 sdev1 = scsi_device_lookup(host,
5302 i+MEGASAS_MAX_LD_CHANNELS,
5303 j, 0);
5304
5305 if (instance->ld_ids[ld_index] !=
5306 0xff) {
5307 if (!sdev1) {
5308 scsi_add_device(host,
5309 i + 2,
5310 j, 0);
5311 }
5312 }
5313 if (sdev1) {
5314 scsi_device_put(sdev1);
5315 }
5316 }
5317 }
5318 doscan = 0;
5319 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005320 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005321 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005322 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005323 doscan = 1;
5324 break;
5325 default:
5326 doscan = 0;
5327 break;
5328 }
5329 } else {
5330 printk(KERN_ERR "invalid evt_detail!\n");
5331 kfree(ev);
5332 return;
5333 }
5334
5335 if (doscan) {
5336 printk(KERN_INFO "scanning ...\n");
5337 megasas_get_pd_list(instance);
5338 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5339 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5340 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5341 sdev1 = scsi_device_lookup(host, i, j, 0);
5342 if (instance->pd_list[pd_index].driveState ==
5343 MR_PD_STATE_SYSTEM) {
5344 if (!sdev1) {
5345 scsi_add_device(host, i, j, 0);
5346 }
5347 if (sdev1)
5348 scsi_device_put(sdev1);
5349 } else {
5350 if (sdev1) {
5351 scsi_remove_device(sdev1);
5352 scsi_device_put(sdev1);
5353 }
5354 }
5355 }
5356 }
Yang, Boc9786842009-12-06 08:39:25 -07005357
5358 megasas_get_ld_list(instance);
5359 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5360 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5361 ld_index =
5362 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5363
5364 sdev1 = scsi_device_lookup(host,
5365 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5366 if (instance->ld_ids[ld_index] != 0xff) {
5367 if (!sdev1) {
5368 scsi_add_device(host,
5369 i+2,
5370 j, 0);
5371 } else {
5372 scsi_device_put(sdev1);
5373 }
5374 } else {
5375 if (sdev1) {
5376 scsi_remove_device(sdev1);
5377 scsi_device_put(sdev1);
5378 }
5379 }
5380 }
5381 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005382 }
5383
5384 if ( instance->aen_cmd != NULL ) {
5385 kfree(ev);
5386 return ;
5387 }
5388
5389 seq_num = instance->evt_detail->seq_num + 1;
5390
5391 /* Register AEN with FW for latest sequence number plus 1 */
5392 class_locale.members.reserved = 0;
5393 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5394 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5395 mutex_lock(&instance->aen_mutex);
5396 error = megasas_register_aen(instance, seq_num,
5397 class_locale.word);
5398 mutex_unlock(&instance->aen_mutex);
5399
5400 if (error)
5401 printk(KERN_ERR "register aen failed error %x\n", error);
5402
5403 kfree(ev);
5404}
5405
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005406/**
5407 * megasas_init - Driver load entry point
5408 */
5409static int __init megasas_init(void)
5410{
5411 int rval;
5412
5413 /*
5414 * Announce driver version and other information
5415 */
5416 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5417 MEGASAS_EXT_VERSION);
5418
Yang, Bo72c4fd32009-10-06 14:20:59 -06005419 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005420 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005421
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005422 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5423
5424 /*
5425 * Register character device node
5426 */
5427 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5428
5429 if (rval < 0) {
5430 printk(KERN_DEBUG "megasas: failed to open device node\n");
5431 return rval;
5432 }
5433
5434 megasas_mgmt_majorno = rval;
5435
5436 /*
5437 * Register ourselves as PCI hotplug module
5438 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005439 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005440
5441 if (rval) {
5442 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005443 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005444 }
5445
Jeff Garzik83aabc12006-10-04 06:34:03 -04005446 rval = driver_create_file(&megasas_pci_driver.driver,
5447 &driver_attr_version);
5448 if (rval)
5449 goto err_dcf_attr_ver;
5450 rval = driver_create_file(&megasas_pci_driver.driver,
5451 &driver_attr_release_date);
5452 if (rval)
5453 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005454
5455 rval = driver_create_file(&megasas_pci_driver.driver,
5456 &driver_attr_support_poll_for_event);
5457 if (rval)
5458 goto err_dcf_support_poll_for_event;
5459
Jeff Garzik83aabc12006-10-04 06:34:03 -04005460 rval = driver_create_file(&megasas_pci_driver.driver,
5461 &driver_attr_dbg_lvl);
5462 if (rval)
5463 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005464 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005465 &driver_attr_support_device_change);
5466 if (rval)
5467 goto err_dcf_support_device_change;
5468
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005469 return rval;
bo yangad84db22007-11-09 04:40:16 -05005470
Yang, Bo837f5fe2010-10-11 06:59:20 -06005471err_dcf_support_device_change:
5472 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005473 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005474err_dcf_dbg_lvl:
5475 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005476 &driver_attr_support_poll_for_event);
5477
5478err_dcf_support_poll_for_event:
5479 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005480 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005481
Jeff Garzik83aabc12006-10-04 06:34:03 -04005482err_dcf_rel_date:
5483 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5484err_dcf_attr_ver:
5485 pci_unregister_driver(&megasas_pci_driver);
5486err_pcidrv:
5487 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005488 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005489}
5490
5491/**
5492 * megasas_exit - Driver unload entry point
5493 */
5494static void __exit megasas_exit(void)
5495{
Sumant Patro658dced2006-10-03 13:09:14 -07005496 driver_remove_file(&megasas_pci_driver.driver,
5497 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005498 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005499 &driver_attr_support_poll_for_event);
5500 driver_remove_file(&megasas_pci_driver.driver,
5501 &driver_attr_support_device_change);
5502 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005503 &driver_attr_release_date);
5504 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005505
5506 pci_unregister_driver(&megasas_pci_driver);
5507 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5508}
5509
5510module_init(megasas_init);
5511module_exit(megasas_exit);