blob: 9d5a14a2c5b1c28c60862f9ad3ffc85b92501db2 [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 */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200117 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400118};
119
120MODULE_DEVICE_TABLE(pci, megasas_pci_table);
121
122static int megasas_mgmt_majorno;
123static struct megasas_mgmt_info megasas_mgmt_info;
124static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100125static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400126
Yang, Boc3518832009-10-06 14:18:02 -0600127static int megasas_poll_wait_aen;
128static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600129static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800130u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600131static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700132
Yang, Boc3518832009-10-06 14:18:02 -0600133/* define lock for aen poll */
134spinlock_t poll_aen_lock;
135
adam radford9c915a82010-12-21 13:34:31 -0800136void
bo yang7343eb62007-11-09 04:35:44 -0500137megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
138 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800139static u32
140megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
141static int
142megasas_adp_reset_gen2(struct megasas_instance *instance,
143 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800144static irqreturn_t megasas_isr(int irq, void *devp);
145static u32
146megasas_init_adapter_mfi(struct megasas_instance *instance);
147u32
148megasas_build_and_issue_cmd(struct megasas_instance *instance,
149 struct scsi_cmnd *scmd);
150static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800151void
152megasas_release_fusion(struct megasas_instance *instance);
153int
154megasas_ioc_init_fusion(struct megasas_instance *instance);
155void
156megasas_free_cmds_fusion(struct megasas_instance *instance);
157u8
158megasas_get_map_info(struct megasas_instance *instance);
159int
160megasas_sync_map_info(struct megasas_instance *instance);
161int
162wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
163void megasas_reset_reply_desc(struct megasas_instance *instance);
164u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
165 struct LD_LOAD_BALANCE_INFO *lbInfo);
166int megasas_reset_fusion(struct Scsi_Host *shost);
167void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800168
169void
170megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
171{
172 instance->instancet->fire_cmd(instance,
173 cmd->frame_phys_addr, 0, instance->reg_set);
174}
bo yang7343eb62007-11-09 04:35:44 -0500175
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400176/**
177 * megasas_get_cmd - Get a command from the free pool
178 * @instance: Adapter soft state
179 *
180 * Returns a free command from the pool
181 */
adam radford9c915a82010-12-21 13:34:31 -0800182struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400183 *instance)
184{
185 unsigned long flags;
186 struct megasas_cmd *cmd = NULL;
187
188 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
189
190 if (!list_empty(&instance->cmd_pool)) {
191 cmd = list_entry((&instance->cmd_pool)->next,
192 struct megasas_cmd, list);
193 list_del_init(&cmd->list);
194 } else {
195 printk(KERN_ERR "megasas: Command pool empty!\n");
196 }
197
198 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
199 return cmd;
200}
201
202/**
203 * megasas_return_cmd - Return a cmd to free command pool
204 * @instance: Adapter soft state
205 * @cmd: Command packet to be returned to free command pool
206 */
adam radford9c915a82010-12-21 13:34:31 -0800207inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400208megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
209{
210 unsigned long flags;
211
212 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
213
214 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800215 cmd->frame_count = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400216 list_add_tail(&cmd->list, &instance->cmd_pool);
217
218 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
219}
220
Sumant Patro1341c932006-01-25 12:02:40 -0800221
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400222/**
adam radford0d490162010-12-14 19:17:17 -0800223* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800224* (deviceid : 1064R, PERC5) controllers
225*/
226
227/**
228 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400229 * @regs: MFI register set
230 */
231static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800232megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400233{
bo yang39a98552010-09-22 22:36:29 -0400234 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400235
236 /* Dummy readl to force pci flush */
237 readl(&regs->outbound_intr_mask);
238}
239
240/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700241 * megasas_disable_intr_xscale -Disables interrupt
242 * @regs: MFI register set
243 */
244static inline void
245megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
246{
247 u32 mask = 0x1f;
248 writel(mask, &regs->outbound_intr_mask);
249 /* Dummy readl to force pci flush */
250 readl(&regs->outbound_intr_mask);
251}
252
253/**
Sumant Patro1341c932006-01-25 12:02:40 -0800254 * megasas_read_fw_status_reg_xscale - returns the current FW status value
255 * @regs: MFI register set
256 */
257static u32
258megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
259{
260 return readl(&(regs)->outbound_msg_0);
261}
262/**
263 * megasas_clear_interrupt_xscale - Check & clear interrupt
264 * @regs: MFI register set
265 */
adam radford0d490162010-12-14 19:17:17 -0800266static int
Sumant Patro1341c932006-01-25 12:02:40 -0800267megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
268{
269 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400270 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800271 /*
272 * Check if it is our interrupt
273 */
274 status = readl(&regs->outbound_intr_status);
275
bo yang39a98552010-09-22 22:36:29 -0400276 if (status & MFI_OB_INTR_STATUS_MASK)
277 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
278 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
279 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800280
281 /*
282 * Clear the interrupt by writing back the same value
283 */
bo yang39a98552010-09-22 22:36:29 -0400284 if (mfiStatus)
285 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800286
Yang, Bo06f579d2008-08-10 12:42:37 -0700287 /* Dummy readl to force pci flush */
288 readl(&regs->outbound_intr_status);
289
bo yang39a98552010-09-22 22:36:29 -0400290 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800291}
292
293/**
294 * megasas_fire_cmd_xscale - Sends command to the FW
295 * @frame_phys_addr : Physical address of cmd
296 * @frame_count : Number of frames for the command
297 * @regs : MFI register set
298 */
adam radford0d490162010-12-14 19:17:17 -0800299static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600300megasas_fire_cmd_xscale(struct megasas_instance *instance,
301 dma_addr_t frame_phys_addr,
302 u32 frame_count,
303 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800304{
bo yang39a98552010-09-22 22:36:29 -0400305 unsigned long flags;
306 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800307 writel((frame_phys_addr >> 3)|(frame_count),
308 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400309 spin_unlock_irqrestore(&instance->hba_lock, flags);
310}
311
312/**
313 * megasas_adp_reset_xscale - For controller reset
314 * @regs: MFI register set
315 */
316static int
317megasas_adp_reset_xscale(struct megasas_instance *instance,
318 struct megasas_register_set __iomem *regs)
319{
320 u32 i;
321 u32 pcidata;
322 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
323
324 for (i = 0; i < 3; i++)
325 msleep(1000); /* sleep for 3 secs */
326 pcidata = 0;
327 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
328 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
329 if (pcidata & 0x2) {
330 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
331 pcidata &= ~0x2;
332 pci_write_config_dword(instance->pdev,
333 MFI_1068_PCSR_OFFSET, pcidata);
334
335 for (i = 0; i < 2; i++)
336 msleep(1000); /* need to wait 2 secs again */
337
338 pcidata = 0;
339 pci_read_config_dword(instance->pdev,
340 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
341 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
342 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
343 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
344 pcidata = 0;
345 pci_write_config_dword(instance->pdev,
346 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
347 }
348 }
349 return 0;
350}
351
352/**
353 * megasas_check_reset_xscale - For controller reset check
354 * @regs: MFI register set
355 */
356static int
357megasas_check_reset_xscale(struct megasas_instance *instance,
358 struct megasas_register_set __iomem *regs)
359{
360 u32 consumer;
361 consumer = *instance->consumer;
362
363 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
364 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
365 return 1;
366 }
367 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800368}
369
370static struct megasas_instance_template megasas_instance_template_xscale = {
371
372 .fire_cmd = megasas_fire_cmd_xscale,
373 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700374 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800375 .clear_intr = megasas_clear_intr_xscale,
376 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400377 .adp_reset = megasas_adp_reset_xscale,
378 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800379 .service_isr = megasas_isr,
380 .tasklet = megasas_complete_cmd_dpc,
381 .init_adapter = megasas_init_adapter_mfi,
382 .build_and_issue_cmd = megasas_build_and_issue_cmd,
383 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800384};
385
386/**
adam radford0d490162010-12-14 19:17:17 -0800387* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800388* to xscale (deviceid : 1064R, PERC5) controllers
389*/
390
391/**
adam radford0d490162010-12-14 19:17:17 -0800392* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800393* controllers
394*/
395
396/**
397 * megasas_enable_intr_ppc - Enables interrupts
398 * @regs: MFI register set
399 */
400static inline void
401megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
402{
403 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800404
bo yang39a98552010-09-22 22:36:29 -0400405 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800406
407 /* Dummy readl to force pci flush */
408 readl(&regs->outbound_intr_mask);
409}
410
411/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700412 * megasas_disable_intr_ppc - Disable interrupt
413 * @regs: MFI register set
414 */
415static inline void
416megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
417{
418 u32 mask = 0xFFFFFFFF;
419 writel(mask, &regs->outbound_intr_mask);
420 /* Dummy readl to force pci flush */
421 readl(&regs->outbound_intr_mask);
422}
423
424/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800425 * megasas_read_fw_status_reg_ppc - returns the current FW status value
426 * @regs: MFI register set
427 */
428static u32
429megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
430{
431 return readl(&(regs)->outbound_scratch_pad);
432}
433
434/**
435 * megasas_clear_interrupt_ppc - Check & clear interrupt
436 * @regs: MFI register set
437 */
adam radford0d490162010-12-14 19:17:17 -0800438static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800439megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
440{
adam radford3cc68512011-05-11 18:34:52 -0700441 u32 status, mfiStatus = 0;
442
Sumant Patrof9876f02006-02-03 15:34:35 -0800443 /*
444 * Check if it is our interrupt
445 */
446 status = readl(&regs->outbound_intr_status);
447
adam radford3cc68512011-05-11 18:34:52 -0700448 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
449 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
450
451 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
452 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800453
454 /*
455 * Clear the interrupt by writing back the same value
456 */
457 writel(status, &regs->outbound_doorbell_clear);
458
Yang, Bo06f579d2008-08-10 12:42:37 -0700459 /* Dummy readl to force pci flush */
460 readl(&regs->outbound_doorbell_clear);
461
adam radford3cc68512011-05-11 18:34:52 -0700462 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800463}
adam radford3cc68512011-05-11 18:34:52 -0700464
Sumant Patrof9876f02006-02-03 15:34:35 -0800465/**
466 * megasas_fire_cmd_ppc - Sends command to the FW
467 * @frame_phys_addr : Physical address of cmd
468 * @frame_count : Number of frames for the command
469 * @regs : MFI register set
470 */
adam radford0d490162010-12-14 19:17:17 -0800471static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600472megasas_fire_cmd_ppc(struct megasas_instance *instance,
473 dma_addr_t frame_phys_addr,
474 u32 frame_count,
475 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800476{
bo yang39a98552010-09-22 22:36:29 -0400477 unsigned long flags;
478 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800479 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800480 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400481 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800482}
483
bo yang39a98552010-09-22 22:36:29 -0400484/**
bo yang39a98552010-09-22 22:36:29 -0400485 * megasas_check_reset_ppc - For controller reset check
486 * @regs: MFI register set
487 */
488static int
489megasas_check_reset_ppc(struct megasas_instance *instance,
490 struct megasas_register_set __iomem *regs)
491{
adam radford3cc68512011-05-11 18:34:52 -0700492 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
493 return 1;
494
bo yang39a98552010-09-22 22:36:29 -0400495 return 0;
496}
adam radford3cc68512011-05-11 18:34:52 -0700497
Sumant Patrof9876f02006-02-03 15:34:35 -0800498static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800499
Sumant Patrof9876f02006-02-03 15:34:35 -0800500 .fire_cmd = megasas_fire_cmd_ppc,
501 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700502 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800503 .clear_intr = megasas_clear_intr_ppc,
504 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc68512011-05-11 18:34:52 -0700505 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400506 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800507 .service_isr = megasas_isr,
508 .tasklet = megasas_complete_cmd_dpc,
509 .init_adapter = megasas_init_adapter_mfi,
510 .build_and_issue_cmd = megasas_build_and_issue_cmd,
511 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800512};
513
514/**
Yang, Bo87911122009-10-06 14:31:54 -0600515 * megasas_enable_intr_skinny - Enables interrupts
516 * @regs: MFI register set
517 */
518static inline void
519megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
520{
521 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
522
523 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
524
525 /* Dummy readl to force pci flush */
526 readl(&regs->outbound_intr_mask);
527}
528
529/**
530 * megasas_disable_intr_skinny - Disables interrupt
531 * @regs: MFI register set
532 */
533static inline void
534megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
535{
536 u32 mask = 0xFFFFFFFF;
537 writel(mask, &regs->outbound_intr_mask);
538 /* Dummy readl to force pci flush */
539 readl(&regs->outbound_intr_mask);
540}
541
542/**
543 * megasas_read_fw_status_reg_skinny - returns the current FW status value
544 * @regs: MFI register set
545 */
546static u32
547megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
548{
549 return readl(&(regs)->outbound_scratch_pad);
550}
551
552/**
553 * megasas_clear_interrupt_skinny - Check & clear interrupt
554 * @regs: MFI register set
555 */
556static int
557megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
558{
559 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800560 u32 mfiStatus = 0;
561
Yang, Bo87911122009-10-06 14:31:54 -0600562 /*
563 * Check if it is our interrupt
564 */
565 status = readl(&regs->outbound_intr_status);
566
567 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400568 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600569 }
570
571 /*
adam radfordebf054b2011-02-24 20:57:15 -0800572 * Check if it is our interrupt
573 */
574 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
575 MFI_STATE_FAULT) {
576 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
577 } else
578 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
579
580 /*
Yang, Bo87911122009-10-06 14:31:54 -0600581 * Clear the interrupt by writing back the same value
582 */
583 writel(status, &regs->outbound_intr_status);
584
585 /*
586 * dummy read to flush PCI
587 */
588 readl(&regs->outbound_intr_status);
589
adam radfordebf054b2011-02-24 20:57:15 -0800590 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600591}
592
593/**
594 * megasas_fire_cmd_skinny - Sends command to the FW
595 * @frame_phys_addr : Physical address of cmd
596 * @frame_count : Number of frames for the command
597 * @regs : MFI register set
598 */
599static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600600megasas_fire_cmd_skinny(struct megasas_instance *instance,
601 dma_addr_t frame_phys_addr,
602 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600603 struct megasas_register_set __iomem *regs)
604{
Yang, Bo0c79e682009-10-06 14:47:35 -0600605 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400606 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600607 writel(0, &(regs)->inbound_high_queue_port);
608 writel((frame_phys_addr | (frame_count<<1))|1,
609 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400610 spin_unlock_irqrestore(&instance->hba_lock, flags);
611}
612
613/**
bo yang39a98552010-09-22 22:36:29 -0400614 * megasas_check_reset_skinny - For controller reset check
615 * @regs: MFI register set
616 */
617static int
618megasas_check_reset_skinny(struct megasas_instance *instance,
619 struct megasas_register_set __iomem *regs)
620{
adam radford3cc68512011-05-11 18:34:52 -0700621 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
622 return 1;
623
bo yang39a98552010-09-22 22:36:29 -0400624 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600625}
626
627static struct megasas_instance_template megasas_instance_template_skinny = {
628
629 .fire_cmd = megasas_fire_cmd_skinny,
630 .enable_intr = megasas_enable_intr_skinny,
631 .disable_intr = megasas_disable_intr_skinny,
632 .clear_intr = megasas_clear_intr_skinny,
633 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800634 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400635 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800636 .service_isr = megasas_isr,
637 .tasklet = megasas_complete_cmd_dpc,
638 .init_adapter = megasas_init_adapter_mfi,
639 .build_and_issue_cmd = megasas_build_and_issue_cmd,
640 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600641};
642
643
644/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700645* The following functions are defined for gen2 (deviceid : 0x78 0x79)
646* controllers
647*/
648
649/**
650 * megasas_enable_intr_gen2 - Enables interrupts
651 * @regs: MFI register set
652 */
653static inline void
654megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
655{
656 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
657
658 /* write ~0x00000005 (4 & 1) to the intr mask*/
659 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
660
661 /* Dummy readl to force pci flush */
662 readl(&regs->outbound_intr_mask);
663}
664
665/**
666 * megasas_disable_intr_gen2 - Disables interrupt
667 * @regs: MFI register set
668 */
669static inline void
670megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
671{
672 u32 mask = 0xFFFFFFFF;
673 writel(mask, &regs->outbound_intr_mask);
674 /* Dummy readl to force pci flush */
675 readl(&regs->outbound_intr_mask);
676}
677
678/**
679 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
680 * @regs: MFI register set
681 */
682static u32
683megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
684{
685 return readl(&(regs)->outbound_scratch_pad);
686}
687
688/**
689 * megasas_clear_interrupt_gen2 - Check & clear interrupt
690 * @regs: MFI register set
691 */
692static int
693megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
694{
695 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400696 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700697 /*
698 * Check if it is our interrupt
699 */
700 status = readl(&regs->outbound_intr_status);
701
bo yang39a98552010-09-22 22:36:29 -0400702 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
703 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
704 }
705 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
706 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
707 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700708
709 /*
710 * Clear the interrupt by writing back the same value
711 */
bo yang39a98552010-09-22 22:36:29 -0400712 if (mfiStatus)
713 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700714
715 /* Dummy readl to force pci flush */
716 readl(&regs->outbound_intr_status);
717
bo yang39a98552010-09-22 22:36:29 -0400718 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700719}
720/**
721 * megasas_fire_cmd_gen2 - Sends command to the FW
722 * @frame_phys_addr : Physical address of cmd
723 * @frame_count : Number of frames for the command
724 * @regs : MFI register set
725 */
726static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600727megasas_fire_cmd_gen2(struct megasas_instance *instance,
728 dma_addr_t frame_phys_addr,
729 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700730 struct megasas_register_set __iomem *regs)
731{
bo yang39a98552010-09-22 22:36:29 -0400732 unsigned long flags;
733 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700734 writel((frame_phys_addr | (frame_count<<1))|1,
735 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400736 spin_unlock_irqrestore(&instance->hba_lock, flags);
737}
738
739/**
740 * megasas_adp_reset_gen2 - For controller reset
741 * @regs: MFI register set
742 */
743static int
744megasas_adp_reset_gen2(struct megasas_instance *instance,
745 struct megasas_register_set __iomem *reg_set)
746{
747 u32 retry = 0 ;
748 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800749 u32 *seq_offset = &reg_set->seq_offset;
750 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400751
adam radfordebf054b2011-02-24 20:57:15 -0800752 if (instance->instancet == &megasas_instance_template_skinny) {
753 seq_offset = &reg_set->fusion_seq_offset;
754 hostdiag_offset = &reg_set->fusion_host_diag;
755 }
756
757 writel(0, seq_offset);
758 writel(4, seq_offset);
759 writel(0xb, seq_offset);
760 writel(2, seq_offset);
761 writel(7, seq_offset);
762 writel(0xd, seq_offset);
763
bo yang39a98552010-09-22 22:36:29 -0400764 msleep(1000);
765
adam radfordebf054b2011-02-24 20:57:15 -0800766 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400767
768 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
769 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800770 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400771 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
772 retry, HostDiag);
773
774 if (retry++ >= 100)
775 return 1;
776
777 }
778
779 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
780
adam radfordebf054b2011-02-24 20:57:15 -0800781 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400782
783 ssleep(10);
784
adam radfordebf054b2011-02-24 20:57:15 -0800785 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400786 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
787 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800788 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400789 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
790 retry, HostDiag);
791
792 if (retry++ >= 1000)
793 return 1;
794
795 }
796 return 0;
797}
798
799/**
800 * megasas_check_reset_gen2 - For controller reset check
801 * @regs: MFI register set
802 */
803static int
804megasas_check_reset_gen2(struct megasas_instance *instance,
805 struct megasas_register_set __iomem *regs)
806{
Yang, Bo707e09b2010-10-12 07:20:27 -0600807 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
808 return 1;
809 }
810
bo yang39a98552010-09-22 22:36:29 -0400811 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700812}
813
814static struct megasas_instance_template megasas_instance_template_gen2 = {
815
816 .fire_cmd = megasas_fire_cmd_gen2,
817 .enable_intr = megasas_enable_intr_gen2,
818 .disable_intr = megasas_disable_intr_gen2,
819 .clear_intr = megasas_clear_intr_gen2,
820 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400821 .adp_reset = megasas_adp_reset_gen2,
822 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800823 .service_isr = megasas_isr,
824 .tasklet = megasas_complete_cmd_dpc,
825 .init_adapter = megasas_init_adapter_mfi,
826 .build_and_issue_cmd = megasas_build_and_issue_cmd,
827 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700828};
829
830/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800831* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400832* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800833*/
834
adam radford9c915a82010-12-21 13:34:31 -0800835/*
836 * Template added for TB (Fusion)
837 */
838extern struct megasas_instance_template megasas_instance_template_fusion;
839
Sumant Patrof9876f02006-02-03 15:34:35 -0800840/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400841 * megasas_issue_polled - Issues a polling command
842 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800843 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400844 *
845 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
846 */
adam radford9c915a82010-12-21 13:34:31 -0800847int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400848megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
849{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400850
851 struct megasas_header *frame_hdr = &cmd->frame->hdr;
852
853 frame_hdr->cmd_status = 0xFF;
854 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
855
856 /*
857 * Issue the frame using inbound queue port
858 */
adam radford9c915a82010-12-21 13:34:31 -0800859 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400860
861 /*
862 * Wait for cmd_status to change
863 */
adam radford9c915a82010-12-21 13:34:31 -0800864 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400865}
866
867/**
868 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
869 * @instance: Adapter soft state
870 * @cmd: Command to be issued
871 *
872 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700873 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400874 * Used to issue ioctl commands.
875 */
876static int
877megasas_issue_blocked_cmd(struct megasas_instance *instance,
878 struct megasas_cmd *cmd)
879{
880 cmd->cmd_status = ENODATA;
881
adam radford9c915a82010-12-21 13:34:31 -0800882 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400883
bo yang39a98552010-09-22 22:36:29 -0400884 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400885
886 return 0;
887}
888
889/**
890 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
891 * @instance: Adapter soft state
892 * @cmd_to_abort: Previously issued cmd to be aborted
893 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100894 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400895 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700896 * cmd and waits for return status.
897 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400898 */
899static int
900megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
901 struct megasas_cmd *cmd_to_abort)
902{
903 struct megasas_cmd *cmd;
904 struct megasas_abort_frame *abort_fr;
905
906 cmd = megasas_get_cmd(instance);
907
908 if (!cmd)
909 return -1;
910
911 abort_fr = &cmd->frame->abort;
912
913 /*
914 * Prepare and issue the abort frame
915 */
916 abort_fr->cmd = MFI_CMD_ABORT;
917 abort_fr->cmd_status = 0xFF;
918 abort_fr->flags = 0;
919 abort_fr->abort_context = cmd_to_abort->index;
920 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
921 abort_fr->abort_mfi_phys_addr_hi = 0;
922
923 cmd->sync_cmd = 1;
924 cmd->cmd_status = 0xFF;
925
adam radford9c915a82010-12-21 13:34:31 -0800926 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400927
928 /*
929 * Wait for this cmd to complete
930 */
bo yang39a98552010-09-22 22:36:29 -0400931 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
932 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400933
934 megasas_return_cmd(instance, cmd);
935 return 0;
936}
937
938/**
939 * megasas_make_sgl32 - Prepares 32-bit SGL
940 * @instance: Adapter soft state
941 * @scp: SCSI command from the mid-layer
942 * @mfi_sgl: SGL to be filled in
943 *
944 * If successful, this function returns the number of SG elements. Otherwise,
945 * it returnes -1.
946 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800947static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400948megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
949 union megasas_sgl *mfi_sgl)
950{
951 int i;
952 int sge_count;
953 struct scatterlist *os_sgl;
954
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900955 sge_count = scsi_dma_map(scp);
956 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400957
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900958 if (sge_count) {
959 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
960 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
961 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
962 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400963 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400964 return sge_count;
965}
966
967/**
968 * megasas_make_sgl64 - Prepares 64-bit SGL
969 * @instance: Adapter soft state
970 * @scp: SCSI command from the mid-layer
971 * @mfi_sgl: SGL to be filled in
972 *
973 * If successful, this function returns the number of SG elements. Otherwise,
974 * it returnes -1.
975 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800976static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400977megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
978 union megasas_sgl *mfi_sgl)
979{
980 int i;
981 int sge_count;
982 struct scatterlist *os_sgl;
983
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900984 sge_count = scsi_dma_map(scp);
985 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400986
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900987 if (sge_count) {
988 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
989 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
990 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
991 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400992 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400993 return sge_count;
994}
995
Yang, Bof4c9a132009-10-06 14:43:28 -0600996/**
997 * megasas_make_sgl_skinny - Prepares IEEE SGL
998 * @instance: Adapter soft state
999 * @scp: SCSI command from the mid-layer
1000 * @mfi_sgl: SGL to be filled in
1001 *
1002 * If successful, this function returns the number of SG elements. Otherwise,
1003 * it returnes -1.
1004 */
1005static int
1006megasas_make_sgl_skinny(struct megasas_instance *instance,
1007 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1008{
1009 int i;
1010 int sge_count;
1011 struct scatterlist *os_sgl;
1012
1013 sge_count = scsi_dma_map(scp);
1014
1015 if (sge_count) {
1016 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1017 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1018 mfi_sgl->sge_skinny[i].phys_addr =
1019 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001020 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001021 }
1022 }
1023 return sge_count;
1024}
1025
Sumant Patrob1df99d2006-10-03 12:40:47 -07001026 /**
1027 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001028 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001029 * @sge_count : number of sg elements
1030 *
1031 * Returns the number of frames required for numnber of sge's (sge_count)
1032 */
1033
Yang, Bof4c9a132009-10-06 14:43:28 -06001034static u32 megasas_get_frame_count(struct megasas_instance *instance,
1035 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001036{
1037 int num_cnt;
1038 int sge_bytes;
1039 u32 sge_sz;
1040 u32 frame_count=0;
1041
1042 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1043 sizeof(struct megasas_sge32);
1044
Yang, Bof4c9a132009-10-06 14:43:28 -06001045 if (instance->flag_ieee) {
1046 sge_sz = sizeof(struct megasas_sge_skinny);
1047 }
1048
Sumant Patrob1df99d2006-10-03 12:40:47 -07001049 /*
bo yangd532dbe2008-03-17 03:36:43 -04001050 * Main frame can contain 2 SGEs for 64-bit SGLs and
1051 * 3 SGEs for 32-bit SGLs for ldio &
1052 * 1 SGEs for 64-bit SGLs and
1053 * 2 SGEs for 32-bit SGLs for pthru frame
1054 */
1055 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001056 if (instance->flag_ieee == 1) {
1057 num_cnt = sge_count - 1;
1058 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001059 num_cnt = sge_count - 1;
1060 else
1061 num_cnt = sge_count - 2;
1062 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001063 if (instance->flag_ieee == 1) {
1064 num_cnt = sge_count - 1;
1065 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001066 num_cnt = sge_count - 2;
1067 else
1068 num_cnt = sge_count - 3;
1069 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001070
1071 if(num_cnt>0){
1072 sge_bytes = sge_sz * num_cnt;
1073
1074 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1075 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1076 }
1077 /* Main frame */
1078 frame_count +=1;
1079
1080 if (frame_count > 7)
1081 frame_count = 8;
1082 return frame_count;
1083}
1084
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001085/**
1086 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1087 * @instance: Adapter soft state
1088 * @scp: SCSI command
1089 * @cmd: Command to be prepared in
1090 *
1091 * This function prepares CDB commands. These are typcially pass-through
1092 * commands to the devices.
1093 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001094static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001095megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1096 struct megasas_cmd *cmd)
1097{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001098 u32 is_logical;
1099 u32 device_id;
1100 u16 flags = 0;
1101 struct megasas_pthru_frame *pthru;
1102
1103 is_logical = MEGASAS_IS_LOGICAL(scp);
1104 device_id = MEGASAS_DEV_INDEX(instance, scp);
1105 pthru = (struct megasas_pthru_frame *)cmd->frame;
1106
1107 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1108 flags = MFI_FRAME_DIR_WRITE;
1109 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1110 flags = MFI_FRAME_DIR_READ;
1111 else if (scp->sc_data_direction == PCI_DMA_NONE)
1112 flags = MFI_FRAME_DIR_NONE;
1113
Yang, Bof4c9a132009-10-06 14:43:28 -06001114 if (instance->flag_ieee == 1) {
1115 flags |= MFI_FRAME_IEEE;
1116 }
1117
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001118 /*
1119 * Prepare the DCDB frame
1120 */
1121 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1122 pthru->cmd_status = 0x0;
1123 pthru->scsi_status = 0x0;
1124 pthru->target_id = device_id;
1125 pthru->lun = scp->device->lun;
1126 pthru->cdb_len = scp->cmd_len;
1127 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001128 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001129 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001130 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001131
1132 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1133
1134 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001135 * If the command is for the tape device, set the
1136 * pthru timeout to the os layer timeout value.
1137 */
1138 if (scp->device->type == TYPE_TAPE) {
1139 if ((scp->request->timeout / HZ) > 0xFFFF)
1140 pthru->timeout = 0xFFFF;
1141 else
1142 pthru->timeout = scp->request->timeout / HZ;
1143 }
1144
1145 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001146 * Construct SGL
1147 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001148 if (instance->flag_ieee == 1) {
1149 pthru->flags |= MFI_FRAME_SGL64;
1150 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1151 &pthru->sgl);
1152 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001153 pthru->flags |= MFI_FRAME_SGL64;
1154 pthru->sge_count = megasas_make_sgl64(instance, scp,
1155 &pthru->sgl);
1156 } else
1157 pthru->sge_count = megasas_make_sgl32(instance, scp,
1158 &pthru->sgl);
1159
Yang, Bobdc6fb82009-12-06 08:30:19 -07001160 if (pthru->sge_count > instance->max_num_sge) {
1161 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1162 pthru->sge_count);
1163 return 0;
1164 }
1165
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001166 /*
1167 * Sense info specific
1168 */
1169 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1170 pthru->sense_buf_phys_addr_hi = 0;
1171 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1172
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001173 /*
1174 * Compute the total number of frames this command consumes. FW uses
1175 * this number to pull sufficient number of frames from host memory.
1176 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001177 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001178 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001179
1180 return cmd->frame_count;
1181}
1182
1183/**
1184 * megasas_build_ldio - Prepares IOs to logical devices
1185 * @instance: Adapter soft state
1186 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001187 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001188 *
1189 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1190 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001191static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001192megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1193 struct megasas_cmd *cmd)
1194{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001195 u32 device_id;
1196 u8 sc = scp->cmnd[0];
1197 u16 flags = 0;
1198 struct megasas_io_frame *ldio;
1199
1200 device_id = MEGASAS_DEV_INDEX(instance, scp);
1201 ldio = (struct megasas_io_frame *)cmd->frame;
1202
1203 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1204 flags = MFI_FRAME_DIR_WRITE;
1205 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1206 flags = MFI_FRAME_DIR_READ;
1207
Yang, Bof4c9a132009-10-06 14:43:28 -06001208 if (instance->flag_ieee == 1) {
1209 flags |= MFI_FRAME_IEEE;
1210 }
1211
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001212 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001213 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001214 */
1215 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1216 ldio->cmd_status = 0x0;
1217 ldio->scsi_status = 0x0;
1218 ldio->target_id = device_id;
1219 ldio->timeout = 0;
1220 ldio->reserved_0 = 0;
1221 ldio->pad_0 = 0;
1222 ldio->flags = flags;
1223 ldio->start_lba_hi = 0;
1224 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1225
1226 /*
1227 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1228 */
1229 if (scp->cmd_len == 6) {
1230 ldio->lba_count = (u32) scp->cmnd[4];
1231 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1232 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1233
1234 ldio->start_lba_lo &= 0x1FFFFF;
1235 }
1236
1237 /*
1238 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1239 */
1240 else if (scp->cmd_len == 10) {
1241 ldio->lba_count = (u32) scp->cmnd[8] |
1242 ((u32) scp->cmnd[7] << 8);
1243 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1244 ((u32) scp->cmnd[3] << 16) |
1245 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1246 }
1247
1248 /*
1249 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1250 */
1251 else if (scp->cmd_len == 12) {
1252 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1253 ((u32) scp->cmnd[7] << 16) |
1254 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1255
1256 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1257 ((u32) scp->cmnd[3] << 16) |
1258 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1259 }
1260
1261 /*
1262 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1263 */
1264 else if (scp->cmd_len == 16) {
1265 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1266 ((u32) scp->cmnd[11] << 16) |
1267 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1268
1269 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1270 ((u32) scp->cmnd[7] << 16) |
1271 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1272
1273 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1274 ((u32) scp->cmnd[3] << 16) |
1275 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1276
1277 }
1278
1279 /*
1280 * Construct SGL
1281 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001282 if (instance->flag_ieee) {
1283 ldio->flags |= MFI_FRAME_SGL64;
1284 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1285 &ldio->sgl);
1286 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001287 ldio->flags |= MFI_FRAME_SGL64;
1288 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1289 } else
1290 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1291
Yang, Bobdc6fb82009-12-06 08:30:19 -07001292 if (ldio->sge_count > instance->max_num_sge) {
1293 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1294 ldio->sge_count);
1295 return 0;
1296 }
1297
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001298 /*
1299 * Sense info specific
1300 */
1301 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1302 ldio->sense_buf_phys_addr_hi = 0;
1303 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1304
Sumant Patrob1df99d2006-10-03 12:40:47 -07001305 /*
1306 * Compute the total number of frames this command consumes. FW uses
1307 * this number to pull sufficient number of frames from host memory.
1308 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001309 cmd->frame_count = megasas_get_frame_count(instance,
1310 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001311
1312 return cmd->frame_count;
1313}
1314
1315/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001316 * megasas_is_ldio - Checks if the cmd is for logical drive
1317 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001318 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001319 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001320 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001321 */
adam radford9c915a82010-12-21 13:34:31 -08001322inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001323{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001324 if (!MEGASAS_IS_LOGICAL(cmd))
1325 return 0;
1326 switch (cmd->cmnd[0]) {
1327 case READ_10:
1328 case WRITE_10:
1329 case READ_12:
1330 case WRITE_12:
1331 case READ_6:
1332 case WRITE_6:
1333 case READ_16:
1334 case WRITE_16:
1335 return 1;
1336 default:
1337 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001338 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001339}
1340
Sumant Patro658dced2006-10-03 13:09:14 -07001341 /**
1342 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1343 * in FW
1344 * @instance: Adapter soft state
1345 */
1346static inline void
1347megasas_dump_pending_frames(struct megasas_instance *instance)
1348{
1349 struct megasas_cmd *cmd;
1350 int i,n;
1351 union megasas_sgl *mfi_sgl;
1352 struct megasas_io_frame *ldio;
1353 struct megasas_pthru_frame *pthru;
1354 u32 sgcount;
1355 u32 max_cmd = instance->max_fw_cmds;
1356
1357 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1358 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1359 if (IS_DMA64)
1360 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1361 else
1362 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1363
1364 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1365 for (i = 0; i < max_cmd; i++) {
1366 cmd = instance->cmd_list[i];
1367 if(!cmd->scmd)
1368 continue;
1369 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1370 if (megasas_is_ldio(cmd->scmd)){
1371 ldio = (struct megasas_io_frame *)cmd->frame;
1372 mfi_sgl = &ldio->sgl;
1373 sgcount = ldio->sge_count;
1374 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);
1375 }
1376 else {
1377 pthru = (struct megasas_pthru_frame *) cmd->frame;
1378 mfi_sgl = &pthru->sgl;
1379 sgcount = pthru->sge_count;
1380 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);
1381 }
1382 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1383 for (n = 0; n < sgcount; n++){
1384 if (IS_DMA64)
1385 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) ;
1386 else
1387 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1388 }
1389 }
1390 printk(KERN_ERR "\n");
1391 } /*for max_cmd*/
1392 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1393 for (i = 0; i < max_cmd; i++) {
1394
1395 cmd = instance->cmd_list[i];
1396
1397 if(cmd->sync_cmd == 1){
1398 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1399 }
1400 }
1401 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1402}
1403
adam radfordcd50ba82010-12-21 10:23:23 -08001404u32
1405megasas_build_and_issue_cmd(struct megasas_instance *instance,
1406 struct scsi_cmnd *scmd)
1407{
1408 struct megasas_cmd *cmd;
1409 u32 frame_count;
1410
1411 cmd = megasas_get_cmd(instance);
1412 if (!cmd)
1413 return SCSI_MLQUEUE_HOST_BUSY;
1414
1415 /*
1416 * Logical drive command
1417 */
1418 if (megasas_is_ldio(scmd))
1419 frame_count = megasas_build_ldio(instance, scmd, cmd);
1420 else
1421 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1422
1423 if (!frame_count)
1424 goto out_return_cmd;
1425
1426 cmd->scmd = scmd;
1427 scmd->SCp.ptr = (char *)cmd;
1428
1429 /*
1430 * Issue the command to the FW
1431 */
1432 atomic_inc(&instance->fw_outstanding);
1433
1434 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1435 cmd->frame_count-1, instance->reg_set);
1436 /*
1437 * Check if we have pend cmds to be completed
1438 */
1439 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1440 tasklet_schedule(&instance->isr_tasklet);
1441
1442 return 0;
1443out_return_cmd:
1444 megasas_return_cmd(instance, cmd);
1445 return 1;
1446}
1447
1448
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001449/**
1450 * megasas_queue_command - Queue entry point
1451 * @scmd: SCSI command to be queued
1452 * @done: Callback entry point
1453 */
1454static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001455megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001456{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001457 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001458 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001459
1460 instance = (struct megasas_instance *)
1461 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001462
bo yang39a98552010-09-22 22:36:29 -04001463 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001464 return SCSI_MLQUEUE_HOST_BUSY;
1465
bo yang39a98552010-09-22 22:36:29 -04001466 spin_lock_irqsave(&instance->hba_lock, flags);
1467 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1468 spin_unlock_irqrestore(&instance->hba_lock, flags);
1469 return SCSI_MLQUEUE_HOST_BUSY;
1470 }
1471
1472 spin_unlock_irqrestore(&instance->hba_lock, flags);
1473
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001474 scmd->scsi_done = done;
1475 scmd->result = 0;
1476
Sumant Patrocb59aa62006-01-25 11:53:25 -08001477 if (MEGASAS_IS_LOGICAL(scmd) &&
1478 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1479 scmd->result = DID_BAD_TARGET << 16;
1480 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001481 }
1482
Sumant Patro02b01e02007-02-14 13:00:55 -08001483 switch (scmd->cmnd[0]) {
1484 case SYNCHRONIZE_CACHE:
1485 /*
1486 * FW takes care of flush cache on its own
1487 * No need to send it down
1488 */
1489 scmd->result = DID_OK << 16;
1490 goto out_done;
1491 default:
1492 break;
1493 }
1494
adam radfordcd50ba82010-12-21 10:23:23 -08001495 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1496 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001497 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001498 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001499
1500 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001501
Sumant Patrocb59aa62006-01-25 11:53:25 -08001502 out_done:
1503 done(scmd);
1504 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001505}
1506
Jeff Garzikf2812332010-11-16 02:10:29 -05001507static DEF_SCSI_QCMD(megasas_queue_command)
1508
Yang, Bo044833b2009-10-06 14:33:06 -06001509static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1510{
1511 int i;
1512
1513 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1514
1515 if ((megasas_mgmt_info.instance[i]) &&
1516 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1517 return megasas_mgmt_info.instance[i];
1518 }
1519
1520 return NULL;
1521}
1522
Christoph Hellwig147aab62006-02-17 12:13:48 +01001523static int megasas_slave_configure(struct scsi_device *sdev)
1524{
Yang, Bo044833b2009-10-06 14:33:06 -06001525 u16 pd_index = 0;
1526 struct megasas_instance *instance ;
1527
1528 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001529
1530 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001531 * Don't export physical disk devices to the disk driver.
1532 *
1533 * FIXME: Currently we don't export them to the midlayer at all.
1534 * That will be fixed once LSI engineers have audited the
1535 * firmware for possible issues.
1536 */
1537 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1538 sdev->type == TYPE_DISK) {
1539 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1540 sdev->id;
1541 if (instance->pd_list[pd_index].driveState ==
1542 MR_PD_STATE_SYSTEM) {
1543 blk_queue_rq_timeout(sdev->request_queue,
1544 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1545 return 0;
1546 }
1547 return -ENXIO;
1548 }
1549
1550 /*
1551 * The RAID firmware may require extended timeouts.
1552 */
1553 blk_queue_rq_timeout(sdev->request_queue,
1554 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1555 return 0;
1556}
1557
1558static int megasas_slave_alloc(struct scsi_device *sdev)
1559{
1560 u16 pd_index = 0;
1561 struct megasas_instance *instance ;
1562 instance = megasas_lookup_instance(sdev->host->host_no);
1563 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1564 (sdev->type == TYPE_DISK)) {
1565 /*
1566 * Open the OS scan to the SYSTEM PD
1567 */
1568 pd_index =
1569 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1570 sdev->id;
1571 if ((instance->pd_list[pd_index].driveState ==
1572 MR_PD_STATE_SYSTEM) &&
1573 (instance->pd_list[pd_index].driveType ==
1574 TYPE_DISK)) {
1575 return 0;
1576 }
1577 return -ENXIO;
1578 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001579 return 0;
1580}
1581
adam radford9c915a82010-12-21 13:34:31 -08001582void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001583{
1584 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001585 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1586 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)) {
1587 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001588 } else {
adam radford9c915a82010-12-21 13:34:31 -08001589 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1590 }
1591}
1592
1593 /**
1594 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1595 * restored to max value
1596 * @instance: Adapter soft state
1597 *
1598 */
1599void
1600megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1601{
1602 unsigned long flags;
1603 if (instance->flag & MEGASAS_FW_BUSY
1604 && time_after(jiffies, instance->last_time + 5 * HZ)
1605 && atomic_read(&instance->fw_outstanding) < 17) {
1606
1607 spin_lock_irqsave(instance->host->host_lock, flags);
1608 instance->flag &= ~MEGASAS_FW_BUSY;
1609 if ((instance->pdev->device ==
1610 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1611 (instance->pdev->device ==
1612 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1613 instance->host->can_queue =
1614 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1615 } else
1616 instance->host->can_queue =
1617 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1618
1619 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001620 }
1621}
1622
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001623/**
bo yang7343eb62007-11-09 04:35:44 -05001624 * megasas_complete_cmd_dpc - Returns FW's controller structure
1625 * @instance_addr: Address of adapter soft state
1626 *
1627 * Tasklet to complete cmds
1628 */
1629static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1630{
1631 u32 producer;
1632 u32 consumer;
1633 u32 context;
1634 struct megasas_cmd *cmd;
1635 struct megasas_instance *instance =
1636 (struct megasas_instance *)instance_addr;
1637 unsigned long flags;
1638
1639 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001640 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001641 return;
1642
1643 spin_lock_irqsave(&instance->completion_lock, flags);
1644
1645 producer = *instance->producer;
1646 consumer = *instance->consumer;
1647
1648 while (consumer != producer) {
1649 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001650 if (context >= instance->max_fw_cmds) {
1651 printk(KERN_ERR "Unexpected context value %x\n",
1652 context);
1653 BUG();
1654 }
bo yang7343eb62007-11-09 04:35:44 -05001655
1656 cmd = instance->cmd_list[context];
1657
1658 megasas_complete_cmd(instance, cmd, DID_OK);
1659
1660 consumer++;
1661 if (consumer == (instance->max_fw_cmds + 1)) {
1662 consumer = 0;
1663 }
1664 }
1665
1666 *instance->consumer = producer;
1667
1668 spin_unlock_irqrestore(&instance->completion_lock, flags);
1669
1670 /*
1671 * Check if we can restore can_queue
1672 */
adam radford9c915a82010-12-21 13:34:31 -08001673 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001674}
1675
Yang, Bo707e09b2010-10-12 07:20:27 -06001676static void
1677megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1678
1679static void
1680process_fw_state_change_wq(struct work_struct *work);
1681
1682void megasas_do_ocr(struct megasas_instance *instance)
1683{
1684 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1685 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1686 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1687 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1688 }
1689 instance->instancet->disable_intr(instance->reg_set);
1690 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1691 instance->issuepend_done = 0;
1692
1693 atomic_set(&instance->fw_outstanding, 0);
1694 megasas_internal_reset_defer_cmds(instance);
1695 process_fw_state_change_wq(&instance->work_init);
1696}
1697
bo yang7343eb62007-11-09 04:35:44 -05001698/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001699 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1700 * @instance: Adapter soft state
1701 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001702 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001703 * complete all its outstanding commands. Returns error if one or more IOs
1704 * are pending after this time period. It also marks the controller dead.
1705 */
1706static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1707{
1708 int i;
bo yang39a98552010-09-22 22:36:29 -04001709 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001710 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001711 u8 adprecovery;
1712 unsigned long flags;
1713 struct list_head clist_local;
1714 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001715 u32 fw_state;
1716 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001717
1718 spin_lock_irqsave(&instance->hba_lock, flags);
1719 adprecovery = instance->adprecovery;
1720 spin_unlock_irqrestore(&instance->hba_lock, flags);
1721
1722 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1723
1724 INIT_LIST_HEAD(&clist_local);
1725 spin_lock_irqsave(&instance->hba_lock, flags);
1726 list_splice_init(&instance->internal_reset_pending_q,
1727 &clist_local);
1728 spin_unlock_irqrestore(&instance->hba_lock, flags);
1729
1730 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1731 for (i = 0; i < wait_time; i++) {
1732 msleep(1000);
1733 spin_lock_irqsave(&instance->hba_lock, flags);
1734 adprecovery = instance->adprecovery;
1735 spin_unlock_irqrestore(&instance->hba_lock, flags);
1736 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1737 break;
1738 }
1739
1740 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1741 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1742 spin_lock_irqsave(&instance->hba_lock, flags);
1743 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1744 spin_unlock_irqrestore(&instance->hba_lock, flags);
1745 return FAILED;
1746 }
1747
1748 reset_index = 0;
1749 while (!list_empty(&clist_local)) {
1750 reset_cmd = list_entry((&clist_local)->next,
1751 struct megasas_cmd, list);
1752 list_del_init(&reset_cmd->list);
1753 if (reset_cmd->scmd) {
1754 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001755 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001756 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001757 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001758
1759 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1760 megasas_return_cmd(instance, reset_cmd);
1761 } else if (reset_cmd->sync_cmd) {
1762 printk(KERN_NOTICE "megasas:%p synch cmds"
1763 "reset queue\n",
1764 reset_cmd);
1765
1766 reset_cmd->cmd_status = ENODATA;
1767 instance->instancet->fire_cmd(instance,
1768 reset_cmd->frame_phys_addr,
1769 0, instance->reg_set);
1770 } else {
1771 printk(KERN_NOTICE "megasas: %p unexpected"
1772 "cmds lst\n",
1773 reset_cmd);
1774 }
1775 reset_index++;
1776 }
1777
1778 return SUCCESS;
1779 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001780
1781 for (i = 0; i < wait_time; i++) {
1782
Sumant Patroe4a082c2006-05-30 12:03:37 -07001783 int outstanding = atomic_read(&instance->fw_outstanding);
1784
1785 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001786 break;
1787
1788 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1789 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001790 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001791 /*
1792 * Call cmd completion routine. Cmd to be
1793 * be completed directly without depending on isr.
1794 */
1795 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001796 }
1797
1798 msleep(1000);
1799 }
1800
Yang, Bo707e09b2010-10-12 07:20:27 -06001801 i = 0;
1802 kill_adapter_flag = 0;
1803 do {
1804 fw_state = instance->instancet->read_fw_status_reg(
1805 instance->reg_set) & MFI_STATE_MASK;
1806 if ((fw_state == MFI_STATE_FAULT) &&
1807 (instance->disableOnlineCtrlReset == 0)) {
1808 if (i == 3) {
1809 kill_adapter_flag = 2;
1810 break;
1811 }
1812 megasas_do_ocr(instance);
1813 kill_adapter_flag = 1;
1814
1815 /* wait for 1 secs to let FW finish the pending cmds */
1816 msleep(1000);
1817 }
1818 i++;
1819 } while (i <= 3);
1820
1821 if (atomic_read(&instance->fw_outstanding) &&
1822 !kill_adapter_flag) {
1823 if (instance->disableOnlineCtrlReset == 0) {
1824
1825 megasas_do_ocr(instance);
1826
1827 /* wait for 5 secs to let FW finish the pending cmds */
1828 for (i = 0; i < wait_time; i++) {
1829 int outstanding =
1830 atomic_read(&instance->fw_outstanding);
1831 if (!outstanding)
1832 return SUCCESS;
1833 msleep(1000);
1834 }
1835 }
1836 }
1837
1838 if (atomic_read(&instance->fw_outstanding) ||
1839 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001840 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001841 /*
1842 * Send signal to FW to stop processing any pending cmds.
1843 * The controller will be taken offline by the OS now.
1844 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001845 if ((instance->pdev->device ==
1846 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1847 (instance->pdev->device ==
1848 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1849 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001850 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001851 } else {
1852 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001853 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001854 }
Sumant Patro658dced2006-10-03 13:09:14 -07001855 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001856 spin_lock_irqsave(&instance->hba_lock, flags);
1857 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1858 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001859 return FAILED;
1860 }
1861
bo yang39a98552010-09-22 22:36:29 -04001862 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1863
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001864 return SUCCESS;
1865}
1866
1867/**
1868 * megasas_generic_reset - Generic reset routine
1869 * @scmd: Mid-layer SCSI command
1870 *
1871 * This routine implements a generic reset handler for device, bus and host
1872 * reset requests. Device, bus and host specific reset handlers can use this
1873 * function after they do their specific tasks.
1874 */
1875static int megasas_generic_reset(struct scsi_cmnd *scmd)
1876{
1877 int ret_val;
1878 struct megasas_instance *instance;
1879
1880 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1881
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001882 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1883 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001884
bo yang39a98552010-09-22 22:36:29 -04001885 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001886 printk(KERN_ERR "megasas: cannot recover from previous reset "
1887 "failures\n");
1888 return FAILED;
1889 }
1890
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001891 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001892 if (ret_val == SUCCESS)
1893 printk(KERN_NOTICE "megasas: reset successful \n");
1894 else
1895 printk(KERN_ERR "megasas: failed to do reset\n");
1896
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001897 return ret_val;
1898}
1899
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001900/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001901 * megasas_reset_timer - quiesce the adapter if required
1902 * @scmd: scsi cmnd
1903 *
1904 * Sets the FW busy flag and reduces the host->can_queue if the
1905 * cmd has not been completed within the timeout period.
1906 */
1907static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001908blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001909{
1910 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1911 struct megasas_instance *instance;
1912 unsigned long flags;
1913
1914 if (time_after(jiffies, scmd->jiffies_at_alloc +
1915 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001916 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001917 }
1918
1919 instance = cmd->instance;
1920 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1921 /* FW is busy, throttle IO */
1922 spin_lock_irqsave(instance->host->host_lock, flags);
1923
1924 instance->host->can_queue = 16;
1925 instance->last_time = jiffies;
1926 instance->flag |= MEGASAS_FW_BUSY;
1927
1928 spin_unlock_irqrestore(instance->host->host_lock, flags);
1929 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001930 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001931}
1932
1933/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001934 * megasas_reset_device - Device reset handler entry point
1935 */
1936static int megasas_reset_device(struct scsi_cmnd *scmd)
1937{
1938 int ret;
1939
1940 /*
1941 * First wait for all commands to complete
1942 */
1943 ret = megasas_generic_reset(scmd);
1944
1945 return ret;
1946}
1947
1948/**
1949 * megasas_reset_bus_host - Bus & host reset handler entry point
1950 */
1951static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1952{
1953 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001954 struct megasas_instance *instance;
1955 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001956
1957 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001958 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001959 */
adam radford9c915a82010-12-21 13:34:31 -08001960 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
1961 ret = megasas_reset_fusion(scmd->device->host);
1962 else
1963 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001964
1965 return ret;
1966}
1967
1968/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001969 * megasas_bios_param - Returns disk geometry for a disk
1970 * @sdev: device handle
1971 * @bdev: block device
1972 * @capacity: drive capacity
1973 * @geom: geometry parameters
1974 */
1975static int
1976megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1977 sector_t capacity, int geom[])
1978{
1979 int heads;
1980 int sectors;
1981 sector_t cylinders;
1982 unsigned long tmp;
1983 /* Default heads (64) & sectors (32) */
1984 heads = 64;
1985 sectors = 32;
1986
1987 tmp = heads * sectors;
1988 cylinders = capacity;
1989
1990 sector_div(cylinders, tmp);
1991
1992 /*
1993 * Handle extended translation size for logical drives > 1Gb
1994 */
1995
1996 if (capacity >= 0x200000) {
1997 heads = 255;
1998 sectors = 63;
1999 tmp = heads*sectors;
2000 cylinders = capacity;
2001 sector_div(cylinders, tmp);
2002 }
2003
2004 geom[0] = heads;
2005 geom[1] = sectors;
2006 geom[2] = cylinders;
2007
2008 return 0;
2009}
2010
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002011static void megasas_aen_polling(struct work_struct *work);
2012
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002013/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002014 * megasas_service_aen - Processes an event notification
2015 * @instance: Adapter soft state
2016 * @cmd: AEN command completed by the ISR
2017 *
2018 * For AEN, driver sends a command down to FW that is held by the FW till an
2019 * event occurs. When an event of interest occurs, FW completes the command
2020 * that it was previously holding.
2021 *
2022 * This routines sends SIGIO signal to processes that have registered with the
2023 * driver for AEN.
2024 */
2025static void
2026megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2027{
Yang, Boc3518832009-10-06 14:18:02 -06002028 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002029 /*
2030 * Don't signal app if it is just an aborted previously registered aen
2031 */
Yang, Boc3518832009-10-06 14:18:02 -06002032 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2033 spin_lock_irqsave(&poll_aen_lock, flags);
2034 megasas_poll_wait_aen = 1;
2035 spin_unlock_irqrestore(&poll_aen_lock, flags);
2036 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002037 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002038 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002039 else
2040 cmd->abort_aen = 0;
2041
2042 instance->aen_cmd = NULL;
2043 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002044
bo yang39a98552010-09-22 22:36:29 -04002045 if ((instance->unload == 0) &&
2046 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002047 struct megasas_aen_event *ev;
2048 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2049 if (!ev) {
2050 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2051 } else {
2052 ev->instance = instance;
2053 instance->ev = ev;
2054 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2055 schedule_delayed_work(
2056 (struct delayed_work *)&ev->hotplug_work, 0);
2057 }
2058 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002059}
2060
adam radford4bcde502011-07-26 15:42:52 -07002061static int megasas_change_queue_depth(struct scsi_device *sdev,
2062 int queue_depth, int reason)
2063{
2064 if (reason != SCSI_QDEPTH_DEFAULT)
2065 return -EOPNOTSUPP;
2066
2067 if (queue_depth > sdev->host->can_queue)
2068 queue_depth = sdev->host->can_queue;
2069 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2070 queue_depth);
2071
2072 return queue_depth;
2073}
2074
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002075/*
2076 * Scsi host template for megaraid_sas driver
2077 */
2078static struct scsi_host_template megasas_template = {
2079
2080 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002081 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002082 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002083 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002084 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002085 .queuecommand = megasas_queue_command,
2086 .eh_device_reset_handler = megasas_reset_device,
2087 .eh_bus_reset_handler = megasas_reset_bus_host,
2088 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002089 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002090 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002091 .use_clustering = ENABLE_CLUSTERING,
adam radford4bcde502011-07-26 15:42:52 -07002092 .change_queue_depth = megasas_change_queue_depth,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002093};
2094
2095/**
2096 * megasas_complete_int_cmd - Completes an internal command
2097 * @instance: Adapter soft state
2098 * @cmd: Command to be completed
2099 *
2100 * The megasas_issue_blocked_cmd() function waits for a command to complete
2101 * after it issues a command. This function wakes up that waiting routine by
2102 * calling wake_up() on the wait queue.
2103 */
2104static void
2105megasas_complete_int_cmd(struct megasas_instance *instance,
2106 struct megasas_cmd *cmd)
2107{
2108 cmd->cmd_status = cmd->frame->io.cmd_status;
2109
2110 if (cmd->cmd_status == ENODATA) {
2111 cmd->cmd_status = 0;
2112 }
2113 wake_up(&instance->int_cmd_wait_q);
2114}
2115
2116/**
2117 * megasas_complete_abort - Completes aborting a command
2118 * @instance: Adapter soft state
2119 * @cmd: Cmd that was issued to abort another cmd
2120 *
adam radford0d490162010-12-14 19:17:17 -08002121 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2122 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002123 * wakes up all functions waiting on the same wait queue.
2124 */
2125static void
2126megasas_complete_abort(struct megasas_instance *instance,
2127 struct megasas_cmd *cmd)
2128{
2129 if (cmd->sync_cmd) {
2130 cmd->sync_cmd = 0;
2131 cmd->cmd_status = 0;
2132 wake_up(&instance->abort_cmd_wait_q);
2133 }
2134
2135 return;
2136}
2137
2138/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002139 * megasas_complete_cmd - Completes a command
2140 * @instance: Adapter soft state
2141 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002142 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002143 * SCSI mid-layer instead of the value returned
2144 * by the FW. This should be used if caller wants
2145 * an alternate status (as in the case of aborted
2146 * commands)
2147 */
adam radford9c915a82010-12-21 13:34:31 -08002148void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002149megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2150 u8 alt_status)
2151{
2152 int exception = 0;
2153 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002154 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002155 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002156
bo yang39a98552010-09-22 22:36:29 -04002157 /* flag for the retry reset */
2158 cmd->retry_for_fw_reset = 0;
2159
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002160 if (cmd->scmd)
2161 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002162
2163 switch (hdr->cmd) {
2164
2165 case MFI_CMD_PD_SCSI_IO:
2166 case MFI_CMD_LD_SCSI_IO:
2167
2168 /*
2169 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2170 * issued either through an IO path or an IOCTL path. If it
2171 * was via IOCTL, we will send it to internal completion.
2172 */
2173 if (cmd->sync_cmd) {
2174 cmd->sync_cmd = 0;
2175 megasas_complete_int_cmd(instance, cmd);
2176 break;
2177 }
2178
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002179 case MFI_CMD_LD_READ:
2180 case MFI_CMD_LD_WRITE:
2181
2182 if (alt_status) {
2183 cmd->scmd->result = alt_status << 16;
2184 exception = 1;
2185 }
2186
2187 if (exception) {
2188
Sumant Patroe4a082c2006-05-30 12:03:37 -07002189 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002190
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002191 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002192 cmd->scmd->scsi_done(cmd->scmd);
2193 megasas_return_cmd(instance, cmd);
2194
2195 break;
2196 }
2197
2198 switch (hdr->cmd_status) {
2199
2200 case MFI_STAT_OK:
2201 cmd->scmd->result = DID_OK << 16;
2202 break;
2203
2204 case MFI_STAT_SCSI_IO_FAILED:
2205 case MFI_STAT_LD_INIT_IN_PROGRESS:
2206 cmd->scmd->result =
2207 (DID_ERROR << 16) | hdr->scsi_status;
2208 break;
2209
2210 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2211
2212 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2213
2214 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2215 memset(cmd->scmd->sense_buffer, 0,
2216 SCSI_SENSE_BUFFERSIZE);
2217 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2218 hdr->sense_len);
2219
2220 cmd->scmd->result |= DRIVER_SENSE << 24;
2221 }
2222
2223 break;
2224
2225 case MFI_STAT_LD_OFFLINE:
2226 case MFI_STAT_DEVICE_NOT_FOUND:
2227 cmd->scmd->result = DID_BAD_TARGET << 16;
2228 break;
2229
2230 default:
2231 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2232 hdr->cmd_status);
2233 cmd->scmd->result = DID_ERROR << 16;
2234 break;
2235 }
2236
Sumant Patroe4a082c2006-05-30 12:03:37 -07002237 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002238
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002239 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002240 cmd->scmd->scsi_done(cmd->scmd);
2241 megasas_return_cmd(instance, cmd);
2242
2243 break;
2244
2245 case MFI_CMD_SMP:
2246 case MFI_CMD_STP:
2247 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002248 /* Check for LD map update */
2249 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2250 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2251 spin_lock_irqsave(instance->host->host_lock, flags);
2252 if (cmd->frame->hdr.cmd_status != 0) {
2253 if (cmd->frame->hdr.cmd_status !=
2254 MFI_STAT_NOT_FOUND)
2255 printk(KERN_WARNING "megasas: map sync"
2256 "failed, status = 0x%x.\n",
2257 cmd->frame->hdr.cmd_status);
2258 else {
2259 megasas_return_cmd(instance, cmd);
2260 spin_unlock_irqrestore(
2261 instance->host->host_lock,
2262 flags);
2263 break;
2264 }
2265 } else
2266 instance->map_id++;
2267 megasas_return_cmd(instance, cmd);
2268 if (MR_ValidateMapInfo(
2269 fusion->ld_map[(instance->map_id & 1)],
2270 fusion->load_balance_info))
2271 fusion->fast_path_io = 1;
2272 else
2273 fusion->fast_path_io = 0;
2274 megasas_sync_map_info(instance);
2275 spin_unlock_irqrestore(instance->host->host_lock,
2276 flags);
2277 break;
2278 }
Yang, Boc3518832009-10-06 14:18:02 -06002279 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2280 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2281 spin_lock_irqsave(&poll_aen_lock, flags);
2282 megasas_poll_wait_aen = 0;
2283 spin_unlock_irqrestore(&poll_aen_lock, flags);
2284 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002285
2286 /*
2287 * See if got an event notification
2288 */
2289 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2290 megasas_service_aen(instance, cmd);
2291 else
2292 megasas_complete_int_cmd(instance, cmd);
2293
2294 break;
2295
2296 case MFI_CMD_ABORT:
2297 /*
2298 * Cmd issued to abort another cmd returned
2299 */
2300 megasas_complete_abort(instance, cmd);
2301 break;
2302
2303 default:
2304 printk("megasas: Unknown command completed! [0x%X]\n",
2305 hdr->cmd);
2306 break;
2307 }
2308}
2309
2310/**
bo yang39a98552010-09-22 22:36:29 -04002311 * megasas_issue_pending_cmds_again - issue all pending cmds
2312 * in FW again because of the fw reset
2313 * @instance: Adapter soft state
2314 */
2315static inline void
2316megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2317{
2318 struct megasas_cmd *cmd;
2319 struct list_head clist_local;
2320 union megasas_evt_class_locale class_locale;
2321 unsigned long flags;
2322 u32 seq_num;
2323
2324 INIT_LIST_HEAD(&clist_local);
2325 spin_lock_irqsave(&instance->hba_lock, flags);
2326 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2327 spin_unlock_irqrestore(&instance->hba_lock, flags);
2328
2329 while (!list_empty(&clist_local)) {
2330 cmd = list_entry((&clist_local)->next,
2331 struct megasas_cmd, list);
2332 list_del_init(&cmd->list);
2333
2334 if (cmd->sync_cmd || cmd->scmd) {
2335 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2336 "detected to be pending while HBA reset.\n",
2337 cmd, cmd->scmd, cmd->sync_cmd);
2338
2339 cmd->retry_for_fw_reset++;
2340
2341 if (cmd->retry_for_fw_reset == 3) {
2342 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2343 "was tried multiple times during reset."
2344 "Shutting down the HBA\n",
2345 cmd, cmd->scmd, cmd->sync_cmd);
2346 megaraid_sas_kill_hba(instance);
2347
2348 instance->adprecovery =
2349 MEGASAS_HW_CRITICAL_ERROR;
2350 return;
2351 }
2352 }
2353
2354 if (cmd->sync_cmd == 1) {
2355 if (cmd->scmd) {
2356 printk(KERN_NOTICE "megaraid_sas: unexpected"
2357 "cmd attached to internal command!\n");
2358 }
2359 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2360 "on the internal reset queue,"
2361 "issue it again.\n", cmd);
2362 cmd->cmd_status = ENODATA;
2363 instance->instancet->fire_cmd(instance,
2364 cmd->frame_phys_addr ,
2365 0, instance->reg_set);
2366 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002367 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002368 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002369 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002370
2371 atomic_inc(&instance->fw_outstanding);
2372 instance->instancet->fire_cmd(instance,
2373 cmd->frame_phys_addr,
2374 cmd->frame_count-1, instance->reg_set);
2375 } else {
2376 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2377 "internal reset defer list while re-issue!!\n",
2378 cmd);
2379 }
2380 }
2381
2382 if (instance->aen_cmd) {
2383 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2384 megasas_return_cmd(instance, instance->aen_cmd);
2385
2386 instance->aen_cmd = NULL;
2387 }
2388
2389 /*
2390 * Initiate AEN (Asynchronous Event Notification)
2391 */
2392 seq_num = instance->last_seq_num;
2393 class_locale.members.reserved = 0;
2394 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2395 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2396
2397 megasas_register_aen(instance, seq_num, class_locale.word);
2398}
2399
2400/**
2401 * Move the internal reset pending commands to a deferred queue.
2402 *
2403 * We move the commands pending at internal reset time to a
2404 * pending queue. This queue would be flushed after successful
2405 * completion of the internal reset sequence. if the internal reset
2406 * did not complete in time, the kernel reset handler would flush
2407 * these commands.
2408 **/
2409static void
2410megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2411{
2412 struct megasas_cmd *cmd;
2413 int i;
2414 u32 max_cmd = instance->max_fw_cmds;
2415 u32 defer_index;
2416 unsigned long flags;
2417
2418 defer_index = 0;
2419 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2420 for (i = 0; i < max_cmd; i++) {
2421 cmd = instance->cmd_list[i];
2422 if (cmd->sync_cmd == 1 || cmd->scmd) {
2423 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2424 "on the defer queue as internal\n",
2425 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2426
2427 if (!list_empty(&cmd->list)) {
2428 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2429 " moving this cmd:%p, %d %p, it was"
2430 "discovered on some list?\n",
2431 cmd, cmd->sync_cmd, cmd->scmd);
2432
2433 list_del_init(&cmd->list);
2434 }
2435 defer_index++;
2436 list_add_tail(&cmd->list,
2437 &instance->internal_reset_pending_q);
2438 }
2439 }
2440 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2441}
2442
2443
2444static void
2445process_fw_state_change_wq(struct work_struct *work)
2446{
2447 struct megasas_instance *instance =
2448 container_of(work, struct megasas_instance, work_init);
2449 u32 wait;
2450 unsigned long flags;
2451
2452 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2453 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2454 instance->adprecovery);
2455 return ;
2456 }
2457
2458 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2459 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2460 "state, restarting it...\n");
2461
2462 instance->instancet->disable_intr(instance->reg_set);
2463 atomic_set(&instance->fw_outstanding, 0);
2464
2465 atomic_set(&instance->fw_reset_no_pci_access, 1);
2466 instance->instancet->adp_reset(instance, instance->reg_set);
2467 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2468
2469 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2470 "initiating next stage...\n");
2471
2472 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2473 "state 2 starting...\n");
2474
2475 /*waitting for about 20 second before start the second init*/
2476 for (wait = 0; wait < 30; wait++) {
2477 msleep(1000);
2478 }
2479
adam radford058a8fa2011-10-08 18:14:27 -07002480 if (megasas_transition_to_ready(instance, 1)) {
bo yang39a98552010-09-22 22:36:29 -04002481 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2482
2483 megaraid_sas_kill_hba(instance);
2484 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2485 return ;
2486 }
2487
2488 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2489 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2490 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2491 ) {
2492 *instance->consumer = *instance->producer;
2493 } else {
2494 *instance->consumer = 0;
2495 *instance->producer = 0;
2496 }
2497
2498 megasas_issue_init_mfi(instance);
2499
2500 spin_lock_irqsave(&instance->hba_lock, flags);
2501 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2502 spin_unlock_irqrestore(&instance->hba_lock, flags);
2503 instance->instancet->enable_intr(instance->reg_set);
2504
2505 megasas_issue_pending_cmds_again(instance);
2506 instance->issuepend_done = 1;
2507 }
2508 return ;
2509}
2510
2511/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002512 * megasas_deplete_reply_queue - Processes all completed commands
2513 * @instance: Adapter soft state
2514 * @alt_status: Alternate status to be returned to
2515 * SCSI mid-layer instead of the status
2516 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002517 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002518 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002519static int
bo yang39a98552010-09-22 22:36:29 -04002520megasas_deplete_reply_queue(struct megasas_instance *instance,
2521 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002522{
bo yang39a98552010-09-22 22:36:29 -04002523 u32 mfiStatus;
2524 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002525
bo yang39a98552010-09-22 22:36:29 -04002526 if ((mfiStatus = instance->instancet->check_reset(instance,
2527 instance->reg_set)) == 1) {
2528 return IRQ_HANDLED;
2529 }
2530
2531 if ((mfiStatus = instance->instancet->clear_intr(
2532 instance->reg_set)
2533 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002534 /* Hardware may not set outbound_intr_status in MSI-X mode */
2535 if (!instance->msi_flag)
2536 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002537 }
2538
2539 instance->mfiStatus = mfiStatus;
2540
2541 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2542 fw_state = instance->instancet->read_fw_status_reg(
2543 instance->reg_set) & MFI_STATE_MASK;
2544
2545 if (fw_state != MFI_STATE_FAULT) {
2546 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2547 fw_state);
2548 }
2549
2550 if ((fw_state == MFI_STATE_FAULT) &&
2551 (instance->disableOnlineCtrlReset == 0)) {
2552 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2553
2554 if ((instance->pdev->device ==
2555 PCI_DEVICE_ID_LSI_SAS1064R) ||
2556 (instance->pdev->device ==
2557 PCI_DEVICE_ID_DELL_PERC5) ||
2558 (instance->pdev->device ==
2559 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2560
2561 *instance->consumer =
2562 MEGASAS_ADPRESET_INPROG_SIGN;
2563 }
2564
2565
2566 instance->instancet->disable_intr(instance->reg_set);
2567 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2568 instance->issuepend_done = 0;
2569
2570 atomic_set(&instance->fw_outstanding, 0);
2571 megasas_internal_reset_defer_cmds(instance);
2572
2573 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2574 fw_state, instance->adprecovery);
2575
2576 schedule_work(&instance->work_init);
2577 return IRQ_HANDLED;
2578
2579 } else {
2580 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2581 fw_state, instance->disableOnlineCtrlReset);
2582 }
2583 }
2584
Sumant Patro5d018ad2006-10-03 13:13:18 -07002585 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002586 return IRQ_HANDLED;
2587}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002588/**
2589 * megasas_isr - isr entry point
2590 */
David Howells7d12e782006-10-05 14:55:46 +01002591static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002592{
bo yang39a98552010-09-22 22:36:29 -04002593 struct megasas_instance *instance;
2594 unsigned long flags;
2595 irqreturn_t rc;
2596
2597 if (atomic_read(
2598 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2599 return IRQ_HANDLED;
2600
2601 instance = (struct megasas_instance *)devp;
2602
2603 spin_lock_irqsave(&instance->hba_lock, flags);
2604 rc = megasas_deplete_reply_queue(instance, DID_OK);
2605 spin_unlock_irqrestore(&instance->hba_lock, flags);
2606
2607 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002608}
2609
2610/**
2611 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002612 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002613 *
2614 * During the initialization, FW passes can potentially be in any one of
2615 * several possible states. If the FW in operational, waiting-for-handshake
2616 * states, driver must take steps to bring it to ready state. Otherwise, it
2617 * has to wait for the ready state.
2618 */
adam radford9c915a82010-12-21 13:34:31 -08002619int
adam radford058a8fa2011-10-08 18:14:27 -07002620megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002621{
2622 int i;
2623 u8 max_wait;
2624 u32 fw_state;
2625 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002626 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002627
Sumant Patro1341c932006-01-25 12:02:40 -08002628 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002629
Sumant Patroe3bbff92006-10-03 12:28:49 -07002630 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002631 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2632 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002633
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002634 while (fw_state != MFI_STATE_READY) {
2635
Yang, Bo7218df62009-10-06 14:52:20 -06002636 abs_state =
2637 instance->instancet->read_fw_status_reg(instance->reg_set);
2638
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002639 switch (fw_state) {
2640
2641 case MFI_STATE_FAULT:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002642 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07002643 if (ocr) {
2644 max_wait = MEGASAS_RESET_WAIT_TIME;
2645 cur_state = MFI_STATE_FAULT;
2646 break;
2647 } else
2648 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002649
2650 case MFI_STATE_WAIT_HANDSHAKE:
2651 /*
2652 * Set the CLR bit in inbound doorbell
2653 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002654 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002655 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2656 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002657 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2658 (instance->pdev->device ==
2659 PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002660 writel(
2661 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002662 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002663 } else {
2664 writel(
2665 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2666 &instance->reg_set->inbound_doorbell);
2667 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002668
Yang, Bo7218df62009-10-06 14:52:20 -06002669 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002670 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2671 break;
2672
Sumant Patroe3bbff92006-10-03 12:28:49 -07002673 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002674 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002675 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2676 (instance->pdev->device ==
2677 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2678 (instance->pdev->device ==
2679 PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002680 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002681 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002682 } else
2683 writel(MFI_INIT_HOTPLUG,
2684 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002685
Yang, Bo7218df62009-10-06 14:52:20 -06002686 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002687 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2688 break;
2689
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002690 case MFI_STATE_OPERATIONAL:
2691 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002692 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002693 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002694 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002695 if ((instance->pdev->device ==
2696 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2697 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002698 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2699 (instance->pdev->device
2700 == PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002701 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002702 &instance->reg_set->doorbell);
2703 if (instance->pdev->device ==
2704 PCI_DEVICE_ID_LSI_FUSION) {
2705 for (i = 0; i < (10 * 1000); i += 20) {
2706 if (readl(
2707 &instance->
2708 reg_set->
2709 doorbell) & 1)
2710 msleep(20);
2711 else
2712 break;
2713 }
2714 }
Yang, Bo87911122009-10-06 14:31:54 -06002715 } else
2716 writel(MFI_RESET_FLAGS,
2717 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002718
Yang, Bo7218df62009-10-06 14:52:20 -06002719 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002720 cur_state = MFI_STATE_OPERATIONAL;
2721 break;
2722
2723 case MFI_STATE_UNDEFINED:
2724 /*
2725 * This state should not last for more than 2 seconds
2726 */
Yang, Bo7218df62009-10-06 14:52:20 -06002727 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002728 cur_state = MFI_STATE_UNDEFINED;
2729 break;
2730
2731 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002732 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002733 cur_state = MFI_STATE_BB_INIT;
2734 break;
2735
2736 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002737 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002738 cur_state = MFI_STATE_FW_INIT;
2739 break;
2740
2741 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002742 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002743 cur_state = MFI_STATE_FW_INIT_2;
2744 break;
2745
2746 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002747 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002748 cur_state = MFI_STATE_DEVICE_SCAN;
2749 break;
2750
2751 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002752 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002753 cur_state = MFI_STATE_FLUSH_CACHE;
2754 break;
2755
2756 default:
2757 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2758 fw_state);
2759 return -ENODEV;
2760 }
2761
2762 /*
2763 * The cur_state should not last for more than max_wait secs
2764 */
2765 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002766 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002767 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002768 curr_abs_state =
2769 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002770
Yang, Bo7218df62009-10-06 14:52:20 -06002771 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002772 msleep(1);
2773 } else
2774 break;
2775 }
2776
2777 /*
2778 * Return error if fw_state hasn't changed after max_wait
2779 */
Yang, Bo7218df62009-10-06 14:52:20 -06002780 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002781 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2782 "in %d secs\n", fw_state, max_wait);
2783 return -ENODEV;
2784 }
bo yang39a98552010-09-22 22:36:29 -04002785 }
adam radford0d490162010-12-14 19:17:17 -08002786 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002787
2788 return 0;
2789}
2790
2791/**
2792 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2793 * @instance: Adapter soft state
2794 */
2795static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2796{
2797 int i;
adam radford9c915a82010-12-21 13:34:31 -08002798 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799 struct megasas_cmd *cmd;
2800
2801 if (!instance->frame_dma_pool)
2802 return;
2803
2804 /*
2805 * Return all frames to pool
2806 */
2807 for (i = 0; i < max_cmd; i++) {
2808
2809 cmd = instance->cmd_list[i];
2810
2811 if (cmd->frame)
2812 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2813 cmd->frame_phys_addr);
2814
2815 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002816 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002817 cmd->sense_phys_addr);
2818 }
2819
2820 /*
2821 * Now destroy the pool itself
2822 */
2823 pci_pool_destroy(instance->frame_dma_pool);
2824 pci_pool_destroy(instance->sense_dma_pool);
2825
2826 instance->frame_dma_pool = NULL;
2827 instance->sense_dma_pool = NULL;
2828}
2829
2830/**
2831 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2832 * @instance: Adapter soft state
2833 *
2834 * Each command packet has an embedded DMA memory buffer that is used for
2835 * filling MFI frame and the SG list that immediately follows the frame. This
2836 * function creates those DMA memory buffers for each command packet by using
2837 * PCI pool facility.
2838 */
2839static int megasas_create_frame_pool(struct megasas_instance *instance)
2840{
2841 int i;
2842 u32 max_cmd;
2843 u32 sge_sz;
2844 u32 sgl_sz;
2845 u32 total_sz;
2846 u32 frame_count;
2847 struct megasas_cmd *cmd;
2848
adam radford9c915a82010-12-21 13:34:31 -08002849 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002850
2851 /*
2852 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2853 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2854 */
2855 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2856 sizeof(struct megasas_sge32);
2857
Yang, Bof4c9a132009-10-06 14:43:28 -06002858 if (instance->flag_ieee) {
2859 sge_sz = sizeof(struct megasas_sge_skinny);
2860 }
2861
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002862 /*
2863 * Calculated the number of 64byte frames required for SGL
2864 */
2865 sgl_sz = sge_sz * instance->max_num_sge;
2866 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002867 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002868
2869 /*
2870 * We need one extra frame for the MFI command
2871 */
2872 frame_count++;
2873
2874 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2875 /*
2876 * Use DMA pool facility provided by PCI layer
2877 */
2878 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2879 instance->pdev, total_sz, 64,
2880 0);
2881
2882 if (!instance->frame_dma_pool) {
2883 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2884 return -ENOMEM;
2885 }
2886
2887 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2888 instance->pdev, 128, 4, 0);
2889
2890 if (!instance->sense_dma_pool) {
2891 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2892
2893 pci_pool_destroy(instance->frame_dma_pool);
2894 instance->frame_dma_pool = NULL;
2895
2896 return -ENOMEM;
2897 }
2898
2899 /*
2900 * Allocate and attach a frame to each of the commands in cmd_list.
2901 * By making cmd->index as the context instead of the &cmd, we can
2902 * always use 32bit context regardless of the architecture
2903 */
2904 for (i = 0; i < max_cmd; i++) {
2905
2906 cmd = instance->cmd_list[i];
2907
2908 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2909 GFP_KERNEL, &cmd->frame_phys_addr);
2910
2911 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2912 GFP_KERNEL, &cmd->sense_phys_addr);
2913
2914 /*
2915 * megasas_teardown_frame_pool() takes care of freeing
2916 * whatever has been allocated
2917 */
2918 if (!cmd->frame || !cmd->sense) {
2919 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2920 megasas_teardown_frame_pool(instance);
2921 return -ENOMEM;
2922 }
2923
Yang, Bo707e09b2010-10-12 07:20:27 -06002924 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002925 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002926 cmd->frame->io.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002927 }
2928
2929 return 0;
2930}
2931
2932/**
2933 * megasas_free_cmds - Free all the cmds in the free cmd pool
2934 * @instance: Adapter soft state
2935 */
adam radford9c915a82010-12-21 13:34:31 -08002936void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002937{
2938 int i;
2939 /* First free the MFI frame pool */
2940 megasas_teardown_frame_pool(instance);
2941
2942 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002943 for (i = 0; i < instance->max_mfi_cmds; i++)
2944
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002945 kfree(instance->cmd_list[i]);
2946
2947 /* Free the cmd_list buffer itself */
2948 kfree(instance->cmd_list);
2949 instance->cmd_list = NULL;
2950
2951 INIT_LIST_HEAD(&instance->cmd_pool);
2952}
2953
2954/**
2955 * megasas_alloc_cmds - Allocates the command packets
2956 * @instance: Adapter soft state
2957 *
2958 * Each command that is issued to the FW, whether IO commands from the OS or
2959 * internal commands like IOCTLs, are wrapped in local data structure called
2960 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2961 * the FW.
2962 *
2963 * Each frame has a 32-bit field called context (tag). This context is used
2964 * to get back the megasas_cmd from the frame when a frame gets completed in
2965 * the ISR. Typically the address of the megasas_cmd itself would be used as
2966 * the context. But we wanted to keep the differences between 32 and 64 bit
2967 * systems to the mininum. We always use 32 bit integers for the context. In
2968 * this driver, the 32 bit values are the indices into an array cmd_list.
2969 * This array is used only to look up the megasas_cmd given the context. The
2970 * free commands themselves are maintained in a linked list called cmd_pool.
2971 */
adam radford9c915a82010-12-21 13:34:31 -08002972int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002973{
2974 int i;
2975 int j;
2976 u32 max_cmd;
2977 struct megasas_cmd *cmd;
2978
adam radford9c915a82010-12-21 13:34:31 -08002979 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002980
2981 /*
2982 * instance->cmd_list is an array of struct megasas_cmd pointers.
2983 * Allocate the dynamic array first and then allocate individual
2984 * commands.
2985 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002986 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002987
2988 if (!instance->cmd_list) {
2989 printk(KERN_DEBUG "megasas: out of memory\n");
2990 return -ENOMEM;
2991 }
2992
adam radford9c915a82010-12-21 13:34:31 -08002993 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002994
2995 for (i = 0; i < max_cmd; i++) {
2996 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2997 GFP_KERNEL);
2998
2999 if (!instance->cmd_list[i]) {
3000
3001 for (j = 0; j < i; j++)
3002 kfree(instance->cmd_list[j]);
3003
3004 kfree(instance->cmd_list);
3005 instance->cmd_list = NULL;
3006
3007 return -ENOMEM;
3008 }
3009 }
3010
3011 /*
3012 * Add all the commands to command pool (instance->cmd_pool)
3013 */
3014 for (i = 0; i < max_cmd; i++) {
3015 cmd = instance->cmd_list[i];
3016 memset(cmd, 0, sizeof(struct megasas_cmd));
3017 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003018 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003019 cmd->instance = instance;
3020
3021 list_add_tail(&cmd->list, &instance->cmd_pool);
3022 }
3023
3024 /*
3025 * Create a frame pool and assign one frame to each cmd
3026 */
3027 if (megasas_create_frame_pool(instance)) {
3028 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3029 megasas_free_cmds(instance);
3030 }
3031
3032 return 0;
3033}
3034
Yang, Bo81e403c2009-10-06 14:27:54 -06003035/*
3036 * megasas_get_pd_list_info - Returns FW's pd_list structure
3037 * @instance: Adapter soft state
3038 * @pd_list: pd_list structure
3039 *
3040 * Issues an internal command (DCMD) to get the FW's controller PD
3041 * list structure. This information is mainly used to find out SYSTEM
3042 * supported by the FW.
3043 */
3044static int
3045megasas_get_pd_list(struct megasas_instance *instance)
3046{
3047 int ret = 0, pd_index = 0;
3048 struct megasas_cmd *cmd;
3049 struct megasas_dcmd_frame *dcmd;
3050 struct MR_PD_LIST *ci;
3051 struct MR_PD_ADDRESS *pd_addr;
3052 dma_addr_t ci_h = 0;
3053
3054 cmd = megasas_get_cmd(instance);
3055
3056 if (!cmd) {
3057 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3058 return -ENOMEM;
3059 }
3060
3061 dcmd = &cmd->frame->dcmd;
3062
3063 ci = pci_alloc_consistent(instance->pdev,
3064 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3065
3066 if (!ci) {
3067 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3068 megasas_return_cmd(instance, cmd);
3069 return -ENOMEM;
3070 }
3071
3072 memset(ci, 0, sizeof(*ci));
3073 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3074
3075 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3076 dcmd->mbox.b[1] = 0;
3077 dcmd->cmd = MFI_CMD_DCMD;
3078 dcmd->cmd_status = 0xFF;
3079 dcmd->sge_count = 1;
3080 dcmd->flags = MFI_FRAME_DIR_READ;
3081 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003082 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003083 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3084 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3085 dcmd->sgl.sge32[0].phys_addr = ci_h;
3086 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3087
3088 if (!megasas_issue_polled(instance, cmd)) {
3089 ret = 0;
3090 } else {
3091 ret = -1;
3092 }
3093
3094 /*
3095 * the following function will get the instance PD LIST.
3096 */
3097
3098 pd_addr = ci->addr;
3099
3100 if ( ret == 0 &&
3101 (ci->count <
3102 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3103
3104 memset(instance->pd_list, 0,
3105 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3106
3107 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3108
3109 instance->pd_list[pd_addr->deviceId].tid =
3110 pd_addr->deviceId;
3111 instance->pd_list[pd_addr->deviceId].driveType =
3112 pd_addr->scsiDevType;
3113 instance->pd_list[pd_addr->deviceId].driveState =
3114 MR_PD_STATE_SYSTEM;
3115 pd_addr++;
3116 }
3117 }
3118
3119 pci_free_consistent(instance->pdev,
3120 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3121 ci, ci_h);
3122 megasas_return_cmd(instance, cmd);
3123
3124 return ret;
3125}
3126
Yang, Bobdc6fb82009-12-06 08:30:19 -07003127/*
3128 * megasas_get_ld_list_info - Returns FW's ld_list structure
3129 * @instance: Adapter soft state
3130 * @ld_list: ld_list structure
3131 *
3132 * Issues an internal command (DCMD) to get the FW's controller PD
3133 * list structure. This information is mainly used to find out SYSTEM
3134 * supported by the FW.
3135 */
3136static int
3137megasas_get_ld_list(struct megasas_instance *instance)
3138{
3139 int ret = 0, ld_index = 0, ids = 0;
3140 struct megasas_cmd *cmd;
3141 struct megasas_dcmd_frame *dcmd;
3142 struct MR_LD_LIST *ci;
3143 dma_addr_t ci_h = 0;
3144
3145 cmd = megasas_get_cmd(instance);
3146
3147 if (!cmd) {
3148 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3149 return -ENOMEM;
3150 }
3151
3152 dcmd = &cmd->frame->dcmd;
3153
3154 ci = pci_alloc_consistent(instance->pdev,
3155 sizeof(struct MR_LD_LIST),
3156 &ci_h);
3157
3158 if (!ci) {
3159 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3160 megasas_return_cmd(instance, cmd);
3161 return -ENOMEM;
3162 }
3163
3164 memset(ci, 0, sizeof(*ci));
3165 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3166
3167 dcmd->cmd = MFI_CMD_DCMD;
3168 dcmd->cmd_status = 0xFF;
3169 dcmd->sge_count = 1;
3170 dcmd->flags = MFI_FRAME_DIR_READ;
3171 dcmd->timeout = 0;
3172 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3173 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3174 dcmd->sgl.sge32[0].phys_addr = ci_h;
3175 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3176 dcmd->pad_0 = 0;
3177
3178 if (!megasas_issue_polled(instance, cmd)) {
3179 ret = 0;
3180 } else {
3181 ret = -1;
3182 }
3183
3184 /* the following function will get the instance PD LIST */
3185
bo yang39a98552010-09-22 22:36:29 -04003186 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003187 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3188
3189 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3190 if (ci->ldList[ld_index].state != 0) {
3191 ids = ci->ldList[ld_index].ref.targetId;
3192 instance->ld_ids[ids] =
3193 ci->ldList[ld_index].ref.targetId;
3194 }
3195 }
3196 }
3197
3198 pci_free_consistent(instance->pdev,
3199 sizeof(struct MR_LD_LIST),
3200 ci,
3201 ci_h);
3202
3203 megasas_return_cmd(instance, cmd);
3204 return ret;
3205}
3206
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003207/**
3208 * megasas_get_controller_info - Returns FW's controller structure
3209 * @instance: Adapter soft state
3210 * @ctrl_info: Controller information structure
3211 *
3212 * Issues an internal command (DCMD) to get the FW's controller structure.
3213 * This information is mainly used to find out the maximum IO transfer per
3214 * command supported by the FW.
3215 */
3216static int
3217megasas_get_ctrl_info(struct megasas_instance *instance,
3218 struct megasas_ctrl_info *ctrl_info)
3219{
3220 int ret = 0;
3221 struct megasas_cmd *cmd;
3222 struct megasas_dcmd_frame *dcmd;
3223 struct megasas_ctrl_info *ci;
3224 dma_addr_t ci_h = 0;
3225
3226 cmd = megasas_get_cmd(instance);
3227
3228 if (!cmd) {
3229 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3230 return -ENOMEM;
3231 }
3232
3233 dcmd = &cmd->frame->dcmd;
3234
3235 ci = pci_alloc_consistent(instance->pdev,
3236 sizeof(struct megasas_ctrl_info), &ci_h);
3237
3238 if (!ci) {
3239 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3240 megasas_return_cmd(instance, cmd);
3241 return -ENOMEM;
3242 }
3243
3244 memset(ci, 0, sizeof(*ci));
3245 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3246
3247 dcmd->cmd = MFI_CMD_DCMD;
3248 dcmd->cmd_status = 0xFF;
3249 dcmd->sge_count = 1;
3250 dcmd->flags = MFI_FRAME_DIR_READ;
3251 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003252 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003253 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3254 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3255 dcmd->sgl.sge32[0].phys_addr = ci_h;
3256 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3257
3258 if (!megasas_issue_polled(instance, cmd)) {
3259 ret = 0;
3260 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3261 } else {
3262 ret = -1;
3263 }
3264
3265 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3266 ci, ci_h);
3267
3268 megasas_return_cmd(instance, cmd);
3269 return ret;
3270}
3271
3272/**
bo yang31ea7082007-11-07 12:09:50 -05003273 * megasas_issue_init_mfi - Initializes the FW
3274 * @instance: Adapter soft state
3275 *
3276 * Issues the INIT MFI cmd
3277 */
3278static int
3279megasas_issue_init_mfi(struct megasas_instance *instance)
3280{
3281 u32 context;
3282
3283 struct megasas_cmd *cmd;
3284
3285 struct megasas_init_frame *init_frame;
3286 struct megasas_init_queue_info *initq_info;
3287 dma_addr_t init_frame_h;
3288 dma_addr_t initq_info_h;
3289
3290 /*
3291 * Prepare a init frame. Note the init frame points to queue info
3292 * structure. Each frame has SGL allocated after first 64 bytes. For
3293 * this frame - since we don't need any SGL - we use SGL's space as
3294 * queue info structure
3295 *
3296 * We will not get a NULL command below. We just created the pool.
3297 */
3298 cmd = megasas_get_cmd(instance);
3299
3300 init_frame = (struct megasas_init_frame *)cmd->frame;
3301 initq_info = (struct megasas_init_queue_info *)
3302 ((unsigned long)init_frame + 64);
3303
3304 init_frame_h = cmd->frame_phys_addr;
3305 initq_info_h = init_frame_h + 64;
3306
3307 context = init_frame->context;
3308 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3309 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3310 init_frame->context = context;
3311
3312 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3313 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3314
3315 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3316 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3317
3318 init_frame->cmd = MFI_CMD_INIT;
3319 init_frame->cmd_status = 0xFF;
3320 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3321
3322 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3323
3324 /*
3325 * disable the intr before firing the init frame to FW
3326 */
3327 instance->instancet->disable_intr(instance->reg_set);
3328
3329 /*
3330 * Issue the init frame in polled mode
3331 */
3332
3333 if (megasas_issue_polled(instance, cmd)) {
3334 printk(KERN_ERR "megasas: Failed to init firmware\n");
3335 megasas_return_cmd(instance, cmd);
3336 goto fail_fw_init;
3337 }
3338
3339 megasas_return_cmd(instance, cmd);
3340
3341 return 0;
3342
3343fail_fw_init:
3344 return -EINVAL;
3345}
3346
3347/**
bo yangad84db22007-11-09 04:40:16 -05003348 * megasas_start_timer - Initializes a timer object
3349 * @instance: Adapter soft state
3350 * @timer: timer object to be initialized
3351 * @fn: timer function
3352 * @interval: time interval between timer function call
3353 */
3354static inline void
3355megasas_start_timer(struct megasas_instance *instance,
3356 struct timer_list *timer,
3357 void *fn, unsigned long interval)
3358{
3359 init_timer(timer);
3360 timer->expires = jiffies + interval;
3361 timer->data = (unsigned long)instance;
3362 timer->function = fn;
3363 add_timer(timer);
3364}
3365
3366/**
3367 * megasas_io_completion_timer - Timer fn
3368 * @instance_addr: Address of adapter soft state
3369 *
3370 * Schedules tasklet for cmd completion
3371 * if poll_mode_io is set
3372 */
3373static void
3374megasas_io_completion_timer(unsigned long instance_addr)
3375{
3376 struct megasas_instance *instance =
3377 (struct megasas_instance *)instance_addr;
3378
3379 if (atomic_read(&instance->fw_outstanding))
3380 tasklet_schedule(&instance->isr_tasklet);
3381
3382 /* Restart timer */
3383 if (poll_mode_io)
3384 mod_timer(&instance->io_completion_timer,
3385 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3386}
3387
adam radfordcd50ba82010-12-21 10:23:23 -08003388static u32
3389megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003390{
adam radfordcd50ba82010-12-21 10:23:23 -08003391 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003392 u32 context_sz;
3393 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003394
3395 reg_set = instance->reg_set;
3396
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003397 /*
3398 * Get various operational parameters from status register
3399 */
Sumant Patro1341c932006-01-25 12:02:40 -08003400 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003401 /*
3402 * Reduce the max supported cmds by 1. This is to ensure that the
3403 * reply_q_sz (1 more than the max cmd that driver may send)
3404 * does not exceed max cmds that the FW can support
3405 */
3406 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003407 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003408 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003409 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003410 /*
3411 * Create a pool of commands
3412 */
3413 if (megasas_alloc_cmds(instance))
3414 goto fail_alloc_cmds;
3415
3416 /*
3417 * Allocate memory for reply queue. Length of reply queue should
3418 * be _one_ more than the maximum commands handled by the firmware.
3419 *
3420 * Note: When FW completes commands, it places corresponding contex
3421 * values in this circular reply queue. This circular queue is a fairly
3422 * typical producer-consumer queue. FW is the producer (of completed
3423 * commands) and the driver is the consumer.
3424 */
3425 context_sz = sizeof(u32);
3426 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3427
3428 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3429 reply_q_sz,
3430 &instance->reply_queue_h);
3431
3432 if (!instance->reply_queue) {
3433 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3434 goto fail_reply_queue;
3435 }
3436
bo yang31ea7082007-11-07 12:09:50 -05003437 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003438 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003439
bo yang39a98552010-09-22 22:36:29 -04003440 instance->fw_support_ieee = 0;
3441 instance->fw_support_ieee =
3442 (instance->instancet->read_fw_status_reg(reg_set) &
3443 0x04000000);
3444
3445 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3446 instance->fw_support_ieee);
3447
3448 if (instance->fw_support_ieee)
3449 instance->flag_ieee = 1;
3450
adam radfordcd50ba82010-12-21 10:23:23 -08003451 return 0;
3452
3453fail_fw_init:
3454
3455 pci_free_consistent(instance->pdev, reply_q_sz,
3456 instance->reply_queue, instance->reply_queue_h);
3457fail_reply_queue:
3458 megasas_free_cmds(instance);
3459
3460fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003461 return 1;
3462}
3463
3464/**
3465 * megasas_init_fw - Initializes the FW
3466 * @instance: Adapter soft state
3467 *
3468 * This is the main function for initializing firmware
3469 */
3470
3471static int megasas_init_fw(struct megasas_instance *instance)
3472{
3473 u32 max_sectors_1;
3474 u32 max_sectors_2;
adam radford3f1abce2011-05-11 18:33:47 -07003475 u32 tmp_sectors, msix_enable;
adam radfordcd50ba82010-12-21 10:23:23 -08003476 struct megasas_register_set __iomem *reg_set;
3477 struct megasas_ctrl_info *ctrl_info;
3478 unsigned long bar_list;
3479
3480 /* Find first memory bar */
3481 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3482 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3483 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3484 if (pci_request_selected_regions(instance->pdev, instance->bar,
3485 "megasas: LSI")) {
3486 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3487 return -EBUSY;
3488 }
3489
3490 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3491
3492 if (!instance->reg_set) {
3493 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3494 goto fail_ioremap;
3495 }
3496
3497 reg_set = instance->reg_set;
3498
3499 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003500 case PCI_DEVICE_ID_LSI_FUSION:
3501 instance->instancet = &megasas_instance_template_fusion;
3502 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003503 case PCI_DEVICE_ID_LSI_SAS1078R:
3504 case PCI_DEVICE_ID_LSI_SAS1078DE:
3505 instance->instancet = &megasas_instance_template_ppc;
3506 break;
3507 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3508 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3509 instance->instancet = &megasas_instance_template_gen2;
3510 break;
3511 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3512 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3513 instance->instancet = &megasas_instance_template_skinny;
3514 break;
3515 case PCI_DEVICE_ID_LSI_SAS1064R:
3516 case PCI_DEVICE_ID_DELL_PERC5:
3517 default:
3518 instance->instancet = &megasas_instance_template_xscale;
3519 break;
3520 }
3521
3522 /*
3523 * We expect the FW state to be READY
3524 */
adam radford058a8fa2011-10-08 18:14:27 -07003525 if (megasas_transition_to_ready(instance, 0))
adam radfordcd50ba82010-12-21 10:23:23 -08003526 goto fail_ready_state;
3527
adam radford3f1abce2011-05-11 18:33:47 -07003528 /* Check if MSI-X is supported while in ready state */
3529 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3530 0x4000000) >> 0x1a;
3531 if (msix_enable && !msix_disable &&
3532 !pci_enable_msix(instance->pdev, &instance->msixentry, 1))
3533 instance->msi_flag = 1;
3534
adam radfordcd50ba82010-12-21 10:23:23 -08003535 /* Get operational params, sge flags, send init cmd to controller */
3536 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003537 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003538
3539 printk(KERN_ERR "megasas: INIT adapter done\n");
3540
bo yang39a98552010-09-22 22:36:29 -04003541 /** for passthrough
3542 * the following function will get the PD LIST.
3543 */
3544
Yang, Bo81e403c2009-10-06 14:27:54 -06003545 memset(instance->pd_list, 0 ,
3546 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3547 megasas_get_pd_list(instance);
3548
Yang, Bobdc6fb82009-12-06 08:30:19 -07003549 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3550 megasas_get_ld_list(instance);
3551
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003552 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3553
3554 /*
3555 * Compute the max allowed sectors per IO: The controller info has two
3556 * limits on max sectors. Driver should use the minimum of these two.
3557 *
3558 * 1 << stripe_sz_ops.min = max sectors per strip
3559 *
3560 * Note that older firmwares ( < FW ver 30) didn't report information
3561 * to calculate max_sectors_1. So the number ended up as zero always.
3562 */
bo yang14faea92007-11-09 04:14:00 -05003563 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003564 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3565
3566 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3567 ctrl_info->max_strips_per_io;
3568 max_sectors_2 = ctrl_info->max_request_size;
3569
bo yang14faea92007-11-09 04:14:00 -05003570 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003571 instance->disableOnlineCtrlReset =
3572 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003573 }
3574
3575 instance->max_sectors_per_req = instance->max_num_sge *
3576 PAGE_SIZE / 512;
3577 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3578 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003579
3580 kfree(ctrl_info);
3581
Sumant Patro5d018ad2006-10-03 13:13:18 -07003582 /*
3583 * Setup tasklet for cmd completion
3584 */
3585
adam radfordf86c5422011-02-24 20:57:00 -08003586 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003587 (unsigned long)instance);
3588
3589 /* Initialize the cmd completion timer */
3590 if (poll_mode_io)
3591 megasas_start_timer(instance, &instance->io_completion_timer,
3592 megasas_io_completion_timer,
3593 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003594 return 0;
3595
adam radfordeb1b1232011-02-24 20:55:56 -08003596fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003597fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003598 iounmap(instance->reg_set);
3599
3600 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003601 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003602
3603 return -EINVAL;
3604}
3605
3606/**
3607 * megasas_release_mfi - Reverses the FW initialization
3608 * @intance: Adapter soft state
3609 */
3610static void megasas_release_mfi(struct megasas_instance *instance)
3611{
adam radford9c915a82010-12-21 13:34:31 -08003612 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003613
adam radford9c915a82010-12-21 13:34:31 -08003614 if (instance->reply_queue)
3615 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003616 instance->reply_queue, instance->reply_queue_h);
3617
3618 megasas_free_cmds(instance);
3619
3620 iounmap(instance->reg_set);
3621
adam radfordb6d5d882010-12-14 18:56:07 -08003622 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003623}
3624
3625/**
3626 * megasas_get_seq_num - Gets latest event sequence numbers
3627 * @instance: Adapter soft state
3628 * @eli: FW event log sequence numbers information
3629 *
3630 * FW maintains a log of all events in a non-volatile area. Upper layers would
3631 * usually find out the latest sequence number of the events, the seq number at
3632 * the boot etc. They would "read" all the events below the latest seq number
3633 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3634 * number), they would subsribe to AEN (asynchronous event notification) and
3635 * wait for the events to happen.
3636 */
3637static int
3638megasas_get_seq_num(struct megasas_instance *instance,
3639 struct megasas_evt_log_info *eli)
3640{
3641 struct megasas_cmd *cmd;
3642 struct megasas_dcmd_frame *dcmd;
3643 struct megasas_evt_log_info *el_info;
3644 dma_addr_t el_info_h = 0;
3645
3646 cmd = megasas_get_cmd(instance);
3647
3648 if (!cmd) {
3649 return -ENOMEM;
3650 }
3651
3652 dcmd = &cmd->frame->dcmd;
3653 el_info = pci_alloc_consistent(instance->pdev,
3654 sizeof(struct megasas_evt_log_info),
3655 &el_info_h);
3656
3657 if (!el_info) {
3658 megasas_return_cmd(instance, cmd);
3659 return -ENOMEM;
3660 }
3661
3662 memset(el_info, 0, sizeof(*el_info));
3663 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3664
3665 dcmd->cmd = MFI_CMD_DCMD;
3666 dcmd->cmd_status = 0x0;
3667 dcmd->sge_count = 1;
3668 dcmd->flags = MFI_FRAME_DIR_READ;
3669 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003670 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003671 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3672 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3673 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3674 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3675
3676 megasas_issue_blocked_cmd(instance, cmd);
3677
3678 /*
3679 * Copy the data back into callers buffer
3680 */
3681 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3682
3683 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3684 el_info, el_info_h);
3685
3686 megasas_return_cmd(instance, cmd);
3687
3688 return 0;
3689}
3690
3691/**
3692 * megasas_register_aen - Registers for asynchronous event notification
3693 * @instance: Adapter soft state
3694 * @seq_num: The starting sequence number
3695 * @class_locale: Class of the event
3696 *
3697 * This function subscribes for AEN for events beyond the @seq_num. It requests
3698 * to be notified if and only if the event is of type @class_locale
3699 */
3700static int
3701megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3702 u32 class_locale_word)
3703{
3704 int ret_val;
3705 struct megasas_cmd *cmd;
3706 struct megasas_dcmd_frame *dcmd;
3707 union megasas_evt_class_locale curr_aen;
3708 union megasas_evt_class_locale prev_aen;
3709
3710 /*
3711 * If there an AEN pending already (aen_cmd), check if the
3712 * class_locale of that pending AEN is inclusive of the new
3713 * AEN request we currently have. If it is, then we don't have
3714 * to do anything. In other words, whichever events the current
3715 * AEN request is subscribing to, have already been subscribed
3716 * to.
3717 *
3718 * If the old_cmd is _not_ inclusive, then we have to abort
3719 * that command, form a class_locale that is superset of both
3720 * old and current and re-issue to the FW
3721 */
3722
3723 curr_aen.word = class_locale_word;
3724
3725 if (instance->aen_cmd) {
3726
3727 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3728
3729 /*
3730 * A class whose enum value is smaller is inclusive of all
3731 * higher values. If a PROGRESS (= -1) was previously
3732 * registered, then a new registration requests for higher
3733 * classes need not be sent to FW. They are automatically
3734 * included.
3735 *
3736 * Locale numbers don't have such hierarchy. They are bitmap
3737 * values
3738 */
3739 if ((prev_aen.members.class <= curr_aen.members.class) &&
3740 !((prev_aen.members.locale & curr_aen.members.locale) ^
3741 curr_aen.members.locale)) {
3742 /*
3743 * Previously issued event registration includes
3744 * current request. Nothing to do.
3745 */
3746 return 0;
3747 } else {
3748 curr_aen.members.locale |= prev_aen.members.locale;
3749
3750 if (prev_aen.members.class < curr_aen.members.class)
3751 curr_aen.members.class = prev_aen.members.class;
3752
3753 instance->aen_cmd->abort_aen = 1;
3754 ret_val = megasas_issue_blocked_abort_cmd(instance,
3755 instance->
3756 aen_cmd);
3757
3758 if (ret_val) {
3759 printk(KERN_DEBUG "megasas: Failed to abort "
3760 "previous AEN command\n");
3761 return ret_val;
3762 }
3763 }
3764 }
3765
3766 cmd = megasas_get_cmd(instance);
3767
3768 if (!cmd)
3769 return -ENOMEM;
3770
3771 dcmd = &cmd->frame->dcmd;
3772
3773 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3774
3775 /*
3776 * Prepare DCMD for aen registration
3777 */
3778 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3779
3780 dcmd->cmd = MFI_CMD_DCMD;
3781 dcmd->cmd_status = 0x0;
3782 dcmd->sge_count = 1;
3783 dcmd->flags = MFI_FRAME_DIR_READ;
3784 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003785 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003786 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003787 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3788 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3789 dcmd->mbox.w[0] = seq_num;
3790 dcmd->mbox.w[1] = curr_aen.word;
3791 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3792 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3793
Yang, Bof4c9a132009-10-06 14:43:28 -06003794 if (instance->aen_cmd != NULL) {
3795 megasas_return_cmd(instance, cmd);
3796 return 0;
3797 }
3798
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003799 /*
3800 * Store reference to the cmd used to register for AEN. When an
3801 * application wants us to register for AEN, we have to abort this
3802 * cmd and re-register with a new EVENT LOCALE supplied by that app
3803 */
3804 instance->aen_cmd = cmd;
3805
3806 /*
3807 * Issue the aen registration frame
3808 */
adam radford9c915a82010-12-21 13:34:31 -08003809 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003810
3811 return 0;
3812}
3813
3814/**
3815 * megasas_start_aen - Subscribes to AEN during driver load time
3816 * @instance: Adapter soft state
3817 */
3818static int megasas_start_aen(struct megasas_instance *instance)
3819{
3820 struct megasas_evt_log_info eli;
3821 union megasas_evt_class_locale class_locale;
3822
3823 /*
3824 * Get the latest sequence number from FW
3825 */
3826 memset(&eli, 0, sizeof(eli));
3827
3828 if (megasas_get_seq_num(instance, &eli))
3829 return -1;
3830
3831 /*
3832 * Register AEN with FW for latest sequence number plus 1
3833 */
3834 class_locale.members.reserved = 0;
3835 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3836 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3837
3838 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3839 class_locale.word);
3840}
3841
3842/**
3843 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3844 * @instance: Adapter soft state
3845 */
3846static int megasas_io_attach(struct megasas_instance *instance)
3847{
3848 struct Scsi_Host *host = instance->host;
3849
3850 /*
3851 * Export parameters required by SCSI mid-layer
3852 */
3853 host->irq = instance->pdev->irq;
3854 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003855 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3856 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3857 host->can_queue =
3858 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3859 } else
3860 host->can_queue =
3861 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003862 host->this_id = instance->init_id;
3863 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003864
3865 if (instance->fw_support_ieee)
3866 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3867
Yang, Bo1fd10682010-10-12 07:18:50 -06003868 /*
3869 * Check if the module parameter value for max_sectors can be used
3870 */
3871 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3872 instance->max_sectors_per_req = max_sectors;
3873 else {
3874 if (max_sectors) {
3875 if (((instance->pdev->device ==
3876 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3877 (instance->pdev->device ==
3878 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3879 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3880 instance->max_sectors_per_req = max_sectors;
3881 } else {
3882 printk(KERN_INFO "megasas: max_sectors should be > 0"
3883 "and <= %d (or < 1MB for GEN2 controller)\n",
3884 instance->max_sectors_per_req);
3885 }
3886 }
3887 }
3888
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003889 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003890 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003891 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3892 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3893 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003894 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003895
adam radford9c915a82010-12-21 13:34:31 -08003896 /* Fusion only supports host reset */
3897 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
3898 host->hostt->eh_device_reset_handler = NULL;
3899 host->hostt->eh_bus_reset_handler = NULL;
3900 }
3901
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003902 /*
3903 * Notify the mid-layer about the new controller
3904 */
3905 if (scsi_add_host(host, &instance->pdev->dev)) {
3906 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3907 return -ENODEV;
3908 }
3909
3910 /*
3911 * Trigger SCSI to scan our drives
3912 */
3913 scsi_scan_host(host);
3914 return 0;
3915}
3916
bo yang31ea7082007-11-07 12:09:50 -05003917static int
3918megasas_set_dma_mask(struct pci_dev *pdev)
3919{
3920 /*
3921 * All our contollers are capable of performing 64-bit DMA
3922 */
3923 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003924 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003925
Yang Hongyang284901a2009-04-06 19:01:15 -07003926 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003927 goto fail_set_dma_mask;
3928 }
3929 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003930 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003931 goto fail_set_dma_mask;
3932 }
3933 return 0;
3934
3935fail_set_dma_mask:
3936 return 1;
3937}
3938
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003939/**
3940 * megasas_probe_one - PCI hotplug entry point
3941 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003942 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003943 */
3944static int __devinit
3945megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3946{
adam radford66192dfe2011-02-24 20:56:28 -08003947 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003948 struct Scsi_Host *host;
3949 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08003950 u16 control = 0;
3951
3952 /* Reset MSI-X in the kdump kernel */
3953 if (reset_devices) {
3954 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3955 if (pos) {
3956 pci_read_config_word(pdev, msi_control_reg(pos),
3957 &control);
3958 if (control & PCI_MSIX_FLAGS_ENABLE) {
3959 dev_info(&pdev->dev, "resetting MSI-X\n");
3960 pci_write_config_word(pdev,
3961 msi_control_reg(pos),
3962 control &
3963 ~PCI_MSIX_FLAGS_ENABLE);
3964 }
3965 }
3966 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003967
3968 /*
3969 * Announce PCI information
3970 */
3971 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3972 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3973 pdev->subsystem_device);
3974
3975 printk("bus %d:slot %d:func %d\n",
3976 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3977
3978 /*
3979 * PCI prepping: enable device set bus mastering and dma mask
3980 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003981 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003982
3983 if (rval) {
3984 return rval;
3985 }
3986
3987 pci_set_master(pdev);
3988
bo yang31ea7082007-11-07 12:09:50 -05003989 if (megasas_set_dma_mask(pdev))
3990 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003991
3992 host = scsi_host_alloc(&megasas_template,
3993 sizeof(struct megasas_instance));
3994
3995 if (!host) {
3996 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3997 goto fail_alloc_instance;
3998 }
3999
4000 instance = (struct megasas_instance *)host->hostdata;
4001 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04004002 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08004003 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004004
adam radford9c915a82010-12-21 13:34:31 -08004005 switch (instance->pdev->device) {
4006 case PCI_DEVICE_ID_LSI_FUSION:
4007 {
4008 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004009
adam radford9c915a82010-12-21 13:34:31 -08004010 instance->ctrl_context =
4011 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4012 if (!instance->ctrl_context) {
4013 printk(KERN_DEBUG "megasas: Failed to allocate "
4014 "memory for Fusion context info\n");
4015 goto fail_alloc_dma_buf;
4016 }
4017 fusion = instance->ctrl_context;
4018 INIT_LIST_HEAD(&fusion->cmd_pool);
4019 spin_lock_init(&fusion->cmd_pool_lock);
4020 }
4021 break;
4022 default: /* For all other supported controllers */
4023
4024 instance->producer =
4025 pci_alloc_consistent(pdev, sizeof(u32),
4026 &instance->producer_h);
4027 instance->consumer =
4028 pci_alloc_consistent(pdev, sizeof(u32),
4029 &instance->consumer_h);
4030
4031 if (!instance->producer || !instance->consumer) {
4032 printk(KERN_DEBUG "megasas: Failed to allocate"
4033 "memory for producer, consumer\n");
4034 goto fail_alloc_dma_buf;
4035 }
4036
4037 *instance->producer = 0;
4038 *instance->consumer = 0;
4039 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004040 }
4041
Yang, Boc3518832009-10-06 14:18:02 -06004042 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004043 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004044 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004045 instance->issuepend_done = 1;
4046 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4047 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004048
4049 instance->evt_detail = pci_alloc_consistent(pdev,
4050 sizeof(struct
4051 megasas_evt_detail),
4052 &instance->evt_detail_h);
4053
4054 if (!instance->evt_detail) {
4055 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4056 "event detail structure\n");
4057 goto fail_alloc_dma_buf;
4058 }
4059
4060 /*
4061 * Initialize locks and queues
4062 */
4063 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004064 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004065
Sumant Patroe4a082c2006-05-30 12:03:37 -07004066 atomic_set(&instance->fw_outstanding,0);
4067
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004068 init_waitqueue_head(&instance->int_cmd_wait_q);
4069 init_waitqueue_head(&instance->abort_cmd_wait_q);
4070
4071 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004072 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004073 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06004074 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004075
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004076 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004077 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004078
4079 /*
4080 * Initialize PCI related and misc parameters
4081 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004082 instance->host = host;
4083 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4084 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4085
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004086 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4087 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004088 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004089 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4090 } else
4091 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4092
Sumant Patro658dced2006-10-03 13:09:14 -07004093 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004094 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004095 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004096 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004097 instance->disableOnlineCtrlReset = 1;
4098
adam radford9c915a82010-12-21 13:34:31 -08004099 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4100 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4101 else
4102 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004103
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004104 /*
adam radford0a770662011-02-24 20:56:12 -08004105 * Initialize MFI Firmware
4106 */
4107 if (megasas_init_fw(instance))
4108 goto fail_init_mfi;
4109
4110 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004111 * Register IRQ
4112 */
adam radford80d9da92010-12-21 10:17:40 -08004113 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004114 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004115 IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004116 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4117 goto fail_irq;
4118 }
4119
Sumant Patro1341c932006-01-25 12:02:40 -08004120 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004121
4122 /*
4123 * Store instance in PCI softstate
4124 */
4125 pci_set_drvdata(pdev, instance);
4126
4127 /*
4128 * Add this controller to megasas_mgmt_info structure so that it
4129 * can be exported to management applications
4130 */
4131 megasas_mgmt_info.count++;
4132 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4133 megasas_mgmt_info.max_index++;
4134
4135 /*
adam radford541f90b2011-05-11 18:34:29 -07004136 * Register with SCSI mid-layer
4137 */
4138 if (megasas_io_attach(instance))
4139 goto fail_io_attach;
4140
4141 instance->unload = 0;
4142
4143 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004144 * Initiate AEN (Asynchronous Event Notification)
4145 */
4146 if (megasas_start_aen(instance)) {
4147 printk(KERN_DEBUG "megasas: start aen failed\n");
4148 goto fail_start_aen;
4149 }
4150
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004151 return 0;
4152
4153 fail_start_aen:
4154 fail_io_attach:
4155 megasas_mgmt_info.count--;
4156 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4157 megasas_mgmt_info.max_index--;
4158
4159 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004160 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004161 free_irq(instance->msi_flag ? instance->msixentry.vector :
4162 instance->pdev->irq, instance);
adam radfordeb1b1232011-02-24 20:55:56 -08004163fail_irq:
adam radfordeb1b1232011-02-24 20:55:56 -08004164 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4165 megasas_release_fusion(instance);
4166 else
4167 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004168 fail_init_mfi:
adam radford0a770662011-02-24 20:56:12 -08004169 if (instance->msi_flag)
4170 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004171 fail_alloc_dma_buf:
4172 if (instance->evt_detail)
4173 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4174 instance->evt_detail,
4175 instance->evt_detail_h);
4176
adam radfordeb1b1232011-02-24 20:55:56 -08004177 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004178 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4179 instance->producer_h);
4180 if (instance->consumer)
4181 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4182 instance->consumer_h);
4183 scsi_host_put(host);
4184
4185 fail_alloc_instance:
4186 fail_set_dma_mask:
4187 pci_disable_device(pdev);
4188
4189 return -ENODEV;
4190}
4191
4192/**
4193 * megasas_flush_cache - Requests FW to flush all its caches
4194 * @instance: Adapter soft state
4195 */
4196static void megasas_flush_cache(struct megasas_instance *instance)
4197{
4198 struct megasas_cmd *cmd;
4199 struct megasas_dcmd_frame *dcmd;
4200
bo yang39a98552010-09-22 22:36:29 -04004201 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4202 return;
4203
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004204 cmd = megasas_get_cmd(instance);
4205
4206 if (!cmd)
4207 return;
4208
4209 dcmd = &cmd->frame->dcmd;
4210
4211 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4212
4213 dcmd->cmd = MFI_CMD_DCMD;
4214 dcmd->cmd_status = 0x0;
4215 dcmd->sge_count = 0;
4216 dcmd->flags = MFI_FRAME_DIR_NONE;
4217 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004218 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004219 dcmd->data_xfer_len = 0;
4220 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4221 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4222
4223 megasas_issue_blocked_cmd(instance, cmd);
4224
4225 megasas_return_cmd(instance, cmd);
4226
4227 return;
4228}
4229
4230/**
4231 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4232 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004233 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004234 */
bo yang31ea7082007-11-07 12:09:50 -05004235static void megasas_shutdown_controller(struct megasas_instance *instance,
4236 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004237{
4238 struct megasas_cmd *cmd;
4239 struct megasas_dcmd_frame *dcmd;
4240
bo yang39a98552010-09-22 22:36:29 -04004241 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4242 return;
4243
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004244 cmd = megasas_get_cmd(instance);
4245
4246 if (!cmd)
4247 return;
4248
4249 if (instance->aen_cmd)
4250 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004251 if (instance->map_update_cmd)
4252 megasas_issue_blocked_abort_cmd(instance,
4253 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004254 dcmd = &cmd->frame->dcmd;
4255
4256 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4257
4258 dcmd->cmd = MFI_CMD_DCMD;
4259 dcmd->cmd_status = 0x0;
4260 dcmd->sge_count = 0;
4261 dcmd->flags = MFI_FRAME_DIR_NONE;
4262 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004263 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004264 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004265 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004266
4267 megasas_issue_blocked_cmd(instance, cmd);
4268
4269 megasas_return_cmd(instance, cmd);
4270
4271 return;
4272}
4273
Jiri Slaby33139b22008-05-01 17:56:02 +02004274#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004275/**
bo yangad84db22007-11-09 04:40:16 -05004276 * megasas_suspend - driver suspend entry point
4277 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004278 * @state: PCI power state to suspend routine
4279 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004280static int
bo yang31ea7082007-11-07 12:09:50 -05004281megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4282{
4283 struct Scsi_Host *host;
4284 struct megasas_instance *instance;
4285
4286 instance = pci_get_drvdata(pdev);
4287 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004288 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004289
bo yangad84db22007-11-09 04:40:16 -05004290 if (poll_mode_io)
4291 del_timer_sync(&instance->io_completion_timer);
4292
bo yang31ea7082007-11-07 12:09:50 -05004293 megasas_flush_cache(instance);
4294 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004295
4296 /* cancel the delayed work if this work still in queue */
4297 if (instance->ev != NULL) {
4298 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004299 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004300 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004301 instance->ev = NULL;
4302 }
4303
bo yang31ea7082007-11-07 12:09:50 -05004304 tasklet_kill(&instance->isr_tasklet);
4305
4306 pci_set_drvdata(instance->pdev, instance);
4307 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004308 free_irq(instance->msi_flag ? instance->msixentry.vector :
4309 instance->pdev->irq, instance);
4310 if (instance->msi_flag)
4311 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004312
4313 pci_save_state(pdev);
4314 pci_disable_device(pdev);
4315
4316 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4317
4318 return 0;
4319}
4320
4321/**
4322 * megasas_resume- driver resume entry point
4323 * @pdev: PCI device structure
4324 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004325static int
bo yang31ea7082007-11-07 12:09:50 -05004326megasas_resume(struct pci_dev *pdev)
4327{
4328 int rval;
4329 struct Scsi_Host *host;
4330 struct megasas_instance *instance;
4331
4332 instance = pci_get_drvdata(pdev);
4333 host = instance->host;
4334 pci_set_power_state(pdev, PCI_D0);
4335 pci_enable_wake(pdev, PCI_D0, 0);
4336 pci_restore_state(pdev);
4337
4338 /*
4339 * PCI prepping: enable device set bus mastering and dma mask
4340 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004341 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004342
4343 if (rval) {
4344 printk(KERN_ERR "megasas: Enable device failed\n");
4345 return rval;
4346 }
4347
4348 pci_set_master(pdev);
4349
4350 if (megasas_set_dma_mask(pdev))
4351 goto fail_set_dma_mask;
4352
4353 /*
4354 * Initialize MFI Firmware
4355 */
4356
bo yang31ea7082007-11-07 12:09:50 -05004357 atomic_set(&instance->fw_outstanding, 0);
4358
4359 /*
4360 * We expect the FW state to be READY
4361 */
adam radford058a8fa2011-10-08 18:14:27 -07004362 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05004363 goto fail_ready_state;
4364
adam radford3f1abce2011-05-11 18:33:47 -07004365 /* Now re-enable MSI-X */
4366 if (instance->msi_flag)
4367 pci_enable_msix(instance->pdev, &instance->msixentry, 1);
4368
adam radford9c915a82010-12-21 13:34:31 -08004369 switch (instance->pdev->device) {
4370 case PCI_DEVICE_ID_LSI_FUSION:
4371 {
4372 megasas_reset_reply_desc(instance);
4373 if (megasas_ioc_init_fusion(instance)) {
4374 megasas_free_cmds(instance);
4375 megasas_free_cmds_fusion(instance);
4376 goto fail_init_mfi;
4377 }
4378 if (!megasas_get_map_info(instance))
4379 megasas_sync_map_info(instance);
4380 }
4381 break;
4382 default:
4383 *instance->producer = 0;
4384 *instance->consumer = 0;
4385 if (megasas_issue_init_mfi(instance))
4386 goto fail_init_mfi;
4387 break;
4388 }
bo yang31ea7082007-11-07 12:09:50 -05004389
adam radford9c915a82010-12-21 13:34:31 -08004390 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4391 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004392
4393 /*
4394 * Register IRQ
4395 */
adam radford80d9da92010-12-21 10:17:40 -08004396 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004397 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004398 IRQF_SHARED, "megasas", instance)) {
bo yang31ea7082007-11-07 12:09:50 -05004399 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4400 goto fail_irq;
4401 }
4402
4403 instance->instancet->enable_intr(instance->reg_set);
4404
bo yangad84db22007-11-09 04:40:16 -05004405 /* Initialize the cmd completion timer */
4406 if (poll_mode_io)
4407 megasas_start_timer(instance, &instance->io_completion_timer,
4408 megasas_io_completion_timer,
4409 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06004410 instance->unload = 0;
4411
adam radford541f90b2011-05-11 18:34:29 -07004412 /*
4413 * Initiate AEN (Asynchronous Event Notification)
4414 */
4415 if (megasas_start_aen(instance))
4416 printk(KERN_ERR "megasas: Start AEN failed\n");
4417
bo yang31ea7082007-11-07 12:09:50 -05004418 return 0;
4419
4420fail_irq:
4421fail_init_mfi:
4422 if (instance->evt_detail)
4423 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4424 instance->evt_detail,
4425 instance->evt_detail_h);
4426
4427 if (instance->producer)
4428 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4429 instance->producer_h);
4430 if (instance->consumer)
4431 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4432 instance->consumer_h);
4433 scsi_host_put(host);
4434
4435fail_set_dma_mask:
4436fail_ready_state:
4437
4438 pci_disable_device(pdev);
4439
4440 return -ENODEV;
4441}
Jiri Slaby33139b22008-05-01 17:56:02 +02004442#else
4443#define megasas_suspend NULL
4444#define megasas_resume NULL
4445#endif
bo yang31ea7082007-11-07 12:09:50 -05004446
4447/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004448 * megasas_detach_one - PCI hot"un"plug entry point
4449 * @pdev: PCI device structure
4450 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004451static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004452{
4453 int i;
4454 struct Scsi_Host *host;
4455 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004456 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004457
4458 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004459 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004460 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004461 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004462
bo yangad84db22007-11-09 04:40:16 -05004463 if (poll_mode_io)
4464 del_timer_sync(&instance->io_completion_timer);
4465
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004466 scsi_remove_host(instance->host);
4467 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004468 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004469
4470 /* cancel the delayed work if this work still in queue*/
4471 if (instance->ev != NULL) {
4472 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004473 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004474 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004475 instance->ev = NULL;
4476 }
4477
Sumant Patro5d018ad2006-10-03 13:13:18 -07004478 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004479
4480 /*
4481 * Take the instance off the instance array. Note that we will not
4482 * decrement the max_index. We let this array be sparse array
4483 */
4484 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4485 if (megasas_mgmt_info.instance[i] == instance) {
4486 megasas_mgmt_info.count--;
4487 megasas_mgmt_info.instance[i] = NULL;
4488
4489 break;
4490 }
4491 }
4492
4493 pci_set_drvdata(instance->pdev, NULL);
4494
Sumant Patrob274cab2006-10-03 12:52:12 -07004495 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004496
adam radford80d9da92010-12-21 10:17:40 -08004497 free_irq(instance->msi_flag ? instance->msixentry.vector :
4498 instance->pdev->irq, instance);
4499 if (instance->msi_flag)
4500 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004501
adam radford9c915a82010-12-21 13:34:31 -08004502 switch (instance->pdev->device) {
4503 case PCI_DEVICE_ID_LSI_FUSION:
4504 megasas_release_fusion(instance);
4505 for (i = 0; i < 2 ; i++)
4506 if (fusion->ld_map[i])
4507 dma_free_coherent(&instance->pdev->dev,
4508 fusion->map_sz,
4509 fusion->ld_map[i],
4510 fusion->
4511 ld_map_phys[i]);
4512 kfree(instance->ctrl_context);
4513 break;
4514 default:
4515 megasas_release_mfi(instance);
4516 pci_free_consistent(pdev,
4517 sizeof(struct megasas_evt_detail),
4518 instance->evt_detail,
4519 instance->evt_detail_h);
4520 pci_free_consistent(pdev, sizeof(u32),
4521 instance->producer,
4522 instance->producer_h);
4523 pci_free_consistent(pdev, sizeof(u32),
4524 instance->consumer,
4525 instance->consumer_h);
4526 break;
4527 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004528
4529 scsi_host_put(host);
4530
4531 pci_set_drvdata(pdev, NULL);
4532
4533 pci_disable_device(pdev);
4534
4535 return;
4536}
4537
4538/**
4539 * megasas_shutdown - Shutdown entry point
4540 * @device: Generic device structure
4541 */
4542static void megasas_shutdown(struct pci_dev *pdev)
4543{
4544 struct megasas_instance *instance = pci_get_drvdata(pdev);
Yang, Bo0c79e682009-10-06 14:47:35 -06004545 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004546 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004547 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
adam radford46fd2562011-05-11 18:34:17 -07004548 instance->instancet->disable_intr(instance->reg_set);
4549 free_irq(instance->msi_flag ? instance->msixentry.vector :
4550 instance->pdev->irq, instance);
4551 if (instance->msi_flag)
4552 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004553}
4554
4555/**
4556 * megasas_mgmt_open - char node "open" entry point
4557 */
4558static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4559{
4560 /*
4561 * Allow only those users with admin rights
4562 */
4563 if (!capable(CAP_SYS_ADMIN))
4564 return -EACCES;
4565
4566 return 0;
4567}
4568
4569/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004570 * megasas_mgmt_fasync - Async notifier registration from applications
4571 *
4572 * This function adds the calling process to a driver global queue. When an
4573 * event occurs, SIGIO will be sent to all processes in this queue.
4574 */
4575static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4576{
4577 int rc;
4578
Arjan van de Ven0b950672006-01-11 13:16:10 +01004579 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004580
4581 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4582
Arjan van de Ven0b950672006-01-11 13:16:10 +01004583 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004584
4585 if (rc >= 0) {
4586 /* For sanity check when we get ioctl */
4587 filep->private_data = filep;
4588 return 0;
4589 }
4590
4591 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4592
4593 return rc;
4594}
4595
4596/**
Yang, Boc3518832009-10-06 14:18:02 -06004597 * megasas_mgmt_poll - char node "poll" entry point
4598 * */
4599static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4600{
4601 unsigned int mask;
4602 unsigned long flags;
4603 poll_wait(file, &megasas_poll_wait, wait);
4604 spin_lock_irqsave(&poll_aen_lock, flags);
4605 if (megasas_poll_wait_aen)
4606 mask = (POLLIN | POLLRDNORM);
4607 else
4608 mask = 0;
4609 spin_unlock_irqrestore(&poll_aen_lock, flags);
4610 return mask;
4611}
4612
4613/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004614 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4615 * @instance: Adapter soft state
4616 * @argp: User's ioctl packet
4617 */
4618static int
4619megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4620 struct megasas_iocpacket __user * user_ioc,
4621 struct megasas_iocpacket *ioc)
4622{
4623 struct megasas_sge32 *kern_sge32;
4624 struct megasas_cmd *cmd;
4625 void *kbuff_arr[MAX_IOCTL_SGE];
4626 dma_addr_t buf_handle = 0;
4627 int error = 0, i;
4628 void *sense = NULL;
4629 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004630 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004631
4632 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4633
4634 if (ioc->sge_count > MAX_IOCTL_SGE) {
4635 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4636 ioc->sge_count, MAX_IOCTL_SGE);
4637 return -EINVAL;
4638 }
4639
4640 cmd = megasas_get_cmd(instance);
4641 if (!cmd) {
4642 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4643 return -ENOMEM;
4644 }
4645
4646 /*
4647 * User's IOCTL packet has 2 frames (maximum). Copy those two
4648 * frames into our cmd's frames. cmd->frame's context will get
4649 * overwritten when we copy from user's frames. So set that value
4650 * alone separately
4651 */
4652 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4653 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004654 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004655
4656 /*
4657 * The management interface between applications and the fw uses
4658 * MFI frames. E.g, RAID configuration changes, LD property changes
4659 * etc are accomplishes through different kinds of MFI frames. The
4660 * driver needs to care only about substituting user buffers with
4661 * kernel buffers in SGLs. The location of SGL is embedded in the
4662 * struct iocpacket itself.
4663 */
4664 kern_sge32 = (struct megasas_sge32 *)
4665 ((unsigned long)cmd->frame + ioc->sgl_off);
4666
4667 /*
4668 * For each user buffer, create a mirror buffer and copy in
4669 */
4670 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004671 if (!ioc->sgl[i].iov_len)
4672 continue;
4673
Sumant Patro9f35fa82007-02-14 12:55:45 -08004674 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004675 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004676 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004677 if (!kbuff_arr[i]) {
4678 printk(KERN_DEBUG "megasas: Failed to alloc "
4679 "kernel SGL buffer for IOCTL \n");
4680 error = -ENOMEM;
4681 goto out;
4682 }
4683
4684 /*
4685 * We don't change the dma_coherent_mask, so
4686 * pci_alloc_consistent only returns 32bit addresses
4687 */
4688 kern_sge32[i].phys_addr = (u32) buf_handle;
4689 kern_sge32[i].length = ioc->sgl[i].iov_len;
4690
4691 /*
4692 * We created a kernel buffer corresponding to the
4693 * user buffer. Now copy in from the user buffer
4694 */
4695 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4696 (u32) (ioc->sgl[i].iov_len))) {
4697 error = -EFAULT;
4698 goto out;
4699 }
4700 }
4701
4702 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004703 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4704 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004705 if (!sense) {
4706 error = -ENOMEM;
4707 goto out;
4708 }
4709
4710 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004711 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004712 *sense_ptr = sense_handle;
4713 }
4714
4715 /*
4716 * Set the sync_cmd flag so that the ISR knows not to complete this
4717 * cmd to the SCSI mid-layer
4718 */
4719 cmd->sync_cmd = 1;
4720 megasas_issue_blocked_cmd(instance, cmd);
4721 cmd->sync_cmd = 0;
4722
4723 /*
4724 * copy out the kernel buffers to user buffers
4725 */
4726 for (i = 0; i < ioc->sge_count; i++) {
4727 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4728 ioc->sgl[i].iov_len)) {
4729 error = -EFAULT;
4730 goto out;
4731 }
4732 }
4733
4734 /*
4735 * copy out the sense
4736 */
4737 if (ioc->sense_len) {
4738 /*
bo yangb70a41e2008-03-18 03:13:06 -04004739 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004740 * sense buffer address
4741 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004742 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4743 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004744
bo yangb70a41e2008-03-18 03:13:06 -04004745 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4746 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004747 printk(KERN_ERR "megasas: Failed to copy out to user "
4748 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004749 error = -EFAULT;
4750 goto out;
4751 }
4752 }
4753
4754 /*
4755 * copy the status codes returned by the fw
4756 */
4757 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4758 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4759 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4760 error = -EFAULT;
4761 }
4762
4763 out:
4764 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004765 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004766 sense, sense_handle);
4767 }
4768
4769 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004770 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004771 kern_sge32[i].length,
4772 kbuff_arr[i], kern_sge32[i].phys_addr);
4773 }
4774
4775 megasas_return_cmd(instance, cmd);
4776 return error;
4777}
4778
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004779static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4780{
4781 struct megasas_iocpacket __user *user_ioc =
4782 (struct megasas_iocpacket __user *)arg;
4783 struct megasas_iocpacket *ioc;
4784 struct megasas_instance *instance;
4785 int error;
bo yang39a98552010-09-22 22:36:29 -04004786 int i;
4787 unsigned long flags;
4788 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004789
4790 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4791 if (!ioc)
4792 return -ENOMEM;
4793
4794 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4795 error = -EFAULT;
4796 goto out_kfree_ioc;
4797 }
4798
4799 instance = megasas_lookup_instance(ioc->host_no);
4800 if (!instance) {
4801 error = -ENODEV;
4802 goto out_kfree_ioc;
4803 }
4804
bo yang39a98552010-09-22 22:36:29 -04004805 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4806 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004807 error = -ENODEV;
4808 goto out_kfree_ioc;
4809 }
4810
4811 if (instance->unload == 1) {
4812 error = -ENODEV;
4813 goto out_kfree_ioc;
4814 }
4815
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004816 /*
4817 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4818 */
4819 if (down_interruptible(&instance->ioctl_sem)) {
4820 error = -ERESTARTSYS;
4821 goto out_kfree_ioc;
4822 }
bo yang39a98552010-09-22 22:36:29 -04004823
4824 for (i = 0; i < wait_time; i++) {
4825
4826 spin_lock_irqsave(&instance->hba_lock, flags);
4827 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4828 spin_unlock_irqrestore(&instance->hba_lock, flags);
4829 break;
4830 }
4831 spin_unlock_irqrestore(&instance->hba_lock, flags);
4832
4833 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4834 printk(KERN_NOTICE "megasas: waiting"
4835 "for controller reset to finish\n");
4836 }
4837
4838 msleep(1000);
4839 }
4840
4841 spin_lock_irqsave(&instance->hba_lock, flags);
4842 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4843 spin_unlock_irqrestore(&instance->hba_lock, flags);
4844
4845 printk(KERN_ERR "megaraid_sas: timed out while"
4846 "waiting for HBA to recover\n");
4847 error = -ENODEV;
4848 goto out_kfree_ioc;
4849 }
4850 spin_unlock_irqrestore(&instance->hba_lock, flags);
4851
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004852 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4853 up(&instance->ioctl_sem);
4854
4855 out_kfree_ioc:
4856 kfree(ioc);
4857 return error;
4858}
4859
4860static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4861{
4862 struct megasas_instance *instance;
4863 struct megasas_aen aen;
4864 int error;
bo yang39a98552010-09-22 22:36:29 -04004865 int i;
4866 unsigned long flags;
4867 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004868
4869 if (file->private_data != file) {
4870 printk(KERN_DEBUG "megasas: fasync_helper was not "
4871 "called first\n");
4872 return -EINVAL;
4873 }
4874
4875 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4876 return -EFAULT;
4877
4878 instance = megasas_lookup_instance(aen.host_no);
4879
4880 if (!instance)
4881 return -ENODEV;
4882
bo yang39a98552010-09-22 22:36:29 -04004883 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4884 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004885 }
4886
4887 if (instance->unload == 1) {
4888 return -ENODEV;
4889 }
4890
bo yang39a98552010-09-22 22:36:29 -04004891 for (i = 0; i < wait_time; i++) {
4892
4893 spin_lock_irqsave(&instance->hba_lock, flags);
4894 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4895 spin_unlock_irqrestore(&instance->hba_lock,
4896 flags);
4897 break;
4898 }
4899
4900 spin_unlock_irqrestore(&instance->hba_lock, flags);
4901
4902 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4903 printk(KERN_NOTICE "megasas: waiting for"
4904 "controller reset to finish\n");
4905 }
4906
4907 msleep(1000);
4908 }
4909
4910 spin_lock_irqsave(&instance->hba_lock, flags);
4911 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4912 spin_unlock_irqrestore(&instance->hba_lock, flags);
4913 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4914 "for HBA to recover.\n");
4915 return -ENODEV;
4916 }
4917 spin_unlock_irqrestore(&instance->hba_lock, flags);
4918
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004919 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004920 error = megasas_register_aen(instance, aen.seq_num,
4921 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004922 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004923 return error;
4924}
4925
4926/**
4927 * megasas_mgmt_ioctl - char node ioctl entry point
4928 */
4929static long
4930megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4931{
4932 switch (cmd) {
4933 case MEGASAS_IOC_FIRMWARE:
4934 return megasas_mgmt_ioctl_fw(file, arg);
4935
4936 case MEGASAS_IOC_GET_AEN:
4937 return megasas_mgmt_ioctl_aen(file, arg);
4938 }
4939
4940 return -ENOTTY;
4941}
4942
4943#ifdef CONFIG_COMPAT
4944static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4945{
4946 struct compat_megasas_iocpacket __user *cioc =
4947 (struct compat_megasas_iocpacket __user *)arg;
4948 struct megasas_iocpacket __user *ioc =
4949 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4950 int i;
4951 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004952 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004953
Jeff Garzik83aabc12006-10-04 06:34:03 -04004954 if (clear_user(ioc, sizeof(*ioc)))
4955 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004956
4957 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4958 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4959 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4960 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4961 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4962 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4963 return -EFAULT;
4964
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004965 /*
4966 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4967 * sense_len is not null, so prepare the 64bit value under
4968 * the same condition.
4969 */
4970 if (ioc->sense_len) {
4971 void __user **sense_ioc_ptr =
4972 (void __user **)(ioc->frame.raw + ioc->sense_off);
4973 compat_uptr_t *sense_cioc_ptr =
4974 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4975 if (get_user(ptr, sense_cioc_ptr) ||
4976 put_user(compat_ptr(ptr), sense_ioc_ptr))
4977 return -EFAULT;
4978 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004979
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004980 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004981 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4982 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4983 copy_in_user(&ioc->sgl[i].iov_len,
4984 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4985 return -EFAULT;
4986 }
4987
4988 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4989
4990 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4991 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4992 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4993 return -EFAULT;
4994 }
4995 return error;
4996}
4997
4998static long
4999megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5000 unsigned long arg)
5001{
5002 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08005003 case MEGASAS_IOC_FIRMWARE32:
5004 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005005 case MEGASAS_IOC_GET_AEN:
5006 return megasas_mgmt_ioctl_aen(file, arg);
5007 }
5008
5009 return -ENOTTY;
5010}
5011#endif
5012
5013/*
5014 * File operations structure for management interface
5015 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08005016static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005017 .owner = THIS_MODULE,
5018 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005019 .fasync = megasas_mgmt_fasync,
5020 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005021 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005022#ifdef CONFIG_COMPAT
5023 .compat_ioctl = megasas_mgmt_compat_ioctl,
5024#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005025 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005026};
5027
5028/*
5029 * PCI hotplug support registration structure
5030 */
5031static struct pci_driver megasas_pci_driver = {
5032
5033 .name = "megaraid_sas",
5034 .id_table = megasas_pci_table,
5035 .probe = megasas_probe_one,
5036 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05005037 .suspend = megasas_suspend,
5038 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005039 .shutdown = megasas_shutdown,
5040};
5041
5042/*
5043 * Sysfs driver attributes
5044 */
5045static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5046{
5047 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5048 MEGASAS_VERSION);
5049}
5050
5051static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5052
5053static ssize_t
5054megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5055{
5056 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5057 MEGASAS_RELDATE);
5058}
5059
5060static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5061 NULL);
5062
Sumant Patro658dced2006-10-03 13:09:14 -07005063static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005064megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5065{
5066 return sprintf(buf, "%u\n", support_poll_for_event);
5067}
5068
5069static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5070 megasas_sysfs_show_support_poll_for_event, NULL);
5071
Yang, Bo837f5fe2010-10-11 06:59:20 -06005072 static ssize_t
5073megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5074{
5075 return sprintf(buf, "%u\n", support_device_change);
5076}
5077
5078static DRIVER_ATTR(support_device_change, S_IRUGO,
5079 megasas_sysfs_show_support_device_change, NULL);
5080
Yang, Bo72c4fd32009-10-06 14:20:59 -06005081static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005082megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5083{
bo yangad84db22007-11-09 04:40:16 -05005084 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005085}
5086
5087static ssize_t
5088megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5089{
5090 int retval = count;
5091 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5092 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5093 retval = -EINVAL;
5094 }
5095 return retval;
5096}
5097
Joe Malicki66dca9b2008-08-14 17:14:48 -04005098static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005099 megasas_sysfs_set_dbg_lvl);
5100
5101static ssize_t
5102megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
5103{
5104 return sprintf(buf, "%u\n", poll_mode_io);
5105}
5106
5107static ssize_t
5108megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
5109 const char *buf, size_t count)
5110{
5111 int retval = count;
5112 int tmp = poll_mode_io;
5113 int i;
5114 struct megasas_instance *instance;
5115
5116 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
5117 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
5118 retval = -EINVAL;
5119 }
5120
5121 /*
5122 * Check if poll_mode_io is already set or is same as previous value
5123 */
5124 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
5125 goto out;
5126
5127 if (poll_mode_io) {
5128 /*
5129 * Start timers for all adapters
5130 */
5131 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5132 instance = megasas_mgmt_info.instance[i];
5133 if (instance) {
5134 megasas_start_timer(instance,
5135 &instance->io_completion_timer,
5136 megasas_io_completion_timer,
5137 MEGASAS_COMPLETION_TIMER_INTERVAL);
5138 }
5139 }
5140 } else {
5141 /*
5142 * Delete timers for all adapters
5143 */
5144 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5145 instance = megasas_mgmt_info.instance[i];
5146 if (instance)
5147 del_timer_sync(&instance->io_completion_timer);
5148 }
5149 }
5150
5151out:
5152 return retval;
5153}
5154
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005155static void
5156megasas_aen_polling(struct work_struct *work)
5157{
5158 struct megasas_aen_event *ev =
5159 container_of(work, struct megasas_aen_event, hotplug_work);
5160 struct megasas_instance *instance = ev->instance;
5161 union megasas_evt_class_locale class_locale;
5162 struct Scsi_Host *host;
5163 struct scsi_device *sdev1;
5164 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005165 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005166 int i, j, doscan = 0;
5167 u32 seq_num;
5168 int error;
5169
5170 if (!instance) {
5171 printk(KERN_ERR "invalid instance!\n");
5172 kfree(ev);
5173 return;
5174 }
5175 instance->ev = NULL;
5176 host = instance->host;
5177 if (instance->evt_detail) {
5178
5179 switch (instance->evt_detail->code) {
5180 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005181 if (megasas_get_pd_list(instance) == 0) {
5182 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5183 for (j = 0;
5184 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5185 j++) {
5186
5187 pd_index =
5188 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5189
5190 sdev1 =
5191 scsi_device_lookup(host, i, j, 0);
5192
5193 if (instance->pd_list[pd_index].driveState
5194 == MR_PD_STATE_SYSTEM) {
5195 if (!sdev1) {
5196 scsi_add_device(host, i, j, 0);
5197 }
5198
5199 if (sdev1)
5200 scsi_device_put(sdev1);
5201 }
5202 }
5203 }
5204 }
5205 doscan = 0;
5206 break;
5207
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005208 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005209 if (megasas_get_pd_list(instance) == 0) {
5210 megasas_get_pd_list(instance);
5211 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5212 for (j = 0;
5213 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5214 j++) {
5215
5216 pd_index =
5217 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5218
5219 sdev1 =
5220 scsi_device_lookup(host, i, j, 0);
5221
5222 if (instance->pd_list[pd_index].driveState
5223 == MR_PD_STATE_SYSTEM) {
5224 if (sdev1) {
5225 scsi_device_put(sdev1);
5226 }
5227 } else {
5228 if (sdev1) {
5229 scsi_remove_device(sdev1);
5230 scsi_device_put(sdev1);
5231 }
5232 }
5233 }
5234 }
5235 }
5236 doscan = 0;
5237 break;
5238
5239 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005240 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005241 case MR_EVT_LD_DELETED:
5242 megasas_get_ld_list(instance);
5243 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5244 for (j = 0;
5245 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5246 j++) {
5247
5248 ld_index =
5249 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5250
5251 sdev1 = scsi_device_lookup(host,
5252 i + MEGASAS_MAX_LD_CHANNELS,
5253 j,
5254 0);
5255
5256 if (instance->ld_ids[ld_index] != 0xff) {
5257 if (sdev1) {
5258 scsi_device_put(sdev1);
5259 }
5260 } else {
5261 if (sdev1) {
5262 scsi_remove_device(sdev1);
5263 scsi_device_put(sdev1);
5264 }
5265 }
5266 }
5267 }
5268 doscan = 0;
5269 break;
5270 case MR_EVT_LD_CREATED:
5271 megasas_get_ld_list(instance);
5272 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5273 for (j = 0;
5274 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5275 j++) {
5276 ld_index =
5277 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5278
5279 sdev1 = scsi_device_lookup(host,
5280 i+MEGASAS_MAX_LD_CHANNELS,
5281 j, 0);
5282
5283 if (instance->ld_ids[ld_index] !=
5284 0xff) {
5285 if (!sdev1) {
5286 scsi_add_device(host,
5287 i + 2,
5288 j, 0);
5289 }
5290 }
5291 if (sdev1) {
5292 scsi_device_put(sdev1);
5293 }
5294 }
5295 }
5296 doscan = 0;
5297 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005298 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005299 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005300 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005301 doscan = 1;
5302 break;
5303 default:
5304 doscan = 0;
5305 break;
5306 }
5307 } else {
5308 printk(KERN_ERR "invalid evt_detail!\n");
5309 kfree(ev);
5310 return;
5311 }
5312
5313 if (doscan) {
5314 printk(KERN_INFO "scanning ...\n");
5315 megasas_get_pd_list(instance);
5316 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5317 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5318 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5319 sdev1 = scsi_device_lookup(host, i, j, 0);
5320 if (instance->pd_list[pd_index].driveState ==
5321 MR_PD_STATE_SYSTEM) {
5322 if (!sdev1) {
5323 scsi_add_device(host, i, j, 0);
5324 }
5325 if (sdev1)
5326 scsi_device_put(sdev1);
5327 } else {
5328 if (sdev1) {
5329 scsi_remove_device(sdev1);
5330 scsi_device_put(sdev1);
5331 }
5332 }
5333 }
5334 }
Yang, Boc9786842009-12-06 08:39:25 -07005335
5336 megasas_get_ld_list(instance);
5337 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5338 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5339 ld_index =
5340 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5341
5342 sdev1 = scsi_device_lookup(host,
5343 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5344 if (instance->ld_ids[ld_index] != 0xff) {
5345 if (!sdev1) {
5346 scsi_add_device(host,
5347 i+2,
5348 j, 0);
5349 } else {
5350 scsi_device_put(sdev1);
5351 }
5352 } else {
5353 if (sdev1) {
5354 scsi_remove_device(sdev1);
5355 scsi_device_put(sdev1);
5356 }
5357 }
5358 }
5359 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005360 }
5361
5362 if ( instance->aen_cmd != NULL ) {
5363 kfree(ev);
5364 return ;
5365 }
5366
5367 seq_num = instance->evt_detail->seq_num + 1;
5368
5369 /* Register AEN with FW for latest sequence number plus 1 */
5370 class_locale.members.reserved = 0;
5371 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5372 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5373 mutex_lock(&instance->aen_mutex);
5374 error = megasas_register_aen(instance, seq_num,
5375 class_locale.word);
5376 mutex_unlock(&instance->aen_mutex);
5377
5378 if (error)
5379 printk(KERN_ERR "register aen failed error %x\n", error);
5380
5381 kfree(ev);
5382}
5383
5384
Bryn M. Reevesbb7d3f22009-11-12 18:31:54 +00005385static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
bo yangad84db22007-11-09 04:40:16 -05005386 megasas_sysfs_show_poll_mode_io,
5387 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07005388
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005389/**
5390 * megasas_init - Driver load entry point
5391 */
5392static int __init megasas_init(void)
5393{
5394 int rval;
5395
5396 /*
5397 * Announce driver version and other information
5398 */
5399 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5400 MEGASAS_EXT_VERSION);
5401
Yang, Bo72c4fd32009-10-06 14:20:59 -06005402 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005403 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005404
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005405 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5406
5407 /*
5408 * Register character device node
5409 */
5410 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5411
5412 if (rval < 0) {
5413 printk(KERN_DEBUG "megasas: failed to open device node\n");
5414 return rval;
5415 }
5416
5417 megasas_mgmt_majorno = rval;
5418
5419 /*
5420 * Register ourselves as PCI hotplug module
5421 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005422 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005423
5424 if (rval) {
5425 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005426 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005427 }
5428
Jeff Garzik83aabc12006-10-04 06:34:03 -04005429 rval = driver_create_file(&megasas_pci_driver.driver,
5430 &driver_attr_version);
5431 if (rval)
5432 goto err_dcf_attr_ver;
5433 rval = driver_create_file(&megasas_pci_driver.driver,
5434 &driver_attr_release_date);
5435 if (rval)
5436 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005437
5438 rval = driver_create_file(&megasas_pci_driver.driver,
5439 &driver_attr_support_poll_for_event);
5440 if (rval)
5441 goto err_dcf_support_poll_for_event;
5442
Jeff Garzik83aabc12006-10-04 06:34:03 -04005443 rval = driver_create_file(&megasas_pci_driver.driver,
5444 &driver_attr_dbg_lvl);
5445 if (rval)
5446 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005447 rval = driver_create_file(&megasas_pci_driver.driver,
5448 &driver_attr_poll_mode_io);
5449 if (rval)
5450 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005451
Yang, Bo837f5fe2010-10-11 06:59:20 -06005452 rval = driver_create_file(&megasas_pci_driver.driver,
5453 &driver_attr_support_device_change);
5454 if (rval)
5455 goto err_dcf_support_device_change;
5456
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005457 return rval;
bo yangad84db22007-11-09 04:40:16 -05005458
Yang, Bo837f5fe2010-10-11 06:59:20 -06005459err_dcf_support_device_change:
5460 driver_remove_file(&megasas_pci_driver.driver,
5461 &driver_attr_poll_mode_io);
5462
bo yangad84db22007-11-09 04:40:16 -05005463err_dcf_poll_mode_io:
5464 driver_remove_file(&megasas_pci_driver.driver,
5465 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005466err_dcf_dbg_lvl:
5467 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005468 &driver_attr_support_poll_for_event);
5469
5470err_dcf_support_poll_for_event:
5471 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005472 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005473
Jeff Garzik83aabc12006-10-04 06:34:03 -04005474err_dcf_rel_date:
5475 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5476err_dcf_attr_ver:
5477 pci_unregister_driver(&megasas_pci_driver);
5478err_pcidrv:
5479 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005480 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005481}
5482
5483/**
5484 * megasas_exit - Driver unload entry point
5485 */
5486static void __exit megasas_exit(void)
5487{
Sumant Patro658dced2006-10-03 13:09:14 -07005488 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005489 &driver_attr_poll_mode_io);
5490 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07005491 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005492 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005493 &driver_attr_support_poll_for_event);
5494 driver_remove_file(&megasas_pci_driver.driver,
5495 &driver_attr_support_device_change);
5496 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005497 &driver_attr_release_date);
5498 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005499
5500 pci_unregister_driver(&megasas_pci_driver);
5501 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5502}
5503
5504module_init(megasas_init);
5505module_exit(megasas_exit);