blob: f2cf768c896d89dd46d3f49ee77d904b2e4e3eb8 [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 radforde1703582011-07-26 15:43:01 -070021 * Version : v00.00.05.40-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;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400218 list_add_tail(&cmd->list, &instance->cmd_pool);
219
220 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
221}
222
Sumant Patro1341c932006-01-25 12:02:40 -0800223
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400224/**
adam radford0d490162010-12-14 19:17:17 -0800225* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800226* (deviceid : 1064R, PERC5) controllers
227*/
228
229/**
230 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400231 * @regs: MFI register set
232 */
233static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800234megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400235{
bo yang39a98552010-09-22 22:36:29 -0400236 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400237
238 /* Dummy readl to force pci flush */
239 readl(&regs->outbound_intr_mask);
240}
241
242/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700243 * megasas_disable_intr_xscale -Disables interrupt
244 * @regs: MFI register set
245 */
246static inline void
247megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
248{
249 u32 mask = 0x1f;
250 writel(mask, &regs->outbound_intr_mask);
251 /* Dummy readl to force pci flush */
252 readl(&regs->outbound_intr_mask);
253}
254
255/**
Sumant Patro1341c932006-01-25 12:02:40 -0800256 * megasas_read_fw_status_reg_xscale - returns the current FW status value
257 * @regs: MFI register set
258 */
259static u32
260megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
261{
262 return readl(&(regs)->outbound_msg_0);
263}
264/**
265 * megasas_clear_interrupt_xscale - Check & clear interrupt
266 * @regs: MFI register set
267 */
adam radford0d490162010-12-14 19:17:17 -0800268static int
Sumant Patro1341c932006-01-25 12:02:40 -0800269megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
270{
271 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400272 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800273 /*
274 * Check if it is our interrupt
275 */
276 status = readl(&regs->outbound_intr_status);
277
bo yang39a98552010-09-22 22:36:29 -0400278 if (status & MFI_OB_INTR_STATUS_MASK)
279 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
280 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
281 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800282
283 /*
284 * Clear the interrupt by writing back the same value
285 */
bo yang39a98552010-09-22 22:36:29 -0400286 if (mfiStatus)
287 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800288
Yang, Bo06f579d2008-08-10 12:42:37 -0700289 /* Dummy readl to force pci flush */
290 readl(&regs->outbound_intr_status);
291
bo yang39a98552010-09-22 22:36:29 -0400292 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800293}
294
295/**
296 * megasas_fire_cmd_xscale - Sends command to the FW
297 * @frame_phys_addr : Physical address of cmd
298 * @frame_count : Number of frames for the command
299 * @regs : MFI register set
300 */
adam radford0d490162010-12-14 19:17:17 -0800301static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600302megasas_fire_cmd_xscale(struct megasas_instance *instance,
303 dma_addr_t frame_phys_addr,
304 u32 frame_count,
305 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800306{
bo yang39a98552010-09-22 22:36:29 -0400307 unsigned long flags;
308 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800309 writel((frame_phys_addr >> 3)|(frame_count),
310 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400311 spin_unlock_irqrestore(&instance->hba_lock, flags);
312}
313
314/**
315 * megasas_adp_reset_xscale - For controller reset
316 * @regs: MFI register set
317 */
318static int
319megasas_adp_reset_xscale(struct megasas_instance *instance,
320 struct megasas_register_set __iomem *regs)
321{
322 u32 i;
323 u32 pcidata;
324 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
325
326 for (i = 0; i < 3; i++)
327 msleep(1000); /* sleep for 3 secs */
328 pcidata = 0;
329 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
330 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
331 if (pcidata & 0x2) {
332 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
333 pcidata &= ~0x2;
334 pci_write_config_dword(instance->pdev,
335 MFI_1068_PCSR_OFFSET, pcidata);
336
337 for (i = 0; i < 2; i++)
338 msleep(1000); /* need to wait 2 secs again */
339
340 pcidata = 0;
341 pci_read_config_dword(instance->pdev,
342 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
343 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
344 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
345 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
346 pcidata = 0;
347 pci_write_config_dword(instance->pdev,
348 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
349 }
350 }
351 return 0;
352}
353
354/**
355 * megasas_check_reset_xscale - For controller reset check
356 * @regs: MFI register set
357 */
358static int
359megasas_check_reset_xscale(struct megasas_instance *instance,
360 struct megasas_register_set __iomem *regs)
361{
362 u32 consumer;
363 consumer = *instance->consumer;
364
365 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
366 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
367 return 1;
368 }
369 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800370}
371
372static struct megasas_instance_template megasas_instance_template_xscale = {
373
374 .fire_cmd = megasas_fire_cmd_xscale,
375 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700376 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800377 .clear_intr = megasas_clear_intr_xscale,
378 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400379 .adp_reset = megasas_adp_reset_xscale,
380 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800381 .service_isr = megasas_isr,
382 .tasklet = megasas_complete_cmd_dpc,
383 .init_adapter = megasas_init_adapter_mfi,
384 .build_and_issue_cmd = megasas_build_and_issue_cmd,
385 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800386};
387
388/**
adam radford0d490162010-12-14 19:17:17 -0800389* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800390* to xscale (deviceid : 1064R, PERC5) controllers
391*/
392
393/**
adam radford0d490162010-12-14 19:17:17 -0800394* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800395* controllers
396*/
397
398/**
399 * megasas_enable_intr_ppc - Enables interrupts
400 * @regs: MFI register set
401 */
402static inline void
403megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
404{
405 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800406
bo yang39a98552010-09-22 22:36:29 -0400407 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800408
409 /* Dummy readl to force pci flush */
410 readl(&regs->outbound_intr_mask);
411}
412
413/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700414 * megasas_disable_intr_ppc - Disable interrupt
415 * @regs: MFI register set
416 */
417static inline void
418megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
419{
420 u32 mask = 0xFFFFFFFF;
421 writel(mask, &regs->outbound_intr_mask);
422 /* Dummy readl to force pci flush */
423 readl(&regs->outbound_intr_mask);
424}
425
426/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800427 * megasas_read_fw_status_reg_ppc - returns the current FW status value
428 * @regs: MFI register set
429 */
430static u32
431megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
432{
433 return readl(&(regs)->outbound_scratch_pad);
434}
435
436/**
437 * megasas_clear_interrupt_ppc - Check & clear interrupt
438 * @regs: MFI register set
439 */
adam radford0d490162010-12-14 19:17:17 -0800440static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800441megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
442{
adam radford3cc6851f92011-05-11 18:34:52 -0700443 u32 status, mfiStatus = 0;
444
Sumant Patrof9876f02006-02-03 15:34:35 -0800445 /*
446 * Check if it is our interrupt
447 */
448 status = readl(&regs->outbound_intr_status);
449
adam radford3cc6851f92011-05-11 18:34:52 -0700450 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
451 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
452
453 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
454 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800455
456 /*
457 * Clear the interrupt by writing back the same value
458 */
459 writel(status, &regs->outbound_doorbell_clear);
460
Yang, Bo06f579d2008-08-10 12:42:37 -0700461 /* Dummy readl to force pci flush */
462 readl(&regs->outbound_doorbell_clear);
463
adam radford3cc6851f92011-05-11 18:34:52 -0700464 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800465}
adam radford3cc6851f92011-05-11 18:34:52 -0700466
Sumant Patrof9876f02006-02-03 15:34:35 -0800467/**
468 * megasas_fire_cmd_ppc - Sends command to the FW
469 * @frame_phys_addr : Physical address of cmd
470 * @frame_count : Number of frames for the command
471 * @regs : MFI register set
472 */
adam radford0d490162010-12-14 19:17:17 -0800473static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600474megasas_fire_cmd_ppc(struct megasas_instance *instance,
475 dma_addr_t frame_phys_addr,
476 u32 frame_count,
477 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800478{
bo yang39a98552010-09-22 22:36:29 -0400479 unsigned long flags;
480 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800481 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800482 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400483 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800484}
485
bo yang39a98552010-09-22 22:36:29 -0400486/**
bo yang39a98552010-09-22 22:36:29 -0400487 * megasas_check_reset_ppc - For controller reset check
488 * @regs: MFI register set
489 */
490static int
491megasas_check_reset_ppc(struct megasas_instance *instance,
492 struct megasas_register_set __iomem *regs)
493{
adam radford3cc6851f92011-05-11 18:34:52 -0700494 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
495 return 1;
496
bo yang39a98552010-09-22 22:36:29 -0400497 return 0;
498}
adam radford3cc6851f92011-05-11 18:34:52 -0700499
Sumant Patrof9876f02006-02-03 15:34:35 -0800500static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800501
Sumant Patrof9876f02006-02-03 15:34:35 -0800502 .fire_cmd = megasas_fire_cmd_ppc,
503 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700504 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800505 .clear_intr = megasas_clear_intr_ppc,
506 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700507 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400508 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800509 .service_isr = megasas_isr,
510 .tasklet = megasas_complete_cmd_dpc,
511 .init_adapter = megasas_init_adapter_mfi,
512 .build_and_issue_cmd = megasas_build_and_issue_cmd,
513 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800514};
515
516/**
Yang, Bo87911122009-10-06 14:31:54 -0600517 * megasas_enable_intr_skinny - Enables interrupts
518 * @regs: MFI register set
519 */
520static inline void
521megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
522{
523 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
524
525 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
526
527 /* Dummy readl to force pci flush */
528 readl(&regs->outbound_intr_mask);
529}
530
531/**
532 * megasas_disable_intr_skinny - Disables interrupt
533 * @regs: MFI register set
534 */
535static inline void
536megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
537{
538 u32 mask = 0xFFFFFFFF;
539 writel(mask, &regs->outbound_intr_mask);
540 /* Dummy readl to force pci flush */
541 readl(&regs->outbound_intr_mask);
542}
543
544/**
545 * megasas_read_fw_status_reg_skinny - returns the current FW status value
546 * @regs: MFI register set
547 */
548static u32
549megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
550{
551 return readl(&(regs)->outbound_scratch_pad);
552}
553
554/**
555 * megasas_clear_interrupt_skinny - Check & clear interrupt
556 * @regs: MFI register set
557 */
558static int
559megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
560{
561 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800562 u32 mfiStatus = 0;
563
Yang, Bo87911122009-10-06 14:31:54 -0600564 /*
565 * Check if it is our interrupt
566 */
567 status = readl(&regs->outbound_intr_status);
568
569 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400570 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600571 }
572
573 /*
adam radfordebf054b2011-02-24 20:57:15 -0800574 * Check if it is our interrupt
575 */
576 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
577 MFI_STATE_FAULT) {
578 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
579 } else
580 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
581
582 /*
Yang, Bo87911122009-10-06 14:31:54 -0600583 * Clear the interrupt by writing back the same value
584 */
585 writel(status, &regs->outbound_intr_status);
586
587 /*
588 * dummy read to flush PCI
589 */
590 readl(&regs->outbound_intr_status);
591
adam radfordebf054b2011-02-24 20:57:15 -0800592 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600593}
594
595/**
596 * megasas_fire_cmd_skinny - Sends command to the FW
597 * @frame_phys_addr : Physical address of cmd
598 * @frame_count : Number of frames for the command
599 * @regs : MFI register set
600 */
601static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600602megasas_fire_cmd_skinny(struct megasas_instance *instance,
603 dma_addr_t frame_phys_addr,
604 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600605 struct megasas_register_set __iomem *regs)
606{
Yang, Bo0c79e682009-10-06 14:47:35 -0600607 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400608 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600609 writel(0, &(regs)->inbound_high_queue_port);
610 writel((frame_phys_addr | (frame_count<<1))|1,
611 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400612 spin_unlock_irqrestore(&instance->hba_lock, flags);
613}
614
615/**
bo yang39a98552010-09-22 22:36:29 -0400616 * megasas_check_reset_skinny - For controller reset check
617 * @regs: MFI register set
618 */
619static int
620megasas_check_reset_skinny(struct megasas_instance *instance,
621 struct megasas_register_set __iomem *regs)
622{
adam radford3cc6851f92011-05-11 18:34:52 -0700623 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
624 return 1;
625
bo yang39a98552010-09-22 22:36:29 -0400626 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600627}
628
629static struct megasas_instance_template megasas_instance_template_skinny = {
630
631 .fire_cmd = megasas_fire_cmd_skinny,
632 .enable_intr = megasas_enable_intr_skinny,
633 .disable_intr = megasas_disable_intr_skinny,
634 .clear_intr = megasas_clear_intr_skinny,
635 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800636 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400637 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800638 .service_isr = megasas_isr,
639 .tasklet = megasas_complete_cmd_dpc,
640 .init_adapter = megasas_init_adapter_mfi,
641 .build_and_issue_cmd = megasas_build_and_issue_cmd,
642 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600643};
644
645
646/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700647* The following functions are defined for gen2 (deviceid : 0x78 0x79)
648* controllers
649*/
650
651/**
652 * megasas_enable_intr_gen2 - Enables interrupts
653 * @regs: MFI register set
654 */
655static inline void
656megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
657{
658 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
659
660 /* write ~0x00000005 (4 & 1) to the intr mask*/
661 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
662
663 /* Dummy readl to force pci flush */
664 readl(&regs->outbound_intr_mask);
665}
666
667/**
668 * megasas_disable_intr_gen2 - Disables interrupt
669 * @regs: MFI register set
670 */
671static inline void
672megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
673{
674 u32 mask = 0xFFFFFFFF;
675 writel(mask, &regs->outbound_intr_mask);
676 /* Dummy readl to force pci flush */
677 readl(&regs->outbound_intr_mask);
678}
679
680/**
681 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
682 * @regs: MFI register set
683 */
684static u32
685megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
686{
687 return readl(&(regs)->outbound_scratch_pad);
688}
689
690/**
691 * megasas_clear_interrupt_gen2 - Check & clear interrupt
692 * @regs: MFI register set
693 */
694static int
695megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
696{
697 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400698 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700699 /*
700 * Check if it is our interrupt
701 */
702 status = readl(&regs->outbound_intr_status);
703
bo yang39a98552010-09-22 22:36:29 -0400704 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
705 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
706 }
707 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
708 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
709 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700710
711 /*
712 * Clear the interrupt by writing back the same value
713 */
bo yang39a98552010-09-22 22:36:29 -0400714 if (mfiStatus)
715 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700716
717 /* Dummy readl to force pci flush */
718 readl(&regs->outbound_intr_status);
719
bo yang39a98552010-09-22 22:36:29 -0400720 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700721}
722/**
723 * megasas_fire_cmd_gen2 - Sends command to the FW
724 * @frame_phys_addr : Physical address of cmd
725 * @frame_count : Number of frames for the command
726 * @regs : MFI register set
727 */
728static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600729megasas_fire_cmd_gen2(struct megasas_instance *instance,
730 dma_addr_t frame_phys_addr,
731 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700732 struct megasas_register_set __iomem *regs)
733{
bo yang39a98552010-09-22 22:36:29 -0400734 unsigned long flags;
735 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700736 writel((frame_phys_addr | (frame_count<<1))|1,
737 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400738 spin_unlock_irqrestore(&instance->hba_lock, flags);
739}
740
741/**
742 * megasas_adp_reset_gen2 - For controller reset
743 * @regs: MFI register set
744 */
745static int
746megasas_adp_reset_gen2(struct megasas_instance *instance,
747 struct megasas_register_set __iomem *reg_set)
748{
749 u32 retry = 0 ;
750 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800751 u32 *seq_offset = &reg_set->seq_offset;
752 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400753
adam radfordebf054b2011-02-24 20:57:15 -0800754 if (instance->instancet == &megasas_instance_template_skinny) {
755 seq_offset = &reg_set->fusion_seq_offset;
756 hostdiag_offset = &reg_set->fusion_host_diag;
757 }
758
759 writel(0, seq_offset);
760 writel(4, seq_offset);
761 writel(0xb, seq_offset);
762 writel(2, seq_offset);
763 writel(7, seq_offset);
764 writel(0xd, seq_offset);
765
bo yang39a98552010-09-22 22:36:29 -0400766 msleep(1000);
767
adam radfordebf054b2011-02-24 20:57:15 -0800768 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400769
770 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
771 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800772 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400773 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
774 retry, HostDiag);
775
776 if (retry++ >= 100)
777 return 1;
778
779 }
780
781 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
782
adam radfordebf054b2011-02-24 20:57:15 -0800783 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400784
785 ssleep(10);
786
adam radfordebf054b2011-02-24 20:57:15 -0800787 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400788 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
789 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800790 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400791 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
792 retry, HostDiag);
793
794 if (retry++ >= 1000)
795 return 1;
796
797 }
798 return 0;
799}
800
801/**
802 * megasas_check_reset_gen2 - For controller reset check
803 * @regs: MFI register set
804 */
805static int
806megasas_check_reset_gen2(struct megasas_instance *instance,
807 struct megasas_register_set __iomem *regs)
808{
Yang, Bo707e09b2010-10-12 07:20:27 -0600809 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
810 return 1;
811 }
812
bo yang39a98552010-09-22 22:36:29 -0400813 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700814}
815
816static struct megasas_instance_template megasas_instance_template_gen2 = {
817
818 .fire_cmd = megasas_fire_cmd_gen2,
819 .enable_intr = megasas_enable_intr_gen2,
820 .disable_intr = megasas_disable_intr_gen2,
821 .clear_intr = megasas_clear_intr_gen2,
822 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400823 .adp_reset = megasas_adp_reset_gen2,
824 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800825 .service_isr = megasas_isr,
826 .tasklet = megasas_complete_cmd_dpc,
827 .init_adapter = megasas_init_adapter_mfi,
828 .build_and_issue_cmd = megasas_build_and_issue_cmd,
829 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700830};
831
832/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800833* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400834* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800835*/
836
adam radford9c915a82010-12-21 13:34:31 -0800837/*
838 * Template added for TB (Fusion)
839 */
840extern struct megasas_instance_template megasas_instance_template_fusion;
841
Sumant Patrof9876f02006-02-03 15:34:35 -0800842/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400843 * megasas_issue_polled - Issues a polling command
844 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800845 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400846 *
847 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
848 */
adam radford9c915a82010-12-21 13:34:31 -0800849int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400850megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
851{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400852
853 struct megasas_header *frame_hdr = &cmd->frame->hdr;
854
855 frame_hdr->cmd_status = 0xFF;
856 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
857
858 /*
859 * Issue the frame using inbound queue port
860 */
adam radford9c915a82010-12-21 13:34:31 -0800861 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400862
863 /*
864 * Wait for cmd_status to change
865 */
adam radford9c915a82010-12-21 13:34:31 -0800866 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400867}
868
869/**
870 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
871 * @instance: Adapter soft state
872 * @cmd: Command to be issued
873 *
874 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700875 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400876 * Used to issue ioctl commands.
877 */
878static int
879megasas_issue_blocked_cmd(struct megasas_instance *instance,
880 struct megasas_cmd *cmd)
881{
882 cmd->cmd_status = ENODATA;
883
adam radford9c915a82010-12-21 13:34:31 -0800884 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400885
bo yang39a98552010-09-22 22:36:29 -0400886 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400887
888 return 0;
889}
890
891/**
892 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
893 * @instance: Adapter soft state
894 * @cmd_to_abort: Previously issued cmd to be aborted
895 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100896 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400897 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700898 * cmd and waits for return status.
899 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400900 */
901static int
902megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
903 struct megasas_cmd *cmd_to_abort)
904{
905 struct megasas_cmd *cmd;
906 struct megasas_abort_frame *abort_fr;
907
908 cmd = megasas_get_cmd(instance);
909
910 if (!cmd)
911 return -1;
912
913 abort_fr = &cmd->frame->abort;
914
915 /*
916 * Prepare and issue the abort frame
917 */
918 abort_fr->cmd = MFI_CMD_ABORT;
919 abort_fr->cmd_status = 0xFF;
920 abort_fr->flags = 0;
921 abort_fr->abort_context = cmd_to_abort->index;
922 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
923 abort_fr->abort_mfi_phys_addr_hi = 0;
924
925 cmd->sync_cmd = 1;
926 cmd->cmd_status = 0xFF;
927
adam radford9c915a82010-12-21 13:34:31 -0800928 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400929
930 /*
931 * Wait for this cmd to complete
932 */
bo yang39a98552010-09-22 22:36:29 -0400933 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
934 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400935
936 megasas_return_cmd(instance, cmd);
937 return 0;
938}
939
940/**
941 * megasas_make_sgl32 - Prepares 32-bit SGL
942 * @instance: Adapter soft state
943 * @scp: SCSI command from the mid-layer
944 * @mfi_sgl: SGL to be filled in
945 *
946 * If successful, this function returns the number of SG elements. Otherwise,
947 * it returnes -1.
948 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800949static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400950megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
951 union megasas_sgl *mfi_sgl)
952{
953 int i;
954 int sge_count;
955 struct scatterlist *os_sgl;
956
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900957 sge_count = scsi_dma_map(scp);
958 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400959
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900960 if (sge_count) {
961 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
962 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
963 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
964 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400965 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400966 return sge_count;
967}
968
969/**
970 * megasas_make_sgl64 - Prepares 64-bit SGL
971 * @instance: Adapter soft state
972 * @scp: SCSI command from the mid-layer
973 * @mfi_sgl: SGL to be filled in
974 *
975 * If successful, this function returns the number of SG elements. Otherwise,
976 * it returnes -1.
977 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800978static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400979megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
980 union megasas_sgl *mfi_sgl)
981{
982 int i;
983 int sge_count;
984 struct scatterlist *os_sgl;
985
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900986 sge_count = scsi_dma_map(scp);
987 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400988
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900989 if (sge_count) {
990 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
991 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
992 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
993 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400994 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400995 return sge_count;
996}
997
Yang, Bof4c9a132009-10-06 14:43:28 -0600998/**
999 * megasas_make_sgl_skinny - Prepares IEEE SGL
1000 * @instance: Adapter soft state
1001 * @scp: SCSI command from the mid-layer
1002 * @mfi_sgl: SGL to be filled in
1003 *
1004 * If successful, this function returns the number of SG elements. Otherwise,
1005 * it returnes -1.
1006 */
1007static int
1008megasas_make_sgl_skinny(struct megasas_instance *instance,
1009 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1010{
1011 int i;
1012 int sge_count;
1013 struct scatterlist *os_sgl;
1014
1015 sge_count = scsi_dma_map(scp);
1016
1017 if (sge_count) {
1018 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1019 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1020 mfi_sgl->sge_skinny[i].phys_addr =
1021 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001022 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001023 }
1024 }
1025 return sge_count;
1026}
1027
Sumant Patrob1df99d2006-10-03 12:40:47 -07001028 /**
1029 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001030 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001031 * @sge_count : number of sg elements
1032 *
1033 * Returns the number of frames required for numnber of sge's (sge_count)
1034 */
1035
Yang, Bof4c9a132009-10-06 14:43:28 -06001036static u32 megasas_get_frame_count(struct megasas_instance *instance,
1037 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001038{
1039 int num_cnt;
1040 int sge_bytes;
1041 u32 sge_sz;
1042 u32 frame_count=0;
1043
1044 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1045 sizeof(struct megasas_sge32);
1046
Yang, Bof4c9a132009-10-06 14:43:28 -06001047 if (instance->flag_ieee) {
1048 sge_sz = sizeof(struct megasas_sge_skinny);
1049 }
1050
Sumant Patrob1df99d2006-10-03 12:40:47 -07001051 /*
bo yangd532dbe2008-03-17 03:36:43 -04001052 * Main frame can contain 2 SGEs for 64-bit SGLs and
1053 * 3 SGEs for 32-bit SGLs for ldio &
1054 * 1 SGEs for 64-bit SGLs and
1055 * 2 SGEs for 32-bit SGLs for pthru frame
1056 */
1057 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001058 if (instance->flag_ieee == 1) {
1059 num_cnt = sge_count - 1;
1060 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001061 num_cnt = sge_count - 1;
1062 else
1063 num_cnt = sge_count - 2;
1064 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001065 if (instance->flag_ieee == 1) {
1066 num_cnt = sge_count - 1;
1067 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001068 num_cnt = sge_count - 2;
1069 else
1070 num_cnt = sge_count - 3;
1071 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001072
1073 if(num_cnt>0){
1074 sge_bytes = sge_sz * num_cnt;
1075
1076 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1077 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1078 }
1079 /* Main frame */
1080 frame_count +=1;
1081
1082 if (frame_count > 7)
1083 frame_count = 8;
1084 return frame_count;
1085}
1086
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001087/**
1088 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1089 * @instance: Adapter soft state
1090 * @scp: SCSI command
1091 * @cmd: Command to be prepared in
1092 *
1093 * This function prepares CDB commands. These are typcially pass-through
1094 * commands to the devices.
1095 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001096static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001097megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1098 struct megasas_cmd *cmd)
1099{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001100 u32 is_logical;
1101 u32 device_id;
1102 u16 flags = 0;
1103 struct megasas_pthru_frame *pthru;
1104
1105 is_logical = MEGASAS_IS_LOGICAL(scp);
1106 device_id = MEGASAS_DEV_INDEX(instance, scp);
1107 pthru = (struct megasas_pthru_frame *)cmd->frame;
1108
1109 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1110 flags = MFI_FRAME_DIR_WRITE;
1111 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1112 flags = MFI_FRAME_DIR_READ;
1113 else if (scp->sc_data_direction == PCI_DMA_NONE)
1114 flags = MFI_FRAME_DIR_NONE;
1115
Yang, Bof4c9a132009-10-06 14:43:28 -06001116 if (instance->flag_ieee == 1) {
1117 flags |= MFI_FRAME_IEEE;
1118 }
1119
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001120 /*
1121 * Prepare the DCDB frame
1122 */
1123 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1124 pthru->cmd_status = 0x0;
1125 pthru->scsi_status = 0x0;
1126 pthru->target_id = device_id;
1127 pthru->lun = scp->device->lun;
1128 pthru->cdb_len = scp->cmd_len;
1129 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001130 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001131 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001132 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001133
1134 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1135
1136 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001137 * If the command is for the tape device, set the
1138 * pthru timeout to the os layer timeout value.
1139 */
1140 if (scp->device->type == TYPE_TAPE) {
1141 if ((scp->request->timeout / HZ) > 0xFFFF)
1142 pthru->timeout = 0xFFFF;
1143 else
1144 pthru->timeout = scp->request->timeout / HZ;
1145 }
1146
1147 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001148 * Construct SGL
1149 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001150 if (instance->flag_ieee == 1) {
1151 pthru->flags |= MFI_FRAME_SGL64;
1152 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1153 &pthru->sgl);
1154 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001155 pthru->flags |= MFI_FRAME_SGL64;
1156 pthru->sge_count = megasas_make_sgl64(instance, scp,
1157 &pthru->sgl);
1158 } else
1159 pthru->sge_count = megasas_make_sgl32(instance, scp,
1160 &pthru->sgl);
1161
Yang, Bobdc6fb82009-12-06 08:30:19 -07001162 if (pthru->sge_count > instance->max_num_sge) {
1163 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1164 pthru->sge_count);
1165 return 0;
1166 }
1167
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001168 /*
1169 * Sense info specific
1170 */
1171 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1172 pthru->sense_buf_phys_addr_hi = 0;
1173 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1174
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001175 /*
1176 * Compute the total number of frames this command consumes. FW uses
1177 * this number to pull sufficient number of frames from host memory.
1178 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001179 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001180 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001181
1182 return cmd->frame_count;
1183}
1184
1185/**
1186 * megasas_build_ldio - Prepares IOs to logical devices
1187 * @instance: Adapter soft state
1188 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001189 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001190 *
1191 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1192 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001193static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001194megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1195 struct megasas_cmd *cmd)
1196{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001197 u32 device_id;
1198 u8 sc = scp->cmnd[0];
1199 u16 flags = 0;
1200 struct megasas_io_frame *ldio;
1201
1202 device_id = MEGASAS_DEV_INDEX(instance, scp);
1203 ldio = (struct megasas_io_frame *)cmd->frame;
1204
1205 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1206 flags = MFI_FRAME_DIR_WRITE;
1207 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1208 flags = MFI_FRAME_DIR_READ;
1209
Yang, Bof4c9a132009-10-06 14:43:28 -06001210 if (instance->flag_ieee == 1) {
1211 flags |= MFI_FRAME_IEEE;
1212 }
1213
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001214 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001215 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001216 */
1217 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1218 ldio->cmd_status = 0x0;
1219 ldio->scsi_status = 0x0;
1220 ldio->target_id = device_id;
1221 ldio->timeout = 0;
1222 ldio->reserved_0 = 0;
1223 ldio->pad_0 = 0;
1224 ldio->flags = flags;
1225 ldio->start_lba_hi = 0;
1226 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1227
1228 /*
1229 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1230 */
1231 if (scp->cmd_len == 6) {
1232 ldio->lba_count = (u32) scp->cmnd[4];
1233 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1234 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1235
1236 ldio->start_lba_lo &= 0x1FFFFF;
1237 }
1238
1239 /*
1240 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1241 */
1242 else if (scp->cmd_len == 10) {
1243 ldio->lba_count = (u32) scp->cmnd[8] |
1244 ((u32) scp->cmnd[7] << 8);
1245 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1246 ((u32) scp->cmnd[3] << 16) |
1247 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1248 }
1249
1250 /*
1251 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1252 */
1253 else if (scp->cmd_len == 12) {
1254 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1255 ((u32) scp->cmnd[7] << 16) |
1256 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1257
1258 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1259 ((u32) scp->cmnd[3] << 16) |
1260 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1261 }
1262
1263 /*
1264 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1265 */
1266 else if (scp->cmd_len == 16) {
1267 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1268 ((u32) scp->cmnd[11] << 16) |
1269 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1270
1271 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1272 ((u32) scp->cmnd[7] << 16) |
1273 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1274
1275 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1276 ((u32) scp->cmnd[3] << 16) |
1277 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1278
1279 }
1280
1281 /*
1282 * Construct SGL
1283 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001284 if (instance->flag_ieee) {
1285 ldio->flags |= MFI_FRAME_SGL64;
1286 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1287 &ldio->sgl);
1288 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001289 ldio->flags |= MFI_FRAME_SGL64;
1290 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1291 } else
1292 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1293
Yang, Bobdc6fb82009-12-06 08:30:19 -07001294 if (ldio->sge_count > instance->max_num_sge) {
1295 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1296 ldio->sge_count);
1297 return 0;
1298 }
1299
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001300 /*
1301 * Sense info specific
1302 */
1303 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1304 ldio->sense_buf_phys_addr_hi = 0;
1305 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1306
Sumant Patrob1df99d2006-10-03 12:40:47 -07001307 /*
1308 * Compute the total number of frames this command consumes. FW uses
1309 * this number to pull sufficient number of frames from host memory.
1310 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001311 cmd->frame_count = megasas_get_frame_count(instance,
1312 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001313
1314 return cmd->frame_count;
1315}
1316
1317/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001318 * megasas_is_ldio - Checks if the cmd is for logical drive
1319 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001320 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001321 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001322 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001323 */
adam radford9c915a82010-12-21 13:34:31 -08001324inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001325{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001326 if (!MEGASAS_IS_LOGICAL(cmd))
1327 return 0;
1328 switch (cmd->cmnd[0]) {
1329 case READ_10:
1330 case WRITE_10:
1331 case READ_12:
1332 case WRITE_12:
1333 case READ_6:
1334 case WRITE_6:
1335 case READ_16:
1336 case WRITE_16:
1337 return 1;
1338 default:
1339 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001340 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001341}
1342
Sumant Patro658dced2006-10-03 13:09:14 -07001343 /**
1344 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1345 * in FW
1346 * @instance: Adapter soft state
1347 */
1348static inline void
1349megasas_dump_pending_frames(struct megasas_instance *instance)
1350{
1351 struct megasas_cmd *cmd;
1352 int i,n;
1353 union megasas_sgl *mfi_sgl;
1354 struct megasas_io_frame *ldio;
1355 struct megasas_pthru_frame *pthru;
1356 u32 sgcount;
1357 u32 max_cmd = instance->max_fw_cmds;
1358
1359 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1360 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1361 if (IS_DMA64)
1362 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1363 else
1364 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1365
1366 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1367 for (i = 0; i < max_cmd; i++) {
1368 cmd = instance->cmd_list[i];
1369 if(!cmd->scmd)
1370 continue;
1371 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1372 if (megasas_is_ldio(cmd->scmd)){
1373 ldio = (struct megasas_io_frame *)cmd->frame;
1374 mfi_sgl = &ldio->sgl;
1375 sgcount = ldio->sge_count;
1376 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);
1377 }
1378 else {
1379 pthru = (struct megasas_pthru_frame *) cmd->frame;
1380 mfi_sgl = &pthru->sgl;
1381 sgcount = pthru->sge_count;
1382 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);
1383 }
1384 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1385 for (n = 0; n < sgcount; n++){
1386 if (IS_DMA64)
1387 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) ;
1388 else
1389 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1390 }
1391 }
1392 printk(KERN_ERR "\n");
1393 } /*for max_cmd*/
1394 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1395 for (i = 0; i < max_cmd; i++) {
1396
1397 cmd = instance->cmd_list[i];
1398
1399 if(cmd->sync_cmd == 1){
1400 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1401 }
1402 }
1403 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1404}
1405
adam radfordcd50ba82010-12-21 10:23:23 -08001406u32
1407megasas_build_and_issue_cmd(struct megasas_instance *instance,
1408 struct scsi_cmnd *scmd)
1409{
1410 struct megasas_cmd *cmd;
1411 u32 frame_count;
1412
1413 cmd = megasas_get_cmd(instance);
1414 if (!cmd)
1415 return SCSI_MLQUEUE_HOST_BUSY;
1416
1417 /*
1418 * Logical drive command
1419 */
1420 if (megasas_is_ldio(scmd))
1421 frame_count = megasas_build_ldio(instance, scmd, cmd);
1422 else
1423 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1424
1425 if (!frame_count)
1426 goto out_return_cmd;
1427
1428 cmd->scmd = scmd;
1429 scmd->SCp.ptr = (char *)cmd;
1430
1431 /*
1432 * Issue the command to the FW
1433 */
1434 atomic_inc(&instance->fw_outstanding);
1435
1436 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1437 cmd->frame_count-1, instance->reg_set);
1438 /*
1439 * Check if we have pend cmds to be completed
1440 */
1441 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1442 tasklet_schedule(&instance->isr_tasklet);
1443
1444 return 0;
1445out_return_cmd:
1446 megasas_return_cmd(instance, cmd);
1447 return 1;
1448}
1449
1450
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001451/**
1452 * megasas_queue_command - Queue entry point
1453 * @scmd: SCSI command to be queued
1454 * @done: Callback entry point
1455 */
1456static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001457megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001458{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001459 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001460 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001461
1462 instance = (struct megasas_instance *)
1463 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001464
bo yang39a98552010-09-22 22:36:29 -04001465 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001466 return SCSI_MLQUEUE_HOST_BUSY;
1467
bo yang39a98552010-09-22 22:36:29 -04001468 spin_lock_irqsave(&instance->hba_lock, flags);
1469 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1470 spin_unlock_irqrestore(&instance->hba_lock, flags);
1471 return SCSI_MLQUEUE_HOST_BUSY;
1472 }
1473
1474 spin_unlock_irqrestore(&instance->hba_lock, flags);
1475
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001476 scmd->scsi_done = done;
1477 scmd->result = 0;
1478
Sumant Patrocb59aa62006-01-25 11:53:25 -08001479 if (MEGASAS_IS_LOGICAL(scmd) &&
1480 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1481 scmd->result = DID_BAD_TARGET << 16;
1482 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001483 }
1484
Sumant Patro02b01e02007-02-14 13:00:55 -08001485 switch (scmd->cmnd[0]) {
1486 case SYNCHRONIZE_CACHE:
1487 /*
1488 * FW takes care of flush cache on its own
1489 * No need to send it down
1490 */
1491 scmd->result = DID_OK << 16;
1492 goto out_done;
1493 default:
1494 break;
1495 }
1496
adam radfordcd50ba82010-12-21 10:23:23 -08001497 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1498 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001499 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001500 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001501
1502 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001503
Sumant Patrocb59aa62006-01-25 11:53:25 -08001504 out_done:
1505 done(scmd);
1506 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001507}
1508
Jeff Garzikf2812332010-11-16 02:10:29 -05001509static DEF_SCSI_QCMD(megasas_queue_command)
1510
Yang, Bo044833b2009-10-06 14:33:06 -06001511static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1512{
1513 int i;
1514
1515 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1516
1517 if ((megasas_mgmt_info.instance[i]) &&
1518 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1519 return megasas_mgmt_info.instance[i];
1520 }
1521
1522 return NULL;
1523}
1524
Christoph Hellwig147aab62006-02-17 12:13:48 +01001525static int megasas_slave_configure(struct scsi_device *sdev)
1526{
Yang, Bo044833b2009-10-06 14:33:06 -06001527 u16 pd_index = 0;
1528 struct megasas_instance *instance ;
1529
1530 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001531
1532 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001533 * Don't export physical disk devices to the disk driver.
1534 *
1535 * FIXME: Currently we don't export them to the midlayer at all.
1536 * That will be fixed once LSI engineers have audited the
1537 * firmware for possible issues.
1538 */
1539 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1540 sdev->type == TYPE_DISK) {
1541 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1542 sdev->id;
1543 if (instance->pd_list[pd_index].driveState ==
1544 MR_PD_STATE_SYSTEM) {
1545 blk_queue_rq_timeout(sdev->request_queue,
1546 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1547 return 0;
1548 }
1549 return -ENXIO;
1550 }
1551
1552 /*
1553 * The RAID firmware may require extended timeouts.
1554 */
1555 blk_queue_rq_timeout(sdev->request_queue,
1556 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1557 return 0;
1558}
1559
1560static int megasas_slave_alloc(struct scsi_device *sdev)
1561{
1562 u16 pd_index = 0;
1563 struct megasas_instance *instance ;
1564 instance = megasas_lookup_instance(sdev->host->host_no);
1565 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1566 (sdev->type == TYPE_DISK)) {
1567 /*
1568 * Open the OS scan to the SYSTEM PD
1569 */
1570 pd_index =
1571 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1572 sdev->id;
1573 if ((instance->pd_list[pd_index].driveState ==
1574 MR_PD_STATE_SYSTEM) &&
1575 (instance->pd_list[pd_index].driveType ==
1576 TYPE_DISK)) {
1577 return 0;
1578 }
1579 return -ENXIO;
1580 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001581 return 0;
1582}
1583
adam radford9c915a82010-12-21 13:34:31 -08001584void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001585{
1586 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001587 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
adam radford36807e62011-10-08 18:15:06 -07001588 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1589 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08001590 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001591 } else {
adam radford9c915a82010-12-21 13:34:31 -08001592 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1593 }
1594}
1595
1596 /**
1597 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1598 * restored to max value
1599 * @instance: Adapter soft state
1600 *
1601 */
1602void
1603megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1604{
1605 unsigned long flags;
1606 if (instance->flag & MEGASAS_FW_BUSY
1607 && time_after(jiffies, instance->last_time + 5 * HZ)
1608 && atomic_read(&instance->fw_outstanding) < 17) {
1609
1610 spin_lock_irqsave(instance->host->host_lock, flags);
1611 instance->flag &= ~MEGASAS_FW_BUSY;
1612 if ((instance->pdev->device ==
1613 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1614 (instance->pdev->device ==
1615 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1616 instance->host->can_queue =
1617 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1618 } else
1619 instance->host->can_queue =
1620 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1621
1622 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001623 }
1624}
1625
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001626/**
bo yang7343eb62007-11-09 04:35:44 -05001627 * megasas_complete_cmd_dpc - Returns FW's controller structure
1628 * @instance_addr: Address of adapter soft state
1629 *
1630 * Tasklet to complete cmds
1631 */
1632static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1633{
1634 u32 producer;
1635 u32 consumer;
1636 u32 context;
1637 struct megasas_cmd *cmd;
1638 struct megasas_instance *instance =
1639 (struct megasas_instance *)instance_addr;
1640 unsigned long flags;
1641
1642 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001643 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001644 return;
1645
1646 spin_lock_irqsave(&instance->completion_lock, flags);
1647
1648 producer = *instance->producer;
1649 consumer = *instance->consumer;
1650
1651 while (consumer != producer) {
1652 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001653 if (context >= instance->max_fw_cmds) {
1654 printk(KERN_ERR "Unexpected context value %x\n",
1655 context);
1656 BUG();
1657 }
bo yang7343eb62007-11-09 04:35:44 -05001658
1659 cmd = instance->cmd_list[context];
1660
1661 megasas_complete_cmd(instance, cmd, DID_OK);
1662
1663 consumer++;
1664 if (consumer == (instance->max_fw_cmds + 1)) {
1665 consumer = 0;
1666 }
1667 }
1668
1669 *instance->consumer = producer;
1670
1671 spin_unlock_irqrestore(&instance->completion_lock, flags);
1672
1673 /*
1674 * Check if we can restore can_queue
1675 */
adam radford9c915a82010-12-21 13:34:31 -08001676 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001677}
1678
Yang, Bo707e09b2010-10-12 07:20:27 -06001679static void
1680megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1681
1682static void
1683process_fw_state_change_wq(struct work_struct *work);
1684
1685void megasas_do_ocr(struct megasas_instance *instance)
1686{
1687 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1688 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1689 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1690 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1691 }
1692 instance->instancet->disable_intr(instance->reg_set);
1693 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1694 instance->issuepend_done = 0;
1695
1696 atomic_set(&instance->fw_outstanding, 0);
1697 megasas_internal_reset_defer_cmds(instance);
1698 process_fw_state_change_wq(&instance->work_init);
1699}
1700
bo yang7343eb62007-11-09 04:35:44 -05001701/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001702 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1703 * @instance: Adapter soft state
1704 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001705 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001706 * complete all its outstanding commands. Returns error if one or more IOs
1707 * are pending after this time period. It also marks the controller dead.
1708 */
1709static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1710{
1711 int i;
bo yang39a98552010-09-22 22:36:29 -04001712 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001713 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001714 u8 adprecovery;
1715 unsigned long flags;
1716 struct list_head clist_local;
1717 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001718 u32 fw_state;
1719 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001720
1721 spin_lock_irqsave(&instance->hba_lock, flags);
1722 adprecovery = instance->adprecovery;
1723 spin_unlock_irqrestore(&instance->hba_lock, flags);
1724
1725 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1726
1727 INIT_LIST_HEAD(&clist_local);
1728 spin_lock_irqsave(&instance->hba_lock, flags);
1729 list_splice_init(&instance->internal_reset_pending_q,
1730 &clist_local);
1731 spin_unlock_irqrestore(&instance->hba_lock, flags);
1732
1733 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1734 for (i = 0; i < wait_time; i++) {
1735 msleep(1000);
1736 spin_lock_irqsave(&instance->hba_lock, flags);
1737 adprecovery = instance->adprecovery;
1738 spin_unlock_irqrestore(&instance->hba_lock, flags);
1739 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1740 break;
1741 }
1742
1743 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1744 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1745 spin_lock_irqsave(&instance->hba_lock, flags);
1746 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1747 spin_unlock_irqrestore(&instance->hba_lock, flags);
1748 return FAILED;
1749 }
1750
1751 reset_index = 0;
1752 while (!list_empty(&clist_local)) {
1753 reset_cmd = list_entry((&clist_local)->next,
1754 struct megasas_cmd, list);
1755 list_del_init(&reset_cmd->list);
1756 if (reset_cmd->scmd) {
1757 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001758 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001759 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001760 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001761
1762 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1763 megasas_return_cmd(instance, reset_cmd);
1764 } else if (reset_cmd->sync_cmd) {
1765 printk(KERN_NOTICE "megasas:%p synch cmds"
1766 "reset queue\n",
1767 reset_cmd);
1768
1769 reset_cmd->cmd_status = ENODATA;
1770 instance->instancet->fire_cmd(instance,
1771 reset_cmd->frame_phys_addr,
1772 0, instance->reg_set);
1773 } else {
1774 printk(KERN_NOTICE "megasas: %p unexpected"
1775 "cmds lst\n",
1776 reset_cmd);
1777 }
1778 reset_index++;
1779 }
1780
1781 return SUCCESS;
1782 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001783
1784 for (i = 0; i < wait_time; i++) {
1785
Sumant Patroe4a082c2006-05-30 12:03:37 -07001786 int outstanding = atomic_read(&instance->fw_outstanding);
1787
1788 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001789 break;
1790
1791 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1792 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001793 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001794 /*
1795 * Call cmd completion routine. Cmd to be
1796 * be completed directly without depending on isr.
1797 */
1798 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001799 }
1800
1801 msleep(1000);
1802 }
1803
Yang, Bo707e09b2010-10-12 07:20:27 -06001804 i = 0;
1805 kill_adapter_flag = 0;
1806 do {
1807 fw_state = instance->instancet->read_fw_status_reg(
1808 instance->reg_set) & MFI_STATE_MASK;
1809 if ((fw_state == MFI_STATE_FAULT) &&
1810 (instance->disableOnlineCtrlReset == 0)) {
1811 if (i == 3) {
1812 kill_adapter_flag = 2;
1813 break;
1814 }
1815 megasas_do_ocr(instance);
1816 kill_adapter_flag = 1;
1817
1818 /* wait for 1 secs to let FW finish the pending cmds */
1819 msleep(1000);
1820 }
1821 i++;
1822 } while (i <= 3);
1823
1824 if (atomic_read(&instance->fw_outstanding) &&
1825 !kill_adapter_flag) {
1826 if (instance->disableOnlineCtrlReset == 0) {
1827
1828 megasas_do_ocr(instance);
1829
1830 /* wait for 5 secs to let FW finish the pending cmds */
1831 for (i = 0; i < wait_time; i++) {
1832 int outstanding =
1833 atomic_read(&instance->fw_outstanding);
1834 if (!outstanding)
1835 return SUCCESS;
1836 msleep(1000);
1837 }
1838 }
1839 }
1840
1841 if (atomic_read(&instance->fw_outstanding) ||
1842 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001843 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001844 /*
1845 * Send signal to FW to stop processing any pending cmds.
1846 * The controller will be taken offline by the OS now.
1847 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001848 if ((instance->pdev->device ==
1849 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1850 (instance->pdev->device ==
1851 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1852 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001853 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001854 } else {
1855 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001856 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001857 }
Sumant Patro658dced2006-10-03 13:09:14 -07001858 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001859 spin_lock_irqsave(&instance->hba_lock, flags);
1860 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1861 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001862 return FAILED;
1863 }
1864
bo yang39a98552010-09-22 22:36:29 -04001865 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1866
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001867 return SUCCESS;
1868}
1869
1870/**
1871 * megasas_generic_reset - Generic reset routine
1872 * @scmd: Mid-layer SCSI command
1873 *
1874 * This routine implements a generic reset handler for device, bus and host
1875 * reset requests. Device, bus and host specific reset handlers can use this
1876 * function after they do their specific tasks.
1877 */
1878static int megasas_generic_reset(struct scsi_cmnd *scmd)
1879{
1880 int ret_val;
1881 struct megasas_instance *instance;
1882
1883 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1884
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001885 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1886 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001887
bo yang39a98552010-09-22 22:36:29 -04001888 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001889 printk(KERN_ERR "megasas: cannot recover from previous reset "
1890 "failures\n");
1891 return FAILED;
1892 }
1893
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001894 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001895 if (ret_val == SUCCESS)
1896 printk(KERN_NOTICE "megasas: reset successful \n");
1897 else
1898 printk(KERN_ERR "megasas: failed to do reset\n");
1899
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001900 return ret_val;
1901}
1902
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001903/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001904 * megasas_reset_timer - quiesce the adapter if required
1905 * @scmd: scsi cmnd
1906 *
1907 * Sets the FW busy flag and reduces the host->can_queue if the
1908 * cmd has not been completed within the timeout period.
1909 */
1910static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001911blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001912{
1913 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1914 struct megasas_instance *instance;
1915 unsigned long flags;
1916
1917 if (time_after(jiffies, scmd->jiffies_at_alloc +
1918 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001919 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001920 }
1921
1922 instance = cmd->instance;
1923 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1924 /* FW is busy, throttle IO */
1925 spin_lock_irqsave(instance->host->host_lock, flags);
1926
1927 instance->host->can_queue = 16;
1928 instance->last_time = jiffies;
1929 instance->flag |= MEGASAS_FW_BUSY;
1930
1931 spin_unlock_irqrestore(instance->host->host_lock, flags);
1932 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001933 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001934}
1935
1936/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001937 * megasas_reset_device - Device reset handler entry point
1938 */
1939static int megasas_reset_device(struct scsi_cmnd *scmd)
1940{
1941 int ret;
1942
1943 /*
1944 * First wait for all commands to complete
1945 */
1946 ret = megasas_generic_reset(scmd);
1947
1948 return ret;
1949}
1950
1951/**
1952 * megasas_reset_bus_host - Bus & host reset handler entry point
1953 */
1954static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1955{
1956 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001957 struct megasas_instance *instance;
1958 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001959
1960 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001961 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001962 */
adam radford36807e62011-10-08 18:15:06 -07001963 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1964 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08001965 ret = megasas_reset_fusion(scmd->device->host);
1966 else
1967 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001968
1969 return ret;
1970}
1971
1972/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001973 * megasas_bios_param - Returns disk geometry for a disk
1974 * @sdev: device handle
1975 * @bdev: block device
1976 * @capacity: drive capacity
1977 * @geom: geometry parameters
1978 */
1979static int
1980megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1981 sector_t capacity, int geom[])
1982{
1983 int heads;
1984 int sectors;
1985 sector_t cylinders;
1986 unsigned long tmp;
1987 /* Default heads (64) & sectors (32) */
1988 heads = 64;
1989 sectors = 32;
1990
1991 tmp = heads * sectors;
1992 cylinders = capacity;
1993
1994 sector_div(cylinders, tmp);
1995
1996 /*
1997 * Handle extended translation size for logical drives > 1Gb
1998 */
1999
2000 if (capacity >= 0x200000) {
2001 heads = 255;
2002 sectors = 63;
2003 tmp = heads*sectors;
2004 cylinders = capacity;
2005 sector_div(cylinders, tmp);
2006 }
2007
2008 geom[0] = heads;
2009 geom[1] = sectors;
2010 geom[2] = cylinders;
2011
2012 return 0;
2013}
2014
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002015static void megasas_aen_polling(struct work_struct *work);
2016
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002017/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002018 * megasas_service_aen - Processes an event notification
2019 * @instance: Adapter soft state
2020 * @cmd: AEN command completed by the ISR
2021 *
2022 * For AEN, driver sends a command down to FW that is held by the FW till an
2023 * event occurs. When an event of interest occurs, FW completes the command
2024 * that it was previously holding.
2025 *
2026 * This routines sends SIGIO signal to processes that have registered with the
2027 * driver for AEN.
2028 */
2029static void
2030megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2031{
Yang, Boc3518832009-10-06 14:18:02 -06002032 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002033 /*
2034 * Don't signal app if it is just an aborted previously registered aen
2035 */
Yang, Boc3518832009-10-06 14:18:02 -06002036 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2037 spin_lock_irqsave(&poll_aen_lock, flags);
2038 megasas_poll_wait_aen = 1;
2039 spin_unlock_irqrestore(&poll_aen_lock, flags);
2040 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002041 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002042 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002043 else
2044 cmd->abort_aen = 0;
2045
2046 instance->aen_cmd = NULL;
2047 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002048
bo yang39a98552010-09-22 22:36:29 -04002049 if ((instance->unload == 0) &&
2050 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002051 struct megasas_aen_event *ev;
2052 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2053 if (!ev) {
2054 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2055 } else {
2056 ev->instance = instance;
2057 instance->ev = ev;
2058 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2059 schedule_delayed_work(
2060 (struct delayed_work *)&ev->hotplug_work, 0);
2061 }
2062 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002063}
2064
adam radford4bcde502011-07-26 15:42:52 -07002065static int megasas_change_queue_depth(struct scsi_device *sdev,
2066 int queue_depth, int reason)
2067{
2068 if (reason != SCSI_QDEPTH_DEFAULT)
2069 return -EOPNOTSUPP;
2070
2071 if (queue_depth > sdev->host->can_queue)
2072 queue_depth = sdev->host->can_queue;
2073 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2074 queue_depth);
2075
2076 return queue_depth;
2077}
2078
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002079/*
2080 * Scsi host template for megaraid_sas driver
2081 */
2082static struct scsi_host_template megasas_template = {
2083
2084 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002085 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002086 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002087 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002088 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002089 .queuecommand = megasas_queue_command,
2090 .eh_device_reset_handler = megasas_reset_device,
2091 .eh_bus_reset_handler = megasas_reset_bus_host,
2092 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002093 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002094 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002095 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002096 .change_queue_depth = megasas_change_queue_depth,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002097};
2098
2099/**
2100 * megasas_complete_int_cmd - Completes an internal command
2101 * @instance: Adapter soft state
2102 * @cmd: Command to be completed
2103 *
2104 * The megasas_issue_blocked_cmd() function waits for a command to complete
2105 * after it issues a command. This function wakes up that waiting routine by
2106 * calling wake_up() on the wait queue.
2107 */
2108static void
2109megasas_complete_int_cmd(struct megasas_instance *instance,
2110 struct megasas_cmd *cmd)
2111{
2112 cmd->cmd_status = cmd->frame->io.cmd_status;
2113
2114 if (cmd->cmd_status == ENODATA) {
2115 cmd->cmd_status = 0;
2116 }
2117 wake_up(&instance->int_cmd_wait_q);
2118}
2119
2120/**
2121 * megasas_complete_abort - Completes aborting a command
2122 * @instance: Adapter soft state
2123 * @cmd: Cmd that was issued to abort another cmd
2124 *
adam radford0d490162010-12-14 19:17:17 -08002125 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2126 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002127 * wakes up all functions waiting on the same wait queue.
2128 */
2129static void
2130megasas_complete_abort(struct megasas_instance *instance,
2131 struct megasas_cmd *cmd)
2132{
2133 if (cmd->sync_cmd) {
2134 cmd->sync_cmd = 0;
2135 cmd->cmd_status = 0;
2136 wake_up(&instance->abort_cmd_wait_q);
2137 }
2138
2139 return;
2140}
2141
2142/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002143 * megasas_complete_cmd - Completes a command
2144 * @instance: Adapter soft state
2145 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002146 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002147 * SCSI mid-layer instead of the value returned
2148 * by the FW. This should be used if caller wants
2149 * an alternate status (as in the case of aborted
2150 * commands)
2151 */
adam radford9c915a82010-12-21 13:34:31 -08002152void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002153megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2154 u8 alt_status)
2155{
2156 int exception = 0;
2157 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002158 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002159 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002160
bo yang39a98552010-09-22 22:36:29 -04002161 /* flag for the retry reset */
2162 cmd->retry_for_fw_reset = 0;
2163
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002164 if (cmd->scmd)
2165 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002166
2167 switch (hdr->cmd) {
2168
2169 case MFI_CMD_PD_SCSI_IO:
2170 case MFI_CMD_LD_SCSI_IO:
2171
2172 /*
2173 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2174 * issued either through an IO path or an IOCTL path. If it
2175 * was via IOCTL, we will send it to internal completion.
2176 */
2177 if (cmd->sync_cmd) {
2178 cmd->sync_cmd = 0;
2179 megasas_complete_int_cmd(instance, cmd);
2180 break;
2181 }
2182
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002183 case MFI_CMD_LD_READ:
2184 case MFI_CMD_LD_WRITE:
2185
2186 if (alt_status) {
2187 cmd->scmd->result = alt_status << 16;
2188 exception = 1;
2189 }
2190
2191 if (exception) {
2192
Sumant Patroe4a082c2006-05-30 12:03:37 -07002193 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002194
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002195 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002196 cmd->scmd->scsi_done(cmd->scmd);
2197 megasas_return_cmd(instance, cmd);
2198
2199 break;
2200 }
2201
2202 switch (hdr->cmd_status) {
2203
2204 case MFI_STAT_OK:
2205 cmd->scmd->result = DID_OK << 16;
2206 break;
2207
2208 case MFI_STAT_SCSI_IO_FAILED:
2209 case MFI_STAT_LD_INIT_IN_PROGRESS:
2210 cmd->scmd->result =
2211 (DID_ERROR << 16) | hdr->scsi_status;
2212 break;
2213
2214 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2215
2216 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2217
2218 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2219 memset(cmd->scmd->sense_buffer, 0,
2220 SCSI_SENSE_BUFFERSIZE);
2221 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2222 hdr->sense_len);
2223
2224 cmd->scmd->result |= DRIVER_SENSE << 24;
2225 }
2226
2227 break;
2228
2229 case MFI_STAT_LD_OFFLINE:
2230 case MFI_STAT_DEVICE_NOT_FOUND:
2231 cmd->scmd->result = DID_BAD_TARGET << 16;
2232 break;
2233
2234 default:
2235 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2236 hdr->cmd_status);
2237 cmd->scmd->result = DID_ERROR << 16;
2238 break;
2239 }
2240
Sumant Patroe4a082c2006-05-30 12:03:37 -07002241 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002242
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002243 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002244 cmd->scmd->scsi_done(cmd->scmd);
2245 megasas_return_cmd(instance, cmd);
2246
2247 break;
2248
2249 case MFI_CMD_SMP:
2250 case MFI_CMD_STP:
2251 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002252 /* Check for LD map update */
2253 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2254 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2255 spin_lock_irqsave(instance->host->host_lock, flags);
2256 if (cmd->frame->hdr.cmd_status != 0) {
2257 if (cmd->frame->hdr.cmd_status !=
2258 MFI_STAT_NOT_FOUND)
2259 printk(KERN_WARNING "megasas: map sync"
2260 "failed, status = 0x%x.\n",
2261 cmd->frame->hdr.cmd_status);
2262 else {
2263 megasas_return_cmd(instance, cmd);
2264 spin_unlock_irqrestore(
2265 instance->host->host_lock,
2266 flags);
2267 break;
2268 }
2269 } else
2270 instance->map_id++;
2271 megasas_return_cmd(instance, cmd);
2272 if (MR_ValidateMapInfo(
2273 fusion->ld_map[(instance->map_id & 1)],
2274 fusion->load_balance_info))
2275 fusion->fast_path_io = 1;
2276 else
2277 fusion->fast_path_io = 0;
2278 megasas_sync_map_info(instance);
2279 spin_unlock_irqrestore(instance->host->host_lock,
2280 flags);
2281 break;
2282 }
Yang, Boc3518832009-10-06 14:18:02 -06002283 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2284 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2285 spin_lock_irqsave(&poll_aen_lock, flags);
2286 megasas_poll_wait_aen = 0;
2287 spin_unlock_irqrestore(&poll_aen_lock, flags);
2288 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002289
2290 /*
2291 * See if got an event notification
2292 */
2293 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2294 megasas_service_aen(instance, cmd);
2295 else
2296 megasas_complete_int_cmd(instance, cmd);
2297
2298 break;
2299
2300 case MFI_CMD_ABORT:
2301 /*
2302 * Cmd issued to abort another cmd returned
2303 */
2304 megasas_complete_abort(instance, cmd);
2305 break;
2306
2307 default:
2308 printk("megasas: Unknown command completed! [0x%X]\n",
2309 hdr->cmd);
2310 break;
2311 }
2312}
2313
2314/**
bo yang39a98552010-09-22 22:36:29 -04002315 * megasas_issue_pending_cmds_again - issue all pending cmds
2316 * in FW again because of the fw reset
2317 * @instance: Adapter soft state
2318 */
2319static inline void
2320megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2321{
2322 struct megasas_cmd *cmd;
2323 struct list_head clist_local;
2324 union megasas_evt_class_locale class_locale;
2325 unsigned long flags;
2326 u32 seq_num;
2327
2328 INIT_LIST_HEAD(&clist_local);
2329 spin_lock_irqsave(&instance->hba_lock, flags);
2330 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2331 spin_unlock_irqrestore(&instance->hba_lock, flags);
2332
2333 while (!list_empty(&clist_local)) {
2334 cmd = list_entry((&clist_local)->next,
2335 struct megasas_cmd, list);
2336 list_del_init(&cmd->list);
2337
2338 if (cmd->sync_cmd || cmd->scmd) {
2339 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2340 "detected to be pending while HBA reset.\n",
2341 cmd, cmd->scmd, cmd->sync_cmd);
2342
2343 cmd->retry_for_fw_reset++;
2344
2345 if (cmd->retry_for_fw_reset == 3) {
2346 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2347 "was tried multiple times during reset."
2348 "Shutting down the HBA\n",
2349 cmd, cmd->scmd, cmd->sync_cmd);
2350 megaraid_sas_kill_hba(instance);
2351
2352 instance->adprecovery =
2353 MEGASAS_HW_CRITICAL_ERROR;
2354 return;
2355 }
2356 }
2357
2358 if (cmd->sync_cmd == 1) {
2359 if (cmd->scmd) {
2360 printk(KERN_NOTICE "megaraid_sas: unexpected"
2361 "cmd attached to internal command!\n");
2362 }
2363 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2364 "on the internal reset queue,"
2365 "issue it again.\n", cmd);
2366 cmd->cmd_status = ENODATA;
2367 instance->instancet->fire_cmd(instance,
2368 cmd->frame_phys_addr ,
2369 0, instance->reg_set);
2370 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002371 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002372 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002373 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002374
2375 atomic_inc(&instance->fw_outstanding);
2376 instance->instancet->fire_cmd(instance,
2377 cmd->frame_phys_addr,
2378 cmd->frame_count-1, instance->reg_set);
2379 } else {
2380 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2381 "internal reset defer list while re-issue!!\n",
2382 cmd);
2383 }
2384 }
2385
2386 if (instance->aen_cmd) {
2387 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2388 megasas_return_cmd(instance, instance->aen_cmd);
2389
2390 instance->aen_cmd = NULL;
2391 }
2392
2393 /*
2394 * Initiate AEN (Asynchronous Event Notification)
2395 */
2396 seq_num = instance->last_seq_num;
2397 class_locale.members.reserved = 0;
2398 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2399 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2400
2401 megasas_register_aen(instance, seq_num, class_locale.word);
2402}
2403
2404/**
2405 * Move the internal reset pending commands to a deferred queue.
2406 *
2407 * We move the commands pending at internal reset time to a
2408 * pending queue. This queue would be flushed after successful
2409 * completion of the internal reset sequence. if the internal reset
2410 * did not complete in time, the kernel reset handler would flush
2411 * these commands.
2412 **/
2413static void
2414megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2415{
2416 struct megasas_cmd *cmd;
2417 int i;
2418 u32 max_cmd = instance->max_fw_cmds;
2419 u32 defer_index;
2420 unsigned long flags;
2421
2422 defer_index = 0;
2423 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2424 for (i = 0; i < max_cmd; i++) {
2425 cmd = instance->cmd_list[i];
2426 if (cmd->sync_cmd == 1 || cmd->scmd) {
2427 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2428 "on the defer queue as internal\n",
2429 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2430
2431 if (!list_empty(&cmd->list)) {
2432 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2433 " moving this cmd:%p, %d %p, it was"
2434 "discovered on some list?\n",
2435 cmd, cmd->sync_cmd, cmd->scmd);
2436
2437 list_del_init(&cmd->list);
2438 }
2439 defer_index++;
2440 list_add_tail(&cmd->list,
2441 &instance->internal_reset_pending_q);
2442 }
2443 }
2444 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2445}
2446
2447
2448static void
2449process_fw_state_change_wq(struct work_struct *work)
2450{
2451 struct megasas_instance *instance =
2452 container_of(work, struct megasas_instance, work_init);
2453 u32 wait;
2454 unsigned long flags;
2455
2456 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2457 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2458 instance->adprecovery);
2459 return ;
2460 }
2461
2462 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2463 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2464 "state, restarting it...\n");
2465
2466 instance->instancet->disable_intr(instance->reg_set);
2467 atomic_set(&instance->fw_outstanding, 0);
2468
2469 atomic_set(&instance->fw_reset_no_pci_access, 1);
2470 instance->instancet->adp_reset(instance, instance->reg_set);
2471 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2472
2473 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2474 "initiating next stage...\n");
2475
2476 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2477 "state 2 starting...\n");
2478
2479 /*waitting for about 20 second before start the second init*/
2480 for (wait = 0; wait < 30; wait++) {
2481 msleep(1000);
2482 }
2483
adam radford058a8fa2011-10-08 18:14:27 -07002484 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04002485 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2486
2487 megaraid_sas_kill_hba(instance);
2488 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2489 return ;
2490 }
2491
2492 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2493 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2494 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2495 ) {
2496 *instance->consumer = *instance->producer;
2497 } else {
2498 *instance->consumer = 0;
2499 *instance->producer = 0;
2500 }
2501
2502 megasas_issue_init_mfi(instance);
2503
2504 spin_lock_irqsave(&instance->hba_lock, flags);
2505 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2506 spin_unlock_irqrestore(&instance->hba_lock, flags);
2507 instance->instancet->enable_intr(instance->reg_set);
2508
2509 megasas_issue_pending_cmds_again(instance);
2510 instance->issuepend_done = 1;
2511 }
2512 return ;
2513}
2514
2515/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002516 * megasas_deplete_reply_queue - Processes all completed commands
2517 * @instance: Adapter soft state
2518 * @alt_status: Alternate status to be returned to
2519 * SCSI mid-layer instead of the status
2520 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002521 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002522 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002523static int
bo yang39a98552010-09-22 22:36:29 -04002524megasas_deplete_reply_queue(struct megasas_instance *instance,
2525 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002526{
bo yang39a98552010-09-22 22:36:29 -04002527 u32 mfiStatus;
2528 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002529
bo yang39a98552010-09-22 22:36:29 -04002530 if ((mfiStatus = instance->instancet->check_reset(instance,
2531 instance->reg_set)) == 1) {
2532 return IRQ_HANDLED;
2533 }
2534
2535 if ((mfiStatus = instance->instancet->clear_intr(
2536 instance->reg_set)
2537 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002538 /* Hardware may not set outbound_intr_status in MSI-X mode */
2539 if (!instance->msi_flag)
2540 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002541 }
2542
2543 instance->mfiStatus = mfiStatus;
2544
2545 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2546 fw_state = instance->instancet->read_fw_status_reg(
2547 instance->reg_set) & MFI_STATE_MASK;
2548
2549 if (fw_state != MFI_STATE_FAULT) {
2550 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2551 fw_state);
2552 }
2553
2554 if ((fw_state == MFI_STATE_FAULT) &&
2555 (instance->disableOnlineCtrlReset == 0)) {
2556 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2557
2558 if ((instance->pdev->device ==
2559 PCI_DEVICE_ID_LSI_SAS1064R) ||
2560 (instance->pdev->device ==
2561 PCI_DEVICE_ID_DELL_PERC5) ||
2562 (instance->pdev->device ==
2563 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2564
2565 *instance->consumer =
2566 MEGASAS_ADPRESET_INPROG_SIGN;
2567 }
2568
2569
2570 instance->instancet->disable_intr(instance->reg_set);
2571 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2572 instance->issuepend_done = 0;
2573
2574 atomic_set(&instance->fw_outstanding, 0);
2575 megasas_internal_reset_defer_cmds(instance);
2576
2577 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2578 fw_state, instance->adprecovery);
2579
2580 schedule_work(&instance->work_init);
2581 return IRQ_HANDLED;
2582
2583 } else {
2584 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2585 fw_state, instance->disableOnlineCtrlReset);
2586 }
2587 }
2588
Sumant Patro5d018ad2006-10-03 13:13:18 -07002589 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002590 return IRQ_HANDLED;
2591}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002592/**
2593 * megasas_isr - isr entry point
2594 */
David Howells7d12e782006-10-05 14:55:46 +01002595static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002596{
bo yang39a98552010-09-22 22:36:29 -04002597 struct megasas_instance *instance;
2598 unsigned long flags;
2599 irqreturn_t rc;
2600
2601 if (atomic_read(
2602 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2603 return IRQ_HANDLED;
2604
2605 instance = (struct megasas_instance *)devp;
2606
2607 spin_lock_irqsave(&instance->hba_lock, flags);
2608 rc = megasas_deplete_reply_queue(instance, DID_OK);
2609 spin_unlock_irqrestore(&instance->hba_lock, flags);
2610
2611 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002612}
2613
2614/**
2615 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002616 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002617 *
2618 * During the initialization, FW passes can potentially be in any one of
2619 * several possible states. If the FW in operational, waiting-for-handshake
2620 * states, driver must take steps to bring it to ready state. Otherwise, it
2621 * has to wait for the ready state.
2622 */
adam radford9c915a82010-12-21 13:34:31 -08002623int
adam radford058a8fa2011-10-08 18:14:27 -07002624megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002625{
2626 int i;
2627 u8 max_wait;
2628 u32 fw_state;
2629 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002630 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002631
Sumant Patro1341c932006-01-25 12:02:40 -08002632 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002633
Sumant Patroe3bbff92006-10-03 12:28:49 -07002634 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002635 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2636 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002637
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002638 while (fw_state != MFI_STATE_READY) {
2639
Yang, Bo7218df62009-10-06 14:52:20 -06002640 abs_state =
2641 instance->instancet->read_fw_status_reg(instance->reg_set);
2642
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002643 switch (fw_state) {
2644
2645 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002646 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07002647 if (ocr) {
2648 max_wait = MEGASAS_RESET_WAIT_TIME;
2649 cur_state = MFI_STATE_FAULT;
2650 break;
2651 } else
2652 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002653
2654 case MFI_STATE_WAIT_HANDSHAKE:
2655 /*
2656 * Set the CLR bit in inbound doorbell
2657 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002658 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002659 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2660 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002661 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2662 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002663 PCI_DEVICE_ID_LSI_FUSION) ||
2664 (instance->pdev->device ==
2665 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002666 writel(
2667 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002668 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002669 } else {
2670 writel(
2671 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2672 &instance->reg_set->inbound_doorbell);
2673 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002674
Yang, Bo7218df62009-10-06 14:52:20 -06002675 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002676 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2677 break;
2678
Sumant Patroe3bbff92006-10-03 12:28:49 -07002679 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002680 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002681 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2682 (instance->pdev->device ==
2683 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2684 (instance->pdev->device ==
adam radford36807e62011-10-08 18:15:06 -07002685 PCI_DEVICE_ID_LSI_FUSION) ||
2686 (instance->pdev->device ==
2687 PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002688 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002689 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002690 } else
2691 writel(MFI_INIT_HOTPLUG,
2692 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002693
Yang, Bo7218df62009-10-06 14:52:20 -06002694 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002695 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2696 break;
2697
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002698 case MFI_STATE_OPERATIONAL:
2699 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002700 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002701 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002702 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002703 if ((instance->pdev->device ==
2704 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2705 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002706 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2707 (instance->pdev->device
adam radford36807e62011-10-08 18:15:06 -07002708 == PCI_DEVICE_ID_LSI_FUSION) ||
2709 (instance->pdev->device
2710 == PCI_DEVICE_ID_LSI_INVADER)) {
Yang, Bo87911122009-10-06 14:31:54 -06002711 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002712 &instance->reg_set->doorbell);
adam radford36807e62011-10-08 18:15:06 -07002713 if ((instance->pdev->device ==
2714 PCI_DEVICE_ID_LSI_FUSION) ||
2715 (instance->pdev->device ==
2716 PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08002717 for (i = 0; i < (10 * 1000); i += 20) {
2718 if (readl(
2719 &instance->
2720 reg_set->
2721 doorbell) & 1)
2722 msleep(20);
2723 else
2724 break;
2725 }
2726 }
Yang, Bo87911122009-10-06 14:31:54 -06002727 } else
2728 writel(MFI_RESET_FLAGS,
2729 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002730
Yang, Bo7218df62009-10-06 14:52:20 -06002731 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002732 cur_state = MFI_STATE_OPERATIONAL;
2733 break;
2734
2735 case MFI_STATE_UNDEFINED:
2736 /*
2737 * This state should not last for more than 2 seconds
2738 */
Yang, Bo7218df62009-10-06 14:52:20 -06002739 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002740 cur_state = MFI_STATE_UNDEFINED;
2741 break;
2742
2743 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002744 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002745 cur_state = MFI_STATE_BB_INIT;
2746 break;
2747
2748 case MFI_STATE_FW_INIT:
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_FW_INIT;
2751 break;
2752
2753 case MFI_STATE_FW_INIT_2:
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_FW_INIT_2;
2756 break;
2757
2758 case MFI_STATE_DEVICE_SCAN:
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_DEVICE_SCAN;
2761 break;
2762
2763 case MFI_STATE_FLUSH_CACHE:
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_FLUSH_CACHE;
2766 break;
2767
2768 default:
2769 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2770 fw_state);
2771 return -ENODEV;
2772 }
2773
2774 /*
2775 * The cur_state should not last for more than max_wait secs
2776 */
2777 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002778 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002779 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002780 curr_abs_state =
2781 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002782
Yang, Bo7218df62009-10-06 14:52:20 -06002783 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002784 msleep(1);
2785 } else
2786 break;
2787 }
2788
2789 /*
2790 * Return error if fw_state hasn't changed after max_wait
2791 */
Yang, Bo7218df62009-10-06 14:52:20 -06002792 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002793 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2794 "in %d secs\n", fw_state, max_wait);
2795 return -ENODEV;
2796 }
bo yang39a98552010-09-22 22:36:29 -04002797 }
adam radford0d490162010-12-14 19:17:17 -08002798 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799
2800 return 0;
2801}
2802
2803/**
2804 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2805 * @instance: Adapter soft state
2806 */
2807static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2808{
2809 int i;
adam radford9c915a82010-12-21 13:34:31 -08002810 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002811 struct megasas_cmd *cmd;
2812
2813 if (!instance->frame_dma_pool)
2814 return;
2815
2816 /*
2817 * Return all frames to pool
2818 */
2819 for (i = 0; i < max_cmd; i++) {
2820
2821 cmd = instance->cmd_list[i];
2822
2823 if (cmd->frame)
2824 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2825 cmd->frame_phys_addr);
2826
2827 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002828 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002829 cmd->sense_phys_addr);
2830 }
2831
2832 /*
2833 * Now destroy the pool itself
2834 */
2835 pci_pool_destroy(instance->frame_dma_pool);
2836 pci_pool_destroy(instance->sense_dma_pool);
2837
2838 instance->frame_dma_pool = NULL;
2839 instance->sense_dma_pool = NULL;
2840}
2841
2842/**
2843 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2844 * @instance: Adapter soft state
2845 *
2846 * Each command packet has an embedded DMA memory buffer that is used for
2847 * filling MFI frame and the SG list that immediately follows the frame. This
2848 * function creates those DMA memory buffers for each command packet by using
2849 * PCI pool facility.
2850 */
2851static int megasas_create_frame_pool(struct megasas_instance *instance)
2852{
2853 int i;
2854 u32 max_cmd;
2855 u32 sge_sz;
2856 u32 sgl_sz;
2857 u32 total_sz;
2858 u32 frame_count;
2859 struct megasas_cmd *cmd;
2860
adam radford9c915a82010-12-21 13:34:31 -08002861 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002862
2863 /*
2864 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2865 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2866 */
2867 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2868 sizeof(struct megasas_sge32);
2869
Yang, Bof4c9a132009-10-06 14:43:28 -06002870 if (instance->flag_ieee) {
2871 sge_sz = sizeof(struct megasas_sge_skinny);
2872 }
2873
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002874 /*
2875 * Calculated the number of 64byte frames required for SGL
2876 */
2877 sgl_sz = sge_sz * instance->max_num_sge;
2878 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002879 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002880
2881 /*
2882 * We need one extra frame for the MFI command
2883 */
2884 frame_count++;
2885
2886 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2887 /*
2888 * Use DMA pool facility provided by PCI layer
2889 */
2890 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2891 instance->pdev, total_sz, 64,
2892 0);
2893
2894 if (!instance->frame_dma_pool) {
2895 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2896 return -ENOMEM;
2897 }
2898
2899 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2900 instance->pdev, 128, 4, 0);
2901
2902 if (!instance->sense_dma_pool) {
2903 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2904
2905 pci_pool_destroy(instance->frame_dma_pool);
2906 instance->frame_dma_pool = NULL;
2907
2908 return -ENOMEM;
2909 }
2910
2911 /*
2912 * Allocate and attach a frame to each of the commands in cmd_list.
2913 * By making cmd->index as the context instead of the &cmd, we can
2914 * always use 32bit context regardless of the architecture
2915 */
2916 for (i = 0; i < max_cmd; i++) {
2917
2918 cmd = instance->cmd_list[i];
2919
2920 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2921 GFP_KERNEL, &cmd->frame_phys_addr);
2922
2923 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2924 GFP_KERNEL, &cmd->sense_phys_addr);
2925
2926 /*
2927 * megasas_teardown_frame_pool() takes care of freeing
2928 * whatever has been allocated
2929 */
2930 if (!cmd->frame || !cmd->sense) {
2931 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2932 megasas_teardown_frame_pool(instance);
2933 return -ENOMEM;
2934 }
2935
Yang, Bo707e09b2010-10-12 07:20:27 -06002936 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002937 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002938 cmd->frame->io.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002939 }
2940
2941 return 0;
2942}
2943
2944/**
2945 * megasas_free_cmds - Free all the cmds in the free cmd pool
2946 * @instance: Adapter soft state
2947 */
adam radford9c915a82010-12-21 13:34:31 -08002948void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002949{
2950 int i;
2951 /* First free the MFI frame pool */
2952 megasas_teardown_frame_pool(instance);
2953
2954 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002955 for (i = 0; i < instance->max_mfi_cmds; i++)
2956
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002957 kfree(instance->cmd_list[i]);
2958
2959 /* Free the cmd_list buffer itself */
2960 kfree(instance->cmd_list);
2961 instance->cmd_list = NULL;
2962
2963 INIT_LIST_HEAD(&instance->cmd_pool);
2964}
2965
2966/**
2967 * megasas_alloc_cmds - Allocates the command packets
2968 * @instance: Adapter soft state
2969 *
2970 * Each command that is issued to the FW, whether IO commands from the OS or
2971 * internal commands like IOCTLs, are wrapped in local data structure called
2972 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2973 * the FW.
2974 *
2975 * Each frame has a 32-bit field called context (tag). This context is used
2976 * to get back the megasas_cmd from the frame when a frame gets completed in
2977 * the ISR. Typically the address of the megasas_cmd itself would be used as
2978 * the context. But we wanted to keep the differences between 32 and 64 bit
2979 * systems to the mininum. We always use 32 bit integers for the context. In
2980 * this driver, the 32 bit values are the indices into an array cmd_list.
2981 * This array is used only to look up the megasas_cmd given the context. The
2982 * free commands themselves are maintained in a linked list called cmd_pool.
2983 */
adam radford9c915a82010-12-21 13:34:31 -08002984int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002985{
2986 int i;
2987 int j;
2988 u32 max_cmd;
2989 struct megasas_cmd *cmd;
2990
adam radford9c915a82010-12-21 13:34:31 -08002991 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002992
2993 /*
2994 * instance->cmd_list is an array of struct megasas_cmd pointers.
2995 * Allocate the dynamic array first and then allocate individual
2996 * commands.
2997 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002998 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002999
3000 if (!instance->cmd_list) {
3001 printk(KERN_DEBUG "megasas: out of memory\n");
3002 return -ENOMEM;
3003 }
3004
adam radford9c915a82010-12-21 13:34:31 -08003005 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003006
3007 for (i = 0; i < max_cmd; i++) {
3008 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3009 GFP_KERNEL);
3010
3011 if (!instance->cmd_list[i]) {
3012
3013 for (j = 0; j < i; j++)
3014 kfree(instance->cmd_list[j]);
3015
3016 kfree(instance->cmd_list);
3017 instance->cmd_list = NULL;
3018
3019 return -ENOMEM;
3020 }
3021 }
3022
3023 /*
3024 * Add all the commands to command pool (instance->cmd_pool)
3025 */
3026 for (i = 0; i < max_cmd; i++) {
3027 cmd = instance->cmd_list[i];
3028 memset(cmd, 0, sizeof(struct megasas_cmd));
3029 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003030 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003031 cmd->instance = instance;
3032
3033 list_add_tail(&cmd->list, &instance->cmd_pool);
3034 }
3035
3036 /*
3037 * Create a frame pool and assign one frame to each cmd
3038 */
3039 if (megasas_create_frame_pool(instance)) {
3040 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3041 megasas_free_cmds(instance);
3042 }
3043
3044 return 0;
3045}
3046
Yang, Bo81e403c2009-10-06 14:27:54 -06003047/*
3048 * megasas_get_pd_list_info - Returns FW's pd_list structure
3049 * @instance: Adapter soft state
3050 * @pd_list: pd_list structure
3051 *
3052 * Issues an internal command (DCMD) to get the FW's controller PD
3053 * list structure. This information is mainly used to find out SYSTEM
3054 * supported by the FW.
3055 */
3056static int
3057megasas_get_pd_list(struct megasas_instance *instance)
3058{
3059 int ret = 0, pd_index = 0;
3060 struct megasas_cmd *cmd;
3061 struct megasas_dcmd_frame *dcmd;
3062 struct MR_PD_LIST *ci;
3063 struct MR_PD_ADDRESS *pd_addr;
3064 dma_addr_t ci_h = 0;
3065
3066 cmd = megasas_get_cmd(instance);
3067
3068 if (!cmd) {
3069 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3070 return -ENOMEM;
3071 }
3072
3073 dcmd = &cmd->frame->dcmd;
3074
3075 ci = pci_alloc_consistent(instance->pdev,
3076 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3077
3078 if (!ci) {
3079 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3080 megasas_return_cmd(instance, cmd);
3081 return -ENOMEM;
3082 }
3083
3084 memset(ci, 0, sizeof(*ci));
3085 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3086
3087 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3088 dcmd->mbox.b[1] = 0;
3089 dcmd->cmd = MFI_CMD_DCMD;
3090 dcmd->cmd_status = 0xFF;
3091 dcmd->sge_count = 1;
3092 dcmd->flags = MFI_FRAME_DIR_READ;
3093 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003094 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003095 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3096 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3097 dcmd->sgl.sge32[0].phys_addr = ci_h;
3098 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3099
3100 if (!megasas_issue_polled(instance, cmd)) {
3101 ret = 0;
3102 } else {
3103 ret = -1;
3104 }
3105
3106 /*
3107 * the following function will get the instance PD LIST.
3108 */
3109
3110 pd_addr = ci->addr;
3111
3112 if ( ret == 0 &&
3113 (ci->count <
3114 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3115
3116 memset(instance->pd_list, 0,
3117 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3118
3119 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3120
3121 instance->pd_list[pd_addr->deviceId].tid =
3122 pd_addr->deviceId;
3123 instance->pd_list[pd_addr->deviceId].driveType =
3124 pd_addr->scsiDevType;
3125 instance->pd_list[pd_addr->deviceId].driveState =
3126 MR_PD_STATE_SYSTEM;
3127 pd_addr++;
3128 }
3129 }
3130
3131 pci_free_consistent(instance->pdev,
3132 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3133 ci, ci_h);
3134 megasas_return_cmd(instance, cmd);
3135
3136 return ret;
3137}
3138
Yang, Bobdc6fb82009-12-06 08:30:19 -07003139/*
3140 * megasas_get_ld_list_info - Returns FW's ld_list structure
3141 * @instance: Adapter soft state
3142 * @ld_list: ld_list structure
3143 *
3144 * Issues an internal command (DCMD) to get the FW's controller PD
3145 * list structure. This information is mainly used to find out SYSTEM
3146 * supported by the FW.
3147 */
3148static int
3149megasas_get_ld_list(struct megasas_instance *instance)
3150{
3151 int ret = 0, ld_index = 0, ids = 0;
3152 struct megasas_cmd *cmd;
3153 struct megasas_dcmd_frame *dcmd;
3154 struct MR_LD_LIST *ci;
3155 dma_addr_t ci_h = 0;
3156
3157 cmd = megasas_get_cmd(instance);
3158
3159 if (!cmd) {
3160 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3161 return -ENOMEM;
3162 }
3163
3164 dcmd = &cmd->frame->dcmd;
3165
3166 ci = pci_alloc_consistent(instance->pdev,
3167 sizeof(struct MR_LD_LIST),
3168 &ci_h);
3169
3170 if (!ci) {
3171 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3172 megasas_return_cmd(instance, cmd);
3173 return -ENOMEM;
3174 }
3175
3176 memset(ci, 0, sizeof(*ci));
3177 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3178
3179 dcmd->cmd = MFI_CMD_DCMD;
3180 dcmd->cmd_status = 0xFF;
3181 dcmd->sge_count = 1;
3182 dcmd->flags = MFI_FRAME_DIR_READ;
3183 dcmd->timeout = 0;
3184 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3185 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3186 dcmd->sgl.sge32[0].phys_addr = ci_h;
3187 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3188 dcmd->pad_0 = 0;
3189
3190 if (!megasas_issue_polled(instance, cmd)) {
3191 ret = 0;
3192 } else {
3193 ret = -1;
3194 }
3195
3196 /* the following function will get the instance PD LIST */
3197
bo yang39a98552010-09-22 22:36:29 -04003198 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003199 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3200
3201 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3202 if (ci->ldList[ld_index].state != 0) {
3203 ids = ci->ldList[ld_index].ref.targetId;
3204 instance->ld_ids[ids] =
3205 ci->ldList[ld_index].ref.targetId;
3206 }
3207 }
3208 }
3209
3210 pci_free_consistent(instance->pdev,
3211 sizeof(struct MR_LD_LIST),
3212 ci,
3213 ci_h);
3214
3215 megasas_return_cmd(instance, cmd);
3216 return ret;
3217}
3218
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003219/**
3220 * megasas_get_controller_info - Returns FW's controller structure
3221 * @instance: Adapter soft state
3222 * @ctrl_info: Controller information structure
3223 *
3224 * Issues an internal command (DCMD) to get the FW's controller structure.
3225 * This information is mainly used to find out the maximum IO transfer per
3226 * command supported by the FW.
3227 */
3228static int
3229megasas_get_ctrl_info(struct megasas_instance *instance,
3230 struct megasas_ctrl_info *ctrl_info)
3231{
3232 int ret = 0;
3233 struct megasas_cmd *cmd;
3234 struct megasas_dcmd_frame *dcmd;
3235 struct megasas_ctrl_info *ci;
3236 dma_addr_t ci_h = 0;
3237
3238 cmd = megasas_get_cmd(instance);
3239
3240 if (!cmd) {
3241 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3242 return -ENOMEM;
3243 }
3244
3245 dcmd = &cmd->frame->dcmd;
3246
3247 ci = pci_alloc_consistent(instance->pdev,
3248 sizeof(struct megasas_ctrl_info), &ci_h);
3249
3250 if (!ci) {
3251 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3252 megasas_return_cmd(instance, cmd);
3253 return -ENOMEM;
3254 }
3255
3256 memset(ci, 0, sizeof(*ci));
3257 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3258
3259 dcmd->cmd = MFI_CMD_DCMD;
3260 dcmd->cmd_status = 0xFF;
3261 dcmd->sge_count = 1;
3262 dcmd->flags = MFI_FRAME_DIR_READ;
3263 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003264 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003265 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3266 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3267 dcmd->sgl.sge32[0].phys_addr = ci_h;
3268 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3269
3270 if (!megasas_issue_polled(instance, cmd)) {
3271 ret = 0;
3272 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3273 } else {
3274 ret = -1;
3275 }
3276
3277 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3278 ci, ci_h);
3279
3280 megasas_return_cmd(instance, cmd);
3281 return ret;
3282}
3283
3284/**
bo yang31ea7082007-11-07 12:09:50 -05003285 * megasas_issue_init_mfi - Initializes the FW
3286 * @instance: Adapter soft state
3287 *
3288 * Issues the INIT MFI cmd
3289 */
3290static int
3291megasas_issue_init_mfi(struct megasas_instance *instance)
3292{
3293 u32 context;
3294
3295 struct megasas_cmd *cmd;
3296
3297 struct megasas_init_frame *init_frame;
3298 struct megasas_init_queue_info *initq_info;
3299 dma_addr_t init_frame_h;
3300 dma_addr_t initq_info_h;
3301
3302 /*
3303 * Prepare a init frame. Note the init frame points to queue info
3304 * structure. Each frame has SGL allocated after first 64 bytes. For
3305 * this frame - since we don't need any SGL - we use SGL's space as
3306 * queue info structure
3307 *
3308 * We will not get a NULL command below. We just created the pool.
3309 */
3310 cmd = megasas_get_cmd(instance);
3311
3312 init_frame = (struct megasas_init_frame *)cmd->frame;
3313 initq_info = (struct megasas_init_queue_info *)
3314 ((unsigned long)init_frame + 64);
3315
3316 init_frame_h = cmd->frame_phys_addr;
3317 initq_info_h = init_frame_h + 64;
3318
3319 context = init_frame->context;
3320 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3321 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3322 init_frame->context = context;
3323
3324 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3325 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3326
3327 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3328 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3329
3330 init_frame->cmd = MFI_CMD_INIT;
3331 init_frame->cmd_status = 0xFF;
3332 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3333
3334 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3335
3336 /*
3337 * disable the intr before firing the init frame to FW
3338 */
3339 instance->instancet->disable_intr(instance->reg_set);
3340
3341 /*
3342 * Issue the init frame in polled mode
3343 */
3344
3345 if (megasas_issue_polled(instance, cmd)) {
3346 printk(KERN_ERR "megasas: Failed to init firmware\n");
3347 megasas_return_cmd(instance, cmd);
3348 goto fail_fw_init;
3349 }
3350
3351 megasas_return_cmd(instance, cmd);
3352
3353 return 0;
3354
3355fail_fw_init:
3356 return -EINVAL;
3357}
3358
3359/**
bo yangad84db22007-11-09 04:40:16 -05003360 * megasas_start_timer - Initializes a timer object
3361 * @instance: Adapter soft state
3362 * @timer: timer object to be initialized
3363 * @fn: timer function
3364 * @interval: time interval between timer function call
3365 */
3366static inline void
3367megasas_start_timer(struct megasas_instance *instance,
3368 struct timer_list *timer,
3369 void *fn, unsigned long interval)
3370{
3371 init_timer(timer);
3372 timer->expires = jiffies + interval;
3373 timer->data = (unsigned long)instance;
3374 timer->function = fn;
3375 add_timer(timer);
3376}
3377
3378/**
3379 * megasas_io_completion_timer - Timer fn
3380 * @instance_addr: Address of adapter soft state
3381 *
3382 * Schedules tasklet for cmd completion
3383 * if poll_mode_io is set
3384 */
3385static void
3386megasas_io_completion_timer(unsigned long instance_addr)
3387{
3388 struct megasas_instance *instance =
3389 (struct megasas_instance *)instance_addr;
3390
3391 if (atomic_read(&instance->fw_outstanding))
3392 tasklet_schedule(&instance->isr_tasklet);
3393
3394 /* Restart timer */
3395 if (poll_mode_io)
3396 mod_timer(&instance->io_completion_timer,
3397 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3398}
3399
adam radfordcd50ba82010-12-21 10:23:23 -08003400static u32
3401megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003402{
adam radfordcd50ba82010-12-21 10:23:23 -08003403 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003404 u32 context_sz;
3405 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003406
3407 reg_set = instance->reg_set;
3408
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003409 /*
3410 * Get various operational parameters from status register
3411 */
Sumant Patro1341c932006-01-25 12:02:40 -08003412 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003413 /*
3414 * Reduce the max supported cmds by 1. This is to ensure that the
3415 * reply_q_sz (1 more than the max cmd that driver may send)
3416 * does not exceed max cmds that the FW can support
3417 */
3418 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003419 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003420 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003421 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003422 /*
3423 * Create a pool of commands
3424 */
3425 if (megasas_alloc_cmds(instance))
3426 goto fail_alloc_cmds;
3427
3428 /*
3429 * Allocate memory for reply queue. Length of reply queue should
3430 * be _one_ more than the maximum commands handled by the firmware.
3431 *
3432 * Note: When FW completes commands, it places corresponding contex
3433 * values in this circular reply queue. This circular queue is a fairly
3434 * typical producer-consumer queue. FW is the producer (of completed
3435 * commands) and the driver is the consumer.
3436 */
3437 context_sz = sizeof(u32);
3438 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3439
3440 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3441 reply_q_sz,
3442 &instance->reply_queue_h);
3443
3444 if (!instance->reply_queue) {
3445 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3446 goto fail_reply_queue;
3447 }
3448
bo yang31ea7082007-11-07 12:09:50 -05003449 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003450 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003451
bo yang39a98552010-09-22 22:36:29 -04003452 instance->fw_support_ieee = 0;
3453 instance->fw_support_ieee =
3454 (instance->instancet->read_fw_status_reg(reg_set) &
3455 0x04000000);
3456
3457 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3458 instance->fw_support_ieee);
3459
3460 if (instance->fw_support_ieee)
3461 instance->flag_ieee = 1;
3462
adam radfordcd50ba82010-12-21 10:23:23 -08003463 return 0;
3464
3465fail_fw_init:
3466
3467 pci_free_consistent(instance->pdev, reply_q_sz,
3468 instance->reply_queue, instance->reply_queue_h);
3469fail_reply_queue:
3470 megasas_free_cmds(instance);
3471
3472fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003473 return 1;
3474}
3475
3476/**
3477 * megasas_init_fw - Initializes the FW
3478 * @instance: Adapter soft state
3479 *
3480 * This is the main function for initializing firmware
3481 */
3482
3483static int megasas_init_fw(struct megasas_instance *instance)
3484{
3485 u32 max_sectors_1;
3486 u32 max_sectors_2;
adam radford3f1abce2011-05-11 18:33:47 -07003487 u32 tmp_sectors, msix_enable;
adam radfordcd50ba82010-12-21 10:23:23 -08003488 struct megasas_register_set __iomem *reg_set;
3489 struct megasas_ctrl_info *ctrl_info;
3490 unsigned long bar_list;
3491
3492 /* Find first memory bar */
3493 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3494 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3495 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3496 if (pci_request_selected_regions(instance->pdev, instance->bar,
3497 "megasas: LSI")) {
3498 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3499 return -EBUSY;
3500 }
3501
3502 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3503
3504 if (!instance->reg_set) {
3505 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3506 goto fail_ioremap;
3507 }
3508
3509 reg_set = instance->reg_set;
3510
3511 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003512 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07003513 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08003514 instance->instancet = &megasas_instance_template_fusion;
3515 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003516 case PCI_DEVICE_ID_LSI_SAS1078R:
3517 case PCI_DEVICE_ID_LSI_SAS1078DE:
3518 instance->instancet = &megasas_instance_template_ppc;
3519 break;
3520 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3521 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3522 instance->instancet = &megasas_instance_template_gen2;
3523 break;
3524 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3525 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3526 instance->instancet = &megasas_instance_template_skinny;
3527 break;
3528 case PCI_DEVICE_ID_LSI_SAS1064R:
3529 case PCI_DEVICE_ID_DELL_PERC5:
3530 default:
3531 instance->instancet = &megasas_instance_template_xscale;
3532 break;
3533 }
3534
3535 /*
3536 * We expect the FW state to be READY
3537 */
adam radford058a8fa2011-10-08 18:14:27 -07003538 if (megasas_transition_to_ready(instance, 0))
adam radfordcd50ba82010-12-21 10:23:23 -08003539 goto fail_ready_state;
3540
adam radford3f1abce2011-05-11 18:33:47 -07003541 /* Check if MSI-X is supported while in ready state */
3542 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3543 0x4000000) >> 0x1a;
3544 if (msix_enable && !msix_disable &&
3545 !pci_enable_msix(instance->pdev, &instance->msixentry, 1))
3546 instance->msi_flag = 1;
3547
adam radfordcd50ba82010-12-21 10:23:23 -08003548 /* Get operational params, sge flags, send init cmd to controller */
3549 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003550 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003551
3552 printk(KERN_ERR "megasas: INIT adapter done\n");
3553
bo yang39a98552010-09-22 22:36:29 -04003554 /** for passthrough
3555 * the following function will get the PD LIST.
3556 */
3557
Yang, Bo81e403c2009-10-06 14:27:54 -06003558 memset(instance->pd_list, 0 ,
3559 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3560 megasas_get_pd_list(instance);
3561
Yang, Bobdc6fb82009-12-06 08:30:19 -07003562 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3563 megasas_get_ld_list(instance);
3564
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3566
3567 /*
3568 * Compute the max allowed sectors per IO: The controller info has two
3569 * limits on max sectors. Driver should use the minimum of these two.
3570 *
3571 * 1 << stripe_sz_ops.min = max sectors per strip
3572 *
3573 * Note that older firmwares ( < FW ver 30) didn't report information
3574 * to calculate max_sectors_1. So the number ended up as zero always.
3575 */
bo yang14faea92007-11-09 04:14:00 -05003576 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003577 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3578
3579 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3580 ctrl_info->max_strips_per_io;
3581 max_sectors_2 = ctrl_info->max_request_size;
3582
bo yang14faea92007-11-09 04:14:00 -05003583 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003584 instance->disableOnlineCtrlReset =
3585 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003586 }
3587
3588 instance->max_sectors_per_req = instance->max_num_sge *
3589 PAGE_SIZE / 512;
3590 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3591 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003592
3593 kfree(ctrl_info);
3594
Sumant Patro5d018ad2006-10-03 13:13:18 -07003595 /*
3596 * Setup tasklet for cmd completion
3597 */
3598
adam radfordf86c5422011-02-24 20:57:00 -08003599 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003600 (unsigned long)instance);
3601
3602 /* Initialize the cmd completion timer */
3603 if (poll_mode_io)
3604 megasas_start_timer(instance, &instance->io_completion_timer,
3605 megasas_io_completion_timer,
3606 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003607 return 0;
3608
adam radfordeb1b1232011-02-24 20:55:56 -08003609fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003610fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003611 iounmap(instance->reg_set);
3612
3613 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003614 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003615
3616 return -EINVAL;
3617}
3618
3619/**
3620 * megasas_release_mfi - Reverses the FW initialization
3621 * @intance: Adapter soft state
3622 */
3623static void megasas_release_mfi(struct megasas_instance *instance)
3624{
adam radford9c915a82010-12-21 13:34:31 -08003625 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003626
adam radford9c915a82010-12-21 13:34:31 -08003627 if (instance->reply_queue)
3628 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003629 instance->reply_queue, instance->reply_queue_h);
3630
3631 megasas_free_cmds(instance);
3632
3633 iounmap(instance->reg_set);
3634
adam radfordb6d5d882010-12-14 18:56:07 -08003635 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003636}
3637
3638/**
3639 * megasas_get_seq_num - Gets latest event sequence numbers
3640 * @instance: Adapter soft state
3641 * @eli: FW event log sequence numbers information
3642 *
3643 * FW maintains a log of all events in a non-volatile area. Upper layers would
3644 * usually find out the latest sequence number of the events, the seq number at
3645 * the boot etc. They would "read" all the events below the latest seq number
3646 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3647 * number), they would subsribe to AEN (asynchronous event notification) and
3648 * wait for the events to happen.
3649 */
3650static int
3651megasas_get_seq_num(struct megasas_instance *instance,
3652 struct megasas_evt_log_info *eli)
3653{
3654 struct megasas_cmd *cmd;
3655 struct megasas_dcmd_frame *dcmd;
3656 struct megasas_evt_log_info *el_info;
3657 dma_addr_t el_info_h = 0;
3658
3659 cmd = megasas_get_cmd(instance);
3660
3661 if (!cmd) {
3662 return -ENOMEM;
3663 }
3664
3665 dcmd = &cmd->frame->dcmd;
3666 el_info = pci_alloc_consistent(instance->pdev,
3667 sizeof(struct megasas_evt_log_info),
3668 &el_info_h);
3669
3670 if (!el_info) {
3671 megasas_return_cmd(instance, cmd);
3672 return -ENOMEM;
3673 }
3674
3675 memset(el_info, 0, sizeof(*el_info));
3676 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3677
3678 dcmd->cmd = MFI_CMD_DCMD;
3679 dcmd->cmd_status = 0x0;
3680 dcmd->sge_count = 1;
3681 dcmd->flags = MFI_FRAME_DIR_READ;
3682 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003683 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003684 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3685 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3686 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3687 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3688
3689 megasas_issue_blocked_cmd(instance, cmd);
3690
3691 /*
3692 * Copy the data back into callers buffer
3693 */
3694 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3695
3696 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3697 el_info, el_info_h);
3698
3699 megasas_return_cmd(instance, cmd);
3700
3701 return 0;
3702}
3703
3704/**
3705 * megasas_register_aen - Registers for asynchronous event notification
3706 * @instance: Adapter soft state
3707 * @seq_num: The starting sequence number
3708 * @class_locale: Class of the event
3709 *
3710 * This function subscribes for AEN for events beyond the @seq_num. It requests
3711 * to be notified if and only if the event is of type @class_locale
3712 */
3713static int
3714megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3715 u32 class_locale_word)
3716{
3717 int ret_val;
3718 struct megasas_cmd *cmd;
3719 struct megasas_dcmd_frame *dcmd;
3720 union megasas_evt_class_locale curr_aen;
3721 union megasas_evt_class_locale prev_aen;
3722
3723 /*
3724 * If there an AEN pending already (aen_cmd), check if the
3725 * class_locale of that pending AEN is inclusive of the new
3726 * AEN request we currently have. If it is, then we don't have
3727 * to do anything. In other words, whichever events the current
3728 * AEN request is subscribing to, have already been subscribed
3729 * to.
3730 *
3731 * If the old_cmd is _not_ inclusive, then we have to abort
3732 * that command, form a class_locale that is superset of both
3733 * old and current and re-issue to the FW
3734 */
3735
3736 curr_aen.word = class_locale_word;
3737
3738 if (instance->aen_cmd) {
3739
3740 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3741
3742 /*
3743 * A class whose enum value is smaller is inclusive of all
3744 * higher values. If a PROGRESS (= -1) was previously
3745 * registered, then a new registration requests for higher
3746 * classes need not be sent to FW. They are automatically
3747 * included.
3748 *
3749 * Locale numbers don't have such hierarchy. They are bitmap
3750 * values
3751 */
3752 if ((prev_aen.members.class <= curr_aen.members.class) &&
3753 !((prev_aen.members.locale & curr_aen.members.locale) ^
3754 curr_aen.members.locale)) {
3755 /*
3756 * Previously issued event registration includes
3757 * current request. Nothing to do.
3758 */
3759 return 0;
3760 } else {
3761 curr_aen.members.locale |= prev_aen.members.locale;
3762
3763 if (prev_aen.members.class < curr_aen.members.class)
3764 curr_aen.members.class = prev_aen.members.class;
3765
3766 instance->aen_cmd->abort_aen = 1;
3767 ret_val = megasas_issue_blocked_abort_cmd(instance,
3768 instance->
3769 aen_cmd);
3770
3771 if (ret_val) {
3772 printk(KERN_DEBUG "megasas: Failed to abort "
3773 "previous AEN command\n");
3774 return ret_val;
3775 }
3776 }
3777 }
3778
3779 cmd = megasas_get_cmd(instance);
3780
3781 if (!cmd)
3782 return -ENOMEM;
3783
3784 dcmd = &cmd->frame->dcmd;
3785
3786 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3787
3788 /*
3789 * Prepare DCMD for aen registration
3790 */
3791 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3792
3793 dcmd->cmd = MFI_CMD_DCMD;
3794 dcmd->cmd_status = 0x0;
3795 dcmd->sge_count = 1;
3796 dcmd->flags = MFI_FRAME_DIR_READ;
3797 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003798 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003799 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003800 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3801 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3802 dcmd->mbox.w[0] = seq_num;
3803 dcmd->mbox.w[1] = curr_aen.word;
3804 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3805 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3806
Yang, Bof4c9a132009-10-06 14:43:28 -06003807 if (instance->aen_cmd != NULL) {
3808 megasas_return_cmd(instance, cmd);
3809 return 0;
3810 }
3811
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003812 /*
3813 * Store reference to the cmd used to register for AEN. When an
3814 * application wants us to register for AEN, we have to abort this
3815 * cmd and re-register with a new EVENT LOCALE supplied by that app
3816 */
3817 instance->aen_cmd = cmd;
3818
3819 /*
3820 * Issue the aen registration frame
3821 */
adam radford9c915a82010-12-21 13:34:31 -08003822 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003823
3824 return 0;
3825}
3826
3827/**
3828 * megasas_start_aen - Subscribes to AEN during driver load time
3829 * @instance: Adapter soft state
3830 */
3831static int megasas_start_aen(struct megasas_instance *instance)
3832{
3833 struct megasas_evt_log_info eli;
3834 union megasas_evt_class_locale class_locale;
3835
3836 /*
3837 * Get the latest sequence number from FW
3838 */
3839 memset(&eli, 0, sizeof(eli));
3840
3841 if (megasas_get_seq_num(instance, &eli))
3842 return -1;
3843
3844 /*
3845 * Register AEN with FW for latest sequence number plus 1
3846 */
3847 class_locale.members.reserved = 0;
3848 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3849 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3850
3851 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3852 class_locale.word);
3853}
3854
3855/**
3856 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3857 * @instance: Adapter soft state
3858 */
3859static int megasas_io_attach(struct megasas_instance *instance)
3860{
3861 struct Scsi_Host *host = instance->host;
3862
3863 /*
3864 * Export parameters required by SCSI mid-layer
3865 */
3866 host->irq = instance->pdev->irq;
3867 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003868 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3869 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3870 host->can_queue =
3871 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3872 } else
3873 host->can_queue =
3874 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003875 host->this_id = instance->init_id;
3876 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003877
3878 if (instance->fw_support_ieee)
3879 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3880
Yang, Bo1fd10682010-10-12 07:18:50 -06003881 /*
3882 * Check if the module parameter value for max_sectors can be used
3883 */
3884 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3885 instance->max_sectors_per_req = max_sectors;
3886 else {
3887 if (max_sectors) {
3888 if (((instance->pdev->device ==
3889 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3890 (instance->pdev->device ==
3891 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3892 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3893 instance->max_sectors_per_req = max_sectors;
3894 } else {
3895 printk(KERN_INFO "megasas: max_sectors should be > 0"
3896 "and <= %d (or < 1MB for GEN2 controller)\n",
3897 instance->max_sectors_per_req);
3898 }
3899 }
3900 }
3901
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003902 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003903 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003904 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3905 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3906 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003907 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003908
adam radford9c915a82010-12-21 13:34:31 -08003909 /* Fusion only supports host reset */
adam radford36807e62011-10-08 18:15:06 -07003910 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3911 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
adam radford9c915a82010-12-21 13:34:31 -08003912 host->hostt->eh_device_reset_handler = NULL;
3913 host->hostt->eh_bus_reset_handler = NULL;
3914 }
3915
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003916 /*
3917 * Notify the mid-layer about the new controller
3918 */
3919 if (scsi_add_host(host, &instance->pdev->dev)) {
3920 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3921 return -ENODEV;
3922 }
3923
3924 /*
3925 * Trigger SCSI to scan our drives
3926 */
3927 scsi_scan_host(host);
3928 return 0;
3929}
3930
bo yang31ea7082007-11-07 12:09:50 -05003931static int
3932megasas_set_dma_mask(struct pci_dev *pdev)
3933{
3934 /*
3935 * All our contollers are capable of performing 64-bit DMA
3936 */
3937 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003938 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003939
Yang Hongyang284901a2009-04-06 19:01:15 -07003940 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003941 goto fail_set_dma_mask;
3942 }
3943 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003944 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003945 goto fail_set_dma_mask;
3946 }
3947 return 0;
3948
3949fail_set_dma_mask:
3950 return 1;
3951}
3952
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003953/**
3954 * megasas_probe_one - PCI hotplug entry point
3955 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003956 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003957 */
3958static int __devinit
3959megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3960{
adam radford66192dfe2011-02-24 20:56:28 -08003961 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003962 struct Scsi_Host *host;
3963 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08003964 u16 control = 0;
3965
3966 /* Reset MSI-X in the kdump kernel */
3967 if (reset_devices) {
3968 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3969 if (pos) {
3970 pci_read_config_word(pdev, msi_control_reg(pos),
3971 &control);
3972 if (control & PCI_MSIX_FLAGS_ENABLE) {
3973 dev_info(&pdev->dev, "resetting MSI-X\n");
3974 pci_write_config_word(pdev,
3975 msi_control_reg(pos),
3976 control &
3977 ~PCI_MSIX_FLAGS_ENABLE);
3978 }
3979 }
3980 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003981
3982 /*
3983 * Announce PCI information
3984 */
3985 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3986 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3987 pdev->subsystem_device);
3988
3989 printk("bus %d:slot %d:func %d\n",
3990 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3991
3992 /*
3993 * PCI prepping: enable device set bus mastering and dma mask
3994 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003995 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003996
3997 if (rval) {
3998 return rval;
3999 }
4000
4001 pci_set_master(pdev);
4002
bo yang31ea7082007-11-07 12:09:50 -05004003 if (megasas_set_dma_mask(pdev))
4004 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004005
4006 host = scsi_host_alloc(&megasas_template,
4007 sizeof(struct megasas_instance));
4008
4009 if (!host) {
4010 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4011 goto fail_alloc_instance;
4012 }
4013
4014 instance = (struct megasas_instance *)host->hostdata;
4015 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04004016 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08004017 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004018
adam radford9c915a82010-12-21 13:34:31 -08004019 switch (instance->pdev->device) {
4020 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004021 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004022 {
4023 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004024
adam radford9c915a82010-12-21 13:34:31 -08004025 instance->ctrl_context =
4026 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4027 if (!instance->ctrl_context) {
4028 printk(KERN_DEBUG "megasas: Failed to allocate "
4029 "memory for Fusion context info\n");
4030 goto fail_alloc_dma_buf;
4031 }
4032 fusion = instance->ctrl_context;
4033 INIT_LIST_HEAD(&fusion->cmd_pool);
4034 spin_lock_init(&fusion->cmd_pool_lock);
4035 }
4036 break;
4037 default: /* For all other supported controllers */
4038
4039 instance->producer =
4040 pci_alloc_consistent(pdev, sizeof(u32),
4041 &instance->producer_h);
4042 instance->consumer =
4043 pci_alloc_consistent(pdev, sizeof(u32),
4044 &instance->consumer_h);
4045
4046 if (!instance->producer || !instance->consumer) {
4047 printk(KERN_DEBUG "megasas: Failed to allocate"
4048 "memory for producer, consumer\n");
4049 goto fail_alloc_dma_buf;
4050 }
4051
4052 *instance->producer = 0;
4053 *instance->consumer = 0;
4054 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004055 }
4056
Yang, Boc3518832009-10-06 14:18:02 -06004057 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004058 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004059 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004060 instance->issuepend_done = 1;
4061 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4062 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004063
4064 instance->evt_detail = pci_alloc_consistent(pdev,
4065 sizeof(struct
4066 megasas_evt_detail),
4067 &instance->evt_detail_h);
4068
4069 if (!instance->evt_detail) {
4070 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4071 "event detail structure\n");
4072 goto fail_alloc_dma_buf;
4073 }
4074
4075 /*
4076 * Initialize locks and queues
4077 */
4078 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004079 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004080
Sumant Patroe4a082c2006-05-30 12:03:37 -07004081 atomic_set(&instance->fw_outstanding,0);
4082
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004083 init_waitqueue_head(&instance->int_cmd_wait_q);
4084 init_waitqueue_head(&instance->abort_cmd_wait_q);
4085
4086 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004087 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004088 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06004089 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004090
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004091 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004092 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004093
4094 /*
4095 * Initialize PCI related and misc parameters
4096 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004097 instance->host = host;
4098 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4099 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4100
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004101 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4102 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004103 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004104 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4105 } else
4106 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4107
Sumant Patro658dced2006-10-03 13:09:14 -07004108 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004109 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004110 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004111 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004112 instance->disableOnlineCtrlReset = 1;
4113
adam radford36807e62011-10-08 18:15:06 -07004114 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4115 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radford9c915a82010-12-21 13:34:31 -08004116 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4117 else
4118 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004119
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004120 /*
adam radford0a770662011-02-24 20:56:12 -08004121 * Initialize MFI Firmware
4122 */
4123 if (megasas_init_fw(instance))
4124 goto fail_init_mfi;
4125
4126 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004127 * Register IRQ
4128 */
adam radford80d9da92010-12-21 10:17:40 -08004129 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004130 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004131 IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004132 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4133 goto fail_irq;
4134 }
4135
Sumant Patro1341c932006-01-25 12:02:40 -08004136 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004137
4138 /*
4139 * Store instance in PCI softstate
4140 */
4141 pci_set_drvdata(pdev, instance);
4142
4143 /*
4144 * Add this controller to megasas_mgmt_info structure so that it
4145 * can be exported to management applications
4146 */
4147 megasas_mgmt_info.count++;
4148 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4149 megasas_mgmt_info.max_index++;
4150
4151 /*
adam radford541f90b2011-05-11 18:34:29 -07004152 * Register with SCSI mid-layer
4153 */
4154 if (megasas_io_attach(instance))
4155 goto fail_io_attach;
4156
4157 instance->unload = 0;
4158
4159 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004160 * Initiate AEN (Asynchronous Event Notification)
4161 */
4162 if (megasas_start_aen(instance)) {
4163 printk(KERN_DEBUG "megasas: start aen failed\n");
4164 goto fail_start_aen;
4165 }
4166
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004167 return 0;
4168
4169 fail_start_aen:
4170 fail_io_attach:
4171 megasas_mgmt_info.count--;
4172 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4173 megasas_mgmt_info.max_index--;
4174
4175 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004176 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004177 free_irq(instance->msi_flag ? instance->msixentry.vector :
4178 instance->pdev->irq, instance);
adam radfordeb1b1232011-02-24 20:55:56 -08004179fail_irq:
adam radford36807e62011-10-08 18:15:06 -07004180 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4181 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
adam radfordeb1b1232011-02-24 20:55:56 -08004182 megasas_release_fusion(instance);
4183 else
4184 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004185 fail_init_mfi:
adam radford0a770662011-02-24 20:56:12 -08004186 if (instance->msi_flag)
4187 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004188 fail_alloc_dma_buf:
4189 if (instance->evt_detail)
4190 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4191 instance->evt_detail,
4192 instance->evt_detail_h);
4193
adam radfordeb1b1232011-02-24 20:55:56 -08004194 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004195 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4196 instance->producer_h);
4197 if (instance->consumer)
4198 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4199 instance->consumer_h);
4200 scsi_host_put(host);
4201
4202 fail_alloc_instance:
4203 fail_set_dma_mask:
4204 pci_disable_device(pdev);
4205
4206 return -ENODEV;
4207}
4208
4209/**
4210 * megasas_flush_cache - Requests FW to flush all its caches
4211 * @instance: Adapter soft state
4212 */
4213static void megasas_flush_cache(struct megasas_instance *instance)
4214{
4215 struct megasas_cmd *cmd;
4216 struct megasas_dcmd_frame *dcmd;
4217
bo yang39a98552010-09-22 22:36:29 -04004218 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4219 return;
4220
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004221 cmd = megasas_get_cmd(instance);
4222
4223 if (!cmd)
4224 return;
4225
4226 dcmd = &cmd->frame->dcmd;
4227
4228 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4229
4230 dcmd->cmd = MFI_CMD_DCMD;
4231 dcmd->cmd_status = 0x0;
4232 dcmd->sge_count = 0;
4233 dcmd->flags = MFI_FRAME_DIR_NONE;
4234 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004235 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004236 dcmd->data_xfer_len = 0;
4237 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4238 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4239
4240 megasas_issue_blocked_cmd(instance, cmd);
4241
4242 megasas_return_cmd(instance, cmd);
4243
4244 return;
4245}
4246
4247/**
4248 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4249 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004250 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004251 */
bo yang31ea7082007-11-07 12:09:50 -05004252static void megasas_shutdown_controller(struct megasas_instance *instance,
4253 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004254{
4255 struct megasas_cmd *cmd;
4256 struct megasas_dcmd_frame *dcmd;
4257
bo yang39a98552010-09-22 22:36:29 -04004258 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4259 return;
4260
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004261 cmd = megasas_get_cmd(instance);
4262
4263 if (!cmd)
4264 return;
4265
4266 if (instance->aen_cmd)
4267 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004268 if (instance->map_update_cmd)
4269 megasas_issue_blocked_abort_cmd(instance,
4270 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004271 dcmd = &cmd->frame->dcmd;
4272
4273 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4274
4275 dcmd->cmd = MFI_CMD_DCMD;
4276 dcmd->cmd_status = 0x0;
4277 dcmd->sge_count = 0;
4278 dcmd->flags = MFI_FRAME_DIR_NONE;
4279 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004280 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004281 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004282 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004283
4284 megasas_issue_blocked_cmd(instance, cmd);
4285
4286 megasas_return_cmd(instance, cmd);
4287
4288 return;
4289}
4290
Jiri Slaby33139b22008-05-01 17:56:02 +02004291#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004292/**
bo yangad84db22007-11-09 04:40:16 -05004293 * megasas_suspend - driver suspend entry point
4294 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004295 * @state: PCI power state to suspend routine
4296 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004297static int
bo yang31ea7082007-11-07 12:09:50 -05004298megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4299{
4300 struct Scsi_Host *host;
4301 struct megasas_instance *instance;
4302
4303 instance = pci_get_drvdata(pdev);
4304 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004305 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004306
bo yangad84db22007-11-09 04:40:16 -05004307 if (poll_mode_io)
4308 del_timer_sync(&instance->io_completion_timer);
4309
bo yang31ea7082007-11-07 12:09:50 -05004310 megasas_flush_cache(instance);
4311 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004312
4313 /* cancel the delayed work if this work still in queue */
4314 if (instance->ev != NULL) {
4315 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004316 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004317 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004318 instance->ev = NULL;
4319 }
4320
bo yang31ea7082007-11-07 12:09:50 -05004321 tasklet_kill(&instance->isr_tasklet);
4322
4323 pci_set_drvdata(instance->pdev, instance);
4324 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004325 free_irq(instance->msi_flag ? instance->msixentry.vector :
4326 instance->pdev->irq, instance);
4327 if (instance->msi_flag)
4328 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004329
4330 pci_save_state(pdev);
4331 pci_disable_device(pdev);
4332
4333 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4334
4335 return 0;
4336}
4337
4338/**
4339 * megasas_resume- driver resume entry point
4340 * @pdev: PCI device structure
4341 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004342static int
bo yang31ea7082007-11-07 12:09:50 -05004343megasas_resume(struct pci_dev *pdev)
4344{
4345 int rval;
4346 struct Scsi_Host *host;
4347 struct megasas_instance *instance;
4348
4349 instance = pci_get_drvdata(pdev);
4350 host = instance->host;
4351 pci_set_power_state(pdev, PCI_D0);
4352 pci_enable_wake(pdev, PCI_D0, 0);
4353 pci_restore_state(pdev);
4354
4355 /*
4356 * PCI prepping: enable device set bus mastering and dma mask
4357 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004358 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004359
4360 if (rval) {
4361 printk(KERN_ERR "megasas: Enable device failed\n");
4362 return rval;
4363 }
4364
4365 pci_set_master(pdev);
4366
4367 if (megasas_set_dma_mask(pdev))
4368 goto fail_set_dma_mask;
4369
4370 /*
4371 * Initialize MFI Firmware
4372 */
4373
bo yang31ea7082007-11-07 12:09:50 -05004374 atomic_set(&instance->fw_outstanding, 0);
4375
4376 /*
4377 * We expect the FW state to be READY
4378 */
adam radford058a8fa2011-10-08 18:14:27 -07004379 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05004380 goto fail_ready_state;
4381
adam radford3f1abce2011-05-11 18:33:47 -07004382 /* Now re-enable MSI-X */
4383 if (instance->msi_flag)
4384 pci_enable_msix(instance->pdev, &instance->msixentry, 1);
4385
adam radford9c915a82010-12-21 13:34:31 -08004386 switch (instance->pdev->device) {
4387 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004388 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004389 {
4390 megasas_reset_reply_desc(instance);
4391 if (megasas_ioc_init_fusion(instance)) {
4392 megasas_free_cmds(instance);
4393 megasas_free_cmds_fusion(instance);
4394 goto fail_init_mfi;
4395 }
4396 if (!megasas_get_map_info(instance))
4397 megasas_sync_map_info(instance);
4398 }
4399 break;
4400 default:
4401 *instance->producer = 0;
4402 *instance->consumer = 0;
4403 if (megasas_issue_init_mfi(instance))
4404 goto fail_init_mfi;
4405 break;
4406 }
bo yang31ea7082007-11-07 12:09:50 -05004407
adam radford9c915a82010-12-21 13:34:31 -08004408 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4409 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004410
4411 /*
4412 * Register IRQ
4413 */
adam radford80d9da92010-12-21 10:17:40 -08004414 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004415 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004416 IRQF_SHARED, "megasas", instance)) {
bo yang31ea7082007-11-07 12:09:50 -05004417 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4418 goto fail_irq;
4419 }
4420
4421 instance->instancet->enable_intr(instance->reg_set);
4422
bo yangad84db22007-11-09 04:40:16 -05004423 /* Initialize the cmd completion timer */
4424 if (poll_mode_io)
4425 megasas_start_timer(instance, &instance->io_completion_timer,
4426 megasas_io_completion_timer,
4427 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06004428 instance->unload = 0;
4429
adam radford541f90b2011-05-11 18:34:29 -07004430 /*
4431 * Initiate AEN (Asynchronous Event Notification)
4432 */
4433 if (megasas_start_aen(instance))
4434 printk(KERN_ERR "megasas: Start AEN failed\n");
4435
bo yang31ea7082007-11-07 12:09:50 -05004436 return 0;
4437
4438fail_irq:
4439fail_init_mfi:
4440 if (instance->evt_detail)
4441 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4442 instance->evt_detail,
4443 instance->evt_detail_h);
4444
4445 if (instance->producer)
4446 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4447 instance->producer_h);
4448 if (instance->consumer)
4449 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4450 instance->consumer_h);
4451 scsi_host_put(host);
4452
4453fail_set_dma_mask:
4454fail_ready_state:
4455
4456 pci_disable_device(pdev);
4457
4458 return -ENODEV;
4459}
Jiri Slaby33139b22008-05-01 17:56:02 +02004460#else
4461#define megasas_suspend NULL
4462#define megasas_resume NULL
4463#endif
bo yang31ea7082007-11-07 12:09:50 -05004464
4465/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004466 * megasas_detach_one - PCI hot"un"plug entry point
4467 * @pdev: PCI device structure
4468 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004469static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004470{
4471 int i;
4472 struct Scsi_Host *host;
4473 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004474 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004475
4476 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004477 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004478 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004479 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004480
bo yangad84db22007-11-09 04:40:16 -05004481 if (poll_mode_io)
4482 del_timer_sync(&instance->io_completion_timer);
4483
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004484 scsi_remove_host(instance->host);
4485 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004486 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004487
4488 /* cancel the delayed work if this work still in queue*/
4489 if (instance->ev != NULL) {
4490 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004491 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004492 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004493 instance->ev = NULL;
4494 }
4495
Sumant Patro5d018ad2006-10-03 13:13:18 -07004496 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004497
4498 /*
4499 * Take the instance off the instance array. Note that we will not
4500 * decrement the max_index. We let this array be sparse array
4501 */
4502 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4503 if (megasas_mgmt_info.instance[i] == instance) {
4504 megasas_mgmt_info.count--;
4505 megasas_mgmt_info.instance[i] = NULL;
4506
4507 break;
4508 }
4509 }
4510
4511 pci_set_drvdata(instance->pdev, NULL);
4512
Sumant Patrob274cab2006-10-03 12:52:12 -07004513 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004514
adam radford80d9da92010-12-21 10:17:40 -08004515 free_irq(instance->msi_flag ? instance->msixentry.vector :
4516 instance->pdev->irq, instance);
4517 if (instance->msi_flag)
4518 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004519
adam radford9c915a82010-12-21 13:34:31 -08004520 switch (instance->pdev->device) {
4521 case PCI_DEVICE_ID_LSI_FUSION:
adam radford36807e62011-10-08 18:15:06 -07004522 case PCI_DEVICE_ID_LSI_INVADER:
adam radford9c915a82010-12-21 13:34:31 -08004523 megasas_release_fusion(instance);
4524 for (i = 0; i < 2 ; i++)
4525 if (fusion->ld_map[i])
4526 dma_free_coherent(&instance->pdev->dev,
4527 fusion->map_sz,
4528 fusion->ld_map[i],
4529 fusion->
4530 ld_map_phys[i]);
4531 kfree(instance->ctrl_context);
4532 break;
4533 default:
4534 megasas_release_mfi(instance);
4535 pci_free_consistent(pdev,
4536 sizeof(struct megasas_evt_detail),
4537 instance->evt_detail,
4538 instance->evt_detail_h);
4539 pci_free_consistent(pdev, sizeof(u32),
4540 instance->producer,
4541 instance->producer_h);
4542 pci_free_consistent(pdev, sizeof(u32),
4543 instance->consumer,
4544 instance->consumer_h);
4545 break;
4546 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004547
4548 scsi_host_put(host);
4549
4550 pci_set_drvdata(pdev, NULL);
4551
4552 pci_disable_device(pdev);
4553
4554 return;
4555}
4556
4557/**
4558 * megasas_shutdown - Shutdown entry point
4559 * @device: Generic device structure
4560 */
4561static void megasas_shutdown(struct pci_dev *pdev)
4562{
4563 struct megasas_instance *instance = pci_get_drvdata(pdev);
Yang, Bo0c79e682009-10-06 14:47:35 -06004564 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004565 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004566 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
adam radford46fd2562011-05-11 18:34:17 -07004567 instance->instancet->disable_intr(instance->reg_set);
4568 free_irq(instance->msi_flag ? instance->msixentry.vector :
4569 instance->pdev->irq, instance);
4570 if (instance->msi_flag)
4571 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004572}
4573
4574/**
4575 * megasas_mgmt_open - char node "open" entry point
4576 */
4577static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4578{
4579 /*
4580 * Allow only those users with admin rights
4581 */
4582 if (!capable(CAP_SYS_ADMIN))
4583 return -EACCES;
4584
4585 return 0;
4586}
4587
4588/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004589 * megasas_mgmt_fasync - Async notifier registration from applications
4590 *
4591 * This function adds the calling process to a driver global queue. When an
4592 * event occurs, SIGIO will be sent to all processes in this queue.
4593 */
4594static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4595{
4596 int rc;
4597
Arjan van de Ven0b950672006-01-11 13:16:10 +01004598 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004599
4600 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4601
Arjan van de Ven0b950672006-01-11 13:16:10 +01004602 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004603
4604 if (rc >= 0) {
4605 /* For sanity check when we get ioctl */
4606 filep->private_data = filep;
4607 return 0;
4608 }
4609
4610 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4611
4612 return rc;
4613}
4614
4615/**
Yang, Boc3518832009-10-06 14:18:02 -06004616 * megasas_mgmt_poll - char node "poll" entry point
4617 * */
4618static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4619{
4620 unsigned int mask;
4621 unsigned long flags;
4622 poll_wait(file, &megasas_poll_wait, wait);
4623 spin_lock_irqsave(&poll_aen_lock, flags);
4624 if (megasas_poll_wait_aen)
4625 mask = (POLLIN | POLLRDNORM);
4626 else
4627 mask = 0;
4628 spin_unlock_irqrestore(&poll_aen_lock, flags);
4629 return mask;
4630}
4631
4632/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004633 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4634 * @instance: Adapter soft state
4635 * @argp: User's ioctl packet
4636 */
4637static int
4638megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4639 struct megasas_iocpacket __user * user_ioc,
4640 struct megasas_iocpacket *ioc)
4641{
4642 struct megasas_sge32 *kern_sge32;
4643 struct megasas_cmd *cmd;
4644 void *kbuff_arr[MAX_IOCTL_SGE];
4645 dma_addr_t buf_handle = 0;
4646 int error = 0, i;
4647 void *sense = NULL;
4648 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004649 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004650
4651 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4652
4653 if (ioc->sge_count > MAX_IOCTL_SGE) {
4654 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4655 ioc->sge_count, MAX_IOCTL_SGE);
4656 return -EINVAL;
4657 }
4658
4659 cmd = megasas_get_cmd(instance);
4660 if (!cmd) {
4661 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4662 return -ENOMEM;
4663 }
4664
4665 /*
4666 * User's IOCTL packet has 2 frames (maximum). Copy those two
4667 * frames into our cmd's frames. cmd->frame's context will get
4668 * overwritten when we copy from user's frames. So set that value
4669 * alone separately
4670 */
4671 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4672 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004673 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004674
4675 /*
4676 * The management interface between applications and the fw uses
4677 * MFI frames. E.g, RAID configuration changes, LD property changes
4678 * etc are accomplishes through different kinds of MFI frames. The
4679 * driver needs to care only about substituting user buffers with
4680 * kernel buffers in SGLs. The location of SGL is embedded in the
4681 * struct iocpacket itself.
4682 */
4683 kern_sge32 = (struct megasas_sge32 *)
4684 ((unsigned long)cmd->frame + ioc->sgl_off);
4685
4686 /*
4687 * For each user buffer, create a mirror buffer and copy in
4688 */
4689 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004690 if (!ioc->sgl[i].iov_len)
4691 continue;
4692
Sumant Patro9f35fa82007-02-14 12:55:45 -08004693 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004694 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004695 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004696 if (!kbuff_arr[i]) {
4697 printk(KERN_DEBUG "megasas: Failed to alloc "
4698 "kernel SGL buffer for IOCTL \n");
4699 error = -ENOMEM;
4700 goto out;
4701 }
4702
4703 /*
4704 * We don't change the dma_coherent_mask, so
4705 * pci_alloc_consistent only returns 32bit addresses
4706 */
4707 kern_sge32[i].phys_addr = (u32) buf_handle;
4708 kern_sge32[i].length = ioc->sgl[i].iov_len;
4709
4710 /*
4711 * We created a kernel buffer corresponding to the
4712 * user buffer. Now copy in from the user buffer
4713 */
4714 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4715 (u32) (ioc->sgl[i].iov_len))) {
4716 error = -EFAULT;
4717 goto out;
4718 }
4719 }
4720
4721 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004722 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4723 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004724 if (!sense) {
4725 error = -ENOMEM;
4726 goto out;
4727 }
4728
4729 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004730 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004731 *sense_ptr = sense_handle;
4732 }
4733
4734 /*
4735 * Set the sync_cmd flag so that the ISR knows not to complete this
4736 * cmd to the SCSI mid-layer
4737 */
4738 cmd->sync_cmd = 1;
4739 megasas_issue_blocked_cmd(instance, cmd);
4740 cmd->sync_cmd = 0;
4741
4742 /*
4743 * copy out the kernel buffers to user buffers
4744 */
4745 for (i = 0; i < ioc->sge_count; i++) {
4746 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4747 ioc->sgl[i].iov_len)) {
4748 error = -EFAULT;
4749 goto out;
4750 }
4751 }
4752
4753 /*
4754 * copy out the sense
4755 */
4756 if (ioc->sense_len) {
4757 /*
bo yangb70a41e2008-03-18 03:13:06 -04004758 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004759 * sense buffer address
4760 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004761 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4762 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004763
bo yangb70a41e2008-03-18 03:13:06 -04004764 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4765 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004766 printk(KERN_ERR "megasas: Failed to copy out to user "
4767 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004768 error = -EFAULT;
4769 goto out;
4770 }
4771 }
4772
4773 /*
4774 * copy the status codes returned by the fw
4775 */
4776 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4777 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4778 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4779 error = -EFAULT;
4780 }
4781
4782 out:
4783 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004784 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004785 sense, sense_handle);
4786 }
4787
4788 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004789 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004790 kern_sge32[i].length,
4791 kbuff_arr[i], kern_sge32[i].phys_addr);
4792 }
4793
4794 megasas_return_cmd(instance, cmd);
4795 return error;
4796}
4797
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004798static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4799{
4800 struct megasas_iocpacket __user *user_ioc =
4801 (struct megasas_iocpacket __user *)arg;
4802 struct megasas_iocpacket *ioc;
4803 struct megasas_instance *instance;
4804 int error;
bo yang39a98552010-09-22 22:36:29 -04004805 int i;
4806 unsigned long flags;
4807 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004808
4809 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4810 if (!ioc)
4811 return -ENOMEM;
4812
4813 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4814 error = -EFAULT;
4815 goto out_kfree_ioc;
4816 }
4817
4818 instance = megasas_lookup_instance(ioc->host_no);
4819 if (!instance) {
4820 error = -ENODEV;
4821 goto out_kfree_ioc;
4822 }
4823
bo yang39a98552010-09-22 22:36:29 -04004824 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4825 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004826 error = -ENODEV;
4827 goto out_kfree_ioc;
4828 }
4829
4830 if (instance->unload == 1) {
4831 error = -ENODEV;
4832 goto out_kfree_ioc;
4833 }
4834
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004835 /*
4836 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4837 */
4838 if (down_interruptible(&instance->ioctl_sem)) {
4839 error = -ERESTARTSYS;
4840 goto out_kfree_ioc;
4841 }
bo yang39a98552010-09-22 22:36:29 -04004842
4843 for (i = 0; i < wait_time; i++) {
4844
4845 spin_lock_irqsave(&instance->hba_lock, flags);
4846 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4847 spin_unlock_irqrestore(&instance->hba_lock, flags);
4848 break;
4849 }
4850 spin_unlock_irqrestore(&instance->hba_lock, flags);
4851
4852 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4853 printk(KERN_NOTICE "megasas: waiting"
4854 "for controller reset to finish\n");
4855 }
4856
4857 msleep(1000);
4858 }
4859
4860 spin_lock_irqsave(&instance->hba_lock, flags);
4861 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4862 spin_unlock_irqrestore(&instance->hba_lock, flags);
4863
4864 printk(KERN_ERR "megaraid_sas: timed out while"
4865 "waiting for HBA to recover\n");
4866 error = -ENODEV;
4867 goto out_kfree_ioc;
4868 }
4869 spin_unlock_irqrestore(&instance->hba_lock, flags);
4870
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004871 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4872 up(&instance->ioctl_sem);
4873
4874 out_kfree_ioc:
4875 kfree(ioc);
4876 return error;
4877}
4878
4879static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4880{
4881 struct megasas_instance *instance;
4882 struct megasas_aen aen;
4883 int error;
bo yang39a98552010-09-22 22:36:29 -04004884 int i;
4885 unsigned long flags;
4886 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004887
4888 if (file->private_data != file) {
4889 printk(KERN_DEBUG "megasas: fasync_helper was not "
4890 "called first\n");
4891 return -EINVAL;
4892 }
4893
4894 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4895 return -EFAULT;
4896
4897 instance = megasas_lookup_instance(aen.host_no);
4898
4899 if (!instance)
4900 return -ENODEV;
4901
bo yang39a98552010-09-22 22:36:29 -04004902 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4903 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004904 }
4905
4906 if (instance->unload == 1) {
4907 return -ENODEV;
4908 }
4909
bo yang39a98552010-09-22 22:36:29 -04004910 for (i = 0; i < wait_time; i++) {
4911
4912 spin_lock_irqsave(&instance->hba_lock, flags);
4913 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4914 spin_unlock_irqrestore(&instance->hba_lock,
4915 flags);
4916 break;
4917 }
4918
4919 spin_unlock_irqrestore(&instance->hba_lock, flags);
4920
4921 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4922 printk(KERN_NOTICE "megasas: waiting for"
4923 "controller reset to finish\n");
4924 }
4925
4926 msleep(1000);
4927 }
4928
4929 spin_lock_irqsave(&instance->hba_lock, flags);
4930 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4931 spin_unlock_irqrestore(&instance->hba_lock, flags);
4932 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4933 "for HBA to recover.\n");
4934 return -ENODEV;
4935 }
4936 spin_unlock_irqrestore(&instance->hba_lock, flags);
4937
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004938 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004939 error = megasas_register_aen(instance, aen.seq_num,
4940 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004941 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004942 return error;
4943}
4944
4945/**
4946 * megasas_mgmt_ioctl - char node ioctl entry point
4947 */
4948static long
4949megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4950{
4951 switch (cmd) {
4952 case MEGASAS_IOC_FIRMWARE:
4953 return megasas_mgmt_ioctl_fw(file, arg);
4954
4955 case MEGASAS_IOC_GET_AEN:
4956 return megasas_mgmt_ioctl_aen(file, arg);
4957 }
4958
4959 return -ENOTTY;
4960}
4961
4962#ifdef CONFIG_COMPAT
4963static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4964{
4965 struct compat_megasas_iocpacket __user *cioc =
4966 (struct compat_megasas_iocpacket __user *)arg;
4967 struct megasas_iocpacket __user *ioc =
4968 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4969 int i;
4970 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004971 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004972
Jeff Garzik83aabc12006-10-04 06:34:03 -04004973 if (clear_user(ioc, sizeof(*ioc)))
4974 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004975
4976 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4977 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4978 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4979 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4980 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4981 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4982 return -EFAULT;
4983
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004984 /*
4985 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4986 * sense_len is not null, so prepare the 64bit value under
4987 * the same condition.
4988 */
4989 if (ioc->sense_len) {
4990 void __user **sense_ioc_ptr =
4991 (void __user **)(ioc->frame.raw + ioc->sense_off);
4992 compat_uptr_t *sense_cioc_ptr =
4993 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4994 if (get_user(ptr, sense_cioc_ptr) ||
4995 put_user(compat_ptr(ptr), sense_ioc_ptr))
4996 return -EFAULT;
4997 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004998
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004999 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005000 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5001 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5002 copy_in_user(&ioc->sgl[i].iov_len,
5003 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5004 return -EFAULT;
5005 }
5006
5007 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5008
5009 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5010 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5011 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5012 return -EFAULT;
5013 }
5014 return error;
5015}
5016
5017static long
5018megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5019 unsigned long arg)
5020{
5021 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08005022 case MEGASAS_IOC_FIRMWARE32:
5023 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005024 case MEGASAS_IOC_GET_AEN:
5025 return megasas_mgmt_ioctl_aen(file, arg);
5026 }
5027
5028 return -ENOTTY;
5029}
5030#endif
5031
5032/*
5033 * File operations structure for management interface
5034 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08005035static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005036 .owner = THIS_MODULE,
5037 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005038 .fasync = megasas_mgmt_fasync,
5039 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005040 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005041#ifdef CONFIG_COMPAT
5042 .compat_ioctl = megasas_mgmt_compat_ioctl,
5043#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005044 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005045};
5046
5047/*
5048 * PCI hotplug support registration structure
5049 */
5050static struct pci_driver megasas_pci_driver = {
5051
5052 .name = "megaraid_sas",
5053 .id_table = megasas_pci_table,
5054 .probe = megasas_probe_one,
5055 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05005056 .suspend = megasas_suspend,
5057 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005058 .shutdown = megasas_shutdown,
5059};
5060
5061/*
5062 * Sysfs driver attributes
5063 */
5064static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5065{
5066 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5067 MEGASAS_VERSION);
5068}
5069
5070static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5071
5072static ssize_t
5073megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5074{
5075 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5076 MEGASAS_RELDATE);
5077}
5078
5079static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5080 NULL);
5081
Sumant Patro658dced2006-10-03 13:09:14 -07005082static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005083megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5084{
5085 return sprintf(buf, "%u\n", support_poll_for_event);
5086}
5087
5088static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5089 megasas_sysfs_show_support_poll_for_event, NULL);
5090
Yang, Bo837f5fe2010-10-11 06:59:20 -06005091 static ssize_t
5092megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5093{
5094 return sprintf(buf, "%u\n", support_device_change);
5095}
5096
5097static DRIVER_ATTR(support_device_change, S_IRUGO,
5098 megasas_sysfs_show_support_device_change, NULL);
5099
Yang, Bo72c4fd32009-10-06 14:20:59 -06005100static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005101megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5102{
bo yangad84db22007-11-09 04:40:16 -05005103 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005104}
5105
5106static ssize_t
5107megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5108{
5109 int retval = count;
5110 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5111 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5112 retval = -EINVAL;
5113 }
5114 return retval;
5115}
5116
Joe Malicki66dca9b2008-08-14 17:14:48 -04005117static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005118 megasas_sysfs_set_dbg_lvl);
5119
5120static ssize_t
5121megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
5122{
5123 return sprintf(buf, "%u\n", poll_mode_io);
5124}
5125
5126static ssize_t
5127megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
5128 const char *buf, size_t count)
5129{
5130 int retval = count;
5131 int tmp = poll_mode_io;
5132 int i;
5133 struct megasas_instance *instance;
5134
5135 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
5136 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
5137 retval = -EINVAL;
5138 }
5139
5140 /*
5141 * Check if poll_mode_io is already set or is same as previous value
5142 */
5143 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
5144 goto out;
5145
5146 if (poll_mode_io) {
5147 /*
5148 * Start timers for all adapters
5149 */
5150 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5151 instance = megasas_mgmt_info.instance[i];
5152 if (instance) {
5153 megasas_start_timer(instance,
5154 &instance->io_completion_timer,
5155 megasas_io_completion_timer,
5156 MEGASAS_COMPLETION_TIMER_INTERVAL);
5157 }
5158 }
5159 } else {
5160 /*
5161 * Delete timers for all adapters
5162 */
5163 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5164 instance = megasas_mgmt_info.instance[i];
5165 if (instance)
5166 del_timer_sync(&instance->io_completion_timer);
5167 }
5168 }
5169
5170out:
5171 return retval;
5172}
5173
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005174static void
5175megasas_aen_polling(struct work_struct *work)
5176{
5177 struct megasas_aen_event *ev =
5178 container_of(work, struct megasas_aen_event, hotplug_work);
5179 struct megasas_instance *instance = ev->instance;
5180 union megasas_evt_class_locale class_locale;
5181 struct Scsi_Host *host;
5182 struct scsi_device *sdev1;
5183 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005184 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005185 int i, j, doscan = 0;
5186 u32 seq_num;
5187 int error;
5188
5189 if (!instance) {
5190 printk(KERN_ERR "invalid instance!\n");
5191 kfree(ev);
5192 return;
5193 }
5194 instance->ev = NULL;
5195 host = instance->host;
5196 if (instance->evt_detail) {
5197
5198 switch (instance->evt_detail->code) {
5199 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005200 if (megasas_get_pd_list(instance) == 0) {
5201 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5202 for (j = 0;
5203 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5204 j++) {
5205
5206 pd_index =
5207 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5208
5209 sdev1 =
5210 scsi_device_lookup(host, i, j, 0);
5211
5212 if (instance->pd_list[pd_index].driveState
5213 == MR_PD_STATE_SYSTEM) {
5214 if (!sdev1) {
5215 scsi_add_device(host, i, j, 0);
5216 }
5217
5218 if (sdev1)
5219 scsi_device_put(sdev1);
5220 }
5221 }
5222 }
5223 }
5224 doscan = 0;
5225 break;
5226
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005227 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005228 if (megasas_get_pd_list(instance) == 0) {
5229 megasas_get_pd_list(instance);
5230 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5231 for (j = 0;
5232 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5233 j++) {
5234
5235 pd_index =
5236 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5237
5238 sdev1 =
5239 scsi_device_lookup(host, i, j, 0);
5240
5241 if (instance->pd_list[pd_index].driveState
5242 == MR_PD_STATE_SYSTEM) {
5243 if (sdev1) {
5244 scsi_device_put(sdev1);
5245 }
5246 } else {
5247 if (sdev1) {
5248 scsi_remove_device(sdev1);
5249 scsi_device_put(sdev1);
5250 }
5251 }
5252 }
5253 }
5254 }
5255 doscan = 0;
5256 break;
5257
5258 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005259 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005260 case MR_EVT_LD_DELETED:
5261 megasas_get_ld_list(instance);
5262 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5263 for (j = 0;
5264 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5265 j++) {
5266
5267 ld_index =
5268 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5269
5270 sdev1 = scsi_device_lookup(host,
5271 i + MEGASAS_MAX_LD_CHANNELS,
5272 j,
5273 0);
5274
5275 if (instance->ld_ids[ld_index] != 0xff) {
5276 if (sdev1) {
5277 scsi_device_put(sdev1);
5278 }
5279 } else {
5280 if (sdev1) {
5281 scsi_remove_device(sdev1);
5282 scsi_device_put(sdev1);
5283 }
5284 }
5285 }
5286 }
5287 doscan = 0;
5288 break;
5289 case MR_EVT_LD_CREATED:
5290 megasas_get_ld_list(instance);
5291 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5292 for (j = 0;
5293 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5294 j++) {
5295 ld_index =
5296 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5297
5298 sdev1 = scsi_device_lookup(host,
5299 i+MEGASAS_MAX_LD_CHANNELS,
5300 j, 0);
5301
5302 if (instance->ld_ids[ld_index] !=
5303 0xff) {
5304 if (!sdev1) {
5305 scsi_add_device(host,
5306 i + 2,
5307 j, 0);
5308 }
5309 }
5310 if (sdev1) {
5311 scsi_device_put(sdev1);
5312 }
5313 }
5314 }
5315 doscan = 0;
5316 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005317 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005318 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005319 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005320 doscan = 1;
5321 break;
5322 default:
5323 doscan = 0;
5324 break;
5325 }
5326 } else {
5327 printk(KERN_ERR "invalid evt_detail!\n");
5328 kfree(ev);
5329 return;
5330 }
5331
5332 if (doscan) {
5333 printk(KERN_INFO "scanning ...\n");
5334 megasas_get_pd_list(instance);
5335 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5336 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5337 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5338 sdev1 = scsi_device_lookup(host, i, j, 0);
5339 if (instance->pd_list[pd_index].driveState ==
5340 MR_PD_STATE_SYSTEM) {
5341 if (!sdev1) {
5342 scsi_add_device(host, i, j, 0);
5343 }
5344 if (sdev1)
5345 scsi_device_put(sdev1);
5346 } else {
5347 if (sdev1) {
5348 scsi_remove_device(sdev1);
5349 scsi_device_put(sdev1);
5350 }
5351 }
5352 }
5353 }
Yang, Boc9786842009-12-06 08:39:25 -07005354
5355 megasas_get_ld_list(instance);
5356 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5357 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5358 ld_index =
5359 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5360
5361 sdev1 = scsi_device_lookup(host,
5362 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5363 if (instance->ld_ids[ld_index] != 0xff) {
5364 if (!sdev1) {
5365 scsi_add_device(host,
5366 i+2,
5367 j, 0);
5368 } else {
5369 scsi_device_put(sdev1);
5370 }
5371 } else {
5372 if (sdev1) {
5373 scsi_remove_device(sdev1);
5374 scsi_device_put(sdev1);
5375 }
5376 }
5377 }
5378 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005379 }
5380
5381 if ( instance->aen_cmd != NULL ) {
5382 kfree(ev);
5383 return ;
5384 }
5385
5386 seq_num = instance->evt_detail->seq_num + 1;
5387
5388 /* Register AEN with FW for latest sequence number plus 1 */
5389 class_locale.members.reserved = 0;
5390 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5391 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5392 mutex_lock(&instance->aen_mutex);
5393 error = megasas_register_aen(instance, seq_num,
5394 class_locale.word);
5395 mutex_unlock(&instance->aen_mutex);
5396
5397 if (error)
5398 printk(KERN_ERR "register aen failed error %x\n", error);
5399
5400 kfree(ev);
5401}
5402
5403
Bryn M. Reevesbb7d3f22009-11-12 18:31:54 +00005404static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
bo yangad84db22007-11-09 04:40:16 -05005405 megasas_sysfs_show_poll_mode_io,
5406 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07005407
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005408/**
5409 * megasas_init - Driver load entry point
5410 */
5411static int __init megasas_init(void)
5412{
5413 int rval;
5414
5415 /*
5416 * Announce driver version and other information
5417 */
5418 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5419 MEGASAS_EXT_VERSION);
5420
Yang, Bo72c4fd32009-10-06 14:20:59 -06005421 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005422 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005423
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005424 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5425
5426 /*
5427 * Register character device node
5428 */
5429 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5430
5431 if (rval < 0) {
5432 printk(KERN_DEBUG "megasas: failed to open device node\n");
5433 return rval;
5434 }
5435
5436 megasas_mgmt_majorno = rval;
5437
5438 /*
5439 * Register ourselves as PCI hotplug module
5440 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005441 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005442
5443 if (rval) {
5444 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005445 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005446 }
5447
Jeff Garzik83aabc12006-10-04 06:34:03 -04005448 rval = driver_create_file(&megasas_pci_driver.driver,
5449 &driver_attr_version);
5450 if (rval)
5451 goto err_dcf_attr_ver;
5452 rval = driver_create_file(&megasas_pci_driver.driver,
5453 &driver_attr_release_date);
5454 if (rval)
5455 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005456
5457 rval = driver_create_file(&megasas_pci_driver.driver,
5458 &driver_attr_support_poll_for_event);
5459 if (rval)
5460 goto err_dcf_support_poll_for_event;
5461
Jeff Garzik83aabc12006-10-04 06:34:03 -04005462 rval = driver_create_file(&megasas_pci_driver.driver,
5463 &driver_attr_dbg_lvl);
5464 if (rval)
5465 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005466 rval = driver_create_file(&megasas_pci_driver.driver,
5467 &driver_attr_poll_mode_io);
5468 if (rval)
5469 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005470
Yang, Bo837f5fe2010-10-11 06:59:20 -06005471 rval = driver_create_file(&megasas_pci_driver.driver,
5472 &driver_attr_support_device_change);
5473 if (rval)
5474 goto err_dcf_support_device_change;
5475
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005476 return rval;
bo yangad84db22007-11-09 04:40:16 -05005477
Yang, Bo837f5fe2010-10-11 06:59:20 -06005478err_dcf_support_device_change:
5479 driver_remove_file(&megasas_pci_driver.driver,
5480 &driver_attr_poll_mode_io);
5481
bo yangad84db22007-11-09 04:40:16 -05005482err_dcf_poll_mode_io:
5483 driver_remove_file(&megasas_pci_driver.driver,
5484 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005485err_dcf_dbg_lvl:
5486 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005487 &driver_attr_support_poll_for_event);
5488
5489err_dcf_support_poll_for_event:
5490 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005491 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005492
Jeff Garzik83aabc12006-10-04 06:34:03 -04005493err_dcf_rel_date:
5494 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5495err_dcf_attr_ver:
5496 pci_unregister_driver(&megasas_pci_driver);
5497err_pcidrv:
5498 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005499 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005500}
5501
5502/**
5503 * megasas_exit - Driver unload entry point
5504 */
5505static void __exit megasas_exit(void)
5506{
Sumant Patro658dced2006-10-03 13:09:14 -07005507 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005508 &driver_attr_poll_mode_io);
5509 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07005510 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005511 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005512 &driver_attr_support_poll_for_event);
5513 driver_remove_file(&megasas_pci_driver.driver,
5514 &driver_attr_support_device_change);
5515 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005516 &driver_attr_release_date);
5517 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005518
5519 pci_unregister_driver(&megasas_pci_driver);
5520 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5521}
5522
5523module_init(megasas_init);
5524module_exit(megasas_exit);