blob: 78b4fe84524587fca74d5bc38c06c58816de8ecf [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
adam radford3f1530c2010-12-14 18:51:48 -08002 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003 *
adam radfordae590572012-10-01 19:27:34 -07004 * Copyright (c) 2003-2012 LSI Corporation.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005 *
adam radford3f1530c2010-12-14 18:51:48 -08006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040010 *
adam radford3f1530c2010-12-14 18:51:48 -080011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040015 *
adam radford3f1530c2010-12-14 18:51:48 -080016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040019 *
adam radford3f1530c2010-12-14 18:51:48 -080020 * FILE: megaraid_sas_base.c
adam radford5eca4a62013-02-09 15:29:25 -080021 * Version : v06.506.00.00-rc1
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040022 *
adam radford3f1530c2010-12-14 18:51:48 -080023 * Authors: LSI Corporation
24 * Sreenivas Bagalkote
25 * Sumant Patro
26 * Bo Yang
adam radford00fa2b12011-02-24 20:57:21 -080027 * Adam Radford <linuxraid@lsi.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040028 *
adam radford3f1530c2010-12-14 18:51:48 -080029 * Send feedback to: <megaraidlinux@lsi.com>
30 *
31 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
32 * ATTN: Linuxraid
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040033 */
34
35#include <linux/kernel.h>
36#include <linux/types.h>
37#include <linux/pci.h>
38#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040039#include <linux/moduleparam.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/interrupt.h>
43#include <linux/delay.h>
44#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040046#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010047#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040048#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080049#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010050#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060051#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040052
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070057#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080058#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040059#include "megaraid_sas.h"
60
bo yangad84db22007-11-09 04:40:16 -050061/*
Yang, Bo1fd10682010-10-12 07:18:50 -060062 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
64 */
65static unsigned int max_sectors;
66module_param_named(max_sectors, max_sectors, int, 0);
67MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
69
adam radford80d9da92010-12-21 10:17:40 -080070static int msix_disable;
71module_param(msix_disable, int, S_IRUGO);
72MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73
adam radford079eadd2012-10-01 19:26:59 -070074static unsigned int msix_vectors;
75module_param(msix_vectors, int, S_IRUGO);
76MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77
adam radfordc5daa6a2012-07-17 18:20:03 -070078static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
79module_param(throttlequeuedepth, int, S_IRUGO);
80MODULE_PARM_DESC(throttlequeuedepth,
81 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
82
adam radfordc007b8b2012-07-17 18:20:24 -070083int resetwaittime = MEGASAS_RESET_WAIT_TIME;
84module_param(resetwaittime, int, S_IRUGO);
85MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
86 "before resetting adapter. Default: 180");
87
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040088MODULE_LICENSE("GPL");
89MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080090MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050091MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040092
adam radford058a8fa2011-10-08 18:14:27 -070093int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -040094static int megasas_get_pd_list(struct megasas_instance *instance);
95static int megasas_issue_init_mfi(struct megasas_instance *instance);
96static int megasas_register_aen(struct megasas_instance *instance,
97 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040098/*
99 * PCI ID table for all supported controllers
100 */
101static struct pci_device_id megasas_pci_table[] = {
102
Henrik Kretzschmarf3d7271c2006-08-15 11:17:21 +0200103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
104 /* xscale IOP */
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
106 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
108 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
110 /* gen2*/
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
112 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
114 /* skinny*/
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
116 /* skinny*/
Henrik Kretzschmarf3d7271c2006-08-15 11:17:21 +0200117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
118 /* xscale IOP, vega */
119 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
120 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
122 /* Fusion */
adam radford36807e62011-10-08 18:15:06 -0700123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
124 /* Invader */
Henrik Kretzschmarf3d7271c2006-08-15 11:17:21 +0200125 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400126};
127
128MODULE_DEVICE_TABLE(pci, megasas_pci_table);
129
130static int megasas_mgmt_majorno;
131static struct megasas_mgmt_info megasas_mgmt_info;
132static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100133static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400134
Yang, Boc3518832009-10-06 14:18:02 -0600135static int megasas_poll_wait_aen;
136static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600137static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800138u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600139static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700140
Yang, Boc3518832009-10-06 14:18:02 -0600141/* define lock for aen poll */
142spinlock_t poll_aen_lock;
143
adam radford9c915a82010-12-21 13:34:31 -0800144void
bo yang7343eb62007-11-09 04:35:44 -0500145megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
146 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800147static u32
148megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
149static int
150megasas_adp_reset_gen2(struct megasas_instance *instance,
151 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800152static irqreturn_t megasas_isr(int irq, void *devp);
153static u32
154megasas_init_adapter_mfi(struct megasas_instance *instance);
155u32
156megasas_build_and_issue_cmd(struct megasas_instance *instance,
157 struct scsi_cmnd *scmd);
158static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800159void
160megasas_release_fusion(struct megasas_instance *instance);
161int
162megasas_ioc_init_fusion(struct megasas_instance *instance);
163void
164megasas_free_cmds_fusion(struct megasas_instance *instance);
165u8
166megasas_get_map_info(struct megasas_instance *instance);
167int
168megasas_sync_map_info(struct megasas_instance *instance);
169int
170wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
171void megasas_reset_reply_desc(struct megasas_instance *instance);
172u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
173 struct LD_LOAD_BALANCE_INFO *lbInfo);
174int megasas_reset_fusion(struct Scsi_Host *shost);
175void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800176
177void
178megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
179{
180 instance->instancet->fire_cmd(instance,
181 cmd->frame_phys_addr, 0, instance->reg_set);
182}
bo yang7343eb62007-11-09 04:35:44 -0500183
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400184/**
185 * megasas_get_cmd - Get a command from the free pool
186 * @instance: Adapter soft state
187 *
188 * Returns a free command from the pool
189 */
adam radford9c915a82010-12-21 13:34:31 -0800190struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400191 *instance)
192{
193 unsigned long flags;
194 struct megasas_cmd *cmd = NULL;
195
196 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
197
198 if (!list_empty(&instance->cmd_pool)) {
199 cmd = list_entry((&instance->cmd_pool)->next,
200 struct megasas_cmd, list);
201 list_del_init(&cmd->list);
202 } else {
203 printk(KERN_ERR "megasas: Command pool empty!\n");
204 }
205
206 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
207 return cmd;
208}
209
210/**
211 * megasas_return_cmd - Return a cmd to free command pool
212 * @instance: Adapter soft state
213 * @cmd: Command packet to be returned to free command pool
214 */
adam radford9c915a82010-12-21 13:34:31 -0800215inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400216megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
221
222 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800223 cmd->frame_count = 0;
adam radforde5f93a32011-10-08 18:15:19 -0700224 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
225 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
226 (reset_devices))
227 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400228 list_add_tail(&cmd->list, &instance->cmd_pool);
229
230 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
231}
232
Sumant Patro1341c932006-01-25 12:02:40 -0800233
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400234/**
adam radford0d490162010-12-14 19:17:17 -0800235* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800236* (deviceid : 1064R, PERC5) controllers
237*/
238
239/**
240 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400241 * @regs: MFI register set
242 */
243static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800244megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400245{
bo yang39a98552010-09-22 22:36:29 -0400246 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400247
248 /* Dummy readl to force pci flush */
249 readl(&regs->outbound_intr_mask);
250}
251
252/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700253 * megasas_disable_intr_xscale -Disables interrupt
254 * @regs: MFI register set
255 */
256static inline void
257megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
258{
259 u32 mask = 0x1f;
260 writel(mask, &regs->outbound_intr_mask);
261 /* Dummy readl to force pci flush */
262 readl(&regs->outbound_intr_mask);
263}
264
265/**
Sumant Patro1341c932006-01-25 12:02:40 -0800266 * megasas_read_fw_status_reg_xscale - returns the current FW status value
267 * @regs: MFI register set
268 */
269static u32
270megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
271{
272 return readl(&(regs)->outbound_msg_0);
273}
274/**
275 * megasas_clear_interrupt_xscale - Check & clear interrupt
276 * @regs: MFI register set
277 */
adam radford0d490162010-12-14 19:17:17 -0800278static int
Sumant Patro1341c932006-01-25 12:02:40 -0800279megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
280{
281 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400282 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800283 /*
284 * Check if it is our interrupt
285 */
286 status = readl(&regs->outbound_intr_status);
287
bo yang39a98552010-09-22 22:36:29 -0400288 if (status & MFI_OB_INTR_STATUS_MASK)
289 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
290 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
291 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800292
293 /*
294 * Clear the interrupt by writing back the same value
295 */
bo yang39a98552010-09-22 22:36:29 -0400296 if (mfiStatus)
297 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800298
Yang, Bo06f579d2008-08-10 12:42:37 -0700299 /* Dummy readl to force pci flush */
300 readl(&regs->outbound_intr_status);
301
bo yang39a98552010-09-22 22:36:29 -0400302 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800303}
304
305/**
306 * megasas_fire_cmd_xscale - Sends command to the FW
307 * @frame_phys_addr : Physical address of cmd
308 * @frame_count : Number of frames for the command
309 * @regs : MFI register set
310 */
adam radford0d490162010-12-14 19:17:17 -0800311static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600312megasas_fire_cmd_xscale(struct megasas_instance *instance,
313 dma_addr_t frame_phys_addr,
314 u32 frame_count,
315 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800316{
bo yang39a98552010-09-22 22:36:29 -0400317 unsigned long flags;
318 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800319 writel((frame_phys_addr >> 3)|(frame_count),
320 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400321 spin_unlock_irqrestore(&instance->hba_lock, flags);
322}
323
324/**
325 * megasas_adp_reset_xscale - For controller reset
326 * @regs: MFI register set
327 */
328static int
329megasas_adp_reset_xscale(struct megasas_instance *instance,
330 struct megasas_register_set __iomem *regs)
331{
332 u32 i;
333 u32 pcidata;
334 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
335
336 for (i = 0; i < 3; i++)
337 msleep(1000); /* sleep for 3 secs */
338 pcidata = 0;
339 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
340 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
341 if (pcidata & 0x2) {
342 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
343 pcidata &= ~0x2;
344 pci_write_config_dword(instance->pdev,
345 MFI_1068_PCSR_OFFSET, pcidata);
346
347 for (i = 0; i < 2; i++)
348 msleep(1000); /* need to wait 2 secs again */
349
350 pcidata = 0;
351 pci_read_config_dword(instance->pdev,
352 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
353 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
354 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
355 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
356 pcidata = 0;
357 pci_write_config_dword(instance->pdev,
358 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
359 }
360 }
361 return 0;
362}
363
364/**
365 * megasas_check_reset_xscale - For controller reset check
366 * @regs: MFI register set
367 */
368static int
369megasas_check_reset_xscale(struct megasas_instance *instance,
370 struct megasas_register_set __iomem *regs)
371{
372 u32 consumer;
373 consumer = *instance->consumer;
374
375 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
376 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
377 return 1;
378 }
379 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800380}
381
382static struct megasas_instance_template megasas_instance_template_xscale = {
383
384 .fire_cmd = megasas_fire_cmd_xscale,
385 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700386 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800387 .clear_intr = megasas_clear_intr_xscale,
388 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400389 .adp_reset = megasas_adp_reset_xscale,
390 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800391 .service_isr = megasas_isr,
392 .tasklet = megasas_complete_cmd_dpc,
393 .init_adapter = megasas_init_adapter_mfi,
394 .build_and_issue_cmd = megasas_build_and_issue_cmd,
395 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800396};
397
398/**
adam radford0d490162010-12-14 19:17:17 -0800399* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800400* to xscale (deviceid : 1064R, PERC5) controllers
401*/
402
403/**
adam radford0d490162010-12-14 19:17:17 -0800404* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800405* controllers
406*/
407
408/**
409 * megasas_enable_intr_ppc - Enables interrupts
410 * @regs: MFI register set
411 */
412static inline void
413megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
414{
415 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800416
bo yang39a98552010-09-22 22:36:29 -0400417 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800418
419 /* Dummy readl to force pci flush */
420 readl(&regs->outbound_intr_mask);
421}
422
423/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700424 * megasas_disable_intr_ppc - Disable interrupt
425 * @regs: MFI register set
426 */
427static inline void
428megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
429{
430 u32 mask = 0xFFFFFFFF;
431 writel(mask, &regs->outbound_intr_mask);
432 /* Dummy readl to force pci flush */
433 readl(&regs->outbound_intr_mask);
434}
435
436/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800437 * megasas_read_fw_status_reg_ppc - returns the current FW status value
438 * @regs: MFI register set
439 */
440static u32
441megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
442{
443 return readl(&(regs)->outbound_scratch_pad);
444}
445
446/**
447 * megasas_clear_interrupt_ppc - Check & clear interrupt
448 * @regs: MFI register set
449 */
adam radford0d490162010-12-14 19:17:17 -0800450static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800451megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
452{
adam radford3cc6851f92011-05-11 18:34:52 -0700453 u32 status, mfiStatus = 0;
454
Sumant Patrof9876f02006-02-03 15:34:35 -0800455 /*
456 * Check if it is our interrupt
457 */
458 status = readl(&regs->outbound_intr_status);
459
adam radford3cc6851f92011-05-11 18:34:52 -0700460 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
461 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
462
463 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
464 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800465
466 /*
467 * Clear the interrupt by writing back the same value
468 */
469 writel(status, &regs->outbound_doorbell_clear);
470
Yang, Bo06f579d2008-08-10 12:42:37 -0700471 /* Dummy readl to force pci flush */
472 readl(&regs->outbound_doorbell_clear);
473
adam radford3cc6851f92011-05-11 18:34:52 -0700474 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800475}
adam radford3cc6851f92011-05-11 18:34:52 -0700476
Sumant Patrof9876f02006-02-03 15:34:35 -0800477/**
478 * megasas_fire_cmd_ppc - Sends command to the FW
479 * @frame_phys_addr : Physical address of cmd
480 * @frame_count : Number of frames for the command
481 * @regs : MFI register set
482 */
adam radford0d490162010-12-14 19:17:17 -0800483static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600484megasas_fire_cmd_ppc(struct megasas_instance *instance,
485 dma_addr_t frame_phys_addr,
486 u32 frame_count,
487 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800488{
bo yang39a98552010-09-22 22:36:29 -0400489 unsigned long flags;
490 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800491 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800492 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400493 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800494}
495
bo yang39a98552010-09-22 22:36:29 -0400496/**
bo yang39a98552010-09-22 22:36:29 -0400497 * megasas_check_reset_ppc - For controller reset check
498 * @regs: MFI register set
499 */
500static int
501megasas_check_reset_ppc(struct megasas_instance *instance,
502 struct megasas_register_set __iomem *regs)
503{
adam radford3cc6851f92011-05-11 18:34:52 -0700504 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
505 return 1;
506
bo yang39a98552010-09-22 22:36:29 -0400507 return 0;
508}
adam radford3cc6851f92011-05-11 18:34:52 -0700509
Sumant Patrof9876f02006-02-03 15:34:35 -0800510static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800511
Sumant Patrof9876f02006-02-03 15:34:35 -0800512 .fire_cmd = megasas_fire_cmd_ppc,
513 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700514 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800515 .clear_intr = megasas_clear_intr_ppc,
516 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700517 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400518 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800519 .service_isr = megasas_isr,
520 .tasklet = megasas_complete_cmd_dpc,
521 .init_adapter = megasas_init_adapter_mfi,
522 .build_and_issue_cmd = megasas_build_and_issue_cmd,
523 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800524};
525
526/**
Yang, Bo87911122009-10-06 14:31:54 -0600527 * megasas_enable_intr_skinny - Enables interrupts
528 * @regs: MFI register set
529 */
530static inline void
531megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
532{
533 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
534
535 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
536
537 /* Dummy readl to force pci flush */
538 readl(&regs->outbound_intr_mask);
539}
540
541/**
542 * megasas_disable_intr_skinny - Disables interrupt
543 * @regs: MFI register set
544 */
545static inline void
546megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
547{
548 u32 mask = 0xFFFFFFFF;
549 writel(mask, &regs->outbound_intr_mask);
550 /* Dummy readl to force pci flush */
551 readl(&regs->outbound_intr_mask);
552}
553
554/**
555 * megasas_read_fw_status_reg_skinny - returns the current FW status value
556 * @regs: MFI register set
557 */
558static u32
559megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
560{
561 return readl(&(regs)->outbound_scratch_pad);
562}
563
564/**
565 * megasas_clear_interrupt_skinny - Check & clear interrupt
566 * @regs: MFI register set
567 */
568static int
569megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
570{
571 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800572 u32 mfiStatus = 0;
573
Yang, Bo87911122009-10-06 14:31:54 -0600574 /*
575 * Check if it is our interrupt
576 */
577 status = readl(&regs->outbound_intr_status);
578
579 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400580 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600581 }
582
583 /*
adam radfordebf054b2011-02-24 20:57:15 -0800584 * Check if it is our interrupt
585 */
586 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
587 MFI_STATE_FAULT) {
588 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
589 } else
590 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
591
592 /*
Yang, Bo87911122009-10-06 14:31:54 -0600593 * Clear the interrupt by writing back the same value
594 */
595 writel(status, &regs->outbound_intr_status);
596
597 /*
598 * dummy read to flush PCI
599 */
600 readl(&regs->outbound_intr_status);
601
adam radfordebf054b2011-02-24 20:57:15 -0800602 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600603}
604
605/**
606 * megasas_fire_cmd_skinny - Sends command to the FW
607 * @frame_phys_addr : Physical address of cmd
608 * @frame_count : Number of frames for the command
609 * @regs : MFI register set
610 */
611static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600612megasas_fire_cmd_skinny(struct megasas_instance *instance,
613 dma_addr_t frame_phys_addr,
614 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600615 struct megasas_register_set __iomem *regs)
616{
Yang, Bo0c79e682009-10-06 14:47:35 -0600617 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400618 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600619 writel(0, &(regs)->inbound_high_queue_port);
620 writel((frame_phys_addr | (frame_count<<1))|1,
621 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400622 spin_unlock_irqrestore(&instance->hba_lock, flags);
623}
624
625/**
bo yang39a98552010-09-22 22:36:29 -0400626 * megasas_check_reset_skinny - For controller reset check
627 * @regs: MFI register set
628 */
629static int
630megasas_check_reset_skinny(struct megasas_instance *instance,
631 struct megasas_register_set __iomem *regs)
632{
adam radford3cc6851f92011-05-11 18:34:52 -0700633 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
634 return 1;
635
bo yang39a98552010-09-22 22:36:29 -0400636 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600637}
638
639static struct megasas_instance_template megasas_instance_template_skinny = {
640
641 .fire_cmd = megasas_fire_cmd_skinny,
642 .enable_intr = megasas_enable_intr_skinny,
643 .disable_intr = megasas_disable_intr_skinny,
644 .clear_intr = megasas_clear_intr_skinny,
645 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800646 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400647 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800648 .service_isr = megasas_isr,
649 .tasklet = megasas_complete_cmd_dpc,
650 .init_adapter = megasas_init_adapter_mfi,
651 .build_and_issue_cmd = megasas_build_and_issue_cmd,
652 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600653};
654
655
656/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700657* The following functions are defined for gen2 (deviceid : 0x78 0x79)
658* controllers
659*/
660
661/**
662 * megasas_enable_intr_gen2 - Enables interrupts
663 * @regs: MFI register set
664 */
665static inline void
666megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
667{
668 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
669
670 /* write ~0x00000005 (4 & 1) to the intr mask*/
671 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
672
673 /* Dummy readl to force pci flush */
674 readl(&regs->outbound_intr_mask);
675}
676
677/**
678 * megasas_disable_intr_gen2 - Disables interrupt
679 * @regs: MFI register set
680 */
681static inline void
682megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
683{
684 u32 mask = 0xFFFFFFFF;
685 writel(mask, &regs->outbound_intr_mask);
686 /* Dummy readl to force pci flush */
687 readl(&regs->outbound_intr_mask);
688}
689
690/**
691 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
692 * @regs: MFI register set
693 */
694static u32
695megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
696{
697 return readl(&(regs)->outbound_scratch_pad);
698}
699
700/**
701 * megasas_clear_interrupt_gen2 - Check & clear interrupt
702 * @regs: MFI register set
703 */
704static int
705megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
706{
707 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400708 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700709 /*
710 * Check if it is our interrupt
711 */
712 status = readl(&regs->outbound_intr_status);
713
bo yang39a98552010-09-22 22:36:29 -0400714 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
715 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
716 }
717 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
718 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
719 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700720
721 /*
722 * Clear the interrupt by writing back the same value
723 */
bo yang39a98552010-09-22 22:36:29 -0400724 if (mfiStatus)
725 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700726
727 /* Dummy readl to force pci flush */
728 readl(&regs->outbound_intr_status);
729
bo yang39a98552010-09-22 22:36:29 -0400730 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700731}
732/**
733 * megasas_fire_cmd_gen2 - Sends command to the FW
734 * @frame_phys_addr : Physical address of cmd
735 * @frame_count : Number of frames for the command
736 * @regs : MFI register set
737 */
738static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600739megasas_fire_cmd_gen2(struct megasas_instance *instance,
740 dma_addr_t frame_phys_addr,
741 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700742 struct megasas_register_set __iomem *regs)
743{
bo yang39a98552010-09-22 22:36:29 -0400744 unsigned long flags;
745 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700746 writel((frame_phys_addr | (frame_count<<1))|1,
747 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400748 spin_unlock_irqrestore(&instance->hba_lock, flags);
749}
750
751/**
752 * megasas_adp_reset_gen2 - For controller reset
753 * @regs: MFI register set
754 */
755static int
756megasas_adp_reset_gen2(struct megasas_instance *instance,
757 struct megasas_register_set __iomem *reg_set)
758{
759 u32 retry = 0 ;
760 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800761 u32 *seq_offset = &reg_set->seq_offset;
762 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400763
adam radfordebf054b2011-02-24 20:57:15 -0800764 if (instance->instancet == &megasas_instance_template_skinny) {
765 seq_offset = &reg_set->fusion_seq_offset;
766 hostdiag_offset = &reg_set->fusion_host_diag;
767 }
768
769 writel(0, seq_offset);
770 writel(4, seq_offset);
771 writel(0xb, seq_offset);
772 writel(2, seq_offset);
773 writel(7, seq_offset);
774 writel(0xd, seq_offset);
775
bo yang39a98552010-09-22 22:36:29 -0400776 msleep(1000);
777
adam radfordebf054b2011-02-24 20:57:15 -0800778 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400779
780 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
781 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800782 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400783 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
784 retry, HostDiag);
785
786 if (retry++ >= 100)
787 return 1;
788
789 }
790
791 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
792
adam radfordebf054b2011-02-24 20:57:15 -0800793 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400794
795 ssleep(10);
796
adam radfordebf054b2011-02-24 20:57:15 -0800797 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400798 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
799 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800800 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400801 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
802 retry, HostDiag);
803
804 if (retry++ >= 1000)
805 return 1;
806
807 }
808 return 0;
809}
810
811/**
812 * megasas_check_reset_gen2 - For controller reset check
813 * @regs: MFI register set
814 */
815static int
816megasas_check_reset_gen2(struct megasas_instance *instance,
817 struct megasas_register_set __iomem *regs)
818{
Yang, Bo707e09b2010-10-12 07:20:27 -0600819 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
820 return 1;
821 }
822
bo yang39a98552010-09-22 22:36:29 -0400823 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700824}
825
826static struct megasas_instance_template megasas_instance_template_gen2 = {
827
828 .fire_cmd = megasas_fire_cmd_gen2,
829 .enable_intr = megasas_enable_intr_gen2,
830 .disable_intr = megasas_disable_intr_gen2,
831 .clear_intr = megasas_clear_intr_gen2,
832 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400833 .adp_reset = megasas_adp_reset_gen2,
834 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800835 .service_isr = megasas_isr,
836 .tasklet = megasas_complete_cmd_dpc,
837 .init_adapter = megasas_init_adapter_mfi,
838 .build_and_issue_cmd = megasas_build_and_issue_cmd,
839 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700840};
841
842/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800843* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400844* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800845*/
846
adam radford9c915a82010-12-21 13:34:31 -0800847/*
848 * Template added for TB (Fusion)
849 */
850extern struct megasas_instance_template megasas_instance_template_fusion;
851
Sumant Patrof9876f02006-02-03 15:34:35 -0800852/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400853 * megasas_issue_polled - Issues a polling command
854 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800855 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400856 *
857 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
858 */
adam radford9c915a82010-12-21 13:34:31 -0800859int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400860megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
861{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400862
863 struct megasas_header *frame_hdr = &cmd->frame->hdr;
864
865 frame_hdr->cmd_status = 0xFF;
866 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
867
868 /*
869 * Issue the frame using inbound queue port
870 */
adam radford9c915a82010-12-21 13:34:31 -0800871 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400872
873 /*
874 * Wait for cmd_status to change
875 */
adam radford9c915a82010-12-21 13:34:31 -0800876 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400877}
878
879/**
880 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
881 * @instance: Adapter soft state
882 * @cmd: Command to be issued
883 *
884 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700885 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400886 * Used to issue ioctl commands.
887 */
888static int
889megasas_issue_blocked_cmd(struct megasas_instance *instance,
890 struct megasas_cmd *cmd)
891{
892 cmd->cmd_status = ENODATA;
893
adam radford9c915a82010-12-21 13:34:31 -0800894 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400895
bo yang39a98552010-09-22 22:36:29 -0400896 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400897
898 return 0;
899}
900
901/**
902 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
903 * @instance: Adapter soft state
904 * @cmd_to_abort: Previously issued cmd to be aborted
905 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100906 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400907 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700908 * cmd and waits for return status.
909 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400910 */
911static int
912megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
913 struct megasas_cmd *cmd_to_abort)
914{
915 struct megasas_cmd *cmd;
916 struct megasas_abort_frame *abort_fr;
917
918 cmd = megasas_get_cmd(instance);
919
920 if (!cmd)
921 return -1;
922
923 abort_fr = &cmd->frame->abort;
924
925 /*
926 * Prepare and issue the abort frame
927 */
928 abort_fr->cmd = MFI_CMD_ABORT;
929 abort_fr->cmd_status = 0xFF;
930 abort_fr->flags = 0;
931 abort_fr->abort_context = cmd_to_abort->index;
932 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
933 abort_fr->abort_mfi_phys_addr_hi = 0;
934
935 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com72f7a5c2014-11-17 15:24:23 +0530936 cmd->cmd_status = ENODATA;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400937
adam radford9c915a82010-12-21 13:34:31 -0800938 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400939
940 /*
941 * Wait for this cmd to complete
942 */
bo yang39a98552010-09-22 22:36:29 -0400943 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
944 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400945
946 megasas_return_cmd(instance, cmd);
947 return 0;
948}
949
950/**
951 * megasas_make_sgl32 - Prepares 32-bit SGL
952 * @instance: Adapter soft state
953 * @scp: SCSI command from the mid-layer
954 * @mfi_sgl: SGL to be filled in
955 *
956 * If successful, this function returns the number of SG elements. Otherwise,
957 * it returnes -1.
958 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800959static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400960megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
961 union megasas_sgl *mfi_sgl)
962{
963 int i;
964 int sge_count;
965 struct scatterlist *os_sgl;
966
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900967 sge_count = scsi_dma_map(scp);
968 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400969
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900970 if (sge_count) {
971 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
972 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
973 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
974 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400975 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400976 return sge_count;
977}
978
979/**
980 * megasas_make_sgl64 - Prepares 64-bit SGL
981 * @instance: Adapter soft state
982 * @scp: SCSI command from the mid-layer
983 * @mfi_sgl: SGL to be filled in
984 *
985 * If successful, this function returns the number of SG elements. Otherwise,
986 * it returnes -1.
987 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800988static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
990 union megasas_sgl *mfi_sgl)
991{
992 int i;
993 int sge_count;
994 struct scatterlist *os_sgl;
995
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900996 sge_count = scsi_dma_map(scp);
997 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900999 if (sge_count) {
1000 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1001 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
1002 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1003 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001004 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001005 return sge_count;
1006}
1007
Yang, Bof4c9a132009-10-06 14:43:28 -06001008/**
1009 * megasas_make_sgl_skinny - Prepares IEEE SGL
1010 * @instance: Adapter soft state
1011 * @scp: SCSI command from the mid-layer
1012 * @mfi_sgl: SGL to be filled in
1013 *
1014 * If successful, this function returns the number of SG elements. Otherwise,
1015 * it returnes -1.
1016 */
1017static int
1018megasas_make_sgl_skinny(struct megasas_instance *instance,
1019 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1020{
1021 int i;
1022 int sge_count;
1023 struct scatterlist *os_sgl;
1024
1025 sge_count = scsi_dma_map(scp);
1026
1027 if (sge_count) {
1028 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1029 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1030 mfi_sgl->sge_skinny[i].phys_addr =
1031 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001032 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001033 }
1034 }
1035 return sge_count;
1036}
1037
Sumant Patrob1df99d2006-10-03 12:40:47 -07001038 /**
1039 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001040 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001041 * @sge_count : number of sg elements
1042 *
1043 * Returns the number of frames required for numnber of sge's (sge_count)
1044 */
1045
Yang, Bof4c9a132009-10-06 14:43:28 -06001046static u32 megasas_get_frame_count(struct megasas_instance *instance,
1047 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001048{
1049 int num_cnt;
1050 int sge_bytes;
1051 u32 sge_sz;
1052 u32 frame_count=0;
1053
1054 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1055 sizeof(struct megasas_sge32);
1056
Yang, Bof4c9a132009-10-06 14:43:28 -06001057 if (instance->flag_ieee) {
1058 sge_sz = sizeof(struct megasas_sge_skinny);
1059 }
1060
Sumant Patrob1df99d2006-10-03 12:40:47 -07001061 /*
bo yangd532dbe2008-03-17 03:36:43 -04001062 * Main frame can contain 2 SGEs for 64-bit SGLs and
1063 * 3 SGEs for 32-bit SGLs for ldio &
1064 * 1 SGEs for 64-bit SGLs and
1065 * 2 SGEs for 32-bit SGLs for pthru frame
1066 */
1067 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001068 if (instance->flag_ieee == 1) {
1069 num_cnt = sge_count - 1;
1070 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001071 num_cnt = sge_count - 1;
1072 else
1073 num_cnt = sge_count - 2;
1074 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001075 if (instance->flag_ieee == 1) {
1076 num_cnt = sge_count - 1;
1077 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001078 num_cnt = sge_count - 2;
1079 else
1080 num_cnt = sge_count - 3;
1081 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001082
1083 if(num_cnt>0){
1084 sge_bytes = sge_sz * num_cnt;
1085
1086 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1087 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1088 }
1089 /* Main frame */
1090 frame_count +=1;
1091
1092 if (frame_count > 7)
1093 frame_count = 8;
1094 return frame_count;
1095}
1096
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001097/**
1098 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1099 * @instance: Adapter soft state
1100 * @scp: SCSI command
1101 * @cmd: Command to be prepared in
1102 *
1103 * This function prepares CDB commands. These are typcially pass-through
1104 * commands to the devices.
1105 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001106static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001107megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1108 struct megasas_cmd *cmd)
1109{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001110 u32 is_logical;
1111 u32 device_id;
1112 u16 flags = 0;
1113 struct megasas_pthru_frame *pthru;
1114
1115 is_logical = MEGASAS_IS_LOGICAL(scp);
1116 device_id = MEGASAS_DEV_INDEX(instance, scp);
1117 pthru = (struct megasas_pthru_frame *)cmd->frame;
1118
1119 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1120 flags = MFI_FRAME_DIR_WRITE;
1121 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1122 flags = MFI_FRAME_DIR_READ;
1123 else if (scp->sc_data_direction == PCI_DMA_NONE)
1124 flags = MFI_FRAME_DIR_NONE;
1125
Yang, Bof4c9a132009-10-06 14:43:28 -06001126 if (instance->flag_ieee == 1) {
1127 flags |= MFI_FRAME_IEEE;
1128 }
1129
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001130 /*
1131 * Prepare the DCDB frame
1132 */
1133 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1134 pthru->cmd_status = 0x0;
1135 pthru->scsi_status = 0x0;
1136 pthru->target_id = device_id;
1137 pthru->lun = scp->device->lun;
1138 pthru->cdb_len = scp->cmd_len;
1139 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001140 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001141 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001142 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001143
1144 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1145
1146 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001147 * If the command is for the tape device, set the
1148 * pthru timeout to the os layer timeout value.
1149 */
1150 if (scp->device->type == TYPE_TAPE) {
1151 if ((scp->request->timeout / HZ) > 0xFFFF)
1152 pthru->timeout = 0xFFFF;
1153 else
1154 pthru->timeout = scp->request->timeout / HZ;
1155 }
1156
1157 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001158 * Construct SGL
1159 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001160 if (instance->flag_ieee == 1) {
1161 pthru->flags |= MFI_FRAME_SGL64;
1162 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1163 &pthru->sgl);
1164 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165 pthru->flags |= MFI_FRAME_SGL64;
1166 pthru->sge_count = megasas_make_sgl64(instance, scp,
1167 &pthru->sgl);
1168 } else
1169 pthru->sge_count = megasas_make_sgl32(instance, scp,
1170 &pthru->sgl);
1171
Yang, Bobdc6fb82009-12-06 08:30:19 -07001172 if (pthru->sge_count > instance->max_num_sge) {
1173 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1174 pthru->sge_count);
1175 return 0;
1176 }
1177
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001178 /*
1179 * Sense info specific
1180 */
1181 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1182 pthru->sense_buf_phys_addr_hi = 0;
1183 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1184
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001185 /*
1186 * Compute the total number of frames this command consumes. FW uses
1187 * this number to pull sufficient number of frames from host memory.
1188 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001189 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001190 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001191
1192 return cmd->frame_count;
1193}
1194
1195/**
1196 * megasas_build_ldio - Prepares IOs to logical devices
1197 * @instance: Adapter soft state
1198 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001199 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001200 *
1201 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1202 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001203static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001204megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1205 struct megasas_cmd *cmd)
1206{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001207 u32 device_id;
1208 u8 sc = scp->cmnd[0];
1209 u16 flags = 0;
1210 struct megasas_io_frame *ldio;
1211
1212 device_id = MEGASAS_DEV_INDEX(instance, scp);
1213 ldio = (struct megasas_io_frame *)cmd->frame;
1214
1215 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1216 flags = MFI_FRAME_DIR_WRITE;
1217 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1218 flags = MFI_FRAME_DIR_READ;
1219
Yang, Bof4c9a132009-10-06 14:43:28 -06001220 if (instance->flag_ieee == 1) {
1221 flags |= MFI_FRAME_IEEE;
1222 }
1223
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001224 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001225 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001226 */
1227 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1228 ldio->cmd_status = 0x0;
1229 ldio->scsi_status = 0x0;
1230 ldio->target_id = device_id;
1231 ldio->timeout = 0;
1232 ldio->reserved_0 = 0;
1233 ldio->pad_0 = 0;
1234 ldio->flags = flags;
1235 ldio->start_lba_hi = 0;
1236 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1237
1238 /*
1239 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1240 */
1241 if (scp->cmd_len == 6) {
1242 ldio->lba_count = (u32) scp->cmnd[4];
1243 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1244 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1245
1246 ldio->start_lba_lo &= 0x1FFFFF;
1247 }
1248
1249 /*
1250 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1251 */
1252 else if (scp->cmd_len == 10) {
1253 ldio->lba_count = (u32) scp->cmnd[8] |
1254 ((u32) scp->cmnd[7] << 8);
1255 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1256 ((u32) scp->cmnd[3] << 16) |
1257 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1258 }
1259
1260 /*
1261 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1262 */
1263 else if (scp->cmd_len == 12) {
1264 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1265 ((u32) scp->cmnd[7] << 16) |
1266 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1267
1268 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1269 ((u32) scp->cmnd[3] << 16) |
1270 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1271 }
1272
1273 /*
1274 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1275 */
1276 else if (scp->cmd_len == 16) {
1277 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1278 ((u32) scp->cmnd[11] << 16) |
1279 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1280
1281 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1282 ((u32) scp->cmnd[7] << 16) |
1283 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1284
1285 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1286 ((u32) scp->cmnd[3] << 16) |
1287 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1288
1289 }
1290
1291 /*
1292 * Construct SGL
1293 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001294 if (instance->flag_ieee) {
1295 ldio->flags |= MFI_FRAME_SGL64;
1296 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1297 &ldio->sgl);
1298 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001299 ldio->flags |= MFI_FRAME_SGL64;
1300 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1301 } else
1302 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1303
Yang, Bobdc6fb82009-12-06 08:30:19 -07001304 if (ldio->sge_count > instance->max_num_sge) {
1305 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1306 ldio->sge_count);
1307 return 0;
1308 }
1309
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001310 /*
1311 * Sense info specific
1312 */
1313 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1314 ldio->sense_buf_phys_addr_hi = 0;
1315 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1316
Sumant Patrob1df99d2006-10-03 12:40:47 -07001317 /*
1318 * Compute the total number of frames this command consumes. FW uses
1319 * this number to pull sufficient number of frames from host memory.
1320 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001321 cmd->frame_count = megasas_get_frame_count(instance,
1322 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001323
1324 return cmd->frame_count;
1325}
1326
1327/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001328 * megasas_is_ldio - Checks if the cmd is for logical drive
1329 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001330 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001331 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001332 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001333 */
adam radford9c915a82010-12-21 13:34:31 -08001334inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001335{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001336 if (!MEGASAS_IS_LOGICAL(cmd))
1337 return 0;
1338 switch (cmd->cmnd[0]) {
1339 case READ_10:
1340 case WRITE_10:
1341 case READ_12:
1342 case WRITE_12:
1343 case READ_6:
1344 case WRITE_6:
1345 case READ_16:
1346 case WRITE_16:
1347 return 1;
1348 default:
1349 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001350 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001351}
1352
Sumant Patro658dced2006-10-03 13:09:14 -07001353 /**
1354 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1355 * in FW
1356 * @instance: Adapter soft state
1357 */
1358static inline void
1359megasas_dump_pending_frames(struct megasas_instance *instance)
1360{
1361 struct megasas_cmd *cmd;
1362 int i,n;
1363 union megasas_sgl *mfi_sgl;
1364 struct megasas_io_frame *ldio;
1365 struct megasas_pthru_frame *pthru;
1366 u32 sgcount;
1367 u32 max_cmd = instance->max_fw_cmds;
1368
1369 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1370 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1371 if (IS_DMA64)
1372 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1373 else
1374 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1375
1376 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1377 for (i = 0; i < max_cmd; i++) {
1378 cmd = instance->cmd_list[i];
1379 if(!cmd->scmd)
1380 continue;
1381 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1382 if (megasas_is_ldio(cmd->scmd)){
1383 ldio = (struct megasas_io_frame *)cmd->frame;
1384 mfi_sgl = &ldio->sgl;
1385 sgcount = ldio->sge_count;
1386 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
1387 }
1388 else {
1389 pthru = (struct megasas_pthru_frame *) cmd->frame;
1390 mfi_sgl = &pthru->sgl;
1391 sgcount = pthru->sge_count;
1392 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
1393 }
1394 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1395 for (n = 0; n < sgcount; n++){
1396 if (IS_DMA64)
1397 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
1398 else
1399 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1400 }
1401 }
1402 printk(KERN_ERR "\n");
1403 } /*for max_cmd*/
1404 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1405 for (i = 0; i < max_cmd; i++) {
1406
1407 cmd = instance->cmd_list[i];
1408
1409 if(cmd->sync_cmd == 1){
1410 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1411 }
1412 }
1413 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1414}
1415
adam radfordcd50ba82010-12-21 10:23:23 -08001416u32
1417megasas_build_and_issue_cmd(struct megasas_instance *instance,
1418 struct scsi_cmnd *scmd)
1419{
1420 struct megasas_cmd *cmd;
1421 u32 frame_count;
1422
1423 cmd = megasas_get_cmd(instance);
1424 if (!cmd)
1425 return SCSI_MLQUEUE_HOST_BUSY;
1426
1427 /*
1428 * Logical drive command
1429 */
1430 if (megasas_is_ldio(scmd))
1431 frame_count = megasas_build_ldio(instance, scmd, cmd);
1432 else
1433 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1434
1435 if (!frame_count)
1436 goto out_return_cmd;
1437
1438 cmd->scmd = scmd;
1439 scmd->SCp.ptr = (char *)cmd;
1440
1441 /*
1442 * Issue the command to the FW
1443 */
1444 atomic_inc(&instance->fw_outstanding);
1445
1446 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1447 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001448
1449 return 0;
1450out_return_cmd:
1451 megasas_return_cmd(instance, cmd);
1452 return 1;
1453}
1454
1455
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001456/**
1457 * megasas_queue_command - Queue entry point
1458 * @scmd: SCSI command to be queued
1459 * @done: Callback entry point
1460 */
1461static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001462megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001463{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001465 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001466
1467 instance = (struct megasas_instance *)
1468 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001469
bo yang39a98552010-09-22 22:36:29 -04001470 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001471 return SCSI_MLQUEUE_HOST_BUSY;
1472
bo yang39a98552010-09-22 22:36:29 -04001473 spin_lock_irqsave(&instance->hba_lock, flags);
1474 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1475 spin_unlock_irqrestore(&instance->hba_lock, flags);
1476 return SCSI_MLQUEUE_HOST_BUSY;
1477 }
1478
1479 spin_unlock_irqrestore(&instance->hba_lock, flags);
1480
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001481 scmd->scsi_done = done;
1482 scmd->result = 0;
1483
Sumant Patrocb59aa62006-01-25 11:53:25 -08001484 if (MEGASAS_IS_LOGICAL(scmd) &&
1485 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1486 scmd->result = DID_BAD_TARGET << 16;
1487 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001488 }
1489
Sumant Patro02b01e02007-02-14 13:00:55 -08001490 switch (scmd->cmnd[0]) {
1491 case SYNCHRONIZE_CACHE:
1492 /*
1493 * FW takes care of flush cache on its own
1494 * No need to send it down
1495 */
1496 scmd->result = DID_OK << 16;
1497 goto out_done;
1498 default:
1499 break;
1500 }
1501
adam radfordcd50ba82010-12-21 10:23:23 -08001502 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1503 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001504 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001505 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001506
1507 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001508
Sumant Patrocb59aa62006-01-25 11:53:25 -08001509 out_done:
1510 done(scmd);
1511 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001512}
1513
Jeff Garzikf2812332010-11-16 02:10:29 -05001514static DEF_SCSI_QCMD(megasas_queue_command)
1515
Yang, Bo044833b2009-10-06 14:33:06 -06001516static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1517{
1518 int i;
1519
1520 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1521
1522 if ((megasas_mgmt_info.instance[i]) &&
1523 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1524 return megasas_mgmt_info.instance[i];
1525 }
1526
1527 return NULL;
1528}
1529
Christoph Hellwig147aab62006-02-17 12:13:48 +01001530static int megasas_slave_configure(struct scsi_device *sdev)
1531{
Yang, Bo044833b2009-10-06 14:33:06 -06001532 u16 pd_index = 0;
1533 struct megasas_instance *instance ;
1534
1535 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001536
1537 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001538 * Don't export physical disk devices to the disk driver.
1539 *
1540 * FIXME: Currently we don't export them to the midlayer at all.
1541 * That will be fixed once LSI engineers have audited the
1542 * firmware for possible issues.
1543 */
1544 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1545 sdev->type == TYPE_DISK) {
1546 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1547 sdev->id;
1548 if (instance->pd_list[pd_index].driveState ==
1549 MR_PD_STATE_SYSTEM) {
1550 blk_queue_rq_timeout(sdev->request_queue,
1551 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1552 return 0;
1553 }
1554 return -ENXIO;
1555 }
1556
1557 /*
1558 * The RAID firmware may require extended timeouts.
1559 */
1560 blk_queue_rq_timeout(sdev->request_queue,
1561 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1562 return 0;
1563}
1564
1565static int megasas_slave_alloc(struct scsi_device *sdev)
1566{
1567 u16 pd_index = 0;
1568 struct megasas_instance *instance ;
1569 instance = megasas_lookup_instance(sdev->host->host_no);
1570 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1571 (sdev->type == TYPE_DISK)) {
1572 /*
1573 * Open the OS scan to the SYSTEM PD
1574 */
1575 pd_index =
1576 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1577 sdev->id;
1578 if ((instance->pd_list[pd_index].driveState ==
1579 MR_PD_STATE_SYSTEM) &&
1580 (instance->pd_list[pd_index].driveType ==
1581 TYPE_DISK)) {
1582 return 0;
1583 }
1584 return -ENXIO;
1585 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001586 return 0;
1587}
1588
adam radford9c915a82010-12-21 13:34:31 -08001589void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001590{
1591 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001592 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
adam radford36807e62011-10-08 18:15:06 -07001593 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1594 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08001595 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001596 } else {
adam radford9c915a82010-12-21 13:34:31 -08001597 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1598 }
1599}
1600
1601 /**
1602 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1603 * restored to max value
1604 * @instance: Adapter soft state
1605 *
1606 */
1607void
1608megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1609{
1610 unsigned long flags;
1611 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001612 && time_after(jiffies, instance->last_time + 5 * HZ)
1613 && atomic_read(&instance->fw_outstanding) <
1614 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001615
1616 spin_lock_irqsave(instance->host->host_lock, flags);
1617 instance->flag &= ~MEGASAS_FW_BUSY;
1618 if ((instance->pdev->device ==
1619 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1620 (instance->pdev->device ==
1621 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1622 instance->host->can_queue =
1623 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1624 } else
1625 instance->host->can_queue =
1626 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1627
1628 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001629 }
1630}
1631
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001632/**
bo yang7343eb62007-11-09 04:35:44 -05001633 * megasas_complete_cmd_dpc - Returns FW's controller structure
1634 * @instance_addr: Address of adapter soft state
1635 *
1636 * Tasklet to complete cmds
1637 */
1638static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1639{
1640 u32 producer;
1641 u32 consumer;
1642 u32 context;
1643 struct megasas_cmd *cmd;
1644 struct megasas_instance *instance =
1645 (struct megasas_instance *)instance_addr;
1646 unsigned long flags;
1647
1648 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001649 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001650 return;
1651
1652 spin_lock_irqsave(&instance->completion_lock, flags);
1653
1654 producer = *instance->producer;
1655 consumer = *instance->consumer;
1656
1657 while (consumer != producer) {
1658 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001659 if (context >= instance->max_fw_cmds) {
1660 printk(KERN_ERR "Unexpected context value %x\n",
1661 context);
1662 BUG();
1663 }
bo yang7343eb62007-11-09 04:35:44 -05001664
1665 cmd = instance->cmd_list[context];
1666
1667 megasas_complete_cmd(instance, cmd, DID_OK);
1668
1669 consumer++;
1670 if (consumer == (instance->max_fw_cmds + 1)) {
1671 consumer = 0;
1672 }
1673 }
1674
1675 *instance->consumer = producer;
1676
1677 spin_unlock_irqrestore(&instance->completion_lock, flags);
1678
1679 /*
1680 * Check if we can restore can_queue
1681 */
adam radford9c915a82010-12-21 13:34:31 -08001682 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001683}
1684
Yang, Bo707e09b2010-10-12 07:20:27 -06001685static void
1686megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1687
1688static void
1689process_fw_state_change_wq(struct work_struct *work);
1690
1691void megasas_do_ocr(struct megasas_instance *instance)
1692{
1693 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1694 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1695 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1696 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1697 }
1698 instance->instancet->disable_intr(instance->reg_set);
1699 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1700 instance->issuepend_done = 0;
1701
1702 atomic_set(&instance->fw_outstanding, 0);
1703 megasas_internal_reset_defer_cmds(instance);
1704 process_fw_state_change_wq(&instance->work_init);
1705}
1706
bo yang7343eb62007-11-09 04:35:44 -05001707/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001708 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1709 * @instance: Adapter soft state
1710 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001711 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001712 * complete all its outstanding commands. Returns error if one or more IOs
1713 * are pending after this time period. It also marks the controller dead.
1714 */
1715static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1716{
1717 int i;
bo yang39a98552010-09-22 22:36:29 -04001718 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001719 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001720 u8 adprecovery;
1721 unsigned long flags;
1722 struct list_head clist_local;
1723 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001724 u32 fw_state;
1725 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001726
1727 spin_lock_irqsave(&instance->hba_lock, flags);
1728 adprecovery = instance->adprecovery;
1729 spin_unlock_irqrestore(&instance->hba_lock, flags);
1730
1731 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1732
1733 INIT_LIST_HEAD(&clist_local);
1734 spin_lock_irqsave(&instance->hba_lock, flags);
1735 list_splice_init(&instance->internal_reset_pending_q,
1736 &clist_local);
1737 spin_unlock_irqrestore(&instance->hba_lock, flags);
1738
1739 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1740 for (i = 0; i < wait_time; i++) {
1741 msleep(1000);
1742 spin_lock_irqsave(&instance->hba_lock, flags);
1743 adprecovery = instance->adprecovery;
1744 spin_unlock_irqrestore(&instance->hba_lock, flags);
1745 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1746 break;
1747 }
1748
1749 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1750 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1751 spin_lock_irqsave(&instance->hba_lock, flags);
1752 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1753 spin_unlock_irqrestore(&instance->hba_lock, flags);
1754 return FAILED;
1755 }
1756
1757 reset_index = 0;
1758 while (!list_empty(&clist_local)) {
1759 reset_cmd = list_entry((&clist_local)->next,
1760 struct megasas_cmd, list);
1761 list_del_init(&reset_cmd->list);
1762 if (reset_cmd->scmd) {
1763 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001764 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001765 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001766 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001767
1768 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1769 megasas_return_cmd(instance, reset_cmd);
1770 } else if (reset_cmd->sync_cmd) {
1771 printk(KERN_NOTICE "megasas:%p synch cmds"
1772 "reset queue\n",
1773 reset_cmd);
1774
1775 reset_cmd->cmd_status = ENODATA;
1776 instance->instancet->fire_cmd(instance,
1777 reset_cmd->frame_phys_addr,
1778 0, instance->reg_set);
1779 } else {
1780 printk(KERN_NOTICE "megasas: %p unexpected"
1781 "cmds lst\n",
1782 reset_cmd);
1783 }
1784 reset_index++;
1785 }
1786
1787 return SUCCESS;
1788 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001789
adam radfordc007b8b2012-07-17 18:20:24 -07001790 for (i = 0; i < resetwaittime; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001791
Sumant Patroe4a082c2006-05-30 12:03:37 -07001792 int outstanding = atomic_read(&instance->fw_outstanding);
1793
1794 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001795 break;
1796
1797 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1798 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001799 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001800 /*
1801 * Call cmd completion routine. Cmd to be
1802 * be completed directly without depending on isr.
1803 */
1804 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001805 }
1806
1807 msleep(1000);
1808 }
1809
Yang, Bo707e09b2010-10-12 07:20:27 -06001810 i = 0;
1811 kill_adapter_flag = 0;
1812 do {
1813 fw_state = instance->instancet->read_fw_status_reg(
1814 instance->reg_set) & MFI_STATE_MASK;
1815 if ((fw_state == MFI_STATE_FAULT) &&
1816 (instance->disableOnlineCtrlReset == 0)) {
1817 if (i == 3) {
1818 kill_adapter_flag = 2;
1819 break;
1820 }
1821 megasas_do_ocr(instance);
1822 kill_adapter_flag = 1;
1823
1824 /* wait for 1 secs to let FW finish the pending cmds */
1825 msleep(1000);
1826 }
1827 i++;
1828 } while (i <= 3);
1829
1830 if (atomic_read(&instance->fw_outstanding) &&
1831 !kill_adapter_flag) {
1832 if (instance->disableOnlineCtrlReset == 0) {
1833
1834 megasas_do_ocr(instance);
1835
1836 /* wait for 5 secs to let FW finish the pending cmds */
1837 for (i = 0; i < wait_time; i++) {
1838 int outstanding =
1839 atomic_read(&instance->fw_outstanding);
1840 if (!outstanding)
1841 return SUCCESS;
1842 msleep(1000);
1843 }
1844 }
1845 }
1846
1847 if (atomic_read(&instance->fw_outstanding) ||
1848 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001849 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001850 /*
1851 * Send signal to FW to stop processing any pending cmds.
1852 * The controller will be taken offline by the OS now.
1853 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001854 if ((instance->pdev->device ==
1855 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1856 (instance->pdev->device ==
1857 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1858 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001859 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001860 } else {
1861 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001862 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001863 }
Sumant Patro658dced2006-10-03 13:09:14 -07001864 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001865 spin_lock_irqsave(&instance->hba_lock, flags);
1866 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1867 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001868 return FAILED;
1869 }
1870
bo yang39a98552010-09-22 22:36:29 -04001871 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1872
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001873 return SUCCESS;
1874}
1875
1876/**
1877 * megasas_generic_reset - Generic reset routine
1878 * @scmd: Mid-layer SCSI command
1879 *
1880 * This routine implements a generic reset handler for device, bus and host
1881 * reset requests. Device, bus and host specific reset handlers can use this
1882 * function after they do their specific tasks.
1883 */
1884static int megasas_generic_reset(struct scsi_cmnd *scmd)
1885{
1886 int ret_val;
1887 struct megasas_instance *instance;
1888
1889 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1890
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001891 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1892 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001893
bo yang39a98552010-09-22 22:36:29 -04001894 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001895 printk(KERN_ERR "megasas: cannot recover from previous reset "
1896 "failures\n");
1897 return FAILED;
1898 }
1899
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001900 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001901 if (ret_val == SUCCESS)
1902 printk(KERN_NOTICE "megasas: reset successful \n");
1903 else
1904 printk(KERN_ERR "megasas: failed to do reset\n");
1905
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001906 return ret_val;
1907}
1908
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001909/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001910 * megasas_reset_timer - quiesce the adapter if required
1911 * @scmd: scsi cmnd
1912 *
1913 * Sets the FW busy flag and reduces the host->can_queue if the
1914 * cmd has not been completed within the timeout period.
1915 */
1916static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001917blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001918{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001919 struct megasas_instance *instance;
1920 unsigned long flags;
1921
1922 if (time_after(jiffies, scmd->jiffies_at_alloc +
1923 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001924 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001925 }
1926
adam radfordf575c5d2011-10-13 16:01:12 -07001927 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001928 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1929 /* FW is busy, throttle IO */
1930 spin_lock_irqsave(instance->host->host_lock, flags);
1931
adam radfordc5daa6a2012-07-17 18:20:03 -07001932 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001933 instance->last_time = jiffies;
1934 instance->flag |= MEGASAS_FW_BUSY;
1935
1936 spin_unlock_irqrestore(instance->host->host_lock, flags);
1937 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001938 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001939}
1940
1941/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001942 * megasas_reset_device - Device reset handler entry point
1943 */
1944static int megasas_reset_device(struct scsi_cmnd *scmd)
1945{
1946 int ret;
1947
1948 /*
1949 * First wait for all commands to complete
1950 */
1951 ret = megasas_generic_reset(scmd);
1952
1953 return ret;
1954}
1955
1956/**
1957 * megasas_reset_bus_host - Bus & host reset handler entry point
1958 */
1959static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1960{
1961 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001962 struct megasas_instance *instance;
1963 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001964
1965 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001966 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001967 */
adam radford36807e62011-10-08 18:15:06 -07001968 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1969 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08001970 ret = megasas_reset_fusion(scmd->device->host);
1971 else
1972 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001973
1974 return ret;
1975}
1976
1977/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001978 * megasas_bios_param - Returns disk geometry for a disk
1979 * @sdev: device handle
1980 * @bdev: block device
1981 * @capacity: drive capacity
1982 * @geom: geometry parameters
1983 */
1984static int
1985megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1986 sector_t capacity, int geom[])
1987{
1988 int heads;
1989 int sectors;
1990 sector_t cylinders;
1991 unsigned long tmp;
1992 /* Default heads (64) & sectors (32) */
1993 heads = 64;
1994 sectors = 32;
1995
1996 tmp = heads * sectors;
1997 cylinders = capacity;
1998
1999 sector_div(cylinders, tmp);
2000
2001 /*
2002 * Handle extended translation size for logical drives > 1Gb
2003 */
2004
2005 if (capacity >= 0x200000) {
2006 heads = 255;
2007 sectors = 63;
2008 tmp = heads*sectors;
2009 cylinders = capacity;
2010 sector_div(cylinders, tmp);
2011 }
2012
2013 geom[0] = heads;
2014 geom[1] = sectors;
2015 geom[2] = cylinders;
2016
2017 return 0;
2018}
2019
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002020static void megasas_aen_polling(struct work_struct *work);
2021
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002022/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002023 * megasas_service_aen - Processes an event notification
2024 * @instance: Adapter soft state
2025 * @cmd: AEN command completed by the ISR
2026 *
2027 * For AEN, driver sends a command down to FW that is held by the FW till an
2028 * event occurs. When an event of interest occurs, FW completes the command
2029 * that it was previously holding.
2030 *
2031 * This routines sends SIGIO signal to processes that have registered with the
2032 * driver for AEN.
2033 */
2034static void
2035megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2036{
Yang, Boc3518832009-10-06 14:18:02 -06002037 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002038 /*
2039 * Don't signal app if it is just an aborted previously registered aen
2040 */
Yang, Boc3518832009-10-06 14:18:02 -06002041 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2042 spin_lock_irqsave(&poll_aen_lock, flags);
2043 megasas_poll_wait_aen = 1;
2044 spin_unlock_irqrestore(&poll_aen_lock, flags);
2045 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002046 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002047 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002048 else
2049 cmd->abort_aen = 0;
2050
2051 instance->aen_cmd = NULL;
2052 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002053
bo yang39a98552010-09-22 22:36:29 -04002054 if ((instance->unload == 0) &&
2055 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002056 struct megasas_aen_event *ev;
2057 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2058 if (!ev) {
2059 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2060 } else {
2061 ev->instance = instance;
2062 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002063 INIT_DELAYED_WORK(&ev->hotplug_work,
2064 megasas_aen_polling);
2065 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002066 }
2067 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002068}
2069
adam radford4bcde502011-07-26 15:42:52 -07002070static int megasas_change_queue_depth(struct scsi_device *sdev,
2071 int queue_depth, int reason)
2072{
2073 if (reason != SCSI_QDEPTH_DEFAULT)
2074 return -EOPNOTSUPP;
2075
2076 if (queue_depth > sdev->host->can_queue)
2077 queue_depth = sdev->host->can_queue;
2078 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2079 queue_depth);
2080
2081 return queue_depth;
2082}
2083
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002084/*
2085 * Scsi host template for megaraid_sas driver
2086 */
2087static struct scsi_host_template megasas_template = {
2088
2089 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002090 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002091 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002092 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002093 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002094 .queuecommand = megasas_queue_command,
2095 .eh_device_reset_handler = megasas_reset_device,
2096 .eh_bus_reset_handler = megasas_reset_bus_host,
2097 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002098 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002099 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002100 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002101 .change_queue_depth = megasas_change_queue_depth,
Martin K. Petersen8562d022013-10-23 06:25:40 -04002102 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002103};
2104
2105/**
2106 * megasas_complete_int_cmd - Completes an internal command
2107 * @instance: Adapter soft state
2108 * @cmd: Command to be completed
2109 *
2110 * The megasas_issue_blocked_cmd() function waits for a command to complete
2111 * after it issues a command. This function wakes up that waiting routine by
2112 * calling wake_up() on the wait queue.
2113 */
2114static void
2115megasas_complete_int_cmd(struct megasas_instance *instance,
2116 struct megasas_cmd *cmd)
2117{
2118 cmd->cmd_status = cmd->frame->io.cmd_status;
2119
2120 if (cmd->cmd_status == ENODATA) {
2121 cmd->cmd_status = 0;
2122 }
2123 wake_up(&instance->int_cmd_wait_q);
2124}
2125
2126/**
2127 * megasas_complete_abort - Completes aborting a command
2128 * @instance: Adapter soft state
2129 * @cmd: Cmd that was issued to abort another cmd
2130 *
adam radford0d490162010-12-14 19:17:17 -08002131 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2132 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002133 * wakes up all functions waiting on the same wait queue.
2134 */
2135static void
2136megasas_complete_abort(struct megasas_instance *instance,
2137 struct megasas_cmd *cmd)
2138{
2139 if (cmd->sync_cmd) {
2140 cmd->sync_cmd = 0;
2141 cmd->cmd_status = 0;
2142 wake_up(&instance->abort_cmd_wait_q);
2143 }
2144
2145 return;
2146}
2147
2148/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002149 * megasas_complete_cmd - Completes a command
2150 * @instance: Adapter soft state
2151 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002152 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002153 * SCSI mid-layer instead of the value returned
2154 * by the FW. This should be used if caller wants
2155 * an alternate status (as in the case of aborted
2156 * commands)
2157 */
adam radford9c915a82010-12-21 13:34:31 -08002158void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002159megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2160 u8 alt_status)
2161{
2162 int exception = 0;
2163 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002164 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002165 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002166
bo yang39a98552010-09-22 22:36:29 -04002167 /* flag for the retry reset */
2168 cmd->retry_for_fw_reset = 0;
2169
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002170 if (cmd->scmd)
2171 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002172
2173 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002174 case MFI_CMD_INVALID:
2175 /* Some older 1068 controller FW may keep a pended
2176 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2177 when booting the kdump kernel. Ignore this command to
2178 prevent a kernel panic on shutdown of the kdump kernel. */
2179 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2180 "completed.\n");
2181 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2182 "other than PERC5, please upgrade your firmware.\n");
2183 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002184 case MFI_CMD_PD_SCSI_IO:
2185 case MFI_CMD_LD_SCSI_IO:
2186
2187 /*
2188 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2189 * issued either through an IO path or an IOCTL path. If it
2190 * was via IOCTL, we will send it to internal completion.
2191 */
2192 if (cmd->sync_cmd) {
2193 cmd->sync_cmd = 0;
2194 megasas_complete_int_cmd(instance, cmd);
2195 break;
2196 }
2197
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002198 case MFI_CMD_LD_READ:
2199 case MFI_CMD_LD_WRITE:
2200
2201 if (alt_status) {
2202 cmd->scmd->result = alt_status << 16;
2203 exception = 1;
2204 }
2205
2206 if (exception) {
2207
Sumant Patroe4a082c2006-05-30 12:03:37 -07002208 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002209
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002210 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002211 cmd->scmd->scsi_done(cmd->scmd);
2212 megasas_return_cmd(instance, cmd);
2213
2214 break;
2215 }
2216
2217 switch (hdr->cmd_status) {
2218
2219 case MFI_STAT_OK:
2220 cmd->scmd->result = DID_OK << 16;
2221 break;
2222
2223 case MFI_STAT_SCSI_IO_FAILED:
2224 case MFI_STAT_LD_INIT_IN_PROGRESS:
2225 cmd->scmd->result =
2226 (DID_ERROR << 16) | hdr->scsi_status;
2227 break;
2228
2229 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2230
2231 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2232
2233 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2234 memset(cmd->scmd->sense_buffer, 0,
2235 SCSI_SENSE_BUFFERSIZE);
2236 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2237 hdr->sense_len);
2238
2239 cmd->scmd->result |= DRIVER_SENSE << 24;
2240 }
2241
2242 break;
2243
2244 case MFI_STAT_LD_OFFLINE:
2245 case MFI_STAT_DEVICE_NOT_FOUND:
2246 cmd->scmd->result = DID_BAD_TARGET << 16;
2247 break;
2248
2249 default:
2250 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2251 hdr->cmd_status);
2252 cmd->scmd->result = DID_ERROR << 16;
2253 break;
2254 }
2255
Sumant Patroe4a082c2006-05-30 12:03:37 -07002256 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002257
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002258 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002259 cmd->scmd->scsi_done(cmd->scmd);
2260 megasas_return_cmd(instance, cmd);
2261
2262 break;
2263
2264 case MFI_CMD_SMP:
2265 case MFI_CMD_STP:
2266 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002267 /* Check for LD map update */
2268 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2269 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2270 spin_lock_irqsave(instance->host->host_lock, flags);
2271 if (cmd->frame->hdr.cmd_status != 0) {
2272 if (cmd->frame->hdr.cmd_status !=
2273 MFI_STAT_NOT_FOUND)
2274 printk(KERN_WARNING "megasas: map sync"
2275 "failed, status = 0x%x.\n",
2276 cmd->frame->hdr.cmd_status);
2277 else {
2278 megasas_return_cmd(instance, cmd);
2279 spin_unlock_irqrestore(
2280 instance->host->host_lock,
2281 flags);
2282 break;
2283 }
2284 } else
2285 instance->map_id++;
2286 megasas_return_cmd(instance, cmd);
2287 if (MR_ValidateMapInfo(
2288 fusion->ld_map[(instance->map_id & 1)],
2289 fusion->load_balance_info))
2290 fusion->fast_path_io = 1;
2291 else
2292 fusion->fast_path_io = 0;
2293 megasas_sync_map_info(instance);
2294 spin_unlock_irqrestore(instance->host->host_lock,
2295 flags);
2296 break;
2297 }
Yang, Boc3518832009-10-06 14:18:02 -06002298 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2299 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2300 spin_lock_irqsave(&poll_aen_lock, flags);
2301 megasas_poll_wait_aen = 0;
2302 spin_unlock_irqrestore(&poll_aen_lock, flags);
2303 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002304
2305 /*
2306 * See if got an event notification
2307 */
2308 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2309 megasas_service_aen(instance, cmd);
2310 else
2311 megasas_complete_int_cmd(instance, cmd);
2312
2313 break;
2314
2315 case MFI_CMD_ABORT:
2316 /*
2317 * Cmd issued to abort another cmd returned
2318 */
2319 megasas_complete_abort(instance, cmd);
2320 break;
2321
2322 default:
2323 printk("megasas: Unknown command completed! [0x%X]\n",
2324 hdr->cmd);
2325 break;
2326 }
2327}
2328
2329/**
bo yang39a98552010-09-22 22:36:29 -04002330 * megasas_issue_pending_cmds_again - issue all pending cmds
2331 * in FW again because of the fw reset
2332 * @instance: Adapter soft state
2333 */
2334static inline void
2335megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2336{
2337 struct megasas_cmd *cmd;
2338 struct list_head clist_local;
2339 union megasas_evt_class_locale class_locale;
2340 unsigned long flags;
2341 u32 seq_num;
2342
2343 INIT_LIST_HEAD(&clist_local);
2344 spin_lock_irqsave(&instance->hba_lock, flags);
2345 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2346 spin_unlock_irqrestore(&instance->hba_lock, flags);
2347
2348 while (!list_empty(&clist_local)) {
2349 cmd = list_entry((&clist_local)->next,
2350 struct megasas_cmd, list);
2351 list_del_init(&cmd->list);
2352
2353 if (cmd->sync_cmd || cmd->scmd) {
2354 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2355 "detected to be pending while HBA reset.\n",
2356 cmd, cmd->scmd, cmd->sync_cmd);
2357
2358 cmd->retry_for_fw_reset++;
2359
2360 if (cmd->retry_for_fw_reset == 3) {
2361 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2362 "was tried multiple times during reset."
2363 "Shutting down the HBA\n",
2364 cmd, cmd->scmd, cmd->sync_cmd);
2365 megaraid_sas_kill_hba(instance);
2366
2367 instance->adprecovery =
2368 MEGASAS_HW_CRITICAL_ERROR;
2369 return;
2370 }
2371 }
2372
2373 if (cmd->sync_cmd == 1) {
2374 if (cmd->scmd) {
2375 printk(KERN_NOTICE "megaraid_sas: unexpected"
2376 "cmd attached to internal command!\n");
2377 }
2378 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2379 "on the internal reset queue,"
2380 "issue it again.\n", cmd);
2381 cmd->cmd_status = ENODATA;
2382 instance->instancet->fire_cmd(instance,
2383 cmd->frame_phys_addr ,
2384 0, instance->reg_set);
2385 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002386 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002387 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002388 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002389
2390 atomic_inc(&instance->fw_outstanding);
2391 instance->instancet->fire_cmd(instance,
2392 cmd->frame_phys_addr,
2393 cmd->frame_count-1, instance->reg_set);
2394 } else {
2395 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2396 "internal reset defer list while re-issue!!\n",
2397 cmd);
2398 }
2399 }
2400
2401 if (instance->aen_cmd) {
2402 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2403 megasas_return_cmd(instance, instance->aen_cmd);
2404
2405 instance->aen_cmd = NULL;
2406 }
2407
2408 /*
2409 * Initiate AEN (Asynchronous Event Notification)
2410 */
2411 seq_num = instance->last_seq_num;
2412 class_locale.members.reserved = 0;
2413 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2414 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2415
2416 megasas_register_aen(instance, seq_num, class_locale.word);
2417}
2418
2419/**
2420 * Move the internal reset pending commands to a deferred queue.
2421 *
2422 * We move the commands pending at internal reset time to a
2423 * pending queue. This queue would be flushed after successful
2424 * completion of the internal reset sequence. if the internal reset
2425 * did not complete in time, the kernel reset handler would flush
2426 * these commands.
2427 **/
2428static void
2429megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2430{
2431 struct megasas_cmd *cmd;
2432 int i;
2433 u32 max_cmd = instance->max_fw_cmds;
2434 u32 defer_index;
2435 unsigned long flags;
2436
2437 defer_index = 0;
2438 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2439 for (i = 0; i < max_cmd; i++) {
2440 cmd = instance->cmd_list[i];
2441 if (cmd->sync_cmd == 1 || cmd->scmd) {
2442 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2443 "on the defer queue as internal\n",
2444 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2445
2446 if (!list_empty(&cmd->list)) {
2447 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2448 " moving this cmd:%p, %d %p, it was"
2449 "discovered on some list?\n",
2450 cmd, cmd->sync_cmd, cmd->scmd);
2451
2452 list_del_init(&cmd->list);
2453 }
2454 defer_index++;
2455 list_add_tail(&cmd->list,
2456 &instance->internal_reset_pending_q);
2457 }
2458 }
2459 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2460}
2461
2462
2463static void
2464process_fw_state_change_wq(struct work_struct *work)
2465{
2466 struct megasas_instance *instance =
2467 container_of(work, struct megasas_instance, work_init);
2468 u32 wait;
2469 unsigned long flags;
2470
2471 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2472 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2473 instance->adprecovery);
2474 return ;
2475 }
2476
2477 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2478 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2479 "state, restarting it...\n");
2480
2481 instance->instancet->disable_intr(instance->reg_set);
2482 atomic_set(&instance->fw_outstanding, 0);
2483
2484 atomic_set(&instance->fw_reset_no_pci_access, 1);
2485 instance->instancet->adp_reset(instance, instance->reg_set);
2486 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2487
2488 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2489 "initiating next stage...\n");
2490
2491 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2492 "state 2 starting...\n");
2493
2494 /*waitting for about 20 second before start the second init*/
2495 for (wait = 0; wait < 30; wait++) {
2496 msleep(1000);
2497 }
2498
adam radford058a8fa2011-10-08 18:14:27 -07002499 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04002500 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2501
2502 megaraid_sas_kill_hba(instance);
2503 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2504 return ;
2505 }
2506
2507 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2508 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2509 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2510 ) {
2511 *instance->consumer = *instance->producer;
2512 } else {
2513 *instance->consumer = 0;
2514 *instance->producer = 0;
2515 }
2516
2517 megasas_issue_init_mfi(instance);
2518
2519 spin_lock_irqsave(&instance->hba_lock, flags);
2520 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2521 spin_unlock_irqrestore(&instance->hba_lock, flags);
2522 instance->instancet->enable_intr(instance->reg_set);
2523
2524 megasas_issue_pending_cmds_again(instance);
2525 instance->issuepend_done = 1;
2526 }
2527 return ;
2528}
2529
2530/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002531 * megasas_deplete_reply_queue - Processes all completed commands
2532 * @instance: Adapter soft state
2533 * @alt_status: Alternate status to be returned to
2534 * SCSI mid-layer instead of the status
2535 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002536 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002537 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002538static int
bo yang39a98552010-09-22 22:36:29 -04002539megasas_deplete_reply_queue(struct megasas_instance *instance,
2540 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002541{
bo yang39a98552010-09-22 22:36:29 -04002542 u32 mfiStatus;
2543 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002544
bo yang39a98552010-09-22 22:36:29 -04002545 if ((mfiStatus = instance->instancet->check_reset(instance,
2546 instance->reg_set)) == 1) {
2547 return IRQ_HANDLED;
2548 }
2549
2550 if ((mfiStatus = instance->instancet->clear_intr(
2551 instance->reg_set)
2552 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002553 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07002554 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08002555 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002556 }
2557
2558 instance->mfiStatus = mfiStatus;
2559
2560 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2561 fw_state = instance->instancet->read_fw_status_reg(
2562 instance->reg_set) & MFI_STATE_MASK;
2563
2564 if (fw_state != MFI_STATE_FAULT) {
2565 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2566 fw_state);
2567 }
2568
2569 if ((fw_state == MFI_STATE_FAULT) &&
2570 (instance->disableOnlineCtrlReset == 0)) {
2571 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2572
2573 if ((instance->pdev->device ==
2574 PCI_DEVICE_ID_LSI_SAS1064R) ||
2575 (instance->pdev->device ==
2576 PCI_DEVICE_ID_DELL_PERC5) ||
2577 (instance->pdev->device ==
2578 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2579
2580 *instance->consumer =
2581 MEGASAS_ADPRESET_INPROG_SIGN;
2582 }
2583
2584
2585 instance->instancet->disable_intr(instance->reg_set);
2586 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2587 instance->issuepend_done = 0;
2588
2589 atomic_set(&instance->fw_outstanding, 0);
2590 megasas_internal_reset_defer_cmds(instance);
2591
2592 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2593 fw_state, instance->adprecovery);
2594
2595 schedule_work(&instance->work_init);
2596 return IRQ_HANDLED;
2597
2598 } else {
2599 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2600 fw_state, instance->disableOnlineCtrlReset);
2601 }
2602 }
2603
Sumant Patro5d018ad2006-10-03 13:13:18 -07002604 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002605 return IRQ_HANDLED;
2606}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002607/**
2608 * megasas_isr - isr entry point
2609 */
David Howells7d12e782006-10-05 14:55:46 +01002610static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002611{
adam radfordc8e858f2011-10-08 18:15:13 -07002612 struct megasas_irq_context *irq_context = devp;
2613 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04002614 unsigned long flags;
2615 irqreturn_t rc;
2616
adam radfordc8e858f2011-10-08 18:15:13 -07002617 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04002618 return IRQ_HANDLED;
2619
bo yang39a98552010-09-22 22:36:29 -04002620 spin_lock_irqsave(&instance->hba_lock, flags);
2621 rc = megasas_deplete_reply_queue(instance, DID_OK);
2622 spin_unlock_irqrestore(&instance->hba_lock, flags);
2623
2624 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002625}
2626
2627/**
2628 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002629 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002630 *
2631 * During the initialization, FW passes can potentially be in any one of
2632 * several possible states. If the FW in operational, waiting-for-handshake
2633 * states, driver must take steps to bring it to ready state. Otherwise, it
2634 * has to wait for the ready state.
2635 */
adam radford9c915a82010-12-21 13:34:31 -08002636int
adam radford058a8fa2011-10-08 18:14:27 -07002637megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002638{
2639 int i;
2640 u8 max_wait;
2641 u32 fw_state;
2642 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002643 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002644
Sumant Patro1341c932006-01-25 12:02:40 -08002645 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002646
Sumant Patroe3bbff92006-10-03 12:28:49 -07002647 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002648 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2649 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002650
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002651 while (fw_state != MFI_STATE_READY) {
2652
Yang, Bo7218df62009-10-06 14:52:20 -06002653 abs_state =
2654 instance->instancet->read_fw_status_reg(instance->reg_set);
2655
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002656 switch (fw_state) {
2657
2658 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002659 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07002660 if (ocr) {
2661 max_wait = MEGASAS_RESET_WAIT_TIME;
2662 cur_state = MFI_STATE_FAULT;
2663 break;
2664 } else
2665 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002666
2667 case MFI_STATE_WAIT_HANDSHAKE:
2668 /*
2669 * Set the CLR bit in inbound doorbell
2670 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002671 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002672 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2673 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002674 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2675 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002676 PCI_DEVICE_ID_LSI_FUSION) ||
2677 (instance->pdev->device ==
2678 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002679 writel(
2680 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002681 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002682 } else {
2683 writel(
2684 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2685 &instance->reg_set->inbound_doorbell);
2686 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002687
Yang, Bo7218df62009-10-06 14:52:20 -06002688 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002689 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2690 break;
2691
Sumant Patroe3bbff92006-10-03 12:28:49 -07002692 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002693 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002694 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2695 (instance->pdev->device ==
2696 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2697 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002698 PCI_DEVICE_ID_LSI_FUSION) ||
2699 (instance->pdev->device ==
2700 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002701 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002702 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002703 } else
2704 writel(MFI_INIT_HOTPLUG,
2705 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002706
Yang, Bo7218df62009-10-06 14:52:20 -06002707 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002708 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2709 break;
2710
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002711 case MFI_STATE_OPERATIONAL:
2712 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002713 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002714 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002715 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002716 if ((instance->pdev->device ==
2717 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2718 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002719 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2720 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07002721 == PCI_DEVICE_ID_LSI_FUSION) ||
2722 (instance->pdev->device
2723 == PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002724 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002725 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07002726 if ((instance->pdev->device ==
2727 PCI_DEVICE_ID_LSI_FUSION) ||
2728 (instance->pdev->device ==
2729 PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08002730 for (i = 0; i < (10 * 1000); i += 20) {
2731 if (readl(
2732 &instance->
2733 reg_set->
2734 doorbell) & 1)
2735 msleep(20);
2736 else
2737 break;
2738 }
2739 }
Yang, Bo87911122009-10-06 14:31:54 -06002740 } else
2741 writel(MFI_RESET_FLAGS,
2742 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002743
Yang, Bo7218df62009-10-06 14:52:20 -06002744 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002745 cur_state = MFI_STATE_OPERATIONAL;
2746 break;
2747
2748 case MFI_STATE_UNDEFINED:
2749 /*
2750 * This state should not last for more than 2 seconds
2751 */
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_UNDEFINED;
2754 break;
2755
2756 case MFI_STATE_BB_INIT:
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_BB_INIT;
2759 break;
2760
2761 case MFI_STATE_FW_INIT:
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_FW_INIT;
2764 break;
2765
2766 case MFI_STATE_FW_INIT_2:
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_FW_INIT_2;
2769 break;
2770
2771 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002772 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002773 cur_state = MFI_STATE_DEVICE_SCAN;
2774 break;
2775
2776 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002777 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002778 cur_state = MFI_STATE_FLUSH_CACHE;
2779 break;
2780
2781 default:
2782 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2783 fw_state);
2784 return -ENODEV;
2785 }
2786
2787 /*
2788 * The cur_state should not last for more than max_wait secs
2789 */
2790 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002791 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002792 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002793 curr_abs_state =
2794 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002795
Yang, Bo7218df62009-10-06 14:52:20 -06002796 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002797 msleep(1);
2798 } else
2799 break;
2800 }
2801
2802 /*
2803 * Return error if fw_state hasn't changed after max_wait
2804 */
Yang, Bo7218df62009-10-06 14:52:20 -06002805 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002806 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2807 "in %d secs\n", fw_state, max_wait);
2808 return -ENODEV;
2809 }
bo yang39a98552010-09-22 22:36:29 -04002810 }
adam radford0d490162010-12-14 19:17:17 -08002811 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002812
2813 return 0;
2814}
2815
2816/**
2817 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2818 * @instance: Adapter soft state
2819 */
2820static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2821{
2822 int i;
adam radford9c915a82010-12-21 13:34:31 -08002823 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002824 struct megasas_cmd *cmd;
2825
2826 if (!instance->frame_dma_pool)
2827 return;
2828
2829 /*
2830 * Return all frames to pool
2831 */
2832 for (i = 0; i < max_cmd; i++) {
2833
2834 cmd = instance->cmd_list[i];
2835
2836 if (cmd->frame)
2837 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2838 cmd->frame_phys_addr);
2839
2840 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002841 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002842 cmd->sense_phys_addr);
2843 }
2844
2845 /*
2846 * Now destroy the pool itself
2847 */
2848 pci_pool_destroy(instance->frame_dma_pool);
2849 pci_pool_destroy(instance->sense_dma_pool);
2850
2851 instance->frame_dma_pool = NULL;
2852 instance->sense_dma_pool = NULL;
2853}
2854
2855/**
2856 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2857 * @instance: Adapter soft state
2858 *
2859 * Each command packet has an embedded DMA memory buffer that is used for
2860 * filling MFI frame and the SG list that immediately follows the frame. This
2861 * function creates those DMA memory buffers for each command packet by using
2862 * PCI pool facility.
2863 */
2864static int megasas_create_frame_pool(struct megasas_instance *instance)
2865{
2866 int i;
2867 u32 max_cmd;
2868 u32 sge_sz;
2869 u32 sgl_sz;
2870 u32 total_sz;
2871 u32 frame_count;
2872 struct megasas_cmd *cmd;
2873
adam radford9c915a82010-12-21 13:34:31 -08002874 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002875
2876 /*
2877 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2878 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2879 */
2880 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2881 sizeof(struct megasas_sge32);
2882
Yang, Bof4c9a132009-10-06 14:43:28 -06002883 if (instance->flag_ieee) {
2884 sge_sz = sizeof(struct megasas_sge_skinny);
2885 }
2886
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002887 /*
2888 * Calculated the number of 64byte frames required for SGL
2889 */
2890 sgl_sz = sge_sz * instance->max_num_sge;
2891 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002892 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002893
2894 /*
2895 * We need one extra frame for the MFI command
2896 */
2897 frame_count++;
2898
2899 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2900 /*
2901 * Use DMA pool facility provided by PCI layer
2902 */
2903 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2904 instance->pdev, total_sz, 64,
2905 0);
2906
2907 if (!instance->frame_dma_pool) {
2908 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2909 return -ENOMEM;
2910 }
2911
2912 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2913 instance->pdev, 128, 4, 0);
2914
2915 if (!instance->sense_dma_pool) {
2916 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2917
2918 pci_pool_destroy(instance->frame_dma_pool);
2919 instance->frame_dma_pool = NULL;
2920
2921 return -ENOMEM;
2922 }
2923
2924 /*
2925 * Allocate and attach a frame to each of the commands in cmd_list.
2926 * By making cmd->index as the context instead of the &cmd, we can
2927 * always use 32bit context regardless of the architecture
2928 */
2929 for (i = 0; i < max_cmd; i++) {
2930
2931 cmd = instance->cmd_list[i];
2932
2933 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2934 GFP_KERNEL, &cmd->frame_phys_addr);
2935
2936 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2937 GFP_KERNEL, &cmd->sense_phys_addr);
2938
2939 /*
2940 * megasas_teardown_frame_pool() takes care of freeing
2941 * whatever has been allocated
2942 */
2943 if (!cmd->frame || !cmd->sense) {
2944 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2945 megasas_teardown_frame_pool(instance);
2946 return -ENOMEM;
2947 }
2948
Yang, Bo707e09b2010-10-12 07:20:27 -06002949 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002950 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002951 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07002952 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2953 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2954 (reset_devices))
2955 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002956 }
2957
2958 return 0;
2959}
2960
2961/**
2962 * megasas_free_cmds - Free all the cmds in the free cmd pool
2963 * @instance: Adapter soft state
2964 */
adam radford9c915a82010-12-21 13:34:31 -08002965void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002966{
2967 int i;
2968 /* First free the MFI frame pool */
2969 megasas_teardown_frame_pool(instance);
2970
2971 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002972 for (i = 0; i < instance->max_mfi_cmds; i++)
2973
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002974 kfree(instance->cmd_list[i]);
2975
2976 /* Free the cmd_list buffer itself */
2977 kfree(instance->cmd_list);
2978 instance->cmd_list = NULL;
2979
2980 INIT_LIST_HEAD(&instance->cmd_pool);
2981}
2982
2983/**
2984 * megasas_alloc_cmds - Allocates the command packets
2985 * @instance: Adapter soft state
2986 *
2987 * Each command that is issued to the FW, whether IO commands from the OS or
2988 * internal commands like IOCTLs, are wrapped in local data structure called
2989 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2990 * the FW.
2991 *
2992 * Each frame has a 32-bit field called context (tag). This context is used
2993 * to get back the megasas_cmd from the frame when a frame gets completed in
2994 * the ISR. Typically the address of the megasas_cmd itself would be used as
2995 * the context. But we wanted to keep the differences between 32 and 64 bit
2996 * systems to the mininum. We always use 32 bit integers for the context. In
2997 * this driver, the 32 bit values are the indices into an array cmd_list.
2998 * This array is used only to look up the megasas_cmd given the context. The
2999 * free commands themselves are maintained in a linked list called cmd_pool.
3000 */
adam radford9c915a82010-12-21 13:34:31 -08003001int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003002{
3003 int i;
3004 int j;
3005 u32 max_cmd;
3006 struct megasas_cmd *cmd;
3007
adam radford9c915a82010-12-21 13:34:31 -08003008 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003009
3010 /*
3011 * instance->cmd_list is an array of struct megasas_cmd pointers.
3012 * Allocate the dynamic array first and then allocate individual
3013 * commands.
3014 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003015 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003016
3017 if (!instance->cmd_list) {
3018 printk(KERN_DEBUG "megasas: out of memory\n");
3019 return -ENOMEM;
3020 }
3021
adam radford9c915a82010-12-21 13:34:31 -08003022 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003023
3024 for (i = 0; i < max_cmd; i++) {
3025 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3026 GFP_KERNEL);
3027
3028 if (!instance->cmd_list[i]) {
3029
3030 for (j = 0; j < i; j++)
3031 kfree(instance->cmd_list[j]);
3032
3033 kfree(instance->cmd_list);
3034 instance->cmd_list = NULL;
3035
3036 return -ENOMEM;
3037 }
3038 }
3039
3040 /*
3041 * Add all the commands to command pool (instance->cmd_pool)
3042 */
3043 for (i = 0; i < max_cmd; i++) {
3044 cmd = instance->cmd_list[i];
3045 memset(cmd, 0, sizeof(struct megasas_cmd));
3046 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003047 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003048 cmd->instance = instance;
3049
3050 list_add_tail(&cmd->list, &instance->cmd_pool);
3051 }
3052
3053 /*
3054 * Create a frame pool and assign one frame to each cmd
3055 */
3056 if (megasas_create_frame_pool(instance)) {
3057 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3058 megasas_free_cmds(instance);
3059 }
3060
3061 return 0;
3062}
3063
Yang, Bo81e403c2009-10-06 14:27:54 -06003064/*
3065 * megasas_get_pd_list_info - Returns FW's pd_list structure
3066 * @instance: Adapter soft state
3067 * @pd_list: pd_list structure
3068 *
3069 * Issues an internal command (DCMD) to get the FW's controller PD
3070 * list structure. This information is mainly used to find out SYSTEM
3071 * supported by the FW.
3072 */
3073static int
3074megasas_get_pd_list(struct megasas_instance *instance)
3075{
3076 int ret = 0, pd_index = 0;
3077 struct megasas_cmd *cmd;
3078 struct megasas_dcmd_frame *dcmd;
3079 struct MR_PD_LIST *ci;
3080 struct MR_PD_ADDRESS *pd_addr;
3081 dma_addr_t ci_h = 0;
3082
3083 cmd = megasas_get_cmd(instance);
3084
3085 if (!cmd) {
3086 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3087 return -ENOMEM;
3088 }
3089
3090 dcmd = &cmd->frame->dcmd;
3091
3092 ci = pci_alloc_consistent(instance->pdev,
3093 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3094
3095 if (!ci) {
3096 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3097 megasas_return_cmd(instance, cmd);
3098 return -ENOMEM;
3099 }
3100
3101 memset(ci, 0, sizeof(*ci));
3102 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3103
3104 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3105 dcmd->mbox.b[1] = 0;
3106 dcmd->cmd = MFI_CMD_DCMD;
3107 dcmd->cmd_status = 0xFF;
3108 dcmd->sge_count = 1;
3109 dcmd->flags = MFI_FRAME_DIR_READ;
3110 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003111 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003112 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3113 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3114 dcmd->sgl.sge32[0].phys_addr = ci_h;
3115 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3116
3117 if (!megasas_issue_polled(instance, cmd)) {
3118 ret = 0;
3119 } else {
3120 ret = -1;
3121 }
3122
3123 /*
3124 * the following function will get the instance PD LIST.
3125 */
3126
3127 pd_addr = ci->addr;
3128
3129 if ( ret == 0 &&
3130 (ci->count <
3131 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3132
3133 memset(instance->pd_list, 0,
3134 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3135
3136 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3137
3138 instance->pd_list[pd_addr->deviceId].tid =
3139 pd_addr->deviceId;
3140 instance->pd_list[pd_addr->deviceId].driveType =
3141 pd_addr->scsiDevType;
3142 instance->pd_list[pd_addr->deviceId].driveState =
3143 MR_PD_STATE_SYSTEM;
3144 pd_addr++;
3145 }
3146 }
3147
3148 pci_free_consistent(instance->pdev,
3149 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3150 ci, ci_h);
3151 megasas_return_cmd(instance, cmd);
3152
3153 return ret;
3154}
3155
Yang, Bobdc6fb82009-12-06 08:30:19 -07003156/*
3157 * megasas_get_ld_list_info - Returns FW's ld_list structure
3158 * @instance: Adapter soft state
3159 * @ld_list: ld_list structure
3160 *
3161 * Issues an internal command (DCMD) to get the FW's controller PD
3162 * list structure. This information is mainly used to find out SYSTEM
3163 * supported by the FW.
3164 */
3165static int
3166megasas_get_ld_list(struct megasas_instance *instance)
3167{
3168 int ret = 0, ld_index = 0, ids = 0;
3169 struct megasas_cmd *cmd;
3170 struct megasas_dcmd_frame *dcmd;
3171 struct MR_LD_LIST *ci;
3172 dma_addr_t ci_h = 0;
3173
3174 cmd = megasas_get_cmd(instance);
3175
3176 if (!cmd) {
3177 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3178 return -ENOMEM;
3179 }
3180
3181 dcmd = &cmd->frame->dcmd;
3182
3183 ci = pci_alloc_consistent(instance->pdev,
3184 sizeof(struct MR_LD_LIST),
3185 &ci_h);
3186
3187 if (!ci) {
3188 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3189 megasas_return_cmd(instance, cmd);
3190 return -ENOMEM;
3191 }
3192
3193 memset(ci, 0, sizeof(*ci));
3194 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3195
3196 dcmd->cmd = MFI_CMD_DCMD;
3197 dcmd->cmd_status = 0xFF;
3198 dcmd->sge_count = 1;
3199 dcmd->flags = MFI_FRAME_DIR_READ;
3200 dcmd->timeout = 0;
3201 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3202 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3203 dcmd->sgl.sge32[0].phys_addr = ci_h;
3204 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3205 dcmd->pad_0 = 0;
3206
3207 if (!megasas_issue_polled(instance, cmd)) {
3208 ret = 0;
3209 } else {
3210 ret = -1;
3211 }
3212
3213 /* the following function will get the instance PD LIST */
3214
bo yang39a98552010-09-22 22:36:29 -04003215 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003216 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3217
3218 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3219 if (ci->ldList[ld_index].state != 0) {
3220 ids = ci->ldList[ld_index].ref.targetId;
3221 instance->ld_ids[ids] =
3222 ci->ldList[ld_index].ref.targetId;
3223 }
3224 }
3225 }
3226
3227 pci_free_consistent(instance->pdev,
3228 sizeof(struct MR_LD_LIST),
3229 ci,
3230 ci_h);
3231
3232 megasas_return_cmd(instance, cmd);
3233 return ret;
3234}
3235
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003236/**
3237 * megasas_get_controller_info - Returns FW's controller structure
3238 * @instance: Adapter soft state
3239 * @ctrl_info: Controller information structure
3240 *
3241 * Issues an internal command (DCMD) to get the FW's controller structure.
3242 * This information is mainly used to find out the maximum IO transfer per
3243 * command supported by the FW.
3244 */
3245static int
3246megasas_get_ctrl_info(struct megasas_instance *instance,
3247 struct megasas_ctrl_info *ctrl_info)
3248{
3249 int ret = 0;
3250 struct megasas_cmd *cmd;
3251 struct megasas_dcmd_frame *dcmd;
3252 struct megasas_ctrl_info *ci;
3253 dma_addr_t ci_h = 0;
3254
3255 cmd = megasas_get_cmd(instance);
3256
3257 if (!cmd) {
3258 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3259 return -ENOMEM;
3260 }
3261
3262 dcmd = &cmd->frame->dcmd;
3263
3264 ci = pci_alloc_consistent(instance->pdev,
3265 sizeof(struct megasas_ctrl_info), &ci_h);
3266
3267 if (!ci) {
3268 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3269 megasas_return_cmd(instance, cmd);
3270 return -ENOMEM;
3271 }
3272
3273 memset(ci, 0, sizeof(*ci));
3274 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3275
3276 dcmd->cmd = MFI_CMD_DCMD;
3277 dcmd->cmd_status = 0xFF;
3278 dcmd->sge_count = 1;
3279 dcmd->flags = MFI_FRAME_DIR_READ;
3280 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003281 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003282 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3283 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3284 dcmd->sgl.sge32[0].phys_addr = ci_h;
3285 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3286
3287 if (!megasas_issue_polled(instance, cmd)) {
3288 ret = 0;
3289 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3290 } else {
3291 ret = -1;
3292 }
3293
3294 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3295 ci, ci_h);
3296
3297 megasas_return_cmd(instance, cmd);
3298 return ret;
3299}
3300
3301/**
bo yang31ea7082007-11-07 12:09:50 -05003302 * megasas_issue_init_mfi - Initializes the FW
3303 * @instance: Adapter soft state
3304 *
3305 * Issues the INIT MFI cmd
3306 */
3307static int
3308megasas_issue_init_mfi(struct megasas_instance *instance)
3309{
3310 u32 context;
3311
3312 struct megasas_cmd *cmd;
3313
3314 struct megasas_init_frame *init_frame;
3315 struct megasas_init_queue_info *initq_info;
3316 dma_addr_t init_frame_h;
3317 dma_addr_t initq_info_h;
3318
3319 /*
3320 * Prepare a init frame. Note the init frame points to queue info
3321 * structure. Each frame has SGL allocated after first 64 bytes. For
3322 * this frame - since we don't need any SGL - we use SGL's space as
3323 * queue info structure
3324 *
3325 * We will not get a NULL command below. We just created the pool.
3326 */
3327 cmd = megasas_get_cmd(instance);
3328
3329 init_frame = (struct megasas_init_frame *)cmd->frame;
3330 initq_info = (struct megasas_init_queue_info *)
3331 ((unsigned long)init_frame + 64);
3332
3333 init_frame_h = cmd->frame_phys_addr;
3334 initq_info_h = init_frame_h + 64;
3335
3336 context = init_frame->context;
3337 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3338 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3339 init_frame->context = context;
3340
3341 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3342 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3343
3344 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3345 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3346
3347 init_frame->cmd = MFI_CMD_INIT;
3348 init_frame->cmd_status = 0xFF;
3349 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3350
3351 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3352
3353 /*
3354 * disable the intr before firing the init frame to FW
3355 */
3356 instance->instancet->disable_intr(instance->reg_set);
3357
3358 /*
3359 * Issue the init frame in polled mode
3360 */
3361
3362 if (megasas_issue_polled(instance, cmd)) {
3363 printk(KERN_ERR "megasas: Failed to init firmware\n");
3364 megasas_return_cmd(instance, cmd);
3365 goto fail_fw_init;
3366 }
3367
3368 megasas_return_cmd(instance, cmd);
3369
3370 return 0;
3371
3372fail_fw_init:
3373 return -EINVAL;
3374}
3375
adam radfordcd50ba82010-12-21 10:23:23 -08003376static u32
3377megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003378{
adam radfordcd50ba82010-12-21 10:23:23 -08003379 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003380 u32 context_sz;
3381 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003382
3383 reg_set = instance->reg_set;
3384
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003385 /*
3386 * Get various operational parameters from status register
3387 */
Sumant Patro1341c932006-01-25 12:02:40 -08003388 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003389 /*
3390 * Reduce the max supported cmds by 1. This is to ensure that the
3391 * reply_q_sz (1 more than the max cmd that driver may send)
3392 * does not exceed max cmds that the FW can support
3393 */
3394 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003395 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003396 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003397 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003398 /*
3399 * Create a pool of commands
3400 */
3401 if (megasas_alloc_cmds(instance))
3402 goto fail_alloc_cmds;
3403
3404 /*
3405 * Allocate memory for reply queue. Length of reply queue should
3406 * be _one_ more than the maximum commands handled by the firmware.
3407 *
3408 * Note: When FW completes commands, it places corresponding contex
3409 * values in this circular reply queue. This circular queue is a fairly
3410 * typical producer-consumer queue. FW is the producer (of completed
3411 * commands) and the driver is the consumer.
3412 */
3413 context_sz = sizeof(u32);
3414 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3415
3416 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3417 reply_q_sz,
3418 &instance->reply_queue_h);
3419
3420 if (!instance->reply_queue) {
3421 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3422 goto fail_reply_queue;
3423 }
3424
bo yang31ea7082007-11-07 12:09:50 -05003425 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003426 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003427
bo yang39a98552010-09-22 22:36:29 -04003428 instance->fw_support_ieee = 0;
3429 instance->fw_support_ieee =
3430 (instance->instancet->read_fw_status_reg(reg_set) &
3431 0x04000000);
3432
3433 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3434 instance->fw_support_ieee);
3435
3436 if (instance->fw_support_ieee)
3437 instance->flag_ieee = 1;
3438
adam radfordcd50ba82010-12-21 10:23:23 -08003439 return 0;
3440
3441fail_fw_init:
3442
3443 pci_free_consistent(instance->pdev, reply_q_sz,
3444 instance->reply_queue, instance->reply_queue_h);
3445fail_reply_queue:
3446 megasas_free_cmds(instance);
3447
3448fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003449 return 1;
3450}
3451
3452/**
3453 * megasas_init_fw - Initializes the FW
3454 * @instance: Adapter soft state
3455 *
3456 * This is the main function for initializing firmware
3457 */
3458
3459static int megasas_init_fw(struct megasas_instance *instance)
3460{
3461 u32 max_sectors_1;
3462 u32 max_sectors_2;
adam radford3f1abce2011-05-11 18:33:47 -07003463 u32 tmp_sectors, msix_enable;
Ben Collins2290f5a2013-09-13 12:46:44 -04003464 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08003465 struct megasas_register_set __iomem *reg_set;
3466 struct megasas_ctrl_info *ctrl_info;
3467 unsigned long bar_list;
adam radfordc8e858f2011-10-08 18:15:13 -07003468 int i;
adam radfordcd50ba82010-12-21 10:23:23 -08003469
3470 /* Find first memory bar */
3471 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3472 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
adam radfordcd50ba82010-12-21 10:23:23 -08003473 if (pci_request_selected_regions(instance->pdev, instance->bar,
3474 "megasas: LSI")) {
3475 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3476 return -EBUSY;
3477 }
3478
Ben Collins2290f5a2013-09-13 12:46:44 -04003479 base_addr = pci_resource_start(instance->pdev, instance->bar);
3480 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08003481
3482 if (!instance->reg_set) {
3483 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3484 goto fail_ioremap;
3485 }
3486
3487 reg_set = instance->reg_set;
3488
3489 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003490 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07003491 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08003492 instance->instancet = &megasas_instance_template_fusion;
3493 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003494 case PCI_DEVICE_ID_LSI_SAS1078R:
3495 case PCI_DEVICE_ID_LSI_SAS1078DE:
3496 instance->instancet = &megasas_instance_template_ppc;
3497 break;
3498 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3499 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3500 instance->instancet = &megasas_instance_template_gen2;
3501 break;
3502 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3503 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3504 instance->instancet = &megasas_instance_template_skinny;
3505 break;
3506 case PCI_DEVICE_ID_LSI_SAS1064R:
3507 case PCI_DEVICE_ID_DELL_PERC5:
3508 default:
3509 instance->instancet = &megasas_instance_template_xscale;
3510 break;
3511 }
3512
Sumit.Saxena@lsi.come842dbe2013-07-16 02:26:05 +05303513 if (megasas_transition_to_ready(instance, 0)) {
3514 atomic_set(&instance->fw_reset_no_pci_access, 1);
3515 instance->instancet->adp_reset
3516 (instance, instance->reg_set);
3517 atomic_set(&instance->fw_reset_no_pci_access, 0);
3518 dev_info(&instance->pdev->dev,
3519 "megasas: FW restarted successfully from %s!\n",
3520 __func__);
3521
3522 /*waitting for about 30 second before retry*/
3523 ssleep(30);
3524
3525 if (megasas_transition_to_ready(instance, 0))
3526 goto fail_ready_state;
3527 }
adam radfordcd50ba82010-12-21 10:23:23 -08003528
adam radford3f1abce2011-05-11 18:33:47 -07003529 /* Check if MSI-X is supported while in ready state */
3530 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3531 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07003532 if (msix_enable && !msix_disable) {
3533 /* Check max MSI-X vectors */
3534 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3535 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3536 instance->msix_vectors = (readl(&instance->reg_set->
3537 outbound_scratch_pad_2
3538 ) & 0x1F) + 1;
adam radford079eadd2012-10-01 19:26:59 -07003539 if (msix_vectors)
3540 instance->msix_vectors =
3541 min(msix_vectors,
3542 instance->msix_vectors);
adam radfordc8e858f2011-10-08 18:15:13 -07003543 } else
3544 instance->msix_vectors = 1;
3545 /* Don't bother allocating more MSI-X vectors than cpus */
3546 instance->msix_vectors = min(instance->msix_vectors,
3547 (unsigned int)num_online_cpus());
3548 for (i = 0; i < instance->msix_vectors; i++)
3549 instance->msixentry[i].entry = i;
3550 i = pci_enable_msix(instance->pdev, instance->msixentry,
3551 instance->msix_vectors);
3552 if (i >= 0) {
3553 if (i) {
3554 if (!pci_enable_msix(instance->pdev,
3555 instance->msixentry, i))
3556 instance->msix_vectors = i;
3557 else
3558 instance->msix_vectors = 0;
3559 }
3560 } else
3561 instance->msix_vectors = 0;
3562 }
adam radford3f1abce2011-05-11 18:33:47 -07003563
adam radfordcd50ba82010-12-21 10:23:23 -08003564 /* Get operational params, sge flags, send init cmd to controller */
3565 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003566 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003567
3568 printk(KERN_ERR "megasas: INIT adapter done\n");
3569
bo yang39a98552010-09-22 22:36:29 -04003570 /** for passthrough
3571 * the following function will get the PD LIST.
3572 */
3573
Yang, Bo81e403c2009-10-06 14:27:54 -06003574 memset(instance->pd_list, 0 ,
3575 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3576 megasas_get_pd_list(instance);
3577
Yang, Bobdc6fb82009-12-06 08:30:19 -07003578 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3579 megasas_get_ld_list(instance);
3580
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003581 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3582
3583 /*
3584 * Compute the max allowed sectors per IO: The controller info has two
3585 * limits on max sectors. Driver should use the minimum of these two.
3586 *
3587 * 1 << stripe_sz_ops.min = max sectors per strip
3588 *
3589 * Note that older firmwares ( < FW ver 30) didn't report information
3590 * to calculate max_sectors_1. So the number ended up as zero always.
3591 */
bo yang14faea92007-11-09 04:14:00 -05003592 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003593 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3594
3595 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3596 ctrl_info->max_strips_per_io;
3597 max_sectors_2 = ctrl_info->max_request_size;
3598
bo yang14faea92007-11-09 04:14:00 -05003599 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003600 instance->disableOnlineCtrlReset =
3601 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003602 }
3603
3604 instance->max_sectors_per_req = instance->max_num_sge *
3605 PAGE_SIZE / 512;
3606 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3607 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003608
3609 kfree(ctrl_info);
3610
adam radfordc5daa6a2012-07-17 18:20:03 -07003611 /* Check for valid throttlequeuedepth module parameter */
3612 if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3613 instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3614 if (throttlequeuedepth > (instance->max_fw_cmds -
3615 MEGASAS_SKINNY_INT_CMDS))
3616 instance->throttlequeuedepth =
3617 MEGASAS_THROTTLE_QUEUE_DEPTH;
3618 else
3619 instance->throttlequeuedepth = throttlequeuedepth;
3620 } else {
3621 if (throttlequeuedepth > (instance->max_fw_cmds -
3622 MEGASAS_INT_CMDS))
3623 instance->throttlequeuedepth =
3624 MEGASAS_THROTTLE_QUEUE_DEPTH;
3625 else
3626 instance->throttlequeuedepth = throttlequeuedepth;
3627 }
3628
Sumant Patro5d018ad2006-10-03 13:13:18 -07003629 /*
3630 * Setup tasklet for cmd completion
3631 */
3632
adam radfordf86c5422011-02-24 20:57:00 -08003633 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003634 (unsigned long)instance);
3635
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003636 return 0;
3637
adam radfordeb1b1232011-02-24 20:55:56 -08003638fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003639fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003640 iounmap(instance->reg_set);
3641
3642 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003643 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003644
3645 return -EINVAL;
3646}
3647
3648/**
3649 * megasas_release_mfi - Reverses the FW initialization
3650 * @intance: Adapter soft state
3651 */
3652static void megasas_release_mfi(struct megasas_instance *instance)
3653{
adam radford9c915a82010-12-21 13:34:31 -08003654 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003655
adam radford9c915a82010-12-21 13:34:31 -08003656 if (instance->reply_queue)
3657 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003658 instance->reply_queue, instance->reply_queue_h);
3659
3660 megasas_free_cmds(instance);
3661
3662 iounmap(instance->reg_set);
3663
adam radfordb6d5d882010-12-14 18:56:07 -08003664 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003665}
3666
3667/**
3668 * megasas_get_seq_num - Gets latest event sequence numbers
3669 * @instance: Adapter soft state
3670 * @eli: FW event log sequence numbers information
3671 *
3672 * FW maintains a log of all events in a non-volatile area. Upper layers would
3673 * usually find out the latest sequence number of the events, the seq number at
3674 * the boot etc. They would "read" all the events below the latest seq number
3675 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3676 * number), they would subsribe to AEN (asynchronous event notification) and
3677 * wait for the events to happen.
3678 */
3679static int
3680megasas_get_seq_num(struct megasas_instance *instance,
3681 struct megasas_evt_log_info *eli)
3682{
3683 struct megasas_cmd *cmd;
3684 struct megasas_dcmd_frame *dcmd;
3685 struct megasas_evt_log_info *el_info;
3686 dma_addr_t el_info_h = 0;
3687
3688 cmd = megasas_get_cmd(instance);
3689
3690 if (!cmd) {
3691 return -ENOMEM;
3692 }
3693
3694 dcmd = &cmd->frame->dcmd;
3695 el_info = pci_alloc_consistent(instance->pdev,
3696 sizeof(struct megasas_evt_log_info),
3697 &el_info_h);
3698
3699 if (!el_info) {
3700 megasas_return_cmd(instance, cmd);
3701 return -ENOMEM;
3702 }
3703
3704 memset(el_info, 0, sizeof(*el_info));
3705 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3706
3707 dcmd->cmd = MFI_CMD_DCMD;
3708 dcmd->cmd_status = 0x0;
3709 dcmd->sge_count = 1;
3710 dcmd->flags = MFI_FRAME_DIR_READ;
3711 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003712 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003713 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3714 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3715 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3716 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3717
3718 megasas_issue_blocked_cmd(instance, cmd);
3719
3720 /*
3721 * Copy the data back into callers buffer
3722 */
3723 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3724
3725 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3726 el_info, el_info_h);
3727
3728 megasas_return_cmd(instance, cmd);
3729
3730 return 0;
3731}
3732
3733/**
3734 * megasas_register_aen - Registers for asynchronous event notification
3735 * @instance: Adapter soft state
3736 * @seq_num: The starting sequence number
3737 * @class_locale: Class of the event
3738 *
3739 * This function subscribes for AEN for events beyond the @seq_num. It requests
3740 * to be notified if and only if the event is of type @class_locale
3741 */
3742static int
3743megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3744 u32 class_locale_word)
3745{
3746 int ret_val;
3747 struct megasas_cmd *cmd;
3748 struct megasas_dcmd_frame *dcmd;
3749 union megasas_evt_class_locale curr_aen;
3750 union megasas_evt_class_locale prev_aen;
3751
3752 /*
3753 * If there an AEN pending already (aen_cmd), check if the
3754 * class_locale of that pending AEN is inclusive of the new
3755 * AEN request we currently have. If it is, then we don't have
3756 * to do anything. In other words, whichever events the current
3757 * AEN request is subscribing to, have already been subscribed
3758 * to.
3759 *
3760 * If the old_cmd is _not_ inclusive, then we have to abort
3761 * that command, form a class_locale that is superset of both
3762 * old and current and re-issue to the FW
3763 */
3764
3765 curr_aen.word = class_locale_word;
3766
3767 if (instance->aen_cmd) {
3768
3769 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3770
3771 /*
3772 * A class whose enum value is smaller is inclusive of all
3773 * higher values. If a PROGRESS (= -1) was previously
3774 * registered, then a new registration requests for higher
3775 * classes need not be sent to FW. They are automatically
3776 * included.
3777 *
3778 * Locale numbers don't have such hierarchy. They are bitmap
3779 * values
3780 */
3781 if ((prev_aen.members.class <= curr_aen.members.class) &&
3782 !((prev_aen.members.locale & curr_aen.members.locale) ^
3783 curr_aen.members.locale)) {
3784 /*
3785 * Previously issued event registration includes
3786 * current request. Nothing to do.
3787 */
3788 return 0;
3789 } else {
3790 curr_aen.members.locale |= prev_aen.members.locale;
3791
3792 if (prev_aen.members.class < curr_aen.members.class)
3793 curr_aen.members.class = prev_aen.members.class;
3794
3795 instance->aen_cmd->abort_aen = 1;
3796 ret_val = megasas_issue_blocked_abort_cmd(instance,
3797 instance->
3798 aen_cmd);
3799
3800 if (ret_val) {
3801 printk(KERN_DEBUG "megasas: Failed to abort "
3802 "previous AEN command\n");
3803 return ret_val;
3804 }
3805 }
3806 }
3807
3808 cmd = megasas_get_cmd(instance);
3809
3810 if (!cmd)
3811 return -ENOMEM;
3812
3813 dcmd = &cmd->frame->dcmd;
3814
3815 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3816
3817 /*
3818 * Prepare DCMD for aen registration
3819 */
3820 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3821
3822 dcmd->cmd = MFI_CMD_DCMD;
3823 dcmd->cmd_status = 0x0;
3824 dcmd->sge_count = 1;
3825 dcmd->flags = MFI_FRAME_DIR_READ;
3826 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003827 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003828 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003829 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3830 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3831 dcmd->mbox.w[0] = seq_num;
3832 dcmd->mbox.w[1] = curr_aen.word;
3833 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3834 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3835
Yang, Bof4c9a132009-10-06 14:43:28 -06003836 if (instance->aen_cmd != NULL) {
3837 megasas_return_cmd(instance, cmd);
3838 return 0;
3839 }
3840
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003841 /*
3842 * Store reference to the cmd used to register for AEN. When an
3843 * application wants us to register for AEN, we have to abort this
3844 * cmd and re-register with a new EVENT LOCALE supplied by that app
3845 */
3846 instance->aen_cmd = cmd;
3847
3848 /*
3849 * Issue the aen registration frame
3850 */
adam radford9c915a82010-12-21 13:34:31 -08003851 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003852
3853 return 0;
3854}
3855
3856/**
3857 * megasas_start_aen - Subscribes to AEN during driver load time
3858 * @instance: Adapter soft state
3859 */
3860static int megasas_start_aen(struct megasas_instance *instance)
3861{
3862 struct megasas_evt_log_info eli;
3863 union megasas_evt_class_locale class_locale;
3864
3865 /*
3866 * Get the latest sequence number from FW
3867 */
3868 memset(&eli, 0, sizeof(eli));
3869
3870 if (megasas_get_seq_num(instance, &eli))
3871 return -1;
3872
3873 /*
3874 * Register AEN with FW for latest sequence number plus 1
3875 */
3876 class_locale.members.reserved = 0;
3877 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3878 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3879
3880 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3881 class_locale.word);
3882}
3883
3884/**
3885 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3886 * @instance: Adapter soft state
3887 */
3888static int megasas_io_attach(struct megasas_instance *instance)
3889{
3890 struct Scsi_Host *host = instance->host;
3891
3892 /*
3893 * Export parameters required by SCSI mid-layer
3894 */
3895 host->irq = instance->pdev->irq;
3896 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003897 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3898 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3899 host->can_queue =
3900 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3901 } else
3902 host->can_queue =
3903 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003904 host->this_id = instance->init_id;
3905 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003906
3907 if (instance->fw_support_ieee)
3908 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3909
Yang, Bo1fd10682010-10-12 07:18:50 -06003910 /*
3911 * Check if the module parameter value for max_sectors can be used
3912 */
3913 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3914 instance->max_sectors_per_req = max_sectors;
3915 else {
3916 if (max_sectors) {
3917 if (((instance->pdev->device ==
3918 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3919 (instance->pdev->device ==
3920 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3921 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3922 instance->max_sectors_per_req = max_sectors;
3923 } else {
3924 printk(KERN_INFO "megasas: max_sectors should be > 0"
3925 "and <= %d (or < 1MB for GEN2 controller)\n",
3926 instance->max_sectors_per_req);
3927 }
3928 }
3929 }
3930
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003931 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003932 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003933 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3934 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3935 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003936 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003937
adam radford9c915a82010-12-21 13:34:31 -08003938 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07003939 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3940 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08003941 host->hostt->eh_device_reset_handler = NULL;
3942 host->hostt->eh_bus_reset_handler = NULL;
3943 }
3944
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003945 /*
3946 * Notify the mid-layer about the new controller
3947 */
3948 if (scsi_add_host(host, &instance->pdev->dev)) {
3949 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3950 return -ENODEV;
3951 }
3952
3953 /*
3954 * Trigger SCSI to scan our drives
3955 */
3956 scsi_scan_host(host);
3957 return 0;
3958}
3959
bo yang31ea7082007-11-07 12:09:50 -05003960static int
3961megasas_set_dma_mask(struct pci_dev *pdev)
3962{
3963 /*
3964 * All our contollers are capable of performing 64-bit DMA
3965 */
3966 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003967 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003968
Yang Hongyang284901a2009-04-06 19:01:15 -07003969 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003970 goto fail_set_dma_mask;
3971 }
3972 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003973 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003974 goto fail_set_dma_mask;
3975 }
3976 return 0;
3977
3978fail_set_dma_mask:
3979 return 1;
3980}
3981
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003982/**
3983 * megasas_probe_one - PCI hotplug entry point
3984 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003985 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003986 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08003987static int megasas_probe_one(struct pci_dev *pdev,
3988 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003989{
adam radfordc8e858f2011-10-08 18:15:13 -07003990 int rval, pos, i, j;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003991 struct Scsi_Host *host;
3992 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08003993 u16 control = 0;
3994
3995 /* Reset MSI-X in the kdump kernel */
3996 if (reset_devices) {
3997 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3998 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06003999 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08004000 &control);
4001 if (control & PCI_MSIX_FLAGS_ENABLE) {
4002 dev_info(&pdev->dev, "resetting MSI-X\n");
4003 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06004004 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08004005 control &
4006 ~PCI_MSIX_FLAGS_ENABLE);
4007 }
4008 }
4009 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004010
4011 /*
4012 * Announce PCI information
4013 */
4014 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4015 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4016 pdev->subsystem_device);
4017
4018 printk("bus %d:slot %d:func %d\n",
4019 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4020
4021 /*
4022 * PCI prepping: enable device set bus mastering and dma mask
4023 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004024 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004025
4026 if (rval) {
4027 return rval;
4028 }
4029
4030 pci_set_master(pdev);
4031
bo yang31ea7082007-11-07 12:09:50 -05004032 if (megasas_set_dma_mask(pdev))
4033 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004034
4035 host = scsi_host_alloc(&megasas_template,
4036 sizeof(struct megasas_instance));
4037
4038 if (!host) {
4039 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4040 goto fail_alloc_instance;
4041 }
4042
4043 instance = (struct megasas_instance *)host->hostdata;
4044 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04004045 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08004046 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004047
adam radford9c915a82010-12-21 13:34:31 -08004048 switch (instance->pdev->device) {
4049 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004050 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004051 {
4052 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004053
adam radford9c915a82010-12-21 13:34:31 -08004054 instance->ctrl_context =
4055 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4056 if (!instance->ctrl_context) {
4057 printk(KERN_DEBUG "megasas: Failed to allocate "
4058 "memory for Fusion context info\n");
4059 goto fail_alloc_dma_buf;
4060 }
4061 fusion = instance->ctrl_context;
4062 INIT_LIST_HEAD(&fusion->cmd_pool);
4063 spin_lock_init(&fusion->cmd_pool_lock);
4064 }
4065 break;
4066 default: /* For all other supported controllers */
4067
4068 instance->producer =
4069 pci_alloc_consistent(pdev, sizeof(u32),
4070 &instance->producer_h);
4071 instance->consumer =
4072 pci_alloc_consistent(pdev, sizeof(u32),
4073 &instance->consumer_h);
4074
4075 if (!instance->producer || !instance->consumer) {
4076 printk(KERN_DEBUG "megasas: Failed to allocate"
4077 "memory for producer, consumer\n");
4078 goto fail_alloc_dma_buf;
4079 }
4080
4081 *instance->producer = 0;
4082 *instance->consumer = 0;
4083 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004084 }
4085
Yang, Boc3518832009-10-06 14:18:02 -06004086 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004087 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004088 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004089 instance->issuepend_done = 1;
4090 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4091 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004092
4093 instance->evt_detail = pci_alloc_consistent(pdev,
4094 sizeof(struct
4095 megasas_evt_detail),
4096 &instance->evt_detail_h);
4097
4098 if (!instance->evt_detail) {
4099 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4100 "event detail structure\n");
4101 goto fail_alloc_dma_buf;
4102 }
4103
4104 /*
4105 * Initialize locks and queues
4106 */
4107 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004108 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004109
Sumant Patroe4a082c2006-05-30 12:03:37 -07004110 atomic_set(&instance->fw_outstanding,0);
4111
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004112 init_waitqueue_head(&instance->int_cmd_wait_q);
4113 init_waitqueue_head(&instance->abort_cmd_wait_q);
4114
4115 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004116 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004117 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004118
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004119 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004120 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004121
4122 /*
4123 * Initialize PCI related and misc parameters
4124 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004125 instance->host = host;
4126 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4127 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4128
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004129 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4130 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004131 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004132 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4133 } else
4134 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4135
Sumant Patro658dced2006-10-03 13:09:14 -07004136 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004137 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004138 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004139 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004140 instance->disableOnlineCtrlReset = 1;
4141
adam radford36807e62011-10-08 18:15:06 -07004142 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4143 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08004144 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4145 else
4146 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004147
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004148 /*
adam radford0a770662011-02-24 20:56:12 -08004149 * Initialize MFI Firmware
4150 */
4151 if (megasas_init_fw(instance))
4152 goto fail_init_mfi;
4153
4154 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004155 * Register IRQ
4156 */
adam radfordc8e858f2011-10-08 18:15:13 -07004157 if (instance->msix_vectors) {
4158 for (i = 0 ; i < instance->msix_vectors; i++) {
4159 instance->irq_context[i].instance = instance;
4160 instance->irq_context[i].MSIxIndex = i;
4161 if (request_irq(instance->msixentry[i].vector,
4162 instance->instancet->service_isr, 0,
4163 "megasas",
4164 &instance->irq_context[i])) {
4165 printk(KERN_DEBUG "megasas: Failed to "
4166 "register IRQ for vector %d.\n", i);
4167 for (j = 0 ; j < i ; j++)
4168 free_irq(
4169 instance->msixentry[j].vector,
4170 &instance->irq_context[j]);
4171 goto fail_irq;
4172 }
4173 }
4174 } else {
4175 instance->irq_context[0].instance = instance;
4176 instance->irq_context[0].MSIxIndex = 0;
4177 if (request_irq(pdev->irq, instance->instancet->service_isr,
4178 IRQF_SHARED, "megasas",
4179 &instance->irq_context[0])) {
4180 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4181 goto fail_irq;
4182 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004183 }
4184
Sumant Patro1341c932006-01-25 12:02:40 -08004185 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004186
4187 /*
4188 * Store instance in PCI softstate
4189 */
4190 pci_set_drvdata(pdev, instance);
4191
4192 /*
4193 * Add this controller to megasas_mgmt_info structure so that it
4194 * can be exported to management applications
4195 */
4196 megasas_mgmt_info.count++;
4197 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4198 megasas_mgmt_info.max_index++;
4199
4200 /*
adam radford541f90b2011-05-11 18:34:29 -07004201 * Register with SCSI mid-layer
4202 */
4203 if (megasas_io_attach(instance))
4204 goto fail_io_attach;
4205
4206 instance->unload = 0;
4207
4208 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004209 * Initiate AEN (Asynchronous Event Notification)
4210 */
4211 if (megasas_start_aen(instance)) {
4212 printk(KERN_DEBUG "megasas: start aen failed\n");
4213 goto fail_start_aen;
4214 }
4215
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004216 return 0;
4217
4218 fail_start_aen:
4219 fail_io_attach:
4220 megasas_mgmt_info.count--;
4221 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4222 megasas_mgmt_info.max_index--;
4223
4224 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004225 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004226 if (instance->msix_vectors)
4227 for (i = 0 ; i < instance->msix_vectors; i++)
4228 free_irq(instance->msixentry[i].vector,
4229 &instance->irq_context[i]);
4230 else
4231 free_irq(instance->pdev->irq, &instance->irq_context[0]);
adam radfordeb1b1232011-02-24 20:55:56 -08004232fail_irq:
adam radford36807e62011-10-08 18:15:06 -07004233 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4234 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radfordeb1b1232011-02-24 20:55:56 -08004235 megasas_release_fusion(instance);
4236 else
4237 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004238 fail_init_mfi:
adam radfordc8e858f2011-10-08 18:15:13 -07004239 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08004240 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004241 fail_alloc_dma_buf:
4242 if (instance->evt_detail)
4243 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4244 instance->evt_detail,
4245 instance->evt_detail_h);
4246
adam radfordeb1b1232011-02-24 20:55:56 -08004247 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004248 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4249 instance->producer_h);
4250 if (instance->consumer)
4251 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4252 instance->consumer_h);
4253 scsi_host_put(host);
4254
4255 fail_alloc_instance:
4256 fail_set_dma_mask:
4257 pci_disable_device(pdev);
4258
4259 return -ENODEV;
4260}
4261
4262/**
4263 * megasas_flush_cache - Requests FW to flush all its caches
4264 * @instance: Adapter soft state
4265 */
4266static void megasas_flush_cache(struct megasas_instance *instance)
4267{
4268 struct megasas_cmd *cmd;
4269 struct megasas_dcmd_frame *dcmd;
4270
bo yang39a98552010-09-22 22:36:29 -04004271 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4272 return;
4273
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004274 cmd = megasas_get_cmd(instance);
4275
4276 if (!cmd)
4277 return;
4278
4279 dcmd = &cmd->frame->dcmd;
4280
4281 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4282
4283 dcmd->cmd = MFI_CMD_DCMD;
4284 dcmd->cmd_status = 0x0;
4285 dcmd->sge_count = 0;
4286 dcmd->flags = MFI_FRAME_DIR_NONE;
4287 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004288 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004289 dcmd->data_xfer_len = 0;
4290 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4291 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4292
4293 megasas_issue_blocked_cmd(instance, cmd);
4294
4295 megasas_return_cmd(instance, cmd);
4296
4297 return;
4298}
4299
4300/**
4301 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4302 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004303 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004304 */
bo yang31ea7082007-11-07 12:09:50 -05004305static void megasas_shutdown_controller(struct megasas_instance *instance,
4306 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004307{
4308 struct megasas_cmd *cmd;
4309 struct megasas_dcmd_frame *dcmd;
4310
bo yang39a98552010-09-22 22:36:29 -04004311 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4312 return;
4313
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004314 cmd = megasas_get_cmd(instance);
4315
4316 if (!cmd)
4317 return;
4318
4319 if (instance->aen_cmd)
4320 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004321 if (instance->map_update_cmd)
4322 megasas_issue_blocked_abort_cmd(instance,
4323 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004324 dcmd = &cmd->frame->dcmd;
4325
4326 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4327
4328 dcmd->cmd = MFI_CMD_DCMD;
4329 dcmd->cmd_status = 0x0;
4330 dcmd->sge_count = 0;
4331 dcmd->flags = MFI_FRAME_DIR_NONE;
4332 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004333 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004334 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004335 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004336
4337 megasas_issue_blocked_cmd(instance, cmd);
4338
4339 megasas_return_cmd(instance, cmd);
4340
4341 return;
4342}
4343
Jiri Slaby33139b22008-05-01 17:56:02 +02004344#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004345/**
bo yangad84db22007-11-09 04:40:16 -05004346 * megasas_suspend - driver suspend entry point
4347 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004348 * @state: PCI power state to suspend routine
4349 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004350static int
bo yang31ea7082007-11-07 12:09:50 -05004351megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4352{
4353 struct Scsi_Host *host;
4354 struct megasas_instance *instance;
adam radfordc8e858f2011-10-08 18:15:13 -07004355 int i;
bo yang31ea7082007-11-07 12:09:50 -05004356
4357 instance = pci_get_drvdata(pdev);
4358 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004359 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004360
4361 megasas_flush_cache(instance);
4362 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004363
4364 /* cancel the delayed work if this work still in queue */
4365 if (instance->ev != NULL) {
4366 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08004367 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004368 instance->ev = NULL;
4369 }
4370
bo yang31ea7082007-11-07 12:09:50 -05004371 tasklet_kill(&instance->isr_tasklet);
4372
4373 pci_set_drvdata(instance->pdev, instance);
4374 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004375
4376 if (instance->msix_vectors)
4377 for (i = 0 ; i < instance->msix_vectors; i++)
4378 free_irq(instance->msixentry[i].vector,
4379 &instance->irq_context[i]);
4380 else
4381 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4382 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004383 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004384
4385 pci_save_state(pdev);
4386 pci_disable_device(pdev);
4387
4388 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4389
4390 return 0;
4391}
4392
4393/**
4394 * megasas_resume- driver resume entry point
4395 * @pdev: PCI device structure
4396 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004397static int
bo yang31ea7082007-11-07 12:09:50 -05004398megasas_resume(struct pci_dev *pdev)
4399{
adam radfordc8e858f2011-10-08 18:15:13 -07004400 int rval, i, j;
bo yang31ea7082007-11-07 12:09:50 -05004401 struct Scsi_Host *host;
4402 struct megasas_instance *instance;
4403
4404 instance = pci_get_drvdata(pdev);
4405 host = instance->host;
4406 pci_set_power_state(pdev, PCI_D0);
4407 pci_enable_wake(pdev, PCI_D0, 0);
4408 pci_restore_state(pdev);
4409
4410 /*
4411 * PCI prepping: enable device set bus mastering and dma mask
4412 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004413 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004414
4415 if (rval) {
4416 printk(KERN_ERR "megasas: Enable device failed\n");
4417 return rval;
4418 }
4419
4420 pci_set_master(pdev);
4421
4422 if (megasas_set_dma_mask(pdev))
4423 goto fail_set_dma_mask;
4424
4425 /*
4426 * Initialize MFI Firmware
4427 */
4428
bo yang31ea7082007-11-07 12:09:50 -05004429 atomic_set(&instance->fw_outstanding, 0);
4430
4431 /*
4432 * We expect the FW state to be READY
4433 */
adam radford058a8fa2011-10-08 18:14:27 -07004434 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05004435 goto fail_ready_state;
4436
adam radford3f1abce2011-05-11 18:33:47 -07004437 /* Now re-enable MSI-X */
adam radfordc8e858f2011-10-08 18:15:13 -07004438 if (instance->msix_vectors)
4439 pci_enable_msix(instance->pdev, instance->msixentry,
4440 instance->msix_vectors);
adam radford3f1abce2011-05-11 18:33:47 -07004441
adam radford9c915a82010-12-21 13:34:31 -08004442 switch (instance->pdev->device) {
4443 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004444 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004445 {
4446 megasas_reset_reply_desc(instance);
4447 if (megasas_ioc_init_fusion(instance)) {
4448 megasas_free_cmds(instance);
4449 megasas_free_cmds_fusion(instance);
4450 goto fail_init_mfi;
4451 }
4452 if (!megasas_get_map_info(instance))
4453 megasas_sync_map_info(instance);
4454 }
4455 break;
4456 default:
4457 *instance->producer = 0;
4458 *instance->consumer = 0;
4459 if (megasas_issue_init_mfi(instance))
4460 goto fail_init_mfi;
4461 break;
4462 }
bo yang31ea7082007-11-07 12:09:50 -05004463
adam radford9c915a82010-12-21 13:34:31 -08004464 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4465 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004466
4467 /*
4468 * Register IRQ
4469 */
adam radfordc8e858f2011-10-08 18:15:13 -07004470 if (instance->msix_vectors) {
4471 for (i = 0 ; i < instance->msix_vectors; i++) {
4472 instance->irq_context[i].instance = instance;
4473 instance->irq_context[i].MSIxIndex = i;
4474 if (request_irq(instance->msixentry[i].vector,
4475 instance->instancet->service_isr, 0,
4476 "megasas",
4477 &instance->irq_context[i])) {
4478 printk(KERN_DEBUG "megasas: Failed to "
4479 "register IRQ for vector %d.\n", i);
4480 for (j = 0 ; j < i ; j++)
4481 free_irq(
4482 instance->msixentry[j].vector,
4483 &instance->irq_context[j]);
4484 goto fail_irq;
4485 }
4486 }
4487 } else {
4488 instance->irq_context[0].instance = instance;
4489 instance->irq_context[0].MSIxIndex = 0;
4490 if (request_irq(pdev->irq, instance->instancet->service_isr,
4491 IRQF_SHARED, "megasas",
4492 &instance->irq_context[0])) {
4493 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4494 goto fail_irq;
4495 }
bo yang31ea7082007-11-07 12:09:50 -05004496 }
4497
4498 instance->instancet->enable_intr(instance->reg_set);
Yang, Bo0c79e682009-10-06 14:47:35 -06004499 instance->unload = 0;
4500
adam radford541f90b2011-05-11 18:34:29 -07004501 /*
4502 * Initiate AEN (Asynchronous Event Notification)
4503 */
4504 if (megasas_start_aen(instance))
4505 printk(KERN_ERR "megasas: Start AEN failed\n");
4506
bo yang31ea7082007-11-07 12:09:50 -05004507 return 0;
4508
4509fail_irq:
4510fail_init_mfi:
4511 if (instance->evt_detail)
4512 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4513 instance->evt_detail,
4514 instance->evt_detail_h);
4515
4516 if (instance->producer)
4517 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4518 instance->producer_h);
4519 if (instance->consumer)
4520 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4521 instance->consumer_h);
4522 scsi_host_put(host);
4523
4524fail_set_dma_mask:
4525fail_ready_state:
4526
4527 pci_disable_device(pdev);
4528
4529 return -ENODEV;
4530}
Jiri Slaby33139b22008-05-01 17:56:02 +02004531#else
4532#define megasas_suspend NULL
4533#define megasas_resume NULL
4534#endif
bo yang31ea7082007-11-07 12:09:50 -05004535
4536/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004537 * megasas_detach_one - PCI hot"un"plug entry point
4538 * @pdev: PCI device structure
4539 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004540static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004541{
4542 int i;
4543 struct Scsi_Host *host;
4544 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004545 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004546
4547 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004548 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004549 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004550 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004551
4552 scsi_remove_host(instance->host);
4553 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004554 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004555
4556 /* cancel the delayed work if this work still in queue*/
4557 if (instance->ev != NULL) {
4558 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08004559 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004560 instance->ev = NULL;
4561 }
4562
Sumant Patro5d018ad2006-10-03 13:13:18 -07004563 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004564
4565 /*
4566 * Take the instance off the instance array. Note that we will not
4567 * decrement the max_index. We let this array be sparse array
4568 */
4569 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4570 if (megasas_mgmt_info.instance[i] == instance) {
4571 megasas_mgmt_info.count--;
4572 megasas_mgmt_info.instance[i] = NULL;
4573
4574 break;
4575 }
4576 }
4577
4578 pci_set_drvdata(instance->pdev, NULL);
4579
Sumant Patrob274cab2006-10-03 12:52:12 -07004580 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004581
adam radfordc8e858f2011-10-08 18:15:13 -07004582 if (instance->msix_vectors)
4583 for (i = 0 ; i < instance->msix_vectors; i++)
4584 free_irq(instance->msixentry[i].vector,
4585 &instance->irq_context[i]);
4586 else
4587 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4588 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004589 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004590
adam radford9c915a82010-12-21 13:34:31 -08004591 switch (instance->pdev->device) {
4592 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004593 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004594 megasas_release_fusion(instance);
4595 for (i = 0; i < 2 ; i++)
4596 if (fusion->ld_map[i])
4597 dma_free_coherent(&instance->pdev->dev,
4598 fusion->map_sz,
4599 fusion->ld_map[i],
4600 fusion->
4601 ld_map_phys[i]);
4602 kfree(instance->ctrl_context);
4603 break;
4604 default:
4605 megasas_release_mfi(instance);
4606 pci_free_consistent(pdev,
4607 sizeof(struct megasas_evt_detail),
4608 instance->evt_detail,
4609 instance->evt_detail_h);
4610 pci_free_consistent(pdev, sizeof(u32),
4611 instance->producer,
4612 instance->producer_h);
4613 pci_free_consistent(pdev, sizeof(u32),
4614 instance->consumer,
4615 instance->consumer_h);
4616 break;
4617 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004618
4619 scsi_host_put(host);
4620
4621 pci_set_drvdata(pdev, NULL);
4622
4623 pci_disable_device(pdev);
4624
4625 return;
4626}
4627
4628/**
4629 * megasas_shutdown - Shutdown entry point
4630 * @device: Generic device structure
4631 */
4632static void megasas_shutdown(struct pci_dev *pdev)
4633{
adam radfordc8e858f2011-10-08 18:15:13 -07004634 int i;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004635 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07004636
Yang, Bo0c79e682009-10-06 14:47:35 -06004637 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004638 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004639 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
adam radford46fd2562011-05-11 18:34:17 -07004640 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004641 if (instance->msix_vectors)
4642 for (i = 0 ; i < instance->msix_vectors; i++)
4643 free_irq(instance->msixentry[i].vector,
4644 &instance->irq_context[i]);
4645 else
4646 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4647 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07004648 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004649}
4650
4651/**
4652 * megasas_mgmt_open - char node "open" entry point
4653 */
4654static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4655{
4656 /*
4657 * Allow only those users with admin rights
4658 */
4659 if (!capable(CAP_SYS_ADMIN))
4660 return -EACCES;
4661
4662 return 0;
4663}
4664
4665/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004666 * megasas_mgmt_fasync - Async notifier registration from applications
4667 *
4668 * This function adds the calling process to a driver global queue. When an
4669 * event occurs, SIGIO will be sent to all processes in this queue.
4670 */
4671static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4672{
4673 int rc;
4674
Arjan van de Ven0b950672006-01-11 13:16:10 +01004675 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004676
4677 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4678
Arjan van de Ven0b950672006-01-11 13:16:10 +01004679 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004680
4681 if (rc >= 0) {
4682 /* For sanity check when we get ioctl */
4683 filep->private_data = filep;
4684 return 0;
4685 }
4686
4687 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4688
4689 return rc;
4690}
4691
4692/**
Yang, Boc3518832009-10-06 14:18:02 -06004693 * megasas_mgmt_poll - char node "poll" entry point
4694 * */
4695static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4696{
4697 unsigned int mask;
4698 unsigned long flags;
4699 poll_wait(file, &megasas_poll_wait, wait);
4700 spin_lock_irqsave(&poll_aen_lock, flags);
4701 if (megasas_poll_wait_aen)
4702 mask = (POLLIN | POLLRDNORM);
4703 else
4704 mask = 0;
4705 spin_unlock_irqrestore(&poll_aen_lock, flags);
4706 return mask;
4707}
4708
4709/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004710 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4711 * @instance: Adapter soft state
4712 * @argp: User's ioctl packet
4713 */
4714static int
4715megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4716 struct megasas_iocpacket __user * user_ioc,
4717 struct megasas_iocpacket *ioc)
4718{
4719 struct megasas_sge32 *kern_sge32;
4720 struct megasas_cmd *cmd;
4721 void *kbuff_arr[MAX_IOCTL_SGE];
4722 dma_addr_t buf_handle = 0;
4723 int error = 0, i;
4724 void *sense = NULL;
4725 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004726 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004727
4728 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4729
4730 if (ioc->sge_count > MAX_IOCTL_SGE) {
4731 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4732 ioc->sge_count, MAX_IOCTL_SGE);
4733 return -EINVAL;
4734 }
4735
4736 cmd = megasas_get_cmd(instance);
4737 if (!cmd) {
4738 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4739 return -ENOMEM;
4740 }
4741
4742 /*
4743 * User's IOCTL packet has 2 frames (maximum). Copy those two
4744 * frames into our cmd's frames. cmd->frame's context will get
4745 * overwritten when we copy from user's frames. So set that value
4746 * alone separately
4747 */
4748 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4749 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004750 cmd->frame->hdr.pad_0 = 0;
adam radford882be7c32012-01-06 17:02:34 -08004751 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4752 MFI_FRAME_SENSE64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004753
4754 /*
4755 * The management interface between applications and the fw uses
4756 * MFI frames. E.g, RAID configuration changes, LD property changes
4757 * etc are accomplishes through different kinds of MFI frames. The
4758 * driver needs to care only about substituting user buffers with
4759 * kernel buffers in SGLs. The location of SGL is embedded in the
4760 * struct iocpacket itself.
4761 */
4762 kern_sge32 = (struct megasas_sge32 *)
4763 ((unsigned long)cmd->frame + ioc->sgl_off);
4764
4765 /*
4766 * For each user buffer, create a mirror buffer and copy in
4767 */
4768 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004769 if (!ioc->sgl[i].iov_len)
4770 continue;
4771
Sumant Patro9f35fa82007-02-14 12:55:45 -08004772 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004773 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004774 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004775 if (!kbuff_arr[i]) {
4776 printk(KERN_DEBUG "megasas: Failed to alloc "
4777 "kernel SGL buffer for IOCTL \n");
4778 error = -ENOMEM;
4779 goto out;
4780 }
4781
4782 /*
4783 * We don't change the dma_coherent_mask, so
4784 * pci_alloc_consistent only returns 32bit addresses
4785 */
4786 kern_sge32[i].phys_addr = (u32) buf_handle;
4787 kern_sge32[i].length = ioc->sgl[i].iov_len;
4788
4789 /*
4790 * We created a kernel buffer corresponding to the
4791 * user buffer. Now copy in from the user buffer
4792 */
4793 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4794 (u32) (ioc->sgl[i].iov_len))) {
4795 error = -EFAULT;
4796 goto out;
4797 }
4798 }
4799
4800 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004801 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4802 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004803 if (!sense) {
4804 error = -ENOMEM;
4805 goto out;
4806 }
4807
4808 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004809 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004810 *sense_ptr = sense_handle;
4811 }
4812
4813 /*
4814 * Set the sync_cmd flag so that the ISR knows not to complete this
4815 * cmd to the SCSI mid-layer
4816 */
4817 cmd->sync_cmd = 1;
4818 megasas_issue_blocked_cmd(instance, cmd);
4819 cmd->sync_cmd = 0;
4820
4821 /*
4822 * copy out the kernel buffers to user buffers
4823 */
4824 for (i = 0; i < ioc->sge_count; i++) {
4825 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4826 ioc->sgl[i].iov_len)) {
4827 error = -EFAULT;
4828 goto out;
4829 }
4830 }
4831
4832 /*
4833 * copy out the sense
4834 */
4835 if (ioc->sense_len) {
4836 /*
bo yangb70a41e2008-03-18 03:13:06 -04004837 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004838 * sense buffer address
4839 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004840 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4841 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004842
bo yangb70a41e2008-03-18 03:13:06 -04004843 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4844 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004845 printk(KERN_ERR "megasas: Failed to copy out to user "
4846 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004847 error = -EFAULT;
4848 goto out;
4849 }
4850 }
4851
4852 /*
4853 * copy the status codes returned by the fw
4854 */
4855 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4856 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4857 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4858 error = -EFAULT;
4859 }
4860
4861 out:
4862 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004863 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004864 sense, sense_handle);
4865 }
4866
Bjørn Mork4ac697c2012-11-21 09:54:48 +01004867 for (i = 0; i < ioc->sge_count; i++) {
4868 if (kbuff_arr[i])
4869 dma_free_coherent(&instance->pdev->dev,
4870 kern_sge32[i].length,
4871 kbuff_arr[i],
4872 kern_sge32[i].phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004873 }
4874
4875 megasas_return_cmd(instance, cmd);
4876 return error;
4877}
4878
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004879static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4880{
4881 struct megasas_iocpacket __user *user_ioc =
4882 (struct megasas_iocpacket __user *)arg;
4883 struct megasas_iocpacket *ioc;
4884 struct megasas_instance *instance;
4885 int error;
bo yang39a98552010-09-22 22:36:29 -04004886 int i;
4887 unsigned long flags;
4888 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004889
4890 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4891 if (!ioc)
4892 return -ENOMEM;
4893
4894 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4895 error = -EFAULT;
4896 goto out_kfree_ioc;
4897 }
4898
4899 instance = megasas_lookup_instance(ioc->host_no);
4900 if (!instance) {
4901 error = -ENODEV;
4902 goto out_kfree_ioc;
4903 }
4904
bo yang39a98552010-09-22 22:36:29 -04004905 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4906 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004907 error = -ENODEV;
4908 goto out_kfree_ioc;
4909 }
4910
4911 if (instance->unload == 1) {
4912 error = -ENODEV;
4913 goto out_kfree_ioc;
4914 }
4915
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004916 /*
4917 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4918 */
4919 if (down_interruptible(&instance->ioctl_sem)) {
4920 error = -ERESTARTSYS;
4921 goto out_kfree_ioc;
4922 }
bo yang39a98552010-09-22 22:36:29 -04004923
4924 for (i = 0; i < wait_time; i++) {
4925
4926 spin_lock_irqsave(&instance->hba_lock, flags);
4927 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4928 spin_unlock_irqrestore(&instance->hba_lock, flags);
4929 break;
4930 }
4931 spin_unlock_irqrestore(&instance->hba_lock, flags);
4932
4933 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4934 printk(KERN_NOTICE "megasas: waiting"
4935 "for controller reset to finish\n");
4936 }
4937
4938 msleep(1000);
4939 }
4940
4941 spin_lock_irqsave(&instance->hba_lock, flags);
4942 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4943 spin_unlock_irqrestore(&instance->hba_lock, flags);
4944
4945 printk(KERN_ERR "megaraid_sas: timed out while"
4946 "waiting for HBA to recover\n");
4947 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03004948 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04004949 }
4950 spin_unlock_irqrestore(&instance->hba_lock, flags);
4951
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004952 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Dan Carpenterc64e4832013-04-16 10:44:19 +03004953 out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004954 up(&instance->ioctl_sem);
4955
4956 out_kfree_ioc:
4957 kfree(ioc);
4958 return error;
4959}
4960
4961static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4962{
4963 struct megasas_instance *instance;
4964 struct megasas_aen aen;
4965 int error;
bo yang39a98552010-09-22 22:36:29 -04004966 int i;
4967 unsigned long flags;
4968 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004969
4970 if (file->private_data != file) {
4971 printk(KERN_DEBUG "megasas: fasync_helper was not "
4972 "called first\n");
4973 return -EINVAL;
4974 }
4975
4976 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4977 return -EFAULT;
4978
4979 instance = megasas_lookup_instance(aen.host_no);
4980
4981 if (!instance)
4982 return -ENODEV;
4983
bo yang39a98552010-09-22 22:36:29 -04004984 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4985 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004986 }
4987
4988 if (instance->unload == 1) {
4989 return -ENODEV;
4990 }
4991
bo yang39a98552010-09-22 22:36:29 -04004992 for (i = 0; i < wait_time; i++) {
4993
4994 spin_lock_irqsave(&instance->hba_lock, flags);
4995 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4996 spin_unlock_irqrestore(&instance->hba_lock,
4997 flags);
4998 break;
4999 }
5000
5001 spin_unlock_irqrestore(&instance->hba_lock, flags);
5002
5003 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5004 printk(KERN_NOTICE "megasas: waiting for"
5005 "controller reset to finish\n");
5006 }
5007
5008 msleep(1000);
5009 }
5010
5011 spin_lock_irqsave(&instance->hba_lock, flags);
5012 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5013 spin_unlock_irqrestore(&instance->hba_lock, flags);
5014 printk(KERN_ERR "megaraid_sas: timed out while waiting"
5015 "for HBA to recover.\n");
5016 return -ENODEV;
5017 }
5018 spin_unlock_irqrestore(&instance->hba_lock, flags);
5019
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005020 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005021 error = megasas_register_aen(instance, aen.seq_num,
5022 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005023 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005024 return error;
5025}
5026
5027/**
5028 * megasas_mgmt_ioctl - char node ioctl entry point
5029 */
5030static long
5031megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5032{
5033 switch (cmd) {
5034 case MEGASAS_IOC_FIRMWARE:
5035 return megasas_mgmt_ioctl_fw(file, arg);
5036
5037 case MEGASAS_IOC_GET_AEN:
5038 return megasas_mgmt_ioctl_aen(file, arg);
5039 }
5040
5041 return -ENOTTY;
5042}
5043
5044#ifdef CONFIG_COMPAT
5045static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5046{
5047 struct compat_megasas_iocpacket __user *cioc =
5048 (struct compat_megasas_iocpacket __user *)arg;
5049 struct megasas_iocpacket __user *ioc =
5050 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5051 int i;
5052 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005053 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005054
Jeff Garzik83aabc12006-10-04 06:34:03 -04005055 if (clear_user(ioc, sizeof(*ioc)))
5056 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005057
5058 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5059 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5060 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5061 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5062 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5063 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5064 return -EFAULT;
5065
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005066 /*
5067 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5068 * sense_len is not null, so prepare the 64bit value under
5069 * the same condition.
5070 */
5071 if (ioc->sense_len) {
5072 void __user **sense_ioc_ptr =
5073 (void __user **)(ioc->frame.raw + ioc->sense_off);
5074 compat_uptr_t *sense_cioc_ptr =
5075 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5076 if (get_user(ptr, sense_cioc_ptr) ||
5077 put_user(compat_ptr(ptr), sense_ioc_ptr))
5078 return -EFAULT;
5079 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005080
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005081 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005082 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5083 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5084 copy_in_user(&ioc->sgl[i].iov_len,
5085 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5086 return -EFAULT;
5087 }
5088
5089 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5090
5091 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5092 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5093 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5094 return -EFAULT;
5095 }
5096 return error;
5097}
5098
5099static long
5100megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5101 unsigned long arg)
5102{
5103 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08005104 case MEGASAS_IOC_FIRMWARE32:
5105 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005106 case MEGASAS_IOC_GET_AEN:
5107 return megasas_mgmt_ioctl_aen(file, arg);
5108 }
5109
5110 return -ENOTTY;
5111}
5112#endif
5113
5114/*
5115 * File operations structure for management interface
5116 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08005117static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005118 .owner = THIS_MODULE,
5119 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005120 .fasync = megasas_mgmt_fasync,
5121 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005122 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005123#ifdef CONFIG_COMPAT
5124 .compat_ioctl = megasas_mgmt_compat_ioctl,
5125#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005126 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005127};
5128
5129/*
5130 * PCI hotplug support registration structure
5131 */
5132static struct pci_driver megasas_pci_driver = {
5133
5134 .name = "megaraid_sas",
5135 .id_table = megasas_pci_table,
5136 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005137 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05005138 .suspend = megasas_suspend,
5139 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005140 .shutdown = megasas_shutdown,
5141};
5142
5143/*
5144 * Sysfs driver attributes
5145 */
5146static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5147{
5148 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5149 MEGASAS_VERSION);
5150}
5151
5152static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5153
5154static ssize_t
5155megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5156{
5157 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5158 MEGASAS_RELDATE);
5159}
5160
5161static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5162 NULL);
5163
Sumant Patro658dced2006-10-03 13:09:14 -07005164static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005165megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5166{
5167 return sprintf(buf, "%u\n", support_poll_for_event);
5168}
5169
5170static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5171 megasas_sysfs_show_support_poll_for_event, NULL);
5172
Yang, Bo837f5fe2010-10-11 06:59:20 -06005173 static ssize_t
5174megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5175{
5176 return sprintf(buf, "%u\n", support_device_change);
5177}
5178
5179static DRIVER_ATTR(support_device_change, S_IRUGO,
5180 megasas_sysfs_show_support_device_change, NULL);
5181
Yang, Bo72c4fd32009-10-06 14:20:59 -06005182static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005183megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5184{
bo yangad84db22007-11-09 04:40:16 -05005185 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005186}
5187
5188static ssize_t
5189megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5190{
5191 int retval = count;
5192 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5193 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5194 retval = -EINVAL;
5195 }
5196 return retval;
5197}
5198
Joe Malicki66dca9b2008-08-14 17:14:48 -04005199static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005200 megasas_sysfs_set_dbg_lvl);
5201
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005202static void
5203megasas_aen_polling(struct work_struct *work)
5204{
5205 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08005206 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005207 struct megasas_instance *instance = ev->instance;
5208 union megasas_evt_class_locale class_locale;
5209 struct Scsi_Host *host;
5210 struct scsi_device *sdev1;
5211 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005212 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005213 int i, j, doscan = 0;
5214 u32 seq_num;
5215 int error;
5216
5217 if (!instance) {
5218 printk(KERN_ERR "invalid instance!\n");
5219 kfree(ev);
5220 return;
5221 }
5222 instance->ev = NULL;
5223 host = instance->host;
5224 if (instance->evt_detail) {
5225
5226 switch (instance->evt_detail->code) {
5227 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005228 if (megasas_get_pd_list(instance) == 0) {
5229 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5230 for (j = 0;
5231 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5232 j++) {
5233
5234 pd_index =
5235 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5236
5237 sdev1 =
5238 scsi_device_lookup(host, i, j, 0);
5239
5240 if (instance->pd_list[pd_index].driveState
5241 == MR_PD_STATE_SYSTEM) {
5242 if (!sdev1) {
5243 scsi_add_device(host, i, j, 0);
5244 }
5245
5246 if (sdev1)
5247 scsi_device_put(sdev1);
5248 }
5249 }
5250 }
5251 }
5252 doscan = 0;
5253 break;
5254
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005255 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005256 if (megasas_get_pd_list(instance) == 0) {
Yang, Boc9786842009-12-06 08:39:25 -07005257 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5258 for (j = 0;
5259 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5260 j++) {
5261
5262 pd_index =
5263 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5264
5265 sdev1 =
5266 scsi_device_lookup(host, i, j, 0);
5267
5268 if (instance->pd_list[pd_index].driveState
5269 == MR_PD_STATE_SYSTEM) {
5270 if (sdev1) {
5271 scsi_device_put(sdev1);
5272 }
5273 } else {
5274 if (sdev1) {
5275 scsi_remove_device(sdev1);
5276 scsi_device_put(sdev1);
5277 }
5278 }
5279 }
5280 }
5281 }
5282 doscan = 0;
5283 break;
5284
5285 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005286 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005287 case MR_EVT_LD_DELETED:
5288 megasas_get_ld_list(instance);
5289 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5290 for (j = 0;
5291 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5292 j++) {
5293
5294 ld_index =
5295 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5296
5297 sdev1 = scsi_device_lookup(host,
5298 i + MEGASAS_MAX_LD_CHANNELS,
5299 j,
5300 0);
5301
5302 if (instance->ld_ids[ld_index] != 0xff) {
5303 if (sdev1) {
5304 scsi_device_put(sdev1);
5305 }
5306 } else {
5307 if (sdev1) {
5308 scsi_remove_device(sdev1);
5309 scsi_device_put(sdev1);
5310 }
5311 }
5312 }
5313 }
5314 doscan = 0;
5315 break;
5316 case MR_EVT_LD_CREATED:
5317 megasas_get_ld_list(instance);
5318 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5319 for (j = 0;
5320 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5321 j++) {
5322 ld_index =
5323 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5324
5325 sdev1 = scsi_device_lookup(host,
5326 i+MEGASAS_MAX_LD_CHANNELS,
5327 j, 0);
5328
5329 if (instance->ld_ids[ld_index] !=
5330 0xff) {
5331 if (!sdev1) {
5332 scsi_add_device(host,
5333 i + 2,
5334 j, 0);
5335 }
5336 }
5337 if (sdev1) {
5338 scsi_device_put(sdev1);
5339 }
5340 }
5341 }
5342 doscan = 0;
5343 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005344 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005345 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005346 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005347 doscan = 1;
5348 break;
5349 default:
5350 doscan = 0;
5351 break;
5352 }
5353 } else {
5354 printk(KERN_ERR "invalid evt_detail!\n");
5355 kfree(ev);
5356 return;
5357 }
5358
5359 if (doscan) {
5360 printk(KERN_INFO "scanning ...\n");
5361 megasas_get_pd_list(instance);
5362 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5363 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5364 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5365 sdev1 = scsi_device_lookup(host, i, j, 0);
5366 if (instance->pd_list[pd_index].driveState ==
5367 MR_PD_STATE_SYSTEM) {
5368 if (!sdev1) {
5369 scsi_add_device(host, i, j, 0);
5370 }
5371 if (sdev1)
5372 scsi_device_put(sdev1);
5373 } else {
5374 if (sdev1) {
5375 scsi_remove_device(sdev1);
5376 scsi_device_put(sdev1);
5377 }
5378 }
5379 }
5380 }
Yang, Boc9786842009-12-06 08:39:25 -07005381
5382 megasas_get_ld_list(instance);
5383 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5384 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5385 ld_index =
5386 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5387
5388 sdev1 = scsi_device_lookup(host,
5389 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5390 if (instance->ld_ids[ld_index] != 0xff) {
5391 if (!sdev1) {
5392 scsi_add_device(host,
5393 i+2,
5394 j, 0);
5395 } else {
5396 scsi_device_put(sdev1);
5397 }
5398 } else {
5399 if (sdev1) {
5400 scsi_remove_device(sdev1);
5401 scsi_device_put(sdev1);
5402 }
5403 }
5404 }
5405 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005406 }
5407
5408 if ( instance->aen_cmd != NULL ) {
5409 kfree(ev);
5410 return ;
5411 }
5412
5413 seq_num = instance->evt_detail->seq_num + 1;
5414
5415 /* Register AEN with FW for latest sequence number plus 1 */
5416 class_locale.members.reserved = 0;
5417 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5418 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5419 mutex_lock(&instance->aen_mutex);
5420 error = megasas_register_aen(instance, seq_num,
5421 class_locale.word);
5422 mutex_unlock(&instance->aen_mutex);
5423
5424 if (error)
5425 printk(KERN_ERR "register aen failed error %x\n", error);
5426
5427 kfree(ev);
5428}
5429
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005430/**
5431 * megasas_init - Driver load entry point
5432 */
5433static int __init megasas_init(void)
5434{
5435 int rval;
5436
5437 /*
5438 * Announce driver version and other information
5439 */
5440 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5441 MEGASAS_EXT_VERSION);
5442
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07005443 spin_lock_init(&poll_aen_lock);
5444
Yang, Bo72c4fd32009-10-06 14:20:59 -06005445 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005446 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005447
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005448 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5449
5450 /*
5451 * Register character device node
5452 */
5453 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5454
5455 if (rval < 0) {
5456 printk(KERN_DEBUG "megasas: failed to open device node\n");
5457 return rval;
5458 }
5459
5460 megasas_mgmt_majorno = rval;
5461
5462 /*
5463 * Register ourselves as PCI hotplug module
5464 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005465 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005466
5467 if (rval) {
5468 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005469 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005470 }
5471
Jeff Garzik83aabc12006-10-04 06:34:03 -04005472 rval = driver_create_file(&megasas_pci_driver.driver,
5473 &driver_attr_version);
5474 if (rval)
5475 goto err_dcf_attr_ver;
5476 rval = driver_create_file(&megasas_pci_driver.driver,
5477 &driver_attr_release_date);
5478 if (rval)
5479 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005480
5481 rval = driver_create_file(&megasas_pci_driver.driver,
5482 &driver_attr_support_poll_for_event);
5483 if (rval)
5484 goto err_dcf_support_poll_for_event;
5485
Jeff Garzik83aabc12006-10-04 06:34:03 -04005486 rval = driver_create_file(&megasas_pci_driver.driver,
5487 &driver_attr_dbg_lvl);
5488 if (rval)
5489 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005490 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005491 &driver_attr_support_device_change);
5492 if (rval)
5493 goto err_dcf_support_device_change;
5494
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005495 return rval;
bo yangad84db22007-11-09 04:40:16 -05005496
Yang, Bo837f5fe2010-10-11 06:59:20 -06005497err_dcf_support_device_change:
5498 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005499 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005500err_dcf_dbg_lvl:
5501 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005502 &driver_attr_support_poll_for_event);
5503
5504err_dcf_support_poll_for_event:
5505 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005506 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005507
Jeff Garzik83aabc12006-10-04 06:34:03 -04005508err_dcf_rel_date:
5509 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5510err_dcf_attr_ver:
5511 pci_unregister_driver(&megasas_pci_driver);
5512err_pcidrv:
5513 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005514 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005515}
5516
5517/**
5518 * megasas_exit - Driver unload entry point
5519 */
5520static void __exit megasas_exit(void)
5521{
Sumant Patro658dced2006-10-03 13:09:14 -07005522 driver_remove_file(&megasas_pci_driver.driver,
5523 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005524 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005525 &driver_attr_support_poll_for_event);
5526 driver_remove_file(&megasas_pci_driver.driver,
5527 &driver_attr_support_device_change);
5528 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005529 &driver_attr_release_date);
5530 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005531
5532 pci_unregister_driver(&megasas_pci_driver);
5533 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5534}
5535
5536module_init(megasas_init);
5537module_exit(megasas_exit);