blob: 2bdc70754b14704b069dd9b3fea4d60991698cb5 [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
adam radford3f1530c2010-12-14 18:51:48 -08002 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003 *
adam radford3f1530c2010-12-14 18:51:48 -08004 * Copyright (c) 2009-2011 LSI Corporation.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005 *
adam radford3f1530c2010-12-14 18:51:48 -08006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040010 *
adam radford3f1530c2010-12-14 18:51:48 -080011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040015 *
adam radford3f1530c2010-12-14 18:51:48 -080016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040019 *
adam radford3f1530c2010-12-14 18:51:48 -080020 * FILE: megaraid_sas_base.c
adam radford8d6f5ce2011-10-08 18:15:24 -070021 * Version : v00.00.06.12-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/*
62 * poll_mode_io:1- schedule complete completion from q cmd
63 */
64static unsigned int poll_mode_io;
65module_param_named(poll_mode_io, poll_mode_io, int, 0);
66MODULE_PARM_DESC(poll_mode_io,
67 "Complete cmds from IO path, (default=0)");
68
Yang, Bo1fd10682010-10-12 07:18:50 -060069/*
70 * Number of sectors per IO command
71 * Will be set in megasas_init_mfi if user does not provide
72 */
73static unsigned int max_sectors;
74module_param_named(max_sectors, max_sectors, int, 0);
75MODULE_PARM_DESC(max_sectors,
76 "Maximum number of sectors per IO command");
77
adam radford80d9da92010-12-21 10:17:40 -080078static int msix_disable;
79module_param(msix_disable, int, S_IRUGO);
80MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
81
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040082MODULE_LICENSE("GPL");
83MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080084MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050085MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040086
adam radford058a8fa2011-10-08 18:14:27 -070087int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -040088static int megasas_get_pd_list(struct megasas_instance *instance);
89static int megasas_issue_init_mfi(struct megasas_instance *instance);
90static int megasas_register_aen(struct megasas_instance *instance,
91 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040092/*
93 * PCI ID table for all supported controllers
94 */
95static struct pci_device_id megasas_pci_table[] = {
96
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020097 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
98 /* xscale IOP */
99 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
100 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400101 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
102 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
104 /* gen2*/
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
106 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
108 /* skinny*/
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
110 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
112 /* xscale IOP, vega */
113 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
114 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
116 /* Fusion */
adam radford36807e62011-10-08 18:15:06 -0700117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
118 /* Invader */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200119 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400120};
121
122MODULE_DEVICE_TABLE(pci, megasas_pci_table);
123
124static int megasas_mgmt_majorno;
125static struct megasas_mgmt_info megasas_mgmt_info;
126static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100127static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400128
Yang, Boc3518832009-10-06 14:18:02 -0600129static int megasas_poll_wait_aen;
130static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600131static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800132u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600133static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700134
Yang, Boc3518832009-10-06 14:18:02 -0600135/* define lock for aen poll */
136spinlock_t poll_aen_lock;
137
adam radford9c915a82010-12-21 13:34:31 -0800138void
bo yang7343eb62007-11-09 04:35:44 -0500139megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
140 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800141static u32
142megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
143static int
144megasas_adp_reset_gen2(struct megasas_instance *instance,
145 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800146static irqreturn_t megasas_isr(int irq, void *devp);
147static u32
148megasas_init_adapter_mfi(struct megasas_instance *instance);
149u32
150megasas_build_and_issue_cmd(struct megasas_instance *instance,
151 struct scsi_cmnd *scmd);
152static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800153void
154megasas_release_fusion(struct megasas_instance *instance);
155int
156megasas_ioc_init_fusion(struct megasas_instance *instance);
157void
158megasas_free_cmds_fusion(struct megasas_instance *instance);
159u8
160megasas_get_map_info(struct megasas_instance *instance);
161int
162megasas_sync_map_info(struct megasas_instance *instance);
163int
164wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
165void megasas_reset_reply_desc(struct megasas_instance *instance);
166u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
167 struct LD_LOAD_BALANCE_INFO *lbInfo);
168int megasas_reset_fusion(struct Scsi_Host *shost);
169void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800170
171void
172megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
173{
174 instance->instancet->fire_cmd(instance,
175 cmd->frame_phys_addr, 0, instance->reg_set);
176}
bo yang7343eb62007-11-09 04:35:44 -0500177
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400178/**
179 * megasas_get_cmd - Get a command from the free pool
180 * @instance: Adapter soft state
181 *
182 * Returns a free command from the pool
183 */
adam radford9c915a82010-12-21 13:34:31 -0800184struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400185 *instance)
186{
187 unsigned long flags;
188 struct megasas_cmd *cmd = NULL;
189
190 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
191
192 if (!list_empty(&instance->cmd_pool)) {
193 cmd = list_entry((&instance->cmd_pool)->next,
194 struct megasas_cmd, list);
195 list_del_init(&cmd->list);
196 } else {
197 printk(KERN_ERR "megasas: Command pool empty!\n");
198 }
199
200 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
201 return cmd;
202}
203
204/**
205 * megasas_return_cmd - Return a cmd to free command pool
206 * @instance: Adapter soft state
207 * @cmd: Command packet to be returned to free command pool
208 */
adam radford9c915a82010-12-21 13:34:31 -0800209inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400210megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
211{
212 unsigned long flags;
213
214 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
215
216 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800217 cmd->frame_count = 0;
adam radforde5f93a32011-10-08 18:15:19 -0700218 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
219 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
220 (reset_devices))
221 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400222 list_add_tail(&cmd->list, &instance->cmd_pool);
223
224 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
225}
226
Sumant Patro1341c932006-01-25 12:02:40 -0800227
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400228/**
adam radford0d490162010-12-14 19:17:17 -0800229* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800230* (deviceid : 1064R, PERC5) controllers
231*/
232
233/**
234 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400235 * @regs: MFI register set
236 */
237static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800238megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400239{
bo yang39a98552010-09-22 22:36:29 -0400240 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400241
242 /* Dummy readl to force pci flush */
243 readl(&regs->outbound_intr_mask);
244}
245
246/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700247 * megasas_disable_intr_xscale -Disables interrupt
248 * @regs: MFI register set
249 */
250static inline void
251megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
252{
253 u32 mask = 0x1f;
254 writel(mask, &regs->outbound_intr_mask);
255 /* Dummy readl to force pci flush */
256 readl(&regs->outbound_intr_mask);
257}
258
259/**
Sumant Patro1341c932006-01-25 12:02:40 -0800260 * megasas_read_fw_status_reg_xscale - returns the current FW status value
261 * @regs: MFI register set
262 */
263static u32
264megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
265{
266 return readl(&(regs)->outbound_msg_0);
267}
268/**
269 * megasas_clear_interrupt_xscale - Check & clear interrupt
270 * @regs: MFI register set
271 */
adam radford0d490162010-12-14 19:17:17 -0800272static int
Sumant Patro1341c932006-01-25 12:02:40 -0800273megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
274{
275 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400276 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800277 /*
278 * Check if it is our interrupt
279 */
280 status = readl(&regs->outbound_intr_status);
281
bo yang39a98552010-09-22 22:36:29 -0400282 if (status & MFI_OB_INTR_STATUS_MASK)
283 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
284 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
285 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800286
287 /*
288 * Clear the interrupt by writing back the same value
289 */
bo yang39a98552010-09-22 22:36:29 -0400290 if (mfiStatus)
291 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800292
Yang, Bo06f579d2008-08-10 12:42:37 -0700293 /* Dummy readl to force pci flush */
294 readl(&regs->outbound_intr_status);
295
bo yang39a98552010-09-22 22:36:29 -0400296 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800297}
298
299/**
300 * megasas_fire_cmd_xscale - Sends command to the FW
301 * @frame_phys_addr : Physical address of cmd
302 * @frame_count : Number of frames for the command
303 * @regs : MFI register set
304 */
adam radford0d490162010-12-14 19:17:17 -0800305static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600306megasas_fire_cmd_xscale(struct megasas_instance *instance,
307 dma_addr_t frame_phys_addr,
308 u32 frame_count,
309 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800310{
bo yang39a98552010-09-22 22:36:29 -0400311 unsigned long flags;
312 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800313 writel((frame_phys_addr >> 3)|(frame_count),
314 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400315 spin_unlock_irqrestore(&instance->hba_lock, flags);
316}
317
318/**
319 * megasas_adp_reset_xscale - For controller reset
320 * @regs: MFI register set
321 */
322static int
323megasas_adp_reset_xscale(struct megasas_instance *instance,
324 struct megasas_register_set __iomem *regs)
325{
326 u32 i;
327 u32 pcidata;
328 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
329
330 for (i = 0; i < 3; i++)
331 msleep(1000); /* sleep for 3 secs */
332 pcidata = 0;
333 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
334 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
335 if (pcidata & 0x2) {
336 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
337 pcidata &= ~0x2;
338 pci_write_config_dword(instance->pdev,
339 MFI_1068_PCSR_OFFSET, pcidata);
340
341 for (i = 0; i < 2; i++)
342 msleep(1000); /* need to wait 2 secs again */
343
344 pcidata = 0;
345 pci_read_config_dword(instance->pdev,
346 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
347 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
348 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
349 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
350 pcidata = 0;
351 pci_write_config_dword(instance->pdev,
352 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
353 }
354 }
355 return 0;
356}
357
358/**
359 * megasas_check_reset_xscale - For controller reset check
360 * @regs: MFI register set
361 */
362static int
363megasas_check_reset_xscale(struct megasas_instance *instance,
364 struct megasas_register_set __iomem *regs)
365{
366 u32 consumer;
367 consumer = *instance->consumer;
368
369 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
370 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
371 return 1;
372 }
373 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800374}
375
376static struct megasas_instance_template megasas_instance_template_xscale = {
377
378 .fire_cmd = megasas_fire_cmd_xscale,
379 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700380 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800381 .clear_intr = megasas_clear_intr_xscale,
382 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400383 .adp_reset = megasas_adp_reset_xscale,
384 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800385 .service_isr = megasas_isr,
386 .tasklet = megasas_complete_cmd_dpc,
387 .init_adapter = megasas_init_adapter_mfi,
388 .build_and_issue_cmd = megasas_build_and_issue_cmd,
389 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800390};
391
392/**
adam radford0d490162010-12-14 19:17:17 -0800393* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800394* to xscale (deviceid : 1064R, PERC5) controllers
395*/
396
397/**
adam radford0d490162010-12-14 19:17:17 -0800398* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800399* controllers
400*/
401
402/**
403 * megasas_enable_intr_ppc - Enables interrupts
404 * @regs: MFI register set
405 */
406static inline void
407megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
408{
409 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800410
bo yang39a98552010-09-22 22:36:29 -0400411 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800412
413 /* Dummy readl to force pci flush */
414 readl(&regs->outbound_intr_mask);
415}
416
417/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700418 * megasas_disable_intr_ppc - Disable interrupt
419 * @regs: MFI register set
420 */
421static inline void
422megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
423{
424 u32 mask = 0xFFFFFFFF;
425 writel(mask, &regs->outbound_intr_mask);
426 /* Dummy readl to force pci flush */
427 readl(&regs->outbound_intr_mask);
428}
429
430/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800431 * megasas_read_fw_status_reg_ppc - returns the current FW status value
432 * @regs: MFI register set
433 */
434static u32
435megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
436{
437 return readl(&(regs)->outbound_scratch_pad);
438}
439
440/**
441 * megasas_clear_interrupt_ppc - Check & clear interrupt
442 * @regs: MFI register set
443 */
adam radford0d490162010-12-14 19:17:17 -0800444static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800445megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
446{
adam radford3cc6851f92011-05-11 18:34:52 -0700447 u32 status, mfiStatus = 0;
448
Sumant Patrof9876f02006-02-03 15:34:35 -0800449 /*
450 * Check if it is our interrupt
451 */
452 status = readl(&regs->outbound_intr_status);
453
adam radford3cc6851f92011-05-11 18:34:52 -0700454 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
455 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
456
457 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
458 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800459
460 /*
461 * Clear the interrupt by writing back the same value
462 */
463 writel(status, &regs->outbound_doorbell_clear);
464
Yang, Bo06f579d2008-08-10 12:42:37 -0700465 /* Dummy readl to force pci flush */
466 readl(&regs->outbound_doorbell_clear);
467
adam radford3cc6851f92011-05-11 18:34:52 -0700468 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800469}
adam radford3cc6851f92011-05-11 18:34:52 -0700470
Sumant Patrof9876f02006-02-03 15:34:35 -0800471/**
472 * megasas_fire_cmd_ppc - Sends command to the FW
473 * @frame_phys_addr : Physical address of cmd
474 * @frame_count : Number of frames for the command
475 * @regs : MFI register set
476 */
adam radford0d490162010-12-14 19:17:17 -0800477static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600478megasas_fire_cmd_ppc(struct megasas_instance *instance,
479 dma_addr_t frame_phys_addr,
480 u32 frame_count,
481 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800482{
bo yang39a98552010-09-22 22:36:29 -0400483 unsigned long flags;
484 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800485 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800486 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400487 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800488}
489
bo yang39a98552010-09-22 22:36:29 -0400490/**
bo yang39a98552010-09-22 22:36:29 -0400491 * megasas_check_reset_ppc - For controller reset check
492 * @regs: MFI register set
493 */
494static int
495megasas_check_reset_ppc(struct megasas_instance *instance,
496 struct megasas_register_set __iomem *regs)
497{
adam radford3cc6851f92011-05-11 18:34:52 -0700498 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
499 return 1;
500
bo yang39a98552010-09-22 22:36:29 -0400501 return 0;
502}
adam radford3cc6851f92011-05-11 18:34:52 -0700503
Sumant Patrof9876f02006-02-03 15:34:35 -0800504static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800505
Sumant Patrof9876f02006-02-03 15:34:35 -0800506 .fire_cmd = megasas_fire_cmd_ppc,
507 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700508 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800509 .clear_intr = megasas_clear_intr_ppc,
510 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700511 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400512 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800513 .service_isr = megasas_isr,
514 .tasklet = megasas_complete_cmd_dpc,
515 .init_adapter = megasas_init_adapter_mfi,
516 .build_and_issue_cmd = megasas_build_and_issue_cmd,
517 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800518};
519
520/**
Yang, Bo87911122009-10-06 14:31:54 -0600521 * megasas_enable_intr_skinny - Enables interrupts
522 * @regs: MFI register set
523 */
524static inline void
525megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
526{
527 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
528
529 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
530
531 /* Dummy readl to force pci flush */
532 readl(&regs->outbound_intr_mask);
533}
534
535/**
536 * megasas_disable_intr_skinny - Disables interrupt
537 * @regs: MFI register set
538 */
539static inline void
540megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
541{
542 u32 mask = 0xFFFFFFFF;
543 writel(mask, &regs->outbound_intr_mask);
544 /* Dummy readl to force pci flush */
545 readl(&regs->outbound_intr_mask);
546}
547
548/**
549 * megasas_read_fw_status_reg_skinny - returns the current FW status value
550 * @regs: MFI register set
551 */
552static u32
553megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
554{
555 return readl(&(regs)->outbound_scratch_pad);
556}
557
558/**
559 * megasas_clear_interrupt_skinny - Check & clear interrupt
560 * @regs: MFI register set
561 */
562static int
563megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
564{
565 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800566 u32 mfiStatus = 0;
567
Yang, Bo87911122009-10-06 14:31:54 -0600568 /*
569 * Check if it is our interrupt
570 */
571 status = readl(&regs->outbound_intr_status);
572
573 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400574 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600575 }
576
577 /*
adam radfordebf054b2011-02-24 20:57:15 -0800578 * Check if it is our interrupt
579 */
580 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
581 MFI_STATE_FAULT) {
582 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
583 } else
584 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
585
586 /*
Yang, Bo87911122009-10-06 14:31:54 -0600587 * Clear the interrupt by writing back the same value
588 */
589 writel(status, &regs->outbound_intr_status);
590
591 /*
592 * dummy read to flush PCI
593 */
594 readl(&regs->outbound_intr_status);
595
adam radfordebf054b2011-02-24 20:57:15 -0800596 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600597}
598
599/**
600 * megasas_fire_cmd_skinny - Sends command to the FW
601 * @frame_phys_addr : Physical address of cmd
602 * @frame_count : Number of frames for the command
603 * @regs : MFI register set
604 */
605static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600606megasas_fire_cmd_skinny(struct megasas_instance *instance,
607 dma_addr_t frame_phys_addr,
608 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600609 struct megasas_register_set __iomem *regs)
610{
Yang, Bo0c79e682009-10-06 14:47:35 -0600611 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400612 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600613 writel(0, &(regs)->inbound_high_queue_port);
614 writel((frame_phys_addr | (frame_count<<1))|1,
615 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400616 spin_unlock_irqrestore(&instance->hba_lock, flags);
617}
618
619/**
bo yang39a98552010-09-22 22:36:29 -0400620 * megasas_check_reset_skinny - For controller reset check
621 * @regs: MFI register set
622 */
623static int
624megasas_check_reset_skinny(struct megasas_instance *instance,
625 struct megasas_register_set __iomem *regs)
626{
adam radford3cc6851f92011-05-11 18:34:52 -0700627 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
628 return 1;
629
bo yang39a98552010-09-22 22:36:29 -0400630 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600631}
632
633static struct megasas_instance_template megasas_instance_template_skinny = {
634
635 .fire_cmd = megasas_fire_cmd_skinny,
636 .enable_intr = megasas_enable_intr_skinny,
637 .disable_intr = megasas_disable_intr_skinny,
638 .clear_intr = megasas_clear_intr_skinny,
639 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800640 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400641 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800642 .service_isr = megasas_isr,
643 .tasklet = megasas_complete_cmd_dpc,
644 .init_adapter = megasas_init_adapter_mfi,
645 .build_and_issue_cmd = megasas_build_and_issue_cmd,
646 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600647};
648
649
650/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700651* The following functions are defined for gen2 (deviceid : 0x78 0x79)
652* controllers
653*/
654
655/**
656 * megasas_enable_intr_gen2 - Enables interrupts
657 * @regs: MFI register set
658 */
659static inline void
660megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
661{
662 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
663
664 /* write ~0x00000005 (4 & 1) to the intr mask*/
665 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
666
667 /* Dummy readl to force pci flush */
668 readl(&regs->outbound_intr_mask);
669}
670
671/**
672 * megasas_disable_intr_gen2 - Disables interrupt
673 * @regs: MFI register set
674 */
675static inline void
676megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
677{
678 u32 mask = 0xFFFFFFFF;
679 writel(mask, &regs->outbound_intr_mask);
680 /* Dummy readl to force pci flush */
681 readl(&regs->outbound_intr_mask);
682}
683
684/**
685 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
686 * @regs: MFI register set
687 */
688static u32
689megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
690{
691 return readl(&(regs)->outbound_scratch_pad);
692}
693
694/**
695 * megasas_clear_interrupt_gen2 - Check & clear interrupt
696 * @regs: MFI register set
697 */
698static int
699megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
700{
701 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400702 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700703 /*
704 * Check if it is our interrupt
705 */
706 status = readl(&regs->outbound_intr_status);
707
bo yang39a98552010-09-22 22:36:29 -0400708 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
709 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
710 }
711 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
712 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
713 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700714
715 /*
716 * Clear the interrupt by writing back the same value
717 */
bo yang39a98552010-09-22 22:36:29 -0400718 if (mfiStatus)
719 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700720
721 /* Dummy readl to force pci flush */
722 readl(&regs->outbound_intr_status);
723
bo yang39a98552010-09-22 22:36:29 -0400724 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700725}
726/**
727 * megasas_fire_cmd_gen2 - Sends command to the FW
728 * @frame_phys_addr : Physical address of cmd
729 * @frame_count : Number of frames for the command
730 * @regs : MFI register set
731 */
732static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600733megasas_fire_cmd_gen2(struct megasas_instance *instance,
734 dma_addr_t frame_phys_addr,
735 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700736 struct megasas_register_set __iomem *regs)
737{
bo yang39a98552010-09-22 22:36:29 -0400738 unsigned long flags;
739 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700740 writel((frame_phys_addr | (frame_count<<1))|1,
741 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400742 spin_unlock_irqrestore(&instance->hba_lock, flags);
743}
744
745/**
746 * megasas_adp_reset_gen2 - For controller reset
747 * @regs: MFI register set
748 */
749static int
750megasas_adp_reset_gen2(struct megasas_instance *instance,
751 struct megasas_register_set __iomem *reg_set)
752{
753 u32 retry = 0 ;
754 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800755 u32 *seq_offset = &reg_set->seq_offset;
756 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400757
adam radfordebf054b2011-02-24 20:57:15 -0800758 if (instance->instancet == &megasas_instance_template_skinny) {
759 seq_offset = &reg_set->fusion_seq_offset;
760 hostdiag_offset = &reg_set->fusion_host_diag;
761 }
762
763 writel(0, seq_offset);
764 writel(4, seq_offset);
765 writel(0xb, seq_offset);
766 writel(2, seq_offset);
767 writel(7, seq_offset);
768 writel(0xd, seq_offset);
769
bo yang39a98552010-09-22 22:36:29 -0400770 msleep(1000);
771
adam radfordebf054b2011-02-24 20:57:15 -0800772 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400773
774 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
775 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800776 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400777 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
778 retry, HostDiag);
779
780 if (retry++ >= 100)
781 return 1;
782
783 }
784
785 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
786
adam radfordebf054b2011-02-24 20:57:15 -0800787 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400788
789 ssleep(10);
790
adam radfordebf054b2011-02-24 20:57:15 -0800791 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400792 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
793 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800794 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400795 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
796 retry, HostDiag);
797
798 if (retry++ >= 1000)
799 return 1;
800
801 }
802 return 0;
803}
804
805/**
806 * megasas_check_reset_gen2 - For controller reset check
807 * @regs: MFI register set
808 */
809static int
810megasas_check_reset_gen2(struct megasas_instance *instance,
811 struct megasas_register_set __iomem *regs)
812{
Yang, Bo707e09b2010-10-12 07:20:27 -0600813 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
814 return 1;
815 }
816
bo yang39a98552010-09-22 22:36:29 -0400817 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700818}
819
820static struct megasas_instance_template megasas_instance_template_gen2 = {
821
822 .fire_cmd = megasas_fire_cmd_gen2,
823 .enable_intr = megasas_enable_intr_gen2,
824 .disable_intr = megasas_disable_intr_gen2,
825 .clear_intr = megasas_clear_intr_gen2,
826 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400827 .adp_reset = megasas_adp_reset_gen2,
828 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800829 .service_isr = megasas_isr,
830 .tasklet = megasas_complete_cmd_dpc,
831 .init_adapter = megasas_init_adapter_mfi,
832 .build_and_issue_cmd = megasas_build_and_issue_cmd,
833 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700834};
835
836/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800837* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400838* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800839*/
840
adam radford9c915a82010-12-21 13:34:31 -0800841/*
842 * Template added for TB (Fusion)
843 */
844extern struct megasas_instance_template megasas_instance_template_fusion;
845
Sumant Patrof9876f02006-02-03 15:34:35 -0800846/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400847 * megasas_issue_polled - Issues a polling command
848 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800849 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400850 *
851 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
852 */
adam radford9c915a82010-12-21 13:34:31 -0800853int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400854megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
855{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400856
857 struct megasas_header *frame_hdr = &cmd->frame->hdr;
858
859 frame_hdr->cmd_status = 0xFF;
860 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
861
862 /*
863 * Issue the frame using inbound queue port
864 */
adam radford9c915a82010-12-21 13:34:31 -0800865 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400866
867 /*
868 * Wait for cmd_status to change
869 */
adam radford9c915a82010-12-21 13:34:31 -0800870 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400871}
872
873/**
874 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
875 * @instance: Adapter soft state
876 * @cmd: Command to be issued
877 *
878 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700879 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400880 * Used to issue ioctl commands.
881 */
882static int
883megasas_issue_blocked_cmd(struct megasas_instance *instance,
884 struct megasas_cmd *cmd)
885{
886 cmd->cmd_status = ENODATA;
887
adam radford9c915a82010-12-21 13:34:31 -0800888 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400889
bo yang39a98552010-09-22 22:36:29 -0400890 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400891
892 return 0;
893}
894
895/**
896 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
897 * @instance: Adapter soft state
898 * @cmd_to_abort: Previously issued cmd to be aborted
899 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100900 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400901 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700902 * cmd and waits for return status.
903 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400904 */
905static int
906megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
907 struct megasas_cmd *cmd_to_abort)
908{
909 struct megasas_cmd *cmd;
910 struct megasas_abort_frame *abort_fr;
911
912 cmd = megasas_get_cmd(instance);
913
914 if (!cmd)
915 return -1;
916
917 abort_fr = &cmd->frame->abort;
918
919 /*
920 * Prepare and issue the abort frame
921 */
922 abort_fr->cmd = MFI_CMD_ABORT;
923 abort_fr->cmd_status = 0xFF;
924 abort_fr->flags = 0;
925 abort_fr->abort_context = cmd_to_abort->index;
926 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
927 abort_fr->abort_mfi_phys_addr_hi = 0;
928
929 cmd->sync_cmd = 1;
930 cmd->cmd_status = 0xFF;
931
adam radford9c915a82010-12-21 13:34:31 -0800932 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400933
934 /*
935 * Wait for this cmd to complete
936 */
bo yang39a98552010-09-22 22:36:29 -0400937 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
938 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400939
940 megasas_return_cmd(instance, cmd);
941 return 0;
942}
943
944/**
945 * megasas_make_sgl32 - Prepares 32-bit SGL
946 * @instance: Adapter soft state
947 * @scp: SCSI command from the mid-layer
948 * @mfi_sgl: SGL to be filled in
949 *
950 * If successful, this function returns the number of SG elements. Otherwise,
951 * it returnes -1.
952 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800953static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400954megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
955 union megasas_sgl *mfi_sgl)
956{
957 int i;
958 int sge_count;
959 struct scatterlist *os_sgl;
960
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900961 sge_count = scsi_dma_map(scp);
962 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400963
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900964 if (sge_count) {
965 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
966 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
967 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
968 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400969 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400970 return sge_count;
971}
972
973/**
974 * megasas_make_sgl64 - Prepares 64-bit SGL
975 * @instance: Adapter soft state
976 * @scp: SCSI command from the mid-layer
977 * @mfi_sgl: SGL to be filled in
978 *
979 * If successful, this function returns the number of SG elements. Otherwise,
980 * it returnes -1.
981 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800982static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400983megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
984 union megasas_sgl *mfi_sgl)
985{
986 int i;
987 int sge_count;
988 struct scatterlist *os_sgl;
989
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900990 sge_count = scsi_dma_map(scp);
991 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400992
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900993 if (sge_count) {
994 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
995 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
996 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
997 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400999 return sge_count;
1000}
1001
Yang, Bof4c9a132009-10-06 14:43:28 -06001002/**
1003 * megasas_make_sgl_skinny - Prepares IEEE SGL
1004 * @instance: Adapter soft state
1005 * @scp: SCSI command from the mid-layer
1006 * @mfi_sgl: SGL to be filled in
1007 *
1008 * If successful, this function returns the number of SG elements. Otherwise,
1009 * it returnes -1.
1010 */
1011static int
1012megasas_make_sgl_skinny(struct megasas_instance *instance,
1013 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1014{
1015 int i;
1016 int sge_count;
1017 struct scatterlist *os_sgl;
1018
1019 sge_count = scsi_dma_map(scp);
1020
1021 if (sge_count) {
1022 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1023 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1024 mfi_sgl->sge_skinny[i].phys_addr =
1025 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001026 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001027 }
1028 }
1029 return sge_count;
1030}
1031
Sumant Patrob1df99d2006-10-03 12:40:47 -07001032 /**
1033 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001034 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001035 * @sge_count : number of sg elements
1036 *
1037 * Returns the number of frames required for numnber of sge's (sge_count)
1038 */
1039
Yang, Bof4c9a132009-10-06 14:43:28 -06001040static u32 megasas_get_frame_count(struct megasas_instance *instance,
1041 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001042{
1043 int num_cnt;
1044 int sge_bytes;
1045 u32 sge_sz;
1046 u32 frame_count=0;
1047
1048 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1049 sizeof(struct megasas_sge32);
1050
Yang, Bof4c9a132009-10-06 14:43:28 -06001051 if (instance->flag_ieee) {
1052 sge_sz = sizeof(struct megasas_sge_skinny);
1053 }
1054
Sumant Patrob1df99d2006-10-03 12:40:47 -07001055 /*
bo yangd532dbe2008-03-17 03:36:43 -04001056 * Main frame can contain 2 SGEs for 64-bit SGLs and
1057 * 3 SGEs for 32-bit SGLs for ldio &
1058 * 1 SGEs for 64-bit SGLs and
1059 * 2 SGEs for 32-bit SGLs for pthru frame
1060 */
1061 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001062 if (instance->flag_ieee == 1) {
1063 num_cnt = sge_count - 1;
1064 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001065 num_cnt = sge_count - 1;
1066 else
1067 num_cnt = sge_count - 2;
1068 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001069 if (instance->flag_ieee == 1) {
1070 num_cnt = sge_count - 1;
1071 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001072 num_cnt = sge_count - 2;
1073 else
1074 num_cnt = sge_count - 3;
1075 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001076
1077 if(num_cnt>0){
1078 sge_bytes = sge_sz * num_cnt;
1079
1080 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1081 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1082 }
1083 /* Main frame */
1084 frame_count +=1;
1085
1086 if (frame_count > 7)
1087 frame_count = 8;
1088 return frame_count;
1089}
1090
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001091/**
1092 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1093 * @instance: Adapter soft state
1094 * @scp: SCSI command
1095 * @cmd: Command to be prepared in
1096 *
1097 * This function prepares CDB commands. These are typcially pass-through
1098 * commands to the devices.
1099 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001100static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001101megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1102 struct megasas_cmd *cmd)
1103{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001104 u32 is_logical;
1105 u32 device_id;
1106 u16 flags = 0;
1107 struct megasas_pthru_frame *pthru;
1108
1109 is_logical = MEGASAS_IS_LOGICAL(scp);
1110 device_id = MEGASAS_DEV_INDEX(instance, scp);
1111 pthru = (struct megasas_pthru_frame *)cmd->frame;
1112
1113 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1114 flags = MFI_FRAME_DIR_WRITE;
1115 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1116 flags = MFI_FRAME_DIR_READ;
1117 else if (scp->sc_data_direction == PCI_DMA_NONE)
1118 flags = MFI_FRAME_DIR_NONE;
1119
Yang, Bof4c9a132009-10-06 14:43:28 -06001120 if (instance->flag_ieee == 1) {
1121 flags |= MFI_FRAME_IEEE;
1122 }
1123
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001124 /*
1125 * Prepare the DCDB frame
1126 */
1127 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1128 pthru->cmd_status = 0x0;
1129 pthru->scsi_status = 0x0;
1130 pthru->target_id = device_id;
1131 pthru->lun = scp->device->lun;
1132 pthru->cdb_len = scp->cmd_len;
1133 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001134 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001136 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001137
1138 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1139
1140 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001141 * If the command is for the tape device, set the
1142 * pthru timeout to the os layer timeout value.
1143 */
1144 if (scp->device->type == TYPE_TAPE) {
1145 if ((scp->request->timeout / HZ) > 0xFFFF)
1146 pthru->timeout = 0xFFFF;
1147 else
1148 pthru->timeout = scp->request->timeout / HZ;
1149 }
1150
1151 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001152 * Construct SGL
1153 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001154 if (instance->flag_ieee == 1) {
1155 pthru->flags |= MFI_FRAME_SGL64;
1156 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1157 &pthru->sgl);
1158 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001159 pthru->flags |= MFI_FRAME_SGL64;
1160 pthru->sge_count = megasas_make_sgl64(instance, scp,
1161 &pthru->sgl);
1162 } else
1163 pthru->sge_count = megasas_make_sgl32(instance, scp,
1164 &pthru->sgl);
1165
Yang, Bobdc6fb82009-12-06 08:30:19 -07001166 if (pthru->sge_count > instance->max_num_sge) {
1167 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1168 pthru->sge_count);
1169 return 0;
1170 }
1171
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001172 /*
1173 * Sense info specific
1174 */
1175 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1176 pthru->sense_buf_phys_addr_hi = 0;
1177 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1178
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001179 /*
1180 * Compute the total number of frames this command consumes. FW uses
1181 * this number to pull sufficient number of frames from host memory.
1182 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001183 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001184 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001185
1186 return cmd->frame_count;
1187}
1188
1189/**
1190 * megasas_build_ldio - Prepares IOs to logical devices
1191 * @instance: Adapter soft state
1192 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001193 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001194 *
1195 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1196 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001197static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001198megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1199 struct megasas_cmd *cmd)
1200{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001201 u32 device_id;
1202 u8 sc = scp->cmnd[0];
1203 u16 flags = 0;
1204 struct megasas_io_frame *ldio;
1205
1206 device_id = MEGASAS_DEV_INDEX(instance, scp);
1207 ldio = (struct megasas_io_frame *)cmd->frame;
1208
1209 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1210 flags = MFI_FRAME_DIR_WRITE;
1211 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1212 flags = MFI_FRAME_DIR_READ;
1213
Yang, Bof4c9a132009-10-06 14:43:28 -06001214 if (instance->flag_ieee == 1) {
1215 flags |= MFI_FRAME_IEEE;
1216 }
1217
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001218 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001219 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001220 */
1221 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1222 ldio->cmd_status = 0x0;
1223 ldio->scsi_status = 0x0;
1224 ldio->target_id = device_id;
1225 ldio->timeout = 0;
1226 ldio->reserved_0 = 0;
1227 ldio->pad_0 = 0;
1228 ldio->flags = flags;
1229 ldio->start_lba_hi = 0;
1230 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1231
1232 /*
1233 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1234 */
1235 if (scp->cmd_len == 6) {
1236 ldio->lba_count = (u32) scp->cmnd[4];
1237 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1238 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1239
1240 ldio->start_lba_lo &= 0x1FFFFF;
1241 }
1242
1243 /*
1244 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1245 */
1246 else if (scp->cmd_len == 10) {
1247 ldio->lba_count = (u32) scp->cmnd[8] |
1248 ((u32) scp->cmnd[7] << 8);
1249 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1250 ((u32) scp->cmnd[3] << 16) |
1251 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1252 }
1253
1254 /*
1255 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1256 */
1257 else if (scp->cmd_len == 12) {
1258 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1259 ((u32) scp->cmnd[7] << 16) |
1260 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1261
1262 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1263 ((u32) scp->cmnd[3] << 16) |
1264 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1265 }
1266
1267 /*
1268 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1269 */
1270 else if (scp->cmd_len == 16) {
1271 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1272 ((u32) scp->cmnd[11] << 16) |
1273 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1274
1275 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1276 ((u32) scp->cmnd[7] << 16) |
1277 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1278
1279 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1280 ((u32) scp->cmnd[3] << 16) |
1281 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1282
1283 }
1284
1285 /*
1286 * Construct SGL
1287 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001288 if (instance->flag_ieee) {
1289 ldio->flags |= MFI_FRAME_SGL64;
1290 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1291 &ldio->sgl);
1292 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001293 ldio->flags |= MFI_FRAME_SGL64;
1294 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1295 } else
1296 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1297
Yang, Bobdc6fb82009-12-06 08:30:19 -07001298 if (ldio->sge_count > instance->max_num_sge) {
1299 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1300 ldio->sge_count);
1301 return 0;
1302 }
1303
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001304 /*
1305 * Sense info specific
1306 */
1307 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1308 ldio->sense_buf_phys_addr_hi = 0;
1309 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1310
Sumant Patrob1df99d2006-10-03 12:40:47 -07001311 /*
1312 * Compute the total number of frames this command consumes. FW uses
1313 * this number to pull sufficient number of frames from host memory.
1314 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001315 cmd->frame_count = megasas_get_frame_count(instance,
1316 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001317
1318 return cmd->frame_count;
1319}
1320
1321/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001322 * megasas_is_ldio - Checks if the cmd is for logical drive
1323 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001324 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001325 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001326 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001327 */
adam radford9c915a82010-12-21 13:34:31 -08001328inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001329{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001330 if (!MEGASAS_IS_LOGICAL(cmd))
1331 return 0;
1332 switch (cmd->cmnd[0]) {
1333 case READ_10:
1334 case WRITE_10:
1335 case READ_12:
1336 case WRITE_12:
1337 case READ_6:
1338 case WRITE_6:
1339 case READ_16:
1340 case WRITE_16:
1341 return 1;
1342 default:
1343 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001344 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001345}
1346
Sumant Patro658dced2006-10-03 13:09:14 -07001347 /**
1348 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1349 * in FW
1350 * @instance: Adapter soft state
1351 */
1352static inline void
1353megasas_dump_pending_frames(struct megasas_instance *instance)
1354{
1355 struct megasas_cmd *cmd;
1356 int i,n;
1357 union megasas_sgl *mfi_sgl;
1358 struct megasas_io_frame *ldio;
1359 struct megasas_pthru_frame *pthru;
1360 u32 sgcount;
1361 u32 max_cmd = instance->max_fw_cmds;
1362
1363 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1364 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1365 if (IS_DMA64)
1366 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1367 else
1368 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1369
1370 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1371 for (i = 0; i < max_cmd; i++) {
1372 cmd = instance->cmd_list[i];
1373 if(!cmd->scmd)
1374 continue;
1375 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1376 if (megasas_is_ldio(cmd->scmd)){
1377 ldio = (struct megasas_io_frame *)cmd->frame;
1378 mfi_sgl = &ldio->sgl;
1379 sgcount = ldio->sge_count;
1380 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);
1381 }
1382 else {
1383 pthru = (struct megasas_pthru_frame *) cmd->frame;
1384 mfi_sgl = &pthru->sgl;
1385 sgcount = pthru->sge_count;
1386 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);
1387 }
1388 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1389 for (n = 0; n < sgcount; n++){
1390 if (IS_DMA64)
1391 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) ;
1392 else
1393 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1394 }
1395 }
1396 printk(KERN_ERR "\n");
1397 } /*for max_cmd*/
1398 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1399 for (i = 0; i < max_cmd; i++) {
1400
1401 cmd = instance->cmd_list[i];
1402
1403 if(cmd->sync_cmd == 1){
1404 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1405 }
1406 }
1407 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1408}
1409
adam radfordcd50ba82010-12-21 10:23:23 -08001410u32
1411megasas_build_and_issue_cmd(struct megasas_instance *instance,
1412 struct scsi_cmnd *scmd)
1413{
1414 struct megasas_cmd *cmd;
1415 u32 frame_count;
1416
1417 cmd = megasas_get_cmd(instance);
1418 if (!cmd)
1419 return SCSI_MLQUEUE_HOST_BUSY;
1420
1421 /*
1422 * Logical drive command
1423 */
1424 if (megasas_is_ldio(scmd))
1425 frame_count = megasas_build_ldio(instance, scmd, cmd);
1426 else
1427 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1428
1429 if (!frame_count)
1430 goto out_return_cmd;
1431
1432 cmd->scmd = scmd;
1433 scmd->SCp.ptr = (char *)cmd;
1434
1435 /*
1436 * Issue the command to the FW
1437 */
1438 atomic_inc(&instance->fw_outstanding);
1439
1440 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1441 cmd->frame_count-1, instance->reg_set);
1442 /*
1443 * Check if we have pend cmds to be completed
1444 */
1445 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1446 tasklet_schedule(&instance->isr_tasklet);
1447
1448 return 0;
1449out_return_cmd:
1450 megasas_return_cmd(instance, cmd);
1451 return 1;
1452}
1453
1454
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001455/**
1456 * megasas_queue_command - Queue entry point
1457 * @scmd: SCSI command to be queued
1458 * @done: Callback entry point
1459 */
1460static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001461megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001462{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001463 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001464 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001465
1466 instance = (struct megasas_instance *)
1467 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001468
bo yang39a98552010-09-22 22:36:29 -04001469 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001470 return SCSI_MLQUEUE_HOST_BUSY;
1471
bo yang39a98552010-09-22 22:36:29 -04001472 spin_lock_irqsave(&instance->hba_lock, flags);
1473 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1474 spin_unlock_irqrestore(&instance->hba_lock, flags);
1475 return SCSI_MLQUEUE_HOST_BUSY;
1476 }
1477
1478 spin_unlock_irqrestore(&instance->hba_lock, flags);
1479
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001480 scmd->scsi_done = done;
1481 scmd->result = 0;
1482
Sumant Patrocb59aa62006-01-25 11:53:25 -08001483 if (MEGASAS_IS_LOGICAL(scmd) &&
1484 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1485 scmd->result = DID_BAD_TARGET << 16;
1486 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001487 }
1488
Sumant Patro02b01e02007-02-14 13:00:55 -08001489 switch (scmd->cmnd[0]) {
1490 case SYNCHRONIZE_CACHE:
1491 /*
1492 * FW takes care of flush cache on its own
1493 * No need to send it down
1494 */
1495 scmd->result = DID_OK << 16;
1496 goto out_done;
1497 default:
1498 break;
1499 }
1500
adam radfordcd50ba82010-12-21 10:23:23 -08001501 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1502 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001503 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001504 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001505
1506 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001507
Sumant Patrocb59aa62006-01-25 11:53:25 -08001508 out_done:
1509 done(scmd);
1510 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001511}
1512
Jeff Garzikf2812332010-11-16 02:10:29 -05001513static DEF_SCSI_QCMD(megasas_queue_command)
1514
Yang, Bo044833b2009-10-06 14:33:06 -06001515static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1516{
1517 int i;
1518
1519 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1520
1521 if ((megasas_mgmt_info.instance[i]) &&
1522 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1523 return megasas_mgmt_info.instance[i];
1524 }
1525
1526 return NULL;
1527}
1528
Christoph Hellwig147aab62006-02-17 12:13:48 +01001529static int megasas_slave_configure(struct scsi_device *sdev)
1530{
Yang, Bo044833b2009-10-06 14:33:06 -06001531 u16 pd_index = 0;
1532 struct megasas_instance *instance ;
1533
1534 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001535
1536 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001537 * Don't export physical disk devices to the disk driver.
1538 *
1539 * FIXME: Currently we don't export them to the midlayer at all.
1540 * That will be fixed once LSI engineers have audited the
1541 * firmware for possible issues.
1542 */
1543 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1544 sdev->type == TYPE_DISK) {
1545 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1546 sdev->id;
1547 if (instance->pd_list[pd_index].driveState ==
1548 MR_PD_STATE_SYSTEM) {
1549 blk_queue_rq_timeout(sdev->request_queue,
1550 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1551 return 0;
1552 }
1553 return -ENXIO;
1554 }
1555
1556 /*
1557 * The RAID firmware may require extended timeouts.
1558 */
1559 blk_queue_rq_timeout(sdev->request_queue,
1560 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1561 return 0;
1562}
1563
1564static int megasas_slave_alloc(struct scsi_device *sdev)
1565{
1566 u16 pd_index = 0;
1567 struct megasas_instance *instance ;
1568 instance = megasas_lookup_instance(sdev->host->host_no);
1569 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1570 (sdev->type == TYPE_DISK)) {
1571 /*
1572 * Open the OS scan to the SYSTEM PD
1573 */
1574 pd_index =
1575 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1576 sdev->id;
1577 if ((instance->pd_list[pd_index].driveState ==
1578 MR_PD_STATE_SYSTEM) &&
1579 (instance->pd_list[pd_index].driveType ==
1580 TYPE_DISK)) {
1581 return 0;
1582 }
1583 return -ENXIO;
1584 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001585 return 0;
1586}
1587
adam radford9c915a82010-12-21 13:34:31 -08001588void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001589{
1590 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001591 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
adam radford36807e62011-10-08 18:15:06 -07001592 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1593 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08001594 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001595 } else {
adam radford9c915a82010-12-21 13:34:31 -08001596 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1597 }
1598}
1599
1600 /**
1601 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1602 * restored to max value
1603 * @instance: Adapter soft state
1604 *
1605 */
1606void
1607megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1608{
1609 unsigned long flags;
1610 if (instance->flag & MEGASAS_FW_BUSY
1611 && time_after(jiffies, instance->last_time + 5 * HZ)
1612 && atomic_read(&instance->fw_outstanding) < 17) {
1613
1614 spin_lock_irqsave(instance->host->host_lock, flags);
1615 instance->flag &= ~MEGASAS_FW_BUSY;
1616 if ((instance->pdev->device ==
1617 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1618 (instance->pdev->device ==
1619 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1620 instance->host->can_queue =
1621 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1622 } else
1623 instance->host->can_queue =
1624 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1625
1626 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001627 }
1628}
1629
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001630/**
bo yang7343eb62007-11-09 04:35:44 -05001631 * megasas_complete_cmd_dpc - Returns FW's controller structure
1632 * @instance_addr: Address of adapter soft state
1633 *
1634 * Tasklet to complete cmds
1635 */
1636static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1637{
1638 u32 producer;
1639 u32 consumer;
1640 u32 context;
1641 struct megasas_cmd *cmd;
1642 struct megasas_instance *instance =
1643 (struct megasas_instance *)instance_addr;
1644 unsigned long flags;
1645
1646 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001647 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001648 return;
1649
1650 spin_lock_irqsave(&instance->completion_lock, flags);
1651
1652 producer = *instance->producer;
1653 consumer = *instance->consumer;
1654
1655 while (consumer != producer) {
1656 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001657 if (context >= instance->max_fw_cmds) {
1658 printk(KERN_ERR "Unexpected context value %x\n",
1659 context);
1660 BUG();
1661 }
bo yang7343eb62007-11-09 04:35:44 -05001662
1663 cmd = instance->cmd_list[context];
1664
1665 megasas_complete_cmd(instance, cmd, DID_OK);
1666
1667 consumer++;
1668 if (consumer == (instance->max_fw_cmds + 1)) {
1669 consumer = 0;
1670 }
1671 }
1672
1673 *instance->consumer = producer;
1674
1675 spin_unlock_irqrestore(&instance->completion_lock, flags);
1676
1677 /*
1678 * Check if we can restore can_queue
1679 */
adam radford9c915a82010-12-21 13:34:31 -08001680 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001681}
1682
Yang, Bo707e09b2010-10-12 07:20:27 -06001683static void
1684megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1685
1686static void
1687process_fw_state_change_wq(struct work_struct *work);
1688
1689void megasas_do_ocr(struct megasas_instance *instance)
1690{
1691 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1692 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1693 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1694 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1695 }
1696 instance->instancet->disable_intr(instance->reg_set);
1697 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1698 instance->issuepend_done = 0;
1699
1700 atomic_set(&instance->fw_outstanding, 0);
1701 megasas_internal_reset_defer_cmds(instance);
1702 process_fw_state_change_wq(&instance->work_init);
1703}
1704
bo yang7343eb62007-11-09 04:35:44 -05001705/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001706 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1707 * @instance: Adapter soft state
1708 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001709 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001710 * complete all its outstanding commands. Returns error if one or more IOs
1711 * are pending after this time period. It also marks the controller dead.
1712 */
1713static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1714{
1715 int i;
bo yang39a98552010-09-22 22:36:29 -04001716 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001717 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001718 u8 adprecovery;
1719 unsigned long flags;
1720 struct list_head clist_local;
1721 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001722 u32 fw_state;
1723 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001724
1725 spin_lock_irqsave(&instance->hba_lock, flags);
1726 adprecovery = instance->adprecovery;
1727 spin_unlock_irqrestore(&instance->hba_lock, flags);
1728
1729 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1730
1731 INIT_LIST_HEAD(&clist_local);
1732 spin_lock_irqsave(&instance->hba_lock, flags);
1733 list_splice_init(&instance->internal_reset_pending_q,
1734 &clist_local);
1735 spin_unlock_irqrestore(&instance->hba_lock, flags);
1736
1737 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1738 for (i = 0; i < wait_time; i++) {
1739 msleep(1000);
1740 spin_lock_irqsave(&instance->hba_lock, flags);
1741 adprecovery = instance->adprecovery;
1742 spin_unlock_irqrestore(&instance->hba_lock, flags);
1743 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1744 break;
1745 }
1746
1747 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1748 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1749 spin_lock_irqsave(&instance->hba_lock, flags);
1750 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1751 spin_unlock_irqrestore(&instance->hba_lock, flags);
1752 return FAILED;
1753 }
1754
1755 reset_index = 0;
1756 while (!list_empty(&clist_local)) {
1757 reset_cmd = list_entry((&clist_local)->next,
1758 struct megasas_cmd, list);
1759 list_del_init(&reset_cmd->list);
1760 if (reset_cmd->scmd) {
1761 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001762 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001763 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001764 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001765
1766 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1767 megasas_return_cmd(instance, reset_cmd);
1768 } else if (reset_cmd->sync_cmd) {
1769 printk(KERN_NOTICE "megasas:%p synch cmds"
1770 "reset queue\n",
1771 reset_cmd);
1772
1773 reset_cmd->cmd_status = ENODATA;
1774 instance->instancet->fire_cmd(instance,
1775 reset_cmd->frame_phys_addr,
1776 0, instance->reg_set);
1777 } else {
1778 printk(KERN_NOTICE "megasas: %p unexpected"
1779 "cmds lst\n",
1780 reset_cmd);
1781 }
1782 reset_index++;
1783 }
1784
1785 return SUCCESS;
1786 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001787
1788 for (i = 0; i < wait_time; i++) {
1789
Sumant Patroe4a082c2006-05-30 12:03:37 -07001790 int outstanding = atomic_read(&instance->fw_outstanding);
1791
1792 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001793 break;
1794
1795 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1796 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001797 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001798 /*
1799 * Call cmd completion routine. Cmd to be
1800 * be completed directly without depending on isr.
1801 */
1802 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001803 }
1804
1805 msleep(1000);
1806 }
1807
Yang, Bo707e09b2010-10-12 07:20:27 -06001808 i = 0;
1809 kill_adapter_flag = 0;
1810 do {
1811 fw_state = instance->instancet->read_fw_status_reg(
1812 instance->reg_set) & MFI_STATE_MASK;
1813 if ((fw_state == MFI_STATE_FAULT) &&
1814 (instance->disableOnlineCtrlReset == 0)) {
1815 if (i == 3) {
1816 kill_adapter_flag = 2;
1817 break;
1818 }
1819 megasas_do_ocr(instance);
1820 kill_adapter_flag = 1;
1821
1822 /* wait for 1 secs to let FW finish the pending cmds */
1823 msleep(1000);
1824 }
1825 i++;
1826 } while (i <= 3);
1827
1828 if (atomic_read(&instance->fw_outstanding) &&
1829 !kill_adapter_flag) {
1830 if (instance->disableOnlineCtrlReset == 0) {
1831
1832 megasas_do_ocr(instance);
1833
1834 /* wait for 5 secs to let FW finish the pending cmds */
1835 for (i = 0; i < wait_time; i++) {
1836 int outstanding =
1837 atomic_read(&instance->fw_outstanding);
1838 if (!outstanding)
1839 return SUCCESS;
1840 msleep(1000);
1841 }
1842 }
1843 }
1844
1845 if (atomic_read(&instance->fw_outstanding) ||
1846 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001847 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001848 /*
1849 * Send signal to FW to stop processing any pending cmds.
1850 * The controller will be taken offline by the OS now.
1851 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001852 if ((instance->pdev->device ==
1853 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1854 (instance->pdev->device ==
1855 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1856 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001857 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001858 } else {
1859 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001860 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001861 }
Sumant Patro658dced2006-10-03 13:09:14 -07001862 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001863 spin_lock_irqsave(&instance->hba_lock, flags);
1864 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1865 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001866 return FAILED;
1867 }
1868
bo yang39a98552010-09-22 22:36:29 -04001869 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1870
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001871 return SUCCESS;
1872}
1873
1874/**
1875 * megasas_generic_reset - Generic reset routine
1876 * @scmd: Mid-layer SCSI command
1877 *
1878 * This routine implements a generic reset handler for device, bus and host
1879 * reset requests. Device, bus and host specific reset handlers can use this
1880 * function after they do their specific tasks.
1881 */
1882static int megasas_generic_reset(struct scsi_cmnd *scmd)
1883{
1884 int ret_val;
1885 struct megasas_instance *instance;
1886
1887 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1888
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001889 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1890 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001891
bo yang39a98552010-09-22 22:36:29 -04001892 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001893 printk(KERN_ERR "megasas: cannot recover from previous reset "
1894 "failures\n");
1895 return FAILED;
1896 }
1897
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001898 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001899 if (ret_val == SUCCESS)
1900 printk(KERN_NOTICE "megasas: reset successful \n");
1901 else
1902 printk(KERN_ERR "megasas: failed to do reset\n");
1903
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001904 return ret_val;
1905}
1906
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001907/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001908 * megasas_reset_timer - quiesce the adapter if required
1909 * @scmd: scsi cmnd
1910 *
1911 * Sets the FW busy flag and reduces the host->can_queue if the
1912 * cmd has not been completed within the timeout period.
1913 */
1914static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001915blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001916{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001917 struct megasas_instance *instance;
1918 unsigned long flags;
1919
1920 if (time_after(jiffies, scmd->jiffies_at_alloc +
1921 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001922 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001923 }
1924
adam radfordf575c5d2011-10-13 16:01:12 -07001925 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001926 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1927 /* FW is busy, throttle IO */
1928 spin_lock_irqsave(instance->host->host_lock, flags);
1929
1930 instance->host->can_queue = 16;
1931 instance->last_time = jiffies;
1932 instance->flag |= MEGASAS_FW_BUSY;
1933
1934 spin_unlock_irqrestore(instance->host->host_lock, flags);
1935 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001936 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001937}
1938
1939/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001940 * megasas_reset_device - Device reset handler entry point
1941 */
1942static int megasas_reset_device(struct scsi_cmnd *scmd)
1943{
1944 int ret;
1945
1946 /*
1947 * First wait for all commands to complete
1948 */
1949 ret = megasas_generic_reset(scmd);
1950
1951 return ret;
1952}
1953
1954/**
1955 * megasas_reset_bus_host - Bus & host reset handler entry point
1956 */
1957static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1958{
1959 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001960 struct megasas_instance *instance;
1961 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001962
1963 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001964 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001965 */
adam radford36807e62011-10-08 18:15:06 -07001966 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1967 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08001968 ret = megasas_reset_fusion(scmd->device->host);
1969 else
1970 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001971
1972 return ret;
1973}
1974
1975/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001976 * megasas_bios_param - Returns disk geometry for a disk
1977 * @sdev: device handle
1978 * @bdev: block device
1979 * @capacity: drive capacity
1980 * @geom: geometry parameters
1981 */
1982static int
1983megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1984 sector_t capacity, int geom[])
1985{
1986 int heads;
1987 int sectors;
1988 sector_t cylinders;
1989 unsigned long tmp;
1990 /* Default heads (64) & sectors (32) */
1991 heads = 64;
1992 sectors = 32;
1993
1994 tmp = heads * sectors;
1995 cylinders = capacity;
1996
1997 sector_div(cylinders, tmp);
1998
1999 /*
2000 * Handle extended translation size for logical drives > 1Gb
2001 */
2002
2003 if (capacity >= 0x200000) {
2004 heads = 255;
2005 sectors = 63;
2006 tmp = heads*sectors;
2007 cylinders = capacity;
2008 sector_div(cylinders, tmp);
2009 }
2010
2011 geom[0] = heads;
2012 geom[1] = sectors;
2013 geom[2] = cylinders;
2014
2015 return 0;
2016}
2017
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002018static void megasas_aen_polling(struct work_struct *work);
2019
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002020/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002021 * megasas_service_aen - Processes an event notification
2022 * @instance: Adapter soft state
2023 * @cmd: AEN command completed by the ISR
2024 *
2025 * For AEN, driver sends a command down to FW that is held by the FW till an
2026 * event occurs. When an event of interest occurs, FW completes the command
2027 * that it was previously holding.
2028 *
2029 * This routines sends SIGIO signal to processes that have registered with the
2030 * driver for AEN.
2031 */
2032static void
2033megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2034{
Yang, Boc3518832009-10-06 14:18:02 -06002035 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002036 /*
2037 * Don't signal app if it is just an aborted previously registered aen
2038 */
Yang, Boc3518832009-10-06 14:18:02 -06002039 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2040 spin_lock_irqsave(&poll_aen_lock, flags);
2041 megasas_poll_wait_aen = 1;
2042 spin_unlock_irqrestore(&poll_aen_lock, flags);
2043 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002044 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002045 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002046 else
2047 cmd->abort_aen = 0;
2048
2049 instance->aen_cmd = NULL;
2050 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002051
bo yang39a98552010-09-22 22:36:29 -04002052 if ((instance->unload == 0) &&
2053 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002054 struct megasas_aen_event *ev;
2055 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2056 if (!ev) {
2057 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2058 } else {
2059 ev->instance = instance;
2060 instance->ev = ev;
2061 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2062 schedule_delayed_work(
2063 (struct delayed_work *)&ev->hotplug_work, 0);
2064 }
2065 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002066}
2067
adam radford4bcde502011-07-26 15:42:52 -07002068static int megasas_change_queue_depth(struct scsi_device *sdev,
2069 int queue_depth, int reason)
2070{
2071 if (reason != SCSI_QDEPTH_DEFAULT)
2072 return -EOPNOTSUPP;
2073
2074 if (queue_depth > sdev->host->can_queue)
2075 queue_depth = sdev->host->can_queue;
2076 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2077 queue_depth);
2078
2079 return queue_depth;
2080}
2081
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002082/*
2083 * Scsi host template for megaraid_sas driver
2084 */
2085static struct scsi_host_template megasas_template = {
2086
2087 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002088 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002089 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002090 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002091 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002092 .queuecommand = megasas_queue_command,
2093 .eh_device_reset_handler = megasas_reset_device,
2094 .eh_bus_reset_handler = megasas_reset_bus_host,
2095 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002096 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002097 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002098 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002099 .change_queue_depth = megasas_change_queue_depth,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002100};
2101
2102/**
2103 * megasas_complete_int_cmd - Completes an internal command
2104 * @instance: Adapter soft state
2105 * @cmd: Command to be completed
2106 *
2107 * The megasas_issue_blocked_cmd() function waits for a command to complete
2108 * after it issues a command. This function wakes up that waiting routine by
2109 * calling wake_up() on the wait queue.
2110 */
2111static void
2112megasas_complete_int_cmd(struct megasas_instance *instance,
2113 struct megasas_cmd *cmd)
2114{
2115 cmd->cmd_status = cmd->frame->io.cmd_status;
2116
2117 if (cmd->cmd_status == ENODATA) {
2118 cmd->cmd_status = 0;
2119 }
2120 wake_up(&instance->int_cmd_wait_q);
2121}
2122
2123/**
2124 * megasas_complete_abort - Completes aborting a command
2125 * @instance: Adapter soft state
2126 * @cmd: Cmd that was issued to abort another cmd
2127 *
adam radford0d490162010-12-14 19:17:17 -08002128 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2129 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002130 * wakes up all functions waiting on the same wait queue.
2131 */
2132static void
2133megasas_complete_abort(struct megasas_instance *instance,
2134 struct megasas_cmd *cmd)
2135{
2136 if (cmd->sync_cmd) {
2137 cmd->sync_cmd = 0;
2138 cmd->cmd_status = 0;
2139 wake_up(&instance->abort_cmd_wait_q);
2140 }
2141
2142 return;
2143}
2144
2145/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002146 * megasas_complete_cmd - Completes a command
2147 * @instance: Adapter soft state
2148 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002149 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002150 * SCSI mid-layer instead of the value returned
2151 * by the FW. This should be used if caller wants
2152 * an alternate status (as in the case of aborted
2153 * commands)
2154 */
adam radford9c915a82010-12-21 13:34:31 -08002155void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002156megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2157 u8 alt_status)
2158{
2159 int exception = 0;
2160 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002161 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002162 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002163
bo yang39a98552010-09-22 22:36:29 -04002164 /* flag for the retry reset */
2165 cmd->retry_for_fw_reset = 0;
2166
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002167 if (cmd->scmd)
2168 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002169
2170 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07002171 case MFI_CMD_INVALID:
2172 /* Some older 1068 controller FW may keep a pended
2173 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2174 when booting the kdump kernel. Ignore this command to
2175 prevent a kernel panic on shutdown of the kdump kernel. */
2176 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2177 "completed.\n");
2178 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2179 "other than PERC5, please upgrade your firmware.\n");
2180 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002181 case MFI_CMD_PD_SCSI_IO:
2182 case MFI_CMD_LD_SCSI_IO:
2183
2184 /*
2185 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2186 * issued either through an IO path or an IOCTL path. If it
2187 * was via IOCTL, we will send it to internal completion.
2188 */
2189 if (cmd->sync_cmd) {
2190 cmd->sync_cmd = 0;
2191 megasas_complete_int_cmd(instance, cmd);
2192 break;
2193 }
2194
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002195 case MFI_CMD_LD_READ:
2196 case MFI_CMD_LD_WRITE:
2197
2198 if (alt_status) {
2199 cmd->scmd->result = alt_status << 16;
2200 exception = 1;
2201 }
2202
2203 if (exception) {
2204
Sumant Patroe4a082c2006-05-30 12:03:37 -07002205 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002206
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002207 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002208 cmd->scmd->scsi_done(cmd->scmd);
2209 megasas_return_cmd(instance, cmd);
2210
2211 break;
2212 }
2213
2214 switch (hdr->cmd_status) {
2215
2216 case MFI_STAT_OK:
2217 cmd->scmd->result = DID_OK << 16;
2218 break;
2219
2220 case MFI_STAT_SCSI_IO_FAILED:
2221 case MFI_STAT_LD_INIT_IN_PROGRESS:
2222 cmd->scmd->result =
2223 (DID_ERROR << 16) | hdr->scsi_status;
2224 break;
2225
2226 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2227
2228 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2229
2230 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2231 memset(cmd->scmd->sense_buffer, 0,
2232 SCSI_SENSE_BUFFERSIZE);
2233 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2234 hdr->sense_len);
2235
2236 cmd->scmd->result |= DRIVER_SENSE << 24;
2237 }
2238
2239 break;
2240
2241 case MFI_STAT_LD_OFFLINE:
2242 case MFI_STAT_DEVICE_NOT_FOUND:
2243 cmd->scmd->result = DID_BAD_TARGET << 16;
2244 break;
2245
2246 default:
2247 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2248 hdr->cmd_status);
2249 cmd->scmd->result = DID_ERROR << 16;
2250 break;
2251 }
2252
Sumant Patroe4a082c2006-05-30 12:03:37 -07002253 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002254
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002255 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002256 cmd->scmd->scsi_done(cmd->scmd);
2257 megasas_return_cmd(instance, cmd);
2258
2259 break;
2260
2261 case MFI_CMD_SMP:
2262 case MFI_CMD_STP:
2263 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002264 /* Check for LD map update */
2265 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2266 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2267 spin_lock_irqsave(instance->host->host_lock, flags);
2268 if (cmd->frame->hdr.cmd_status != 0) {
2269 if (cmd->frame->hdr.cmd_status !=
2270 MFI_STAT_NOT_FOUND)
2271 printk(KERN_WARNING "megasas: map sync"
2272 "failed, status = 0x%x.\n",
2273 cmd->frame->hdr.cmd_status);
2274 else {
2275 megasas_return_cmd(instance, cmd);
2276 spin_unlock_irqrestore(
2277 instance->host->host_lock,
2278 flags);
2279 break;
2280 }
2281 } else
2282 instance->map_id++;
2283 megasas_return_cmd(instance, cmd);
2284 if (MR_ValidateMapInfo(
2285 fusion->ld_map[(instance->map_id & 1)],
2286 fusion->load_balance_info))
2287 fusion->fast_path_io = 1;
2288 else
2289 fusion->fast_path_io = 0;
2290 megasas_sync_map_info(instance);
2291 spin_unlock_irqrestore(instance->host->host_lock,
2292 flags);
2293 break;
2294 }
Yang, Boc3518832009-10-06 14:18:02 -06002295 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2296 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2297 spin_lock_irqsave(&poll_aen_lock, flags);
2298 megasas_poll_wait_aen = 0;
2299 spin_unlock_irqrestore(&poll_aen_lock, flags);
2300 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002301
2302 /*
2303 * See if got an event notification
2304 */
2305 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2306 megasas_service_aen(instance, cmd);
2307 else
2308 megasas_complete_int_cmd(instance, cmd);
2309
2310 break;
2311
2312 case MFI_CMD_ABORT:
2313 /*
2314 * Cmd issued to abort another cmd returned
2315 */
2316 megasas_complete_abort(instance, cmd);
2317 break;
2318
2319 default:
2320 printk("megasas: Unknown command completed! [0x%X]\n",
2321 hdr->cmd);
2322 break;
2323 }
2324}
2325
2326/**
bo yang39a98552010-09-22 22:36:29 -04002327 * megasas_issue_pending_cmds_again - issue all pending cmds
2328 * in FW again because of the fw reset
2329 * @instance: Adapter soft state
2330 */
2331static inline void
2332megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2333{
2334 struct megasas_cmd *cmd;
2335 struct list_head clist_local;
2336 union megasas_evt_class_locale class_locale;
2337 unsigned long flags;
2338 u32 seq_num;
2339
2340 INIT_LIST_HEAD(&clist_local);
2341 spin_lock_irqsave(&instance->hba_lock, flags);
2342 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2343 spin_unlock_irqrestore(&instance->hba_lock, flags);
2344
2345 while (!list_empty(&clist_local)) {
2346 cmd = list_entry((&clist_local)->next,
2347 struct megasas_cmd, list);
2348 list_del_init(&cmd->list);
2349
2350 if (cmd->sync_cmd || cmd->scmd) {
2351 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2352 "detected to be pending while HBA reset.\n",
2353 cmd, cmd->scmd, cmd->sync_cmd);
2354
2355 cmd->retry_for_fw_reset++;
2356
2357 if (cmd->retry_for_fw_reset == 3) {
2358 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2359 "was tried multiple times during reset."
2360 "Shutting down the HBA\n",
2361 cmd, cmd->scmd, cmd->sync_cmd);
2362 megaraid_sas_kill_hba(instance);
2363
2364 instance->adprecovery =
2365 MEGASAS_HW_CRITICAL_ERROR;
2366 return;
2367 }
2368 }
2369
2370 if (cmd->sync_cmd == 1) {
2371 if (cmd->scmd) {
2372 printk(KERN_NOTICE "megaraid_sas: unexpected"
2373 "cmd attached to internal command!\n");
2374 }
2375 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2376 "on the internal reset queue,"
2377 "issue it again.\n", cmd);
2378 cmd->cmd_status = ENODATA;
2379 instance->instancet->fire_cmd(instance,
2380 cmd->frame_phys_addr ,
2381 0, instance->reg_set);
2382 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002383 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002384 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002385 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002386
2387 atomic_inc(&instance->fw_outstanding);
2388 instance->instancet->fire_cmd(instance,
2389 cmd->frame_phys_addr,
2390 cmd->frame_count-1, instance->reg_set);
2391 } else {
2392 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2393 "internal reset defer list while re-issue!!\n",
2394 cmd);
2395 }
2396 }
2397
2398 if (instance->aen_cmd) {
2399 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2400 megasas_return_cmd(instance, instance->aen_cmd);
2401
2402 instance->aen_cmd = NULL;
2403 }
2404
2405 /*
2406 * Initiate AEN (Asynchronous Event Notification)
2407 */
2408 seq_num = instance->last_seq_num;
2409 class_locale.members.reserved = 0;
2410 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2411 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2412
2413 megasas_register_aen(instance, seq_num, class_locale.word);
2414}
2415
2416/**
2417 * Move the internal reset pending commands to a deferred queue.
2418 *
2419 * We move the commands pending at internal reset time to a
2420 * pending queue. This queue would be flushed after successful
2421 * completion of the internal reset sequence. if the internal reset
2422 * did not complete in time, the kernel reset handler would flush
2423 * these commands.
2424 **/
2425static void
2426megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2427{
2428 struct megasas_cmd *cmd;
2429 int i;
2430 u32 max_cmd = instance->max_fw_cmds;
2431 u32 defer_index;
2432 unsigned long flags;
2433
2434 defer_index = 0;
2435 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2436 for (i = 0; i < max_cmd; i++) {
2437 cmd = instance->cmd_list[i];
2438 if (cmd->sync_cmd == 1 || cmd->scmd) {
2439 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2440 "on the defer queue as internal\n",
2441 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2442
2443 if (!list_empty(&cmd->list)) {
2444 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2445 " moving this cmd:%p, %d %p, it was"
2446 "discovered on some list?\n",
2447 cmd, cmd->sync_cmd, cmd->scmd);
2448
2449 list_del_init(&cmd->list);
2450 }
2451 defer_index++;
2452 list_add_tail(&cmd->list,
2453 &instance->internal_reset_pending_q);
2454 }
2455 }
2456 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2457}
2458
2459
2460static void
2461process_fw_state_change_wq(struct work_struct *work)
2462{
2463 struct megasas_instance *instance =
2464 container_of(work, struct megasas_instance, work_init);
2465 u32 wait;
2466 unsigned long flags;
2467
2468 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2469 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2470 instance->adprecovery);
2471 return ;
2472 }
2473
2474 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2475 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2476 "state, restarting it...\n");
2477
2478 instance->instancet->disable_intr(instance->reg_set);
2479 atomic_set(&instance->fw_outstanding, 0);
2480
2481 atomic_set(&instance->fw_reset_no_pci_access, 1);
2482 instance->instancet->adp_reset(instance, instance->reg_set);
2483 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2484
2485 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2486 "initiating next stage...\n");
2487
2488 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2489 "state 2 starting...\n");
2490
2491 /*waitting for about 20 second before start the second init*/
2492 for (wait = 0; wait < 30; wait++) {
2493 msleep(1000);
2494 }
2495
adam radford058a8fa2011-10-08 18:14:27 -07002496 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04002497 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2498
2499 megaraid_sas_kill_hba(instance);
2500 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2501 return ;
2502 }
2503
2504 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2505 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2506 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2507 ) {
2508 *instance->consumer = *instance->producer;
2509 } else {
2510 *instance->consumer = 0;
2511 *instance->producer = 0;
2512 }
2513
2514 megasas_issue_init_mfi(instance);
2515
2516 spin_lock_irqsave(&instance->hba_lock, flags);
2517 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2518 spin_unlock_irqrestore(&instance->hba_lock, flags);
2519 instance->instancet->enable_intr(instance->reg_set);
2520
2521 megasas_issue_pending_cmds_again(instance);
2522 instance->issuepend_done = 1;
2523 }
2524 return ;
2525}
2526
2527/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002528 * megasas_deplete_reply_queue - Processes all completed commands
2529 * @instance: Adapter soft state
2530 * @alt_status: Alternate status to be returned to
2531 * SCSI mid-layer instead of the status
2532 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002533 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002534 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002535static int
bo yang39a98552010-09-22 22:36:29 -04002536megasas_deplete_reply_queue(struct megasas_instance *instance,
2537 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002538{
bo yang39a98552010-09-22 22:36:29 -04002539 u32 mfiStatus;
2540 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002541
bo yang39a98552010-09-22 22:36:29 -04002542 if ((mfiStatus = instance->instancet->check_reset(instance,
2543 instance->reg_set)) == 1) {
2544 return IRQ_HANDLED;
2545 }
2546
2547 if ((mfiStatus = instance->instancet->clear_intr(
2548 instance->reg_set)
2549 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002550 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07002551 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08002552 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002553 }
2554
2555 instance->mfiStatus = mfiStatus;
2556
2557 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2558 fw_state = instance->instancet->read_fw_status_reg(
2559 instance->reg_set) & MFI_STATE_MASK;
2560
2561 if (fw_state != MFI_STATE_FAULT) {
2562 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2563 fw_state);
2564 }
2565
2566 if ((fw_state == MFI_STATE_FAULT) &&
2567 (instance->disableOnlineCtrlReset == 0)) {
2568 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2569
2570 if ((instance->pdev->device ==
2571 PCI_DEVICE_ID_LSI_SAS1064R) ||
2572 (instance->pdev->device ==
2573 PCI_DEVICE_ID_DELL_PERC5) ||
2574 (instance->pdev->device ==
2575 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2576
2577 *instance->consumer =
2578 MEGASAS_ADPRESET_INPROG_SIGN;
2579 }
2580
2581
2582 instance->instancet->disable_intr(instance->reg_set);
2583 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2584 instance->issuepend_done = 0;
2585
2586 atomic_set(&instance->fw_outstanding, 0);
2587 megasas_internal_reset_defer_cmds(instance);
2588
2589 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2590 fw_state, instance->adprecovery);
2591
2592 schedule_work(&instance->work_init);
2593 return IRQ_HANDLED;
2594
2595 } else {
2596 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2597 fw_state, instance->disableOnlineCtrlReset);
2598 }
2599 }
2600
Sumant Patro5d018ad2006-10-03 13:13:18 -07002601 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002602 return IRQ_HANDLED;
2603}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002604/**
2605 * megasas_isr - isr entry point
2606 */
David Howells7d12e782006-10-05 14:55:46 +01002607static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002608{
adam radfordc8e858f2011-10-08 18:15:13 -07002609 struct megasas_irq_context *irq_context = devp;
2610 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04002611 unsigned long flags;
2612 irqreturn_t rc;
2613
adam radfordc8e858f2011-10-08 18:15:13 -07002614 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04002615 return IRQ_HANDLED;
2616
bo yang39a98552010-09-22 22:36:29 -04002617 spin_lock_irqsave(&instance->hba_lock, flags);
2618 rc = megasas_deplete_reply_queue(instance, DID_OK);
2619 spin_unlock_irqrestore(&instance->hba_lock, flags);
2620
2621 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002622}
2623
2624/**
2625 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002626 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002627 *
2628 * During the initialization, FW passes can potentially be in any one of
2629 * several possible states. If the FW in operational, waiting-for-handshake
2630 * states, driver must take steps to bring it to ready state. Otherwise, it
2631 * has to wait for the ready state.
2632 */
adam radford9c915a82010-12-21 13:34:31 -08002633int
adam radford058a8fa2011-10-08 18:14:27 -07002634megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002635{
2636 int i;
2637 u8 max_wait;
2638 u32 fw_state;
2639 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002640 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002641
Sumant Patro1341c932006-01-25 12:02:40 -08002642 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002643
Sumant Patroe3bbff92006-10-03 12:28:49 -07002644 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002645 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2646 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002647
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002648 while (fw_state != MFI_STATE_READY) {
2649
Yang, Bo7218df62009-10-06 14:52:20 -06002650 abs_state =
2651 instance->instancet->read_fw_status_reg(instance->reg_set);
2652
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002653 switch (fw_state) {
2654
2655 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002656 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07002657 if (ocr) {
2658 max_wait = MEGASAS_RESET_WAIT_TIME;
2659 cur_state = MFI_STATE_FAULT;
2660 break;
2661 } else
2662 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002663
2664 case MFI_STATE_WAIT_HANDSHAKE:
2665 /*
2666 * Set the CLR bit in inbound doorbell
2667 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002668 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002669 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2670 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002671 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2672 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002673 PCI_DEVICE_ID_LSI_FUSION) ||
2674 (instance->pdev->device ==
2675 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002676 writel(
2677 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002678 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002679 } else {
2680 writel(
2681 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2682 &instance->reg_set->inbound_doorbell);
2683 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002684
Yang, Bo7218df62009-10-06 14:52:20 -06002685 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002686 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2687 break;
2688
Sumant Patroe3bbff92006-10-03 12:28:49 -07002689 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002690 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002691 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2692 (instance->pdev->device ==
2693 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2694 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002695 PCI_DEVICE_ID_LSI_FUSION) ||
2696 (instance->pdev->device ==
2697 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002698 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002699 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002700 } else
2701 writel(MFI_INIT_HOTPLUG,
2702 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002703
Yang, Bo7218df62009-10-06 14:52:20 -06002704 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002705 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2706 break;
2707
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002708 case MFI_STATE_OPERATIONAL:
2709 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002710 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002711 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002712 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002713 if ((instance->pdev->device ==
2714 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2715 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002716 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2717 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07002718 == PCI_DEVICE_ID_LSI_FUSION) ||
2719 (instance->pdev->device
2720 == PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002721 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002722 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07002723 if ((instance->pdev->device ==
2724 PCI_DEVICE_ID_LSI_FUSION) ||
2725 (instance->pdev->device ==
2726 PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08002727 for (i = 0; i < (10 * 1000); i += 20) {
2728 if (readl(
2729 &instance->
2730 reg_set->
2731 doorbell) & 1)
2732 msleep(20);
2733 else
2734 break;
2735 }
2736 }
Yang, Bo87911122009-10-06 14:31:54 -06002737 } else
2738 writel(MFI_RESET_FLAGS,
2739 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002740
Yang, Bo7218df62009-10-06 14:52:20 -06002741 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002742 cur_state = MFI_STATE_OPERATIONAL;
2743 break;
2744
2745 case MFI_STATE_UNDEFINED:
2746 /*
2747 * This state should not last for more than 2 seconds
2748 */
Yang, Bo7218df62009-10-06 14:52:20 -06002749 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002750 cur_state = MFI_STATE_UNDEFINED;
2751 break;
2752
2753 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002754 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002755 cur_state = MFI_STATE_BB_INIT;
2756 break;
2757
2758 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002759 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002760 cur_state = MFI_STATE_FW_INIT;
2761 break;
2762
2763 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002764 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002765 cur_state = MFI_STATE_FW_INIT_2;
2766 break;
2767
2768 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002769 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002770 cur_state = MFI_STATE_DEVICE_SCAN;
2771 break;
2772
2773 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002774 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002775 cur_state = MFI_STATE_FLUSH_CACHE;
2776 break;
2777
2778 default:
2779 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2780 fw_state);
2781 return -ENODEV;
2782 }
2783
2784 /*
2785 * The cur_state should not last for more than max_wait secs
2786 */
2787 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002788 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002789 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002790 curr_abs_state =
2791 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002792
Yang, Bo7218df62009-10-06 14:52:20 -06002793 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002794 msleep(1);
2795 } else
2796 break;
2797 }
2798
2799 /*
2800 * Return error if fw_state hasn't changed after max_wait
2801 */
Yang, Bo7218df62009-10-06 14:52:20 -06002802 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002803 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2804 "in %d secs\n", fw_state, max_wait);
2805 return -ENODEV;
2806 }
bo yang39a98552010-09-22 22:36:29 -04002807 }
adam radford0d490162010-12-14 19:17:17 -08002808 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002809
2810 return 0;
2811}
2812
2813/**
2814 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2815 * @instance: Adapter soft state
2816 */
2817static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2818{
2819 int i;
adam radford9c915a82010-12-21 13:34:31 -08002820 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002821 struct megasas_cmd *cmd;
2822
2823 if (!instance->frame_dma_pool)
2824 return;
2825
2826 /*
2827 * Return all frames to pool
2828 */
2829 for (i = 0; i < max_cmd; i++) {
2830
2831 cmd = instance->cmd_list[i];
2832
2833 if (cmd->frame)
2834 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2835 cmd->frame_phys_addr);
2836
2837 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002838 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002839 cmd->sense_phys_addr);
2840 }
2841
2842 /*
2843 * Now destroy the pool itself
2844 */
2845 pci_pool_destroy(instance->frame_dma_pool);
2846 pci_pool_destroy(instance->sense_dma_pool);
2847
2848 instance->frame_dma_pool = NULL;
2849 instance->sense_dma_pool = NULL;
2850}
2851
2852/**
2853 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2854 * @instance: Adapter soft state
2855 *
2856 * Each command packet has an embedded DMA memory buffer that is used for
2857 * filling MFI frame and the SG list that immediately follows the frame. This
2858 * function creates those DMA memory buffers for each command packet by using
2859 * PCI pool facility.
2860 */
2861static int megasas_create_frame_pool(struct megasas_instance *instance)
2862{
2863 int i;
2864 u32 max_cmd;
2865 u32 sge_sz;
2866 u32 sgl_sz;
2867 u32 total_sz;
2868 u32 frame_count;
2869 struct megasas_cmd *cmd;
2870
adam radford9c915a82010-12-21 13:34:31 -08002871 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002872
2873 /*
2874 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2875 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2876 */
2877 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2878 sizeof(struct megasas_sge32);
2879
Yang, Bof4c9a132009-10-06 14:43:28 -06002880 if (instance->flag_ieee) {
2881 sge_sz = sizeof(struct megasas_sge_skinny);
2882 }
2883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002884 /*
2885 * Calculated the number of 64byte frames required for SGL
2886 */
2887 sgl_sz = sge_sz * instance->max_num_sge;
2888 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002889 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002890
2891 /*
2892 * We need one extra frame for the MFI command
2893 */
2894 frame_count++;
2895
2896 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2897 /*
2898 * Use DMA pool facility provided by PCI layer
2899 */
2900 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2901 instance->pdev, total_sz, 64,
2902 0);
2903
2904 if (!instance->frame_dma_pool) {
2905 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2906 return -ENOMEM;
2907 }
2908
2909 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2910 instance->pdev, 128, 4, 0);
2911
2912 if (!instance->sense_dma_pool) {
2913 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2914
2915 pci_pool_destroy(instance->frame_dma_pool);
2916 instance->frame_dma_pool = NULL;
2917
2918 return -ENOMEM;
2919 }
2920
2921 /*
2922 * Allocate and attach a frame to each of the commands in cmd_list.
2923 * By making cmd->index as the context instead of the &cmd, we can
2924 * always use 32bit context regardless of the architecture
2925 */
2926 for (i = 0; i < max_cmd; i++) {
2927
2928 cmd = instance->cmd_list[i];
2929
2930 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2931 GFP_KERNEL, &cmd->frame_phys_addr);
2932
2933 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2934 GFP_KERNEL, &cmd->sense_phys_addr);
2935
2936 /*
2937 * megasas_teardown_frame_pool() takes care of freeing
2938 * whatever has been allocated
2939 */
2940 if (!cmd->frame || !cmd->sense) {
2941 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2942 megasas_teardown_frame_pool(instance);
2943 return -ENOMEM;
2944 }
2945
Yang, Bo707e09b2010-10-12 07:20:27 -06002946 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002947 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002948 cmd->frame->io.pad_0 = 0;
adam radforde5f93a32011-10-08 18:15:19 -07002949 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2950 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2951 (reset_devices))
2952 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002953 }
2954
2955 return 0;
2956}
2957
2958/**
2959 * megasas_free_cmds - Free all the cmds in the free cmd pool
2960 * @instance: Adapter soft state
2961 */
adam radford9c915a82010-12-21 13:34:31 -08002962void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002963{
2964 int i;
2965 /* First free the MFI frame pool */
2966 megasas_teardown_frame_pool(instance);
2967
2968 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002969 for (i = 0; i < instance->max_mfi_cmds; i++)
2970
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002971 kfree(instance->cmd_list[i]);
2972
2973 /* Free the cmd_list buffer itself */
2974 kfree(instance->cmd_list);
2975 instance->cmd_list = NULL;
2976
2977 INIT_LIST_HEAD(&instance->cmd_pool);
2978}
2979
2980/**
2981 * megasas_alloc_cmds - Allocates the command packets
2982 * @instance: Adapter soft state
2983 *
2984 * Each command that is issued to the FW, whether IO commands from the OS or
2985 * internal commands like IOCTLs, are wrapped in local data structure called
2986 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2987 * the FW.
2988 *
2989 * Each frame has a 32-bit field called context (tag). This context is used
2990 * to get back the megasas_cmd from the frame when a frame gets completed in
2991 * the ISR. Typically the address of the megasas_cmd itself would be used as
2992 * the context. But we wanted to keep the differences between 32 and 64 bit
2993 * systems to the mininum. We always use 32 bit integers for the context. In
2994 * this driver, the 32 bit values are the indices into an array cmd_list.
2995 * This array is used only to look up the megasas_cmd given the context. The
2996 * free commands themselves are maintained in a linked list called cmd_pool.
2997 */
adam radford9c915a82010-12-21 13:34:31 -08002998int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002999{
3000 int i;
3001 int j;
3002 u32 max_cmd;
3003 struct megasas_cmd *cmd;
3004
adam radford9c915a82010-12-21 13:34:31 -08003005 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003006
3007 /*
3008 * instance->cmd_list is an array of struct megasas_cmd pointers.
3009 * Allocate the dynamic array first and then allocate individual
3010 * commands.
3011 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003012 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003013
3014 if (!instance->cmd_list) {
3015 printk(KERN_DEBUG "megasas: out of memory\n");
3016 return -ENOMEM;
3017 }
3018
adam radford9c915a82010-12-21 13:34:31 -08003019 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003020
3021 for (i = 0; i < max_cmd; i++) {
3022 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3023 GFP_KERNEL);
3024
3025 if (!instance->cmd_list[i]) {
3026
3027 for (j = 0; j < i; j++)
3028 kfree(instance->cmd_list[j]);
3029
3030 kfree(instance->cmd_list);
3031 instance->cmd_list = NULL;
3032
3033 return -ENOMEM;
3034 }
3035 }
3036
3037 /*
3038 * Add all the commands to command pool (instance->cmd_pool)
3039 */
3040 for (i = 0; i < max_cmd; i++) {
3041 cmd = instance->cmd_list[i];
3042 memset(cmd, 0, sizeof(struct megasas_cmd));
3043 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003044 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003045 cmd->instance = instance;
3046
3047 list_add_tail(&cmd->list, &instance->cmd_pool);
3048 }
3049
3050 /*
3051 * Create a frame pool and assign one frame to each cmd
3052 */
3053 if (megasas_create_frame_pool(instance)) {
3054 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3055 megasas_free_cmds(instance);
3056 }
3057
3058 return 0;
3059}
3060
Yang, Bo81e403c2009-10-06 14:27:54 -06003061/*
3062 * megasas_get_pd_list_info - Returns FW's pd_list structure
3063 * @instance: Adapter soft state
3064 * @pd_list: pd_list structure
3065 *
3066 * Issues an internal command (DCMD) to get the FW's controller PD
3067 * list structure. This information is mainly used to find out SYSTEM
3068 * supported by the FW.
3069 */
3070static int
3071megasas_get_pd_list(struct megasas_instance *instance)
3072{
3073 int ret = 0, pd_index = 0;
3074 struct megasas_cmd *cmd;
3075 struct megasas_dcmd_frame *dcmd;
3076 struct MR_PD_LIST *ci;
3077 struct MR_PD_ADDRESS *pd_addr;
3078 dma_addr_t ci_h = 0;
3079
3080 cmd = megasas_get_cmd(instance);
3081
3082 if (!cmd) {
3083 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3084 return -ENOMEM;
3085 }
3086
3087 dcmd = &cmd->frame->dcmd;
3088
3089 ci = pci_alloc_consistent(instance->pdev,
3090 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3091
3092 if (!ci) {
3093 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3094 megasas_return_cmd(instance, cmd);
3095 return -ENOMEM;
3096 }
3097
3098 memset(ci, 0, sizeof(*ci));
3099 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3100
3101 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3102 dcmd->mbox.b[1] = 0;
3103 dcmd->cmd = MFI_CMD_DCMD;
3104 dcmd->cmd_status = 0xFF;
3105 dcmd->sge_count = 1;
3106 dcmd->flags = MFI_FRAME_DIR_READ;
3107 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003108 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003109 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3110 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3111 dcmd->sgl.sge32[0].phys_addr = ci_h;
3112 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3113
3114 if (!megasas_issue_polled(instance, cmd)) {
3115 ret = 0;
3116 } else {
3117 ret = -1;
3118 }
3119
3120 /*
3121 * the following function will get the instance PD LIST.
3122 */
3123
3124 pd_addr = ci->addr;
3125
3126 if ( ret == 0 &&
3127 (ci->count <
3128 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3129
3130 memset(instance->pd_list, 0,
3131 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3132
3133 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3134
3135 instance->pd_list[pd_addr->deviceId].tid =
3136 pd_addr->deviceId;
3137 instance->pd_list[pd_addr->deviceId].driveType =
3138 pd_addr->scsiDevType;
3139 instance->pd_list[pd_addr->deviceId].driveState =
3140 MR_PD_STATE_SYSTEM;
3141 pd_addr++;
3142 }
3143 }
3144
3145 pci_free_consistent(instance->pdev,
3146 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3147 ci, ci_h);
3148 megasas_return_cmd(instance, cmd);
3149
3150 return ret;
3151}
3152
Yang, Bobdc6fb82009-12-06 08:30:19 -07003153/*
3154 * megasas_get_ld_list_info - Returns FW's ld_list structure
3155 * @instance: Adapter soft state
3156 * @ld_list: ld_list structure
3157 *
3158 * Issues an internal command (DCMD) to get the FW's controller PD
3159 * list structure. This information is mainly used to find out SYSTEM
3160 * supported by the FW.
3161 */
3162static int
3163megasas_get_ld_list(struct megasas_instance *instance)
3164{
3165 int ret = 0, ld_index = 0, ids = 0;
3166 struct megasas_cmd *cmd;
3167 struct megasas_dcmd_frame *dcmd;
3168 struct MR_LD_LIST *ci;
3169 dma_addr_t ci_h = 0;
3170
3171 cmd = megasas_get_cmd(instance);
3172
3173 if (!cmd) {
3174 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3175 return -ENOMEM;
3176 }
3177
3178 dcmd = &cmd->frame->dcmd;
3179
3180 ci = pci_alloc_consistent(instance->pdev,
3181 sizeof(struct MR_LD_LIST),
3182 &ci_h);
3183
3184 if (!ci) {
3185 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3186 megasas_return_cmd(instance, cmd);
3187 return -ENOMEM;
3188 }
3189
3190 memset(ci, 0, sizeof(*ci));
3191 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3192
3193 dcmd->cmd = MFI_CMD_DCMD;
3194 dcmd->cmd_status = 0xFF;
3195 dcmd->sge_count = 1;
3196 dcmd->flags = MFI_FRAME_DIR_READ;
3197 dcmd->timeout = 0;
3198 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3199 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3200 dcmd->sgl.sge32[0].phys_addr = ci_h;
3201 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3202 dcmd->pad_0 = 0;
3203
3204 if (!megasas_issue_polled(instance, cmd)) {
3205 ret = 0;
3206 } else {
3207 ret = -1;
3208 }
3209
3210 /* the following function will get the instance PD LIST */
3211
bo yang39a98552010-09-22 22:36:29 -04003212 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003213 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3214
3215 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3216 if (ci->ldList[ld_index].state != 0) {
3217 ids = ci->ldList[ld_index].ref.targetId;
3218 instance->ld_ids[ids] =
3219 ci->ldList[ld_index].ref.targetId;
3220 }
3221 }
3222 }
3223
3224 pci_free_consistent(instance->pdev,
3225 sizeof(struct MR_LD_LIST),
3226 ci,
3227 ci_h);
3228
3229 megasas_return_cmd(instance, cmd);
3230 return ret;
3231}
3232
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003233/**
3234 * megasas_get_controller_info - Returns FW's controller structure
3235 * @instance: Adapter soft state
3236 * @ctrl_info: Controller information structure
3237 *
3238 * Issues an internal command (DCMD) to get the FW's controller structure.
3239 * This information is mainly used to find out the maximum IO transfer per
3240 * command supported by the FW.
3241 */
3242static int
3243megasas_get_ctrl_info(struct megasas_instance *instance,
3244 struct megasas_ctrl_info *ctrl_info)
3245{
3246 int ret = 0;
3247 struct megasas_cmd *cmd;
3248 struct megasas_dcmd_frame *dcmd;
3249 struct megasas_ctrl_info *ci;
3250 dma_addr_t ci_h = 0;
3251
3252 cmd = megasas_get_cmd(instance);
3253
3254 if (!cmd) {
3255 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3256 return -ENOMEM;
3257 }
3258
3259 dcmd = &cmd->frame->dcmd;
3260
3261 ci = pci_alloc_consistent(instance->pdev,
3262 sizeof(struct megasas_ctrl_info), &ci_h);
3263
3264 if (!ci) {
3265 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3266 megasas_return_cmd(instance, cmd);
3267 return -ENOMEM;
3268 }
3269
3270 memset(ci, 0, sizeof(*ci));
3271 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3272
3273 dcmd->cmd = MFI_CMD_DCMD;
3274 dcmd->cmd_status = 0xFF;
3275 dcmd->sge_count = 1;
3276 dcmd->flags = MFI_FRAME_DIR_READ;
3277 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003278 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003279 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3280 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3281 dcmd->sgl.sge32[0].phys_addr = ci_h;
3282 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3283
3284 if (!megasas_issue_polled(instance, cmd)) {
3285 ret = 0;
3286 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3287 } else {
3288 ret = -1;
3289 }
3290
3291 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3292 ci, ci_h);
3293
3294 megasas_return_cmd(instance, cmd);
3295 return ret;
3296}
3297
3298/**
bo yang31ea7082007-11-07 12:09:50 -05003299 * megasas_issue_init_mfi - Initializes the FW
3300 * @instance: Adapter soft state
3301 *
3302 * Issues the INIT MFI cmd
3303 */
3304static int
3305megasas_issue_init_mfi(struct megasas_instance *instance)
3306{
3307 u32 context;
3308
3309 struct megasas_cmd *cmd;
3310
3311 struct megasas_init_frame *init_frame;
3312 struct megasas_init_queue_info *initq_info;
3313 dma_addr_t init_frame_h;
3314 dma_addr_t initq_info_h;
3315
3316 /*
3317 * Prepare a init frame. Note the init frame points to queue info
3318 * structure. Each frame has SGL allocated after first 64 bytes. For
3319 * this frame - since we don't need any SGL - we use SGL's space as
3320 * queue info structure
3321 *
3322 * We will not get a NULL command below. We just created the pool.
3323 */
3324 cmd = megasas_get_cmd(instance);
3325
3326 init_frame = (struct megasas_init_frame *)cmd->frame;
3327 initq_info = (struct megasas_init_queue_info *)
3328 ((unsigned long)init_frame + 64);
3329
3330 init_frame_h = cmd->frame_phys_addr;
3331 initq_info_h = init_frame_h + 64;
3332
3333 context = init_frame->context;
3334 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3335 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3336 init_frame->context = context;
3337
3338 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3339 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3340
3341 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3342 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3343
3344 init_frame->cmd = MFI_CMD_INIT;
3345 init_frame->cmd_status = 0xFF;
3346 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3347
3348 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3349
3350 /*
3351 * disable the intr before firing the init frame to FW
3352 */
3353 instance->instancet->disable_intr(instance->reg_set);
3354
3355 /*
3356 * Issue the init frame in polled mode
3357 */
3358
3359 if (megasas_issue_polled(instance, cmd)) {
3360 printk(KERN_ERR "megasas: Failed to init firmware\n");
3361 megasas_return_cmd(instance, cmd);
3362 goto fail_fw_init;
3363 }
3364
3365 megasas_return_cmd(instance, cmd);
3366
3367 return 0;
3368
3369fail_fw_init:
3370 return -EINVAL;
3371}
3372
3373/**
bo yangad84db22007-11-09 04:40:16 -05003374 * megasas_start_timer - Initializes a timer object
3375 * @instance: Adapter soft state
3376 * @timer: timer object to be initialized
3377 * @fn: timer function
3378 * @interval: time interval between timer function call
3379 */
3380static inline void
3381megasas_start_timer(struct megasas_instance *instance,
3382 struct timer_list *timer,
3383 void *fn, unsigned long interval)
3384{
3385 init_timer(timer);
3386 timer->expires = jiffies + interval;
3387 timer->data = (unsigned long)instance;
3388 timer->function = fn;
3389 add_timer(timer);
3390}
3391
3392/**
3393 * megasas_io_completion_timer - Timer fn
3394 * @instance_addr: Address of adapter soft state
3395 *
3396 * Schedules tasklet for cmd completion
3397 * if poll_mode_io is set
3398 */
3399static void
3400megasas_io_completion_timer(unsigned long instance_addr)
3401{
3402 struct megasas_instance *instance =
3403 (struct megasas_instance *)instance_addr;
3404
3405 if (atomic_read(&instance->fw_outstanding))
3406 tasklet_schedule(&instance->isr_tasklet);
3407
3408 /* Restart timer */
3409 if (poll_mode_io)
3410 mod_timer(&instance->io_completion_timer,
3411 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3412}
3413
adam radfordcd50ba82010-12-21 10:23:23 -08003414static u32
3415megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003416{
adam radfordcd50ba82010-12-21 10:23:23 -08003417 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003418 u32 context_sz;
3419 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003420
3421 reg_set = instance->reg_set;
3422
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003423 /*
3424 * Get various operational parameters from status register
3425 */
Sumant Patro1341c932006-01-25 12:02:40 -08003426 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003427 /*
3428 * Reduce the max supported cmds by 1. This is to ensure that the
3429 * reply_q_sz (1 more than the max cmd that driver may send)
3430 * does not exceed max cmds that the FW can support
3431 */
3432 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003433 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003434 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003435 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003436 /*
3437 * Create a pool of commands
3438 */
3439 if (megasas_alloc_cmds(instance))
3440 goto fail_alloc_cmds;
3441
3442 /*
3443 * Allocate memory for reply queue. Length of reply queue should
3444 * be _one_ more than the maximum commands handled by the firmware.
3445 *
3446 * Note: When FW completes commands, it places corresponding contex
3447 * values in this circular reply queue. This circular queue is a fairly
3448 * typical producer-consumer queue. FW is the producer (of completed
3449 * commands) and the driver is the consumer.
3450 */
3451 context_sz = sizeof(u32);
3452 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3453
3454 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3455 reply_q_sz,
3456 &instance->reply_queue_h);
3457
3458 if (!instance->reply_queue) {
3459 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3460 goto fail_reply_queue;
3461 }
3462
bo yang31ea7082007-11-07 12:09:50 -05003463 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003464 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003465
bo yang39a98552010-09-22 22:36:29 -04003466 instance->fw_support_ieee = 0;
3467 instance->fw_support_ieee =
3468 (instance->instancet->read_fw_status_reg(reg_set) &
3469 0x04000000);
3470
3471 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3472 instance->fw_support_ieee);
3473
3474 if (instance->fw_support_ieee)
3475 instance->flag_ieee = 1;
3476
adam radfordcd50ba82010-12-21 10:23:23 -08003477 return 0;
3478
3479fail_fw_init:
3480
3481 pci_free_consistent(instance->pdev, reply_q_sz,
3482 instance->reply_queue, instance->reply_queue_h);
3483fail_reply_queue:
3484 megasas_free_cmds(instance);
3485
3486fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003487 return 1;
3488}
3489
3490/**
3491 * megasas_init_fw - Initializes the FW
3492 * @instance: Adapter soft state
3493 *
3494 * This is the main function for initializing firmware
3495 */
3496
3497static int megasas_init_fw(struct megasas_instance *instance)
3498{
3499 u32 max_sectors_1;
3500 u32 max_sectors_2;
adam radford3f1abce2011-05-11 18:33:47 -07003501 u32 tmp_sectors, msix_enable;
adam radfordcd50ba82010-12-21 10:23:23 -08003502 struct megasas_register_set __iomem *reg_set;
3503 struct megasas_ctrl_info *ctrl_info;
3504 unsigned long bar_list;
adam radfordc8e858f2011-10-08 18:15:13 -07003505 int i;
adam radfordcd50ba82010-12-21 10:23:23 -08003506
3507 /* Find first memory bar */
3508 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3509 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3510 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3511 if (pci_request_selected_regions(instance->pdev, instance->bar,
3512 "megasas: LSI")) {
3513 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3514 return -EBUSY;
3515 }
3516
3517 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3518
3519 if (!instance->reg_set) {
3520 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3521 goto fail_ioremap;
3522 }
3523
3524 reg_set = instance->reg_set;
3525
3526 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003527 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07003528 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08003529 instance->instancet = &megasas_instance_template_fusion;
3530 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003531 case PCI_DEVICE_ID_LSI_SAS1078R:
3532 case PCI_DEVICE_ID_LSI_SAS1078DE:
3533 instance->instancet = &megasas_instance_template_ppc;
3534 break;
3535 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3536 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3537 instance->instancet = &megasas_instance_template_gen2;
3538 break;
3539 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3540 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3541 instance->instancet = &megasas_instance_template_skinny;
3542 break;
3543 case PCI_DEVICE_ID_LSI_SAS1064R:
3544 case PCI_DEVICE_ID_DELL_PERC5:
3545 default:
3546 instance->instancet = &megasas_instance_template_xscale;
3547 break;
3548 }
3549
3550 /*
3551 * We expect the FW state to be READY
3552 */
adam radford058a8fa2011-10-08 18:14:27 -07003553 if (megasas_transition_to_ready(instance, 0))
adam radfordcd50ba82010-12-21 10:23:23 -08003554 goto fail_ready_state;
3555
adam radford3f1abce2011-05-11 18:33:47 -07003556 /* Check if MSI-X is supported while in ready state */
3557 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3558 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07003559 if (msix_enable && !msix_disable) {
3560 /* Check max MSI-X vectors */
3561 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3562 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3563 instance->msix_vectors = (readl(&instance->reg_set->
3564 outbound_scratch_pad_2
3565 ) & 0x1F) + 1;
3566 } else
3567 instance->msix_vectors = 1;
3568 /* Don't bother allocating more MSI-X vectors than cpus */
3569 instance->msix_vectors = min(instance->msix_vectors,
3570 (unsigned int)num_online_cpus());
3571 for (i = 0; i < instance->msix_vectors; i++)
3572 instance->msixentry[i].entry = i;
3573 i = pci_enable_msix(instance->pdev, instance->msixentry,
3574 instance->msix_vectors);
3575 if (i >= 0) {
3576 if (i) {
3577 if (!pci_enable_msix(instance->pdev,
3578 instance->msixentry, i))
3579 instance->msix_vectors = i;
3580 else
3581 instance->msix_vectors = 0;
3582 }
3583 } else
3584 instance->msix_vectors = 0;
3585 }
adam radford3f1abce2011-05-11 18:33:47 -07003586
adam radfordcd50ba82010-12-21 10:23:23 -08003587 /* Get operational params, sge flags, send init cmd to controller */
3588 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003589 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003590
3591 printk(KERN_ERR "megasas: INIT adapter done\n");
3592
bo yang39a98552010-09-22 22:36:29 -04003593 /** for passthrough
3594 * the following function will get the PD LIST.
3595 */
3596
Yang, Bo81e403c2009-10-06 14:27:54 -06003597 memset(instance->pd_list, 0 ,
3598 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3599 megasas_get_pd_list(instance);
3600
Yang, Bobdc6fb82009-12-06 08:30:19 -07003601 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3602 megasas_get_ld_list(instance);
3603
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003604 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3605
3606 /*
3607 * Compute the max allowed sectors per IO: The controller info has two
3608 * limits on max sectors. Driver should use the minimum of these two.
3609 *
3610 * 1 << stripe_sz_ops.min = max sectors per strip
3611 *
3612 * Note that older firmwares ( < FW ver 30) didn't report information
3613 * to calculate max_sectors_1. So the number ended up as zero always.
3614 */
bo yang14faea92007-11-09 04:14:00 -05003615 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003616 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3617
3618 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3619 ctrl_info->max_strips_per_io;
3620 max_sectors_2 = ctrl_info->max_request_size;
3621
bo yang14faea92007-11-09 04:14:00 -05003622 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003623 instance->disableOnlineCtrlReset =
3624 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003625 }
3626
3627 instance->max_sectors_per_req = instance->max_num_sge *
3628 PAGE_SIZE / 512;
3629 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3630 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003631
3632 kfree(ctrl_info);
3633
Sumant Patro5d018ad2006-10-03 13:13:18 -07003634 /*
3635 * Setup tasklet for cmd completion
3636 */
3637
adam radfordf86c5422011-02-24 20:57:00 -08003638 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003639 (unsigned long)instance);
3640
3641 /* Initialize the cmd completion timer */
3642 if (poll_mode_io)
3643 megasas_start_timer(instance, &instance->io_completion_timer,
3644 megasas_io_completion_timer,
3645 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003646 return 0;
3647
adam radfordeb1b1232011-02-24 20:55:56 -08003648fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003649fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003650 iounmap(instance->reg_set);
3651
3652 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003653 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003654
3655 return -EINVAL;
3656}
3657
3658/**
3659 * megasas_release_mfi - Reverses the FW initialization
3660 * @intance: Adapter soft state
3661 */
3662static void megasas_release_mfi(struct megasas_instance *instance)
3663{
adam radford9c915a82010-12-21 13:34:31 -08003664 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003665
adam radford9c915a82010-12-21 13:34:31 -08003666 if (instance->reply_queue)
3667 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003668 instance->reply_queue, instance->reply_queue_h);
3669
3670 megasas_free_cmds(instance);
3671
3672 iounmap(instance->reg_set);
3673
adam radfordb6d5d882010-12-14 18:56:07 -08003674 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003675}
3676
3677/**
3678 * megasas_get_seq_num - Gets latest event sequence numbers
3679 * @instance: Adapter soft state
3680 * @eli: FW event log sequence numbers information
3681 *
3682 * FW maintains a log of all events in a non-volatile area. Upper layers would
3683 * usually find out the latest sequence number of the events, the seq number at
3684 * the boot etc. They would "read" all the events below the latest seq number
3685 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3686 * number), they would subsribe to AEN (asynchronous event notification) and
3687 * wait for the events to happen.
3688 */
3689static int
3690megasas_get_seq_num(struct megasas_instance *instance,
3691 struct megasas_evt_log_info *eli)
3692{
3693 struct megasas_cmd *cmd;
3694 struct megasas_dcmd_frame *dcmd;
3695 struct megasas_evt_log_info *el_info;
3696 dma_addr_t el_info_h = 0;
3697
3698 cmd = megasas_get_cmd(instance);
3699
3700 if (!cmd) {
3701 return -ENOMEM;
3702 }
3703
3704 dcmd = &cmd->frame->dcmd;
3705 el_info = pci_alloc_consistent(instance->pdev,
3706 sizeof(struct megasas_evt_log_info),
3707 &el_info_h);
3708
3709 if (!el_info) {
3710 megasas_return_cmd(instance, cmd);
3711 return -ENOMEM;
3712 }
3713
3714 memset(el_info, 0, sizeof(*el_info));
3715 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3716
3717 dcmd->cmd = MFI_CMD_DCMD;
3718 dcmd->cmd_status = 0x0;
3719 dcmd->sge_count = 1;
3720 dcmd->flags = MFI_FRAME_DIR_READ;
3721 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003722 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003723 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3724 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3725 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3726 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3727
3728 megasas_issue_blocked_cmd(instance, cmd);
3729
3730 /*
3731 * Copy the data back into callers buffer
3732 */
3733 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3734
3735 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3736 el_info, el_info_h);
3737
3738 megasas_return_cmd(instance, cmd);
3739
3740 return 0;
3741}
3742
3743/**
3744 * megasas_register_aen - Registers for asynchronous event notification
3745 * @instance: Adapter soft state
3746 * @seq_num: The starting sequence number
3747 * @class_locale: Class of the event
3748 *
3749 * This function subscribes for AEN for events beyond the @seq_num. It requests
3750 * to be notified if and only if the event is of type @class_locale
3751 */
3752static int
3753megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3754 u32 class_locale_word)
3755{
3756 int ret_val;
3757 struct megasas_cmd *cmd;
3758 struct megasas_dcmd_frame *dcmd;
3759 union megasas_evt_class_locale curr_aen;
3760 union megasas_evt_class_locale prev_aen;
3761
3762 /*
3763 * If there an AEN pending already (aen_cmd), check if the
3764 * class_locale of that pending AEN is inclusive of the new
3765 * AEN request we currently have. If it is, then we don't have
3766 * to do anything. In other words, whichever events the current
3767 * AEN request is subscribing to, have already been subscribed
3768 * to.
3769 *
3770 * If the old_cmd is _not_ inclusive, then we have to abort
3771 * that command, form a class_locale that is superset of both
3772 * old and current and re-issue to the FW
3773 */
3774
3775 curr_aen.word = class_locale_word;
3776
3777 if (instance->aen_cmd) {
3778
3779 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3780
3781 /*
3782 * A class whose enum value is smaller is inclusive of all
3783 * higher values. If a PROGRESS (= -1) was previously
3784 * registered, then a new registration requests for higher
3785 * classes need not be sent to FW. They are automatically
3786 * included.
3787 *
3788 * Locale numbers don't have such hierarchy. They are bitmap
3789 * values
3790 */
3791 if ((prev_aen.members.class <= curr_aen.members.class) &&
3792 !((prev_aen.members.locale & curr_aen.members.locale) ^
3793 curr_aen.members.locale)) {
3794 /*
3795 * Previously issued event registration includes
3796 * current request. Nothing to do.
3797 */
3798 return 0;
3799 } else {
3800 curr_aen.members.locale |= prev_aen.members.locale;
3801
3802 if (prev_aen.members.class < curr_aen.members.class)
3803 curr_aen.members.class = prev_aen.members.class;
3804
3805 instance->aen_cmd->abort_aen = 1;
3806 ret_val = megasas_issue_blocked_abort_cmd(instance,
3807 instance->
3808 aen_cmd);
3809
3810 if (ret_val) {
3811 printk(KERN_DEBUG "megasas: Failed to abort "
3812 "previous AEN command\n");
3813 return ret_val;
3814 }
3815 }
3816 }
3817
3818 cmd = megasas_get_cmd(instance);
3819
3820 if (!cmd)
3821 return -ENOMEM;
3822
3823 dcmd = &cmd->frame->dcmd;
3824
3825 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3826
3827 /*
3828 * Prepare DCMD for aen registration
3829 */
3830 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3831
3832 dcmd->cmd = MFI_CMD_DCMD;
3833 dcmd->cmd_status = 0x0;
3834 dcmd->sge_count = 1;
3835 dcmd->flags = MFI_FRAME_DIR_READ;
3836 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003837 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003838 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003839 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3840 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3841 dcmd->mbox.w[0] = seq_num;
3842 dcmd->mbox.w[1] = curr_aen.word;
3843 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3844 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3845
Yang, Bof4c9a132009-10-06 14:43:28 -06003846 if (instance->aen_cmd != NULL) {
3847 megasas_return_cmd(instance, cmd);
3848 return 0;
3849 }
3850
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003851 /*
3852 * Store reference to the cmd used to register for AEN. When an
3853 * application wants us to register for AEN, we have to abort this
3854 * cmd and re-register with a new EVENT LOCALE supplied by that app
3855 */
3856 instance->aen_cmd = cmd;
3857
3858 /*
3859 * Issue the aen registration frame
3860 */
adam radford9c915a82010-12-21 13:34:31 -08003861 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003862
3863 return 0;
3864}
3865
3866/**
3867 * megasas_start_aen - Subscribes to AEN during driver load time
3868 * @instance: Adapter soft state
3869 */
3870static int megasas_start_aen(struct megasas_instance *instance)
3871{
3872 struct megasas_evt_log_info eli;
3873 union megasas_evt_class_locale class_locale;
3874
3875 /*
3876 * Get the latest sequence number from FW
3877 */
3878 memset(&eli, 0, sizeof(eli));
3879
3880 if (megasas_get_seq_num(instance, &eli))
3881 return -1;
3882
3883 /*
3884 * Register AEN with FW for latest sequence number plus 1
3885 */
3886 class_locale.members.reserved = 0;
3887 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3888 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3889
3890 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3891 class_locale.word);
3892}
3893
3894/**
3895 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3896 * @instance: Adapter soft state
3897 */
3898static int megasas_io_attach(struct megasas_instance *instance)
3899{
3900 struct Scsi_Host *host = instance->host;
3901
3902 /*
3903 * Export parameters required by SCSI mid-layer
3904 */
3905 host->irq = instance->pdev->irq;
3906 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003907 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3908 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3909 host->can_queue =
3910 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3911 } else
3912 host->can_queue =
3913 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003914 host->this_id = instance->init_id;
3915 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003916
3917 if (instance->fw_support_ieee)
3918 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3919
Yang, Bo1fd10682010-10-12 07:18:50 -06003920 /*
3921 * Check if the module parameter value for max_sectors can be used
3922 */
3923 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3924 instance->max_sectors_per_req = max_sectors;
3925 else {
3926 if (max_sectors) {
3927 if (((instance->pdev->device ==
3928 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3929 (instance->pdev->device ==
3930 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3931 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3932 instance->max_sectors_per_req = max_sectors;
3933 } else {
3934 printk(KERN_INFO "megasas: max_sectors should be > 0"
3935 "and <= %d (or < 1MB for GEN2 controller)\n",
3936 instance->max_sectors_per_req);
3937 }
3938 }
3939 }
3940
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003941 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003942 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003943 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3944 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3945 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003946 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003947
adam radford9c915a82010-12-21 13:34:31 -08003948 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07003949 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3950 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08003951 host->hostt->eh_device_reset_handler = NULL;
3952 host->hostt->eh_bus_reset_handler = NULL;
3953 }
3954
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003955 /*
3956 * Notify the mid-layer about the new controller
3957 */
3958 if (scsi_add_host(host, &instance->pdev->dev)) {
3959 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3960 return -ENODEV;
3961 }
3962
3963 /*
3964 * Trigger SCSI to scan our drives
3965 */
3966 scsi_scan_host(host);
3967 return 0;
3968}
3969
bo yang31ea7082007-11-07 12:09:50 -05003970static int
3971megasas_set_dma_mask(struct pci_dev *pdev)
3972{
3973 /*
3974 * All our contollers are capable of performing 64-bit DMA
3975 */
3976 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003977 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003978
Yang Hongyang284901a2009-04-06 19:01:15 -07003979 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003980 goto fail_set_dma_mask;
3981 }
3982 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003983 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003984 goto fail_set_dma_mask;
3985 }
3986 return 0;
3987
3988fail_set_dma_mask:
3989 return 1;
3990}
3991
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003992/**
3993 * megasas_probe_one - PCI hotplug entry point
3994 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003995 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003996 */
3997static int __devinit
3998megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3999{
adam radfordc8e858f2011-10-08 18:15:13 -07004000 int rval, pos, i, j;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004001 struct Scsi_Host *host;
4002 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08004003 u16 control = 0;
4004
4005 /* Reset MSI-X in the kdump kernel */
4006 if (reset_devices) {
4007 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
4008 if (pos) {
4009 pci_read_config_word(pdev, msi_control_reg(pos),
4010 &control);
4011 if (control & PCI_MSIX_FLAGS_ENABLE) {
4012 dev_info(&pdev->dev, "resetting MSI-X\n");
4013 pci_write_config_word(pdev,
4014 msi_control_reg(pos),
4015 control &
4016 ~PCI_MSIX_FLAGS_ENABLE);
4017 }
4018 }
4019 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004020
4021 /*
4022 * Announce PCI information
4023 */
4024 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4025 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4026 pdev->subsystem_device);
4027
4028 printk("bus %d:slot %d:func %d\n",
4029 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4030
4031 /*
4032 * PCI prepping: enable device set bus mastering and dma mask
4033 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004034 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004035
4036 if (rval) {
4037 return rval;
4038 }
4039
4040 pci_set_master(pdev);
4041
bo yang31ea7082007-11-07 12:09:50 -05004042 if (megasas_set_dma_mask(pdev))
4043 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004044
4045 host = scsi_host_alloc(&megasas_template,
4046 sizeof(struct megasas_instance));
4047
4048 if (!host) {
4049 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4050 goto fail_alloc_instance;
4051 }
4052
4053 instance = (struct megasas_instance *)host->hostdata;
4054 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04004055 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08004056 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004057
adam radford9c915a82010-12-21 13:34:31 -08004058 switch (instance->pdev->device) {
4059 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004060 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004061 {
4062 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004063
adam radford9c915a82010-12-21 13:34:31 -08004064 instance->ctrl_context =
4065 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4066 if (!instance->ctrl_context) {
4067 printk(KERN_DEBUG "megasas: Failed to allocate "
4068 "memory for Fusion context info\n");
4069 goto fail_alloc_dma_buf;
4070 }
4071 fusion = instance->ctrl_context;
4072 INIT_LIST_HEAD(&fusion->cmd_pool);
4073 spin_lock_init(&fusion->cmd_pool_lock);
4074 }
4075 break;
4076 default: /* For all other supported controllers */
4077
4078 instance->producer =
4079 pci_alloc_consistent(pdev, sizeof(u32),
4080 &instance->producer_h);
4081 instance->consumer =
4082 pci_alloc_consistent(pdev, sizeof(u32),
4083 &instance->consumer_h);
4084
4085 if (!instance->producer || !instance->consumer) {
4086 printk(KERN_DEBUG "megasas: Failed to allocate"
4087 "memory for producer, consumer\n");
4088 goto fail_alloc_dma_buf;
4089 }
4090
4091 *instance->producer = 0;
4092 *instance->consumer = 0;
4093 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004094 }
4095
Yang, Boc3518832009-10-06 14:18:02 -06004096 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004097 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004098 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004099 instance->issuepend_done = 1;
4100 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4101 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004102
4103 instance->evt_detail = pci_alloc_consistent(pdev,
4104 sizeof(struct
4105 megasas_evt_detail),
4106 &instance->evt_detail_h);
4107
4108 if (!instance->evt_detail) {
4109 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4110 "event detail structure\n");
4111 goto fail_alloc_dma_buf;
4112 }
4113
4114 /*
4115 * Initialize locks and queues
4116 */
4117 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004118 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004119
Sumant Patroe4a082c2006-05-30 12:03:37 -07004120 atomic_set(&instance->fw_outstanding,0);
4121
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004122 init_waitqueue_head(&instance->int_cmd_wait_q);
4123 init_waitqueue_head(&instance->abort_cmd_wait_q);
4124
4125 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004126 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004127 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06004128 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004129
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004130 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004131 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004132
4133 /*
4134 * Initialize PCI related and misc parameters
4135 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004136 instance->host = host;
4137 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4138 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4139
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004140 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4141 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004142 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004143 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4144 } else
4145 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4146
Sumant Patro658dced2006-10-03 13:09:14 -07004147 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004148 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004149 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004150 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004151 instance->disableOnlineCtrlReset = 1;
4152
adam radford36807e62011-10-08 18:15:06 -07004153 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4154 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08004155 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4156 else
4157 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004158
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004159 /*
adam radford0a770662011-02-24 20:56:12 -08004160 * Initialize MFI Firmware
4161 */
4162 if (megasas_init_fw(instance))
4163 goto fail_init_mfi;
4164
4165 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004166 * Register IRQ
4167 */
adam radfordc8e858f2011-10-08 18:15:13 -07004168 if (instance->msix_vectors) {
4169 for (i = 0 ; i < instance->msix_vectors; i++) {
4170 instance->irq_context[i].instance = instance;
4171 instance->irq_context[i].MSIxIndex = i;
4172 if (request_irq(instance->msixentry[i].vector,
4173 instance->instancet->service_isr, 0,
4174 "megasas",
4175 &instance->irq_context[i])) {
4176 printk(KERN_DEBUG "megasas: Failed to "
4177 "register IRQ for vector %d.\n", i);
4178 for (j = 0 ; j < i ; j++)
4179 free_irq(
4180 instance->msixentry[j].vector,
4181 &instance->irq_context[j]);
4182 goto fail_irq;
4183 }
4184 }
4185 } else {
4186 instance->irq_context[0].instance = instance;
4187 instance->irq_context[0].MSIxIndex = 0;
4188 if (request_irq(pdev->irq, instance->instancet->service_isr,
4189 IRQF_SHARED, "megasas",
4190 &instance->irq_context[0])) {
4191 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4192 goto fail_irq;
4193 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004194 }
4195
Sumant Patro1341c932006-01-25 12:02:40 -08004196 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004197
4198 /*
4199 * Store instance in PCI softstate
4200 */
4201 pci_set_drvdata(pdev, instance);
4202
4203 /*
4204 * Add this controller to megasas_mgmt_info structure so that it
4205 * can be exported to management applications
4206 */
4207 megasas_mgmt_info.count++;
4208 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4209 megasas_mgmt_info.max_index++;
4210
4211 /*
adam radford541f90b2011-05-11 18:34:29 -07004212 * Register with SCSI mid-layer
4213 */
4214 if (megasas_io_attach(instance))
4215 goto fail_io_attach;
4216
4217 instance->unload = 0;
4218
4219 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004220 * Initiate AEN (Asynchronous Event Notification)
4221 */
4222 if (megasas_start_aen(instance)) {
4223 printk(KERN_DEBUG "megasas: start aen failed\n");
4224 goto fail_start_aen;
4225 }
4226
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004227 return 0;
4228
4229 fail_start_aen:
4230 fail_io_attach:
4231 megasas_mgmt_info.count--;
4232 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4233 megasas_mgmt_info.max_index--;
4234
4235 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004236 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004237 if (instance->msix_vectors)
4238 for (i = 0 ; i < instance->msix_vectors; i++)
4239 free_irq(instance->msixentry[i].vector,
4240 &instance->irq_context[i]);
4241 else
4242 free_irq(instance->pdev->irq, &instance->irq_context[0]);
adam radfordeb1b1232011-02-24 20:55:56 -08004243fail_irq:
adam radford36807e62011-10-08 18:15:06 -07004244 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4245 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radfordeb1b1232011-02-24 20:55:56 -08004246 megasas_release_fusion(instance);
4247 else
4248 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004249 fail_init_mfi:
adam radfordc8e858f2011-10-08 18:15:13 -07004250 if (instance->msix_vectors)
adam radford0a770662011-02-24 20:56:12 -08004251 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004252 fail_alloc_dma_buf:
4253 if (instance->evt_detail)
4254 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4255 instance->evt_detail,
4256 instance->evt_detail_h);
4257
adam radfordeb1b1232011-02-24 20:55:56 -08004258 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004259 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4260 instance->producer_h);
4261 if (instance->consumer)
4262 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4263 instance->consumer_h);
4264 scsi_host_put(host);
4265
4266 fail_alloc_instance:
4267 fail_set_dma_mask:
4268 pci_disable_device(pdev);
4269
4270 return -ENODEV;
4271}
4272
4273/**
4274 * megasas_flush_cache - Requests FW to flush all its caches
4275 * @instance: Adapter soft state
4276 */
4277static void megasas_flush_cache(struct megasas_instance *instance)
4278{
4279 struct megasas_cmd *cmd;
4280 struct megasas_dcmd_frame *dcmd;
4281
bo yang39a98552010-09-22 22:36:29 -04004282 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4283 return;
4284
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004285 cmd = megasas_get_cmd(instance);
4286
4287 if (!cmd)
4288 return;
4289
4290 dcmd = &cmd->frame->dcmd;
4291
4292 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4293
4294 dcmd->cmd = MFI_CMD_DCMD;
4295 dcmd->cmd_status = 0x0;
4296 dcmd->sge_count = 0;
4297 dcmd->flags = MFI_FRAME_DIR_NONE;
4298 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004299 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004300 dcmd->data_xfer_len = 0;
4301 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4302 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4303
4304 megasas_issue_blocked_cmd(instance, cmd);
4305
4306 megasas_return_cmd(instance, cmd);
4307
4308 return;
4309}
4310
4311/**
4312 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4313 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004314 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004315 */
bo yang31ea7082007-11-07 12:09:50 -05004316static void megasas_shutdown_controller(struct megasas_instance *instance,
4317 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004318{
4319 struct megasas_cmd *cmd;
4320 struct megasas_dcmd_frame *dcmd;
4321
bo yang39a98552010-09-22 22:36:29 -04004322 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4323 return;
4324
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004325 cmd = megasas_get_cmd(instance);
4326
4327 if (!cmd)
4328 return;
4329
4330 if (instance->aen_cmd)
4331 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004332 if (instance->map_update_cmd)
4333 megasas_issue_blocked_abort_cmd(instance,
4334 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004335 dcmd = &cmd->frame->dcmd;
4336
4337 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4338
4339 dcmd->cmd = MFI_CMD_DCMD;
4340 dcmd->cmd_status = 0x0;
4341 dcmd->sge_count = 0;
4342 dcmd->flags = MFI_FRAME_DIR_NONE;
4343 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004344 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004345 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004346 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004347
4348 megasas_issue_blocked_cmd(instance, cmd);
4349
4350 megasas_return_cmd(instance, cmd);
4351
4352 return;
4353}
4354
Jiri Slaby33139b22008-05-01 17:56:02 +02004355#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004356/**
bo yangad84db22007-11-09 04:40:16 -05004357 * megasas_suspend - driver suspend entry point
4358 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004359 * @state: PCI power state to suspend routine
4360 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004361static int
bo yang31ea7082007-11-07 12:09:50 -05004362megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4363{
4364 struct Scsi_Host *host;
4365 struct megasas_instance *instance;
adam radfordc8e858f2011-10-08 18:15:13 -07004366 int i;
bo yang31ea7082007-11-07 12:09:50 -05004367
4368 instance = pci_get_drvdata(pdev);
4369 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004370 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004371
bo yangad84db22007-11-09 04:40:16 -05004372 if (poll_mode_io)
4373 del_timer_sync(&instance->io_completion_timer);
4374
bo yang31ea7082007-11-07 12:09:50 -05004375 megasas_flush_cache(instance);
4376 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004377
4378 /* cancel the delayed work if this work still in queue */
4379 if (instance->ev != NULL) {
4380 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004381 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004382 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004383 instance->ev = NULL;
4384 }
4385
bo yang31ea7082007-11-07 12:09:50 -05004386 tasklet_kill(&instance->isr_tasklet);
4387
4388 pci_set_drvdata(instance->pdev, instance);
4389 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004390
4391 if (instance->msix_vectors)
4392 for (i = 0 ; i < instance->msix_vectors; i++)
4393 free_irq(instance->msixentry[i].vector,
4394 &instance->irq_context[i]);
4395 else
4396 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4397 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004398 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004399
4400 pci_save_state(pdev);
4401 pci_disable_device(pdev);
4402
4403 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4404
4405 return 0;
4406}
4407
4408/**
4409 * megasas_resume- driver resume entry point
4410 * @pdev: PCI device structure
4411 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004412static int
bo yang31ea7082007-11-07 12:09:50 -05004413megasas_resume(struct pci_dev *pdev)
4414{
adam radfordc8e858f2011-10-08 18:15:13 -07004415 int rval, i, j;
bo yang31ea7082007-11-07 12:09:50 -05004416 struct Scsi_Host *host;
4417 struct megasas_instance *instance;
4418
4419 instance = pci_get_drvdata(pdev);
4420 host = instance->host;
4421 pci_set_power_state(pdev, PCI_D0);
4422 pci_enable_wake(pdev, PCI_D0, 0);
4423 pci_restore_state(pdev);
4424
4425 /*
4426 * PCI prepping: enable device set bus mastering and dma mask
4427 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004428 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004429
4430 if (rval) {
4431 printk(KERN_ERR "megasas: Enable device failed\n");
4432 return rval;
4433 }
4434
4435 pci_set_master(pdev);
4436
4437 if (megasas_set_dma_mask(pdev))
4438 goto fail_set_dma_mask;
4439
4440 /*
4441 * Initialize MFI Firmware
4442 */
4443
bo yang31ea7082007-11-07 12:09:50 -05004444 atomic_set(&instance->fw_outstanding, 0);
4445
4446 /*
4447 * We expect the FW state to be READY
4448 */
adam radford058a8fa2011-10-08 18:14:27 -07004449 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05004450 goto fail_ready_state;
4451
adam radford3f1abce2011-05-11 18:33:47 -07004452 /* Now re-enable MSI-X */
adam radfordc8e858f2011-10-08 18:15:13 -07004453 if (instance->msix_vectors)
4454 pci_enable_msix(instance->pdev, instance->msixentry,
4455 instance->msix_vectors);
adam radford3f1abce2011-05-11 18:33:47 -07004456
adam radford9c915a82010-12-21 13:34:31 -08004457 switch (instance->pdev->device) {
4458 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004459 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004460 {
4461 megasas_reset_reply_desc(instance);
4462 if (megasas_ioc_init_fusion(instance)) {
4463 megasas_free_cmds(instance);
4464 megasas_free_cmds_fusion(instance);
4465 goto fail_init_mfi;
4466 }
4467 if (!megasas_get_map_info(instance))
4468 megasas_sync_map_info(instance);
4469 }
4470 break;
4471 default:
4472 *instance->producer = 0;
4473 *instance->consumer = 0;
4474 if (megasas_issue_init_mfi(instance))
4475 goto fail_init_mfi;
4476 break;
4477 }
bo yang31ea7082007-11-07 12:09:50 -05004478
adam radford9c915a82010-12-21 13:34:31 -08004479 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4480 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004481
4482 /*
4483 * Register IRQ
4484 */
adam radfordc8e858f2011-10-08 18:15:13 -07004485 if (instance->msix_vectors) {
4486 for (i = 0 ; i < instance->msix_vectors; i++) {
4487 instance->irq_context[i].instance = instance;
4488 instance->irq_context[i].MSIxIndex = i;
4489 if (request_irq(instance->msixentry[i].vector,
4490 instance->instancet->service_isr, 0,
4491 "megasas",
4492 &instance->irq_context[i])) {
4493 printk(KERN_DEBUG "megasas: Failed to "
4494 "register IRQ for vector %d.\n", i);
4495 for (j = 0 ; j < i ; j++)
4496 free_irq(
4497 instance->msixentry[j].vector,
4498 &instance->irq_context[j]);
4499 goto fail_irq;
4500 }
4501 }
4502 } else {
4503 instance->irq_context[0].instance = instance;
4504 instance->irq_context[0].MSIxIndex = 0;
4505 if (request_irq(pdev->irq, instance->instancet->service_isr,
4506 IRQF_SHARED, "megasas",
4507 &instance->irq_context[0])) {
4508 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4509 goto fail_irq;
4510 }
bo yang31ea7082007-11-07 12:09:50 -05004511 }
4512
4513 instance->instancet->enable_intr(instance->reg_set);
4514
bo yangad84db22007-11-09 04:40:16 -05004515 /* Initialize the cmd completion timer */
4516 if (poll_mode_io)
4517 megasas_start_timer(instance, &instance->io_completion_timer,
4518 megasas_io_completion_timer,
4519 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06004520 instance->unload = 0;
4521
adam radford541f90b2011-05-11 18:34:29 -07004522 /*
4523 * Initiate AEN (Asynchronous Event Notification)
4524 */
4525 if (megasas_start_aen(instance))
4526 printk(KERN_ERR "megasas: Start AEN failed\n");
4527
bo yang31ea7082007-11-07 12:09:50 -05004528 return 0;
4529
4530fail_irq:
4531fail_init_mfi:
4532 if (instance->evt_detail)
4533 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4534 instance->evt_detail,
4535 instance->evt_detail_h);
4536
4537 if (instance->producer)
4538 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4539 instance->producer_h);
4540 if (instance->consumer)
4541 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4542 instance->consumer_h);
4543 scsi_host_put(host);
4544
4545fail_set_dma_mask:
4546fail_ready_state:
4547
4548 pci_disable_device(pdev);
4549
4550 return -ENODEV;
4551}
Jiri Slaby33139b22008-05-01 17:56:02 +02004552#else
4553#define megasas_suspend NULL
4554#define megasas_resume NULL
4555#endif
bo yang31ea7082007-11-07 12:09:50 -05004556
4557/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004558 * megasas_detach_one - PCI hot"un"plug entry point
4559 * @pdev: PCI device structure
4560 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004561static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004562{
4563 int i;
4564 struct Scsi_Host *host;
4565 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004566 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004567
4568 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004569 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004570 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004571 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004572
bo yangad84db22007-11-09 04:40:16 -05004573 if (poll_mode_io)
4574 del_timer_sync(&instance->io_completion_timer);
4575
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004576 scsi_remove_host(instance->host);
4577 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004578 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004579
4580 /* cancel the delayed work if this work still in queue*/
4581 if (instance->ev != NULL) {
4582 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004583 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004584 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004585 instance->ev = NULL;
4586 }
4587
Sumant Patro5d018ad2006-10-03 13:13:18 -07004588 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004589
4590 /*
4591 * Take the instance off the instance array. Note that we will not
4592 * decrement the max_index. We let this array be sparse array
4593 */
4594 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4595 if (megasas_mgmt_info.instance[i] == instance) {
4596 megasas_mgmt_info.count--;
4597 megasas_mgmt_info.instance[i] = NULL;
4598
4599 break;
4600 }
4601 }
4602
4603 pci_set_drvdata(instance->pdev, NULL);
4604
Sumant Patrob274cab2006-10-03 12:52:12 -07004605 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004606
adam radfordc8e858f2011-10-08 18:15:13 -07004607 if (instance->msix_vectors)
4608 for (i = 0 ; i < instance->msix_vectors; i++)
4609 free_irq(instance->msixentry[i].vector,
4610 &instance->irq_context[i]);
4611 else
4612 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4613 if (instance->msix_vectors)
adam radford80d9da92010-12-21 10:17:40 -08004614 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004615
adam radford9c915a82010-12-21 13:34:31 -08004616 switch (instance->pdev->device) {
4617 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004618 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004619 megasas_release_fusion(instance);
4620 for (i = 0; i < 2 ; i++)
4621 if (fusion->ld_map[i])
4622 dma_free_coherent(&instance->pdev->dev,
4623 fusion->map_sz,
4624 fusion->ld_map[i],
4625 fusion->
4626 ld_map_phys[i]);
4627 kfree(instance->ctrl_context);
4628 break;
4629 default:
4630 megasas_release_mfi(instance);
4631 pci_free_consistent(pdev,
4632 sizeof(struct megasas_evt_detail),
4633 instance->evt_detail,
4634 instance->evt_detail_h);
4635 pci_free_consistent(pdev, sizeof(u32),
4636 instance->producer,
4637 instance->producer_h);
4638 pci_free_consistent(pdev, sizeof(u32),
4639 instance->consumer,
4640 instance->consumer_h);
4641 break;
4642 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004643
4644 scsi_host_put(host);
4645
4646 pci_set_drvdata(pdev, NULL);
4647
4648 pci_disable_device(pdev);
4649
4650 return;
4651}
4652
4653/**
4654 * megasas_shutdown - Shutdown entry point
4655 * @device: Generic device structure
4656 */
4657static void megasas_shutdown(struct pci_dev *pdev)
4658{
adam radfordc8e858f2011-10-08 18:15:13 -07004659 int i;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004660 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07004661
Yang, Bo0c79e682009-10-06 14:47:35 -06004662 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004663 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004664 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
adam radford46fd2562011-05-11 18:34:17 -07004665 instance->instancet->disable_intr(instance->reg_set);
adam radfordc8e858f2011-10-08 18:15:13 -07004666 if (instance->msix_vectors)
4667 for (i = 0 ; i < instance->msix_vectors; i++)
4668 free_irq(instance->msixentry[i].vector,
4669 &instance->irq_context[i]);
4670 else
4671 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4672 if (instance->msix_vectors)
adam radford46fd2562011-05-11 18:34:17 -07004673 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004674}
4675
4676/**
4677 * megasas_mgmt_open - char node "open" entry point
4678 */
4679static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4680{
4681 /*
4682 * Allow only those users with admin rights
4683 */
4684 if (!capable(CAP_SYS_ADMIN))
4685 return -EACCES;
4686
4687 return 0;
4688}
4689
4690/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004691 * megasas_mgmt_fasync - Async notifier registration from applications
4692 *
4693 * This function adds the calling process to a driver global queue. When an
4694 * event occurs, SIGIO will be sent to all processes in this queue.
4695 */
4696static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4697{
4698 int rc;
4699
Arjan van de Ven0b950672006-01-11 13:16:10 +01004700 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004701
4702 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4703
Arjan van de Ven0b950672006-01-11 13:16:10 +01004704 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004705
4706 if (rc >= 0) {
4707 /* For sanity check when we get ioctl */
4708 filep->private_data = filep;
4709 return 0;
4710 }
4711
4712 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4713
4714 return rc;
4715}
4716
4717/**
Yang, Boc3518832009-10-06 14:18:02 -06004718 * megasas_mgmt_poll - char node "poll" entry point
4719 * */
4720static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4721{
4722 unsigned int mask;
4723 unsigned long flags;
4724 poll_wait(file, &megasas_poll_wait, wait);
4725 spin_lock_irqsave(&poll_aen_lock, flags);
4726 if (megasas_poll_wait_aen)
4727 mask = (POLLIN | POLLRDNORM);
4728 else
4729 mask = 0;
4730 spin_unlock_irqrestore(&poll_aen_lock, flags);
4731 return mask;
4732}
4733
4734/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004735 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4736 * @instance: Adapter soft state
4737 * @argp: User's ioctl packet
4738 */
4739static int
4740megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4741 struct megasas_iocpacket __user * user_ioc,
4742 struct megasas_iocpacket *ioc)
4743{
4744 struct megasas_sge32 *kern_sge32;
4745 struct megasas_cmd *cmd;
4746 void *kbuff_arr[MAX_IOCTL_SGE];
4747 dma_addr_t buf_handle = 0;
4748 int error = 0, i;
4749 void *sense = NULL;
4750 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004751 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004752
4753 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4754
4755 if (ioc->sge_count > MAX_IOCTL_SGE) {
4756 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4757 ioc->sge_count, MAX_IOCTL_SGE);
4758 return -EINVAL;
4759 }
4760
4761 cmd = megasas_get_cmd(instance);
4762 if (!cmd) {
4763 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4764 return -ENOMEM;
4765 }
4766
4767 /*
4768 * User's IOCTL packet has 2 frames (maximum). Copy those two
4769 * frames into our cmd's frames. cmd->frame's context will get
4770 * overwritten when we copy from user's frames. So set that value
4771 * alone separately
4772 */
4773 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4774 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004775 cmd->frame->hdr.pad_0 = 0;
adam radford882be7c32012-01-06 17:02:34 -08004776 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4777 MFI_FRAME_SENSE64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004778
4779 /*
4780 * The management interface between applications and the fw uses
4781 * MFI frames. E.g, RAID configuration changes, LD property changes
4782 * etc are accomplishes through different kinds of MFI frames. The
4783 * driver needs to care only about substituting user buffers with
4784 * kernel buffers in SGLs. The location of SGL is embedded in the
4785 * struct iocpacket itself.
4786 */
4787 kern_sge32 = (struct megasas_sge32 *)
4788 ((unsigned long)cmd->frame + ioc->sgl_off);
4789
4790 /*
4791 * For each user buffer, create a mirror buffer and copy in
4792 */
4793 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004794 if (!ioc->sgl[i].iov_len)
4795 continue;
4796
Sumant Patro9f35fa82007-02-14 12:55:45 -08004797 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004798 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004799 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004800 if (!kbuff_arr[i]) {
4801 printk(KERN_DEBUG "megasas: Failed to alloc "
4802 "kernel SGL buffer for IOCTL \n");
4803 error = -ENOMEM;
4804 goto out;
4805 }
4806
4807 /*
4808 * We don't change the dma_coherent_mask, so
4809 * pci_alloc_consistent only returns 32bit addresses
4810 */
4811 kern_sge32[i].phys_addr = (u32) buf_handle;
4812 kern_sge32[i].length = ioc->sgl[i].iov_len;
4813
4814 /*
4815 * We created a kernel buffer corresponding to the
4816 * user buffer. Now copy in from the user buffer
4817 */
4818 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4819 (u32) (ioc->sgl[i].iov_len))) {
4820 error = -EFAULT;
4821 goto out;
4822 }
4823 }
4824
4825 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004826 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4827 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004828 if (!sense) {
4829 error = -ENOMEM;
4830 goto out;
4831 }
4832
4833 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004834 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004835 *sense_ptr = sense_handle;
4836 }
4837
4838 /*
4839 * Set the sync_cmd flag so that the ISR knows not to complete this
4840 * cmd to the SCSI mid-layer
4841 */
4842 cmd->sync_cmd = 1;
4843 megasas_issue_blocked_cmd(instance, cmd);
4844 cmd->sync_cmd = 0;
4845
4846 /*
4847 * copy out the kernel buffers to user buffers
4848 */
4849 for (i = 0; i < ioc->sge_count; i++) {
4850 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4851 ioc->sgl[i].iov_len)) {
4852 error = -EFAULT;
4853 goto out;
4854 }
4855 }
4856
4857 /*
4858 * copy out the sense
4859 */
4860 if (ioc->sense_len) {
4861 /*
bo yangb70a41e2008-03-18 03:13:06 -04004862 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004863 * sense buffer address
4864 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004865 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4866 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004867
bo yangb70a41e2008-03-18 03:13:06 -04004868 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4869 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004870 printk(KERN_ERR "megasas: Failed to copy out to user "
4871 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004872 error = -EFAULT;
4873 goto out;
4874 }
4875 }
4876
4877 /*
4878 * copy the status codes returned by the fw
4879 */
4880 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4881 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4882 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4883 error = -EFAULT;
4884 }
4885
4886 out:
4887 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004888 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004889 sense, sense_handle);
4890 }
4891
4892 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004893 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004894 kern_sge32[i].length,
4895 kbuff_arr[i], kern_sge32[i].phys_addr);
4896 }
4897
4898 megasas_return_cmd(instance, cmd);
4899 return error;
4900}
4901
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004902static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4903{
4904 struct megasas_iocpacket __user *user_ioc =
4905 (struct megasas_iocpacket __user *)arg;
4906 struct megasas_iocpacket *ioc;
4907 struct megasas_instance *instance;
4908 int error;
bo yang39a98552010-09-22 22:36:29 -04004909 int i;
4910 unsigned long flags;
4911 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004912
4913 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4914 if (!ioc)
4915 return -ENOMEM;
4916
4917 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4918 error = -EFAULT;
4919 goto out_kfree_ioc;
4920 }
4921
4922 instance = megasas_lookup_instance(ioc->host_no);
4923 if (!instance) {
4924 error = -ENODEV;
4925 goto out_kfree_ioc;
4926 }
4927
bo yang39a98552010-09-22 22:36:29 -04004928 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4929 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004930 error = -ENODEV;
4931 goto out_kfree_ioc;
4932 }
4933
4934 if (instance->unload == 1) {
4935 error = -ENODEV;
4936 goto out_kfree_ioc;
4937 }
4938
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004939 /*
4940 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4941 */
4942 if (down_interruptible(&instance->ioctl_sem)) {
4943 error = -ERESTARTSYS;
4944 goto out_kfree_ioc;
4945 }
bo yang39a98552010-09-22 22:36:29 -04004946
4947 for (i = 0; i < wait_time; i++) {
4948
4949 spin_lock_irqsave(&instance->hba_lock, flags);
4950 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4951 spin_unlock_irqrestore(&instance->hba_lock, flags);
4952 break;
4953 }
4954 spin_unlock_irqrestore(&instance->hba_lock, flags);
4955
4956 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4957 printk(KERN_NOTICE "megasas: waiting"
4958 "for controller reset to finish\n");
4959 }
4960
4961 msleep(1000);
4962 }
4963
4964 spin_lock_irqsave(&instance->hba_lock, flags);
4965 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4966 spin_unlock_irqrestore(&instance->hba_lock, flags);
4967
4968 printk(KERN_ERR "megaraid_sas: timed out while"
4969 "waiting for HBA to recover\n");
4970 error = -ENODEV;
4971 goto out_kfree_ioc;
4972 }
4973 spin_unlock_irqrestore(&instance->hba_lock, flags);
4974
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004975 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4976 up(&instance->ioctl_sem);
4977
4978 out_kfree_ioc:
4979 kfree(ioc);
4980 return error;
4981}
4982
4983static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4984{
4985 struct megasas_instance *instance;
4986 struct megasas_aen aen;
4987 int error;
bo yang39a98552010-09-22 22:36:29 -04004988 int i;
4989 unsigned long flags;
4990 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004991
4992 if (file->private_data != file) {
4993 printk(KERN_DEBUG "megasas: fasync_helper was not "
4994 "called first\n");
4995 return -EINVAL;
4996 }
4997
4998 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4999 return -EFAULT;
5000
5001 instance = megasas_lookup_instance(aen.host_no);
5002
5003 if (!instance)
5004 return -ENODEV;
5005
bo yang39a98552010-09-22 22:36:29 -04005006 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
5007 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06005008 }
5009
5010 if (instance->unload == 1) {
5011 return -ENODEV;
5012 }
5013
bo yang39a98552010-09-22 22:36:29 -04005014 for (i = 0; i < wait_time; i++) {
5015
5016 spin_lock_irqsave(&instance->hba_lock, flags);
5017 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
5018 spin_unlock_irqrestore(&instance->hba_lock,
5019 flags);
5020 break;
5021 }
5022
5023 spin_unlock_irqrestore(&instance->hba_lock, flags);
5024
5025 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5026 printk(KERN_NOTICE "megasas: waiting for"
5027 "controller reset to finish\n");
5028 }
5029
5030 msleep(1000);
5031 }
5032
5033 spin_lock_irqsave(&instance->hba_lock, flags);
5034 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5035 spin_unlock_irqrestore(&instance->hba_lock, flags);
5036 printk(KERN_ERR "megaraid_sas: timed out while waiting"
5037 "for HBA to recover.\n");
5038 return -ENODEV;
5039 }
5040 spin_unlock_irqrestore(&instance->hba_lock, flags);
5041
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005042 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005043 error = megasas_register_aen(instance, aen.seq_num,
5044 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02005045 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005046 return error;
5047}
5048
5049/**
5050 * megasas_mgmt_ioctl - char node ioctl entry point
5051 */
5052static long
5053megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5054{
5055 switch (cmd) {
5056 case MEGASAS_IOC_FIRMWARE:
5057 return megasas_mgmt_ioctl_fw(file, arg);
5058
5059 case MEGASAS_IOC_GET_AEN:
5060 return megasas_mgmt_ioctl_aen(file, arg);
5061 }
5062
5063 return -ENOTTY;
5064}
5065
5066#ifdef CONFIG_COMPAT
5067static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5068{
5069 struct compat_megasas_iocpacket __user *cioc =
5070 (struct compat_megasas_iocpacket __user *)arg;
5071 struct megasas_iocpacket __user *ioc =
5072 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5073 int i;
5074 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005075 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005076
Jeff Garzik83aabc12006-10-04 06:34:03 -04005077 if (clear_user(ioc, sizeof(*ioc)))
5078 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005079
5080 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5081 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5082 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5083 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5084 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5085 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5086 return -EFAULT;
5087
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005088 /*
5089 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5090 * sense_len is not null, so prepare the 64bit value under
5091 * the same condition.
5092 */
5093 if (ioc->sense_len) {
5094 void __user **sense_ioc_ptr =
5095 (void __user **)(ioc->frame.raw + ioc->sense_off);
5096 compat_uptr_t *sense_cioc_ptr =
5097 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5098 if (get_user(ptr, sense_cioc_ptr) ||
5099 put_user(compat_ptr(ptr), sense_ioc_ptr))
5100 return -EFAULT;
5101 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005102
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01005103 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005104 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5105 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5106 copy_in_user(&ioc->sgl[i].iov_len,
5107 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5108 return -EFAULT;
5109 }
5110
5111 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5112
5113 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5114 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5115 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5116 return -EFAULT;
5117 }
5118 return error;
5119}
5120
5121static long
5122megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5123 unsigned long arg)
5124{
5125 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08005126 case MEGASAS_IOC_FIRMWARE32:
5127 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005128 case MEGASAS_IOC_GET_AEN:
5129 return megasas_mgmt_ioctl_aen(file, arg);
5130 }
5131
5132 return -ENOTTY;
5133}
5134#endif
5135
5136/*
5137 * File operations structure for management interface
5138 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08005139static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005140 .owner = THIS_MODULE,
5141 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005142 .fasync = megasas_mgmt_fasync,
5143 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005144 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005145#ifdef CONFIG_COMPAT
5146 .compat_ioctl = megasas_mgmt_compat_ioctl,
5147#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005148 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005149};
5150
5151/*
5152 * PCI hotplug support registration structure
5153 */
5154static struct pci_driver megasas_pci_driver = {
5155
5156 .name = "megaraid_sas",
5157 .id_table = megasas_pci_table,
5158 .probe = megasas_probe_one,
5159 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05005160 .suspend = megasas_suspend,
5161 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005162 .shutdown = megasas_shutdown,
5163};
5164
5165/*
5166 * Sysfs driver attributes
5167 */
5168static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5169{
5170 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5171 MEGASAS_VERSION);
5172}
5173
5174static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5175
5176static ssize_t
5177megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5178{
5179 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5180 MEGASAS_RELDATE);
5181}
5182
5183static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5184 NULL);
5185
Sumant Patro658dced2006-10-03 13:09:14 -07005186static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005187megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5188{
5189 return sprintf(buf, "%u\n", support_poll_for_event);
5190}
5191
5192static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5193 megasas_sysfs_show_support_poll_for_event, NULL);
5194
Yang, Bo837f5fe2010-10-11 06:59:20 -06005195 static ssize_t
5196megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5197{
5198 return sprintf(buf, "%u\n", support_device_change);
5199}
5200
5201static DRIVER_ATTR(support_device_change, S_IRUGO,
5202 megasas_sysfs_show_support_device_change, NULL);
5203
Yang, Bo72c4fd32009-10-06 14:20:59 -06005204static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005205megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5206{
bo yangad84db22007-11-09 04:40:16 -05005207 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005208}
5209
5210static ssize_t
5211megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5212{
5213 int retval = count;
5214 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5215 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5216 retval = -EINVAL;
5217 }
5218 return retval;
5219}
5220
Joe Malicki66dca9b2008-08-14 17:14:48 -04005221static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005222 megasas_sysfs_set_dbg_lvl);
5223
5224static ssize_t
5225megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
5226{
5227 return sprintf(buf, "%u\n", poll_mode_io);
5228}
5229
5230static ssize_t
5231megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
5232 const char *buf, size_t count)
5233{
5234 int retval = count;
5235 int tmp = poll_mode_io;
5236 int i;
5237 struct megasas_instance *instance;
5238
5239 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
5240 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
5241 retval = -EINVAL;
5242 }
5243
5244 /*
5245 * Check if poll_mode_io is already set or is same as previous value
5246 */
5247 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
5248 goto out;
5249
5250 if (poll_mode_io) {
5251 /*
5252 * Start timers for all adapters
5253 */
5254 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5255 instance = megasas_mgmt_info.instance[i];
5256 if (instance) {
5257 megasas_start_timer(instance,
5258 &instance->io_completion_timer,
5259 megasas_io_completion_timer,
5260 MEGASAS_COMPLETION_TIMER_INTERVAL);
5261 }
5262 }
5263 } else {
5264 /*
5265 * Delete timers for all adapters
5266 */
5267 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5268 instance = megasas_mgmt_info.instance[i];
5269 if (instance)
5270 del_timer_sync(&instance->io_completion_timer);
5271 }
5272 }
5273
5274out:
5275 return retval;
5276}
5277
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005278static void
5279megasas_aen_polling(struct work_struct *work)
5280{
5281 struct megasas_aen_event *ev =
5282 container_of(work, struct megasas_aen_event, hotplug_work);
5283 struct megasas_instance *instance = ev->instance;
5284 union megasas_evt_class_locale class_locale;
5285 struct Scsi_Host *host;
5286 struct scsi_device *sdev1;
5287 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005288 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005289 int i, j, doscan = 0;
5290 u32 seq_num;
5291 int error;
5292
5293 if (!instance) {
5294 printk(KERN_ERR "invalid instance!\n");
5295 kfree(ev);
5296 return;
5297 }
5298 instance->ev = NULL;
5299 host = instance->host;
5300 if (instance->evt_detail) {
5301
5302 switch (instance->evt_detail->code) {
5303 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005304 if (megasas_get_pd_list(instance) == 0) {
5305 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5306 for (j = 0;
5307 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5308 j++) {
5309
5310 pd_index =
5311 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5312
5313 sdev1 =
5314 scsi_device_lookup(host, i, j, 0);
5315
5316 if (instance->pd_list[pd_index].driveState
5317 == MR_PD_STATE_SYSTEM) {
5318 if (!sdev1) {
5319 scsi_add_device(host, i, j, 0);
5320 }
5321
5322 if (sdev1)
5323 scsi_device_put(sdev1);
5324 }
5325 }
5326 }
5327 }
5328 doscan = 0;
5329 break;
5330
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005331 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005332 if (megasas_get_pd_list(instance) == 0) {
5333 megasas_get_pd_list(instance);
5334 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5335 for (j = 0;
5336 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5337 j++) {
5338
5339 pd_index =
5340 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5341
5342 sdev1 =
5343 scsi_device_lookup(host, i, j, 0);
5344
5345 if (instance->pd_list[pd_index].driveState
5346 == MR_PD_STATE_SYSTEM) {
5347 if (sdev1) {
5348 scsi_device_put(sdev1);
5349 }
5350 } else {
5351 if (sdev1) {
5352 scsi_remove_device(sdev1);
5353 scsi_device_put(sdev1);
5354 }
5355 }
5356 }
5357 }
5358 }
5359 doscan = 0;
5360 break;
5361
5362 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005363 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005364 case MR_EVT_LD_DELETED:
5365 megasas_get_ld_list(instance);
5366 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5367 for (j = 0;
5368 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5369 j++) {
5370
5371 ld_index =
5372 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5373
5374 sdev1 = scsi_device_lookup(host,
5375 i + MEGASAS_MAX_LD_CHANNELS,
5376 j,
5377 0);
5378
5379 if (instance->ld_ids[ld_index] != 0xff) {
5380 if (sdev1) {
5381 scsi_device_put(sdev1);
5382 }
5383 } else {
5384 if (sdev1) {
5385 scsi_remove_device(sdev1);
5386 scsi_device_put(sdev1);
5387 }
5388 }
5389 }
5390 }
5391 doscan = 0;
5392 break;
5393 case MR_EVT_LD_CREATED:
5394 megasas_get_ld_list(instance);
5395 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5396 for (j = 0;
5397 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5398 j++) {
5399 ld_index =
5400 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5401
5402 sdev1 = scsi_device_lookup(host,
5403 i+MEGASAS_MAX_LD_CHANNELS,
5404 j, 0);
5405
5406 if (instance->ld_ids[ld_index] !=
5407 0xff) {
5408 if (!sdev1) {
5409 scsi_add_device(host,
5410 i + 2,
5411 j, 0);
5412 }
5413 }
5414 if (sdev1) {
5415 scsi_device_put(sdev1);
5416 }
5417 }
5418 }
5419 doscan = 0;
5420 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005421 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005422 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005423 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005424 doscan = 1;
5425 break;
5426 default:
5427 doscan = 0;
5428 break;
5429 }
5430 } else {
5431 printk(KERN_ERR "invalid evt_detail!\n");
5432 kfree(ev);
5433 return;
5434 }
5435
5436 if (doscan) {
5437 printk(KERN_INFO "scanning ...\n");
5438 megasas_get_pd_list(instance);
5439 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5440 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5441 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5442 sdev1 = scsi_device_lookup(host, i, j, 0);
5443 if (instance->pd_list[pd_index].driveState ==
5444 MR_PD_STATE_SYSTEM) {
5445 if (!sdev1) {
5446 scsi_add_device(host, i, j, 0);
5447 }
5448 if (sdev1)
5449 scsi_device_put(sdev1);
5450 } else {
5451 if (sdev1) {
5452 scsi_remove_device(sdev1);
5453 scsi_device_put(sdev1);
5454 }
5455 }
5456 }
5457 }
Yang, Boc9786842009-12-06 08:39:25 -07005458
5459 megasas_get_ld_list(instance);
5460 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5461 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5462 ld_index =
5463 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5464
5465 sdev1 = scsi_device_lookup(host,
5466 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5467 if (instance->ld_ids[ld_index] != 0xff) {
5468 if (!sdev1) {
5469 scsi_add_device(host,
5470 i+2,
5471 j, 0);
5472 } else {
5473 scsi_device_put(sdev1);
5474 }
5475 } else {
5476 if (sdev1) {
5477 scsi_remove_device(sdev1);
5478 scsi_device_put(sdev1);
5479 }
5480 }
5481 }
5482 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005483 }
5484
5485 if ( instance->aen_cmd != NULL ) {
5486 kfree(ev);
5487 return ;
5488 }
5489
5490 seq_num = instance->evt_detail->seq_num + 1;
5491
5492 /* Register AEN with FW for latest sequence number plus 1 */
5493 class_locale.members.reserved = 0;
5494 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5495 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5496 mutex_lock(&instance->aen_mutex);
5497 error = megasas_register_aen(instance, seq_num,
5498 class_locale.word);
5499 mutex_unlock(&instance->aen_mutex);
5500
5501 if (error)
5502 printk(KERN_ERR "register aen failed error %x\n", error);
5503
5504 kfree(ev);
5505}
5506
5507
Bryn M. Reevesbb7d3f22009-11-12 18:31:54 +00005508static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
bo yangad84db22007-11-09 04:40:16 -05005509 megasas_sysfs_show_poll_mode_io,
5510 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07005511
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005512/**
5513 * megasas_init - Driver load entry point
5514 */
5515static int __init megasas_init(void)
5516{
5517 int rval;
5518
5519 /*
5520 * Announce driver version and other information
5521 */
5522 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5523 MEGASAS_EXT_VERSION);
5524
Yang, Bo72c4fd32009-10-06 14:20:59 -06005525 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005526 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005527
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005528 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5529
5530 /*
5531 * Register character device node
5532 */
5533 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5534
5535 if (rval < 0) {
5536 printk(KERN_DEBUG "megasas: failed to open device node\n");
5537 return rval;
5538 }
5539
5540 megasas_mgmt_majorno = rval;
5541
5542 /*
5543 * Register ourselves as PCI hotplug module
5544 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005545 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005546
5547 if (rval) {
5548 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005549 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005550 }
5551
Jeff Garzik83aabc12006-10-04 06:34:03 -04005552 rval = driver_create_file(&megasas_pci_driver.driver,
5553 &driver_attr_version);
5554 if (rval)
5555 goto err_dcf_attr_ver;
5556 rval = driver_create_file(&megasas_pci_driver.driver,
5557 &driver_attr_release_date);
5558 if (rval)
5559 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005560
5561 rval = driver_create_file(&megasas_pci_driver.driver,
5562 &driver_attr_support_poll_for_event);
5563 if (rval)
5564 goto err_dcf_support_poll_for_event;
5565
Jeff Garzik83aabc12006-10-04 06:34:03 -04005566 rval = driver_create_file(&megasas_pci_driver.driver,
5567 &driver_attr_dbg_lvl);
5568 if (rval)
5569 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005570 rval = driver_create_file(&megasas_pci_driver.driver,
5571 &driver_attr_poll_mode_io);
5572 if (rval)
5573 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005574
Yang, Bo837f5fe2010-10-11 06:59:20 -06005575 rval = driver_create_file(&megasas_pci_driver.driver,
5576 &driver_attr_support_device_change);
5577 if (rval)
5578 goto err_dcf_support_device_change;
5579
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005580 return rval;
bo yangad84db22007-11-09 04:40:16 -05005581
Yang, Bo837f5fe2010-10-11 06:59:20 -06005582err_dcf_support_device_change:
5583 driver_remove_file(&megasas_pci_driver.driver,
5584 &driver_attr_poll_mode_io);
5585
bo yangad84db22007-11-09 04:40:16 -05005586err_dcf_poll_mode_io:
5587 driver_remove_file(&megasas_pci_driver.driver,
5588 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005589err_dcf_dbg_lvl:
5590 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005591 &driver_attr_support_poll_for_event);
5592
5593err_dcf_support_poll_for_event:
5594 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005595 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005596
Jeff Garzik83aabc12006-10-04 06:34:03 -04005597err_dcf_rel_date:
5598 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5599err_dcf_attr_ver:
5600 pci_unregister_driver(&megasas_pci_driver);
5601err_pcidrv:
5602 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005603 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005604}
5605
5606/**
5607 * megasas_exit - Driver unload entry point
5608 */
5609static void __exit megasas_exit(void)
5610{
Sumant Patro658dced2006-10-03 13:09:14 -07005611 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005612 &driver_attr_poll_mode_io);
5613 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07005614 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005615 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005616 &driver_attr_support_poll_for_event);
5617 driver_remove_file(&megasas_pci_driver.driver,
5618 &driver_attr_support_device_change);
5619 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005620 &driver_attr_release_date);
5621 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005622
5623 pci_unregister_driver(&megasas_pci_driver);
5624 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5625}
5626
5627module_init(megasas_init);
5628module_exit(megasas_exit);