blob: f43ab031edc82190a381d82597f0ea6792de23e4 [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 radford00fa2b12011-02-24 20:57:21 -080021 * Version : v00.00.05.34-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 radford9c915a82010-12-21 13:34:31 -080057#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040058#include "megaraid_sas.h"
59
bo yangad84db22007-11-09 04:40:16 -050060/*
61 * poll_mode_io:1- schedule complete completion from q cmd
62 */
63static unsigned int poll_mode_io;
64module_param_named(poll_mode_io, poll_mode_io, int, 0);
65MODULE_PARM_DESC(poll_mode_io,
66 "Complete cmds from IO path, (default=0)");
67
Yang, Bo1fd10682010-10-12 07:18:50 -060068/*
69 * Number of sectors per IO command
70 * Will be set in megasas_init_mfi if user does not provide
71 */
72static unsigned int max_sectors;
73module_param_named(max_sectors, max_sectors, int, 0);
74MODULE_PARM_DESC(max_sectors,
75 "Maximum number of sectors per IO command");
76
adam radford80d9da92010-12-21 10:17:40 -080077static int msix_disable;
78module_param(msix_disable, int, S_IRUGO);
79MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
80
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040081MODULE_LICENSE("GPL");
82MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080083MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050084MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040085
adam radford9c915a82010-12-21 13:34:31 -080086int megasas_transition_to_ready(struct megasas_instance *instance);
bo yang39a98552010-09-22 22:36:29 -040087static int megasas_get_pd_list(struct megasas_instance *instance);
88static int megasas_issue_init_mfi(struct megasas_instance *instance);
89static int megasas_register_aen(struct megasas_instance *instance,
90 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040091/*
92 * PCI ID table for all supported controllers
93 */
94static struct pci_device_id megasas_pci_table[] = {
95
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020096 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
97 /* xscale IOP */
98 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
99 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400100 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
101 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700102 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
103 /* gen2*/
104 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
105 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600106 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
107 /* skinny*/
108 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
109 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200110 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
111 /* xscale IOP, vega */
112 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
113 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800114 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
115 /* Fusion */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200116 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400117};
118
119MODULE_DEVICE_TABLE(pci, megasas_pci_table);
120
121static int megasas_mgmt_majorno;
122static struct megasas_mgmt_info megasas_mgmt_info;
123static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100124static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400125
Yang, Boc3518832009-10-06 14:18:02 -0600126static int megasas_poll_wait_aen;
127static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600128static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800129u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600130static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700131
Yang, Boc3518832009-10-06 14:18:02 -0600132/* define lock for aen poll */
133spinlock_t poll_aen_lock;
134
adam radford9c915a82010-12-21 13:34:31 -0800135void
bo yang7343eb62007-11-09 04:35:44 -0500136megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
137 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800138static u32
139megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
140static int
141megasas_adp_reset_gen2(struct megasas_instance *instance,
142 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800143static irqreturn_t megasas_isr(int irq, void *devp);
144static u32
145megasas_init_adapter_mfi(struct megasas_instance *instance);
146u32
147megasas_build_and_issue_cmd(struct megasas_instance *instance,
148 struct scsi_cmnd *scmd);
149static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800150void
151megasas_release_fusion(struct megasas_instance *instance);
152int
153megasas_ioc_init_fusion(struct megasas_instance *instance);
154void
155megasas_free_cmds_fusion(struct megasas_instance *instance);
156u8
157megasas_get_map_info(struct megasas_instance *instance);
158int
159megasas_sync_map_info(struct megasas_instance *instance);
160int
161wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
162void megasas_reset_reply_desc(struct megasas_instance *instance);
163u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
164 struct LD_LOAD_BALANCE_INFO *lbInfo);
165int megasas_reset_fusion(struct Scsi_Host *shost);
166void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800167
168void
169megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
170{
171 instance->instancet->fire_cmd(instance,
172 cmd->frame_phys_addr, 0, instance->reg_set);
173}
bo yang7343eb62007-11-09 04:35:44 -0500174
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400175/**
176 * megasas_get_cmd - Get a command from the free pool
177 * @instance: Adapter soft state
178 *
179 * Returns a free command from the pool
180 */
adam radford9c915a82010-12-21 13:34:31 -0800181struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400182 *instance)
183{
184 unsigned long flags;
185 struct megasas_cmd *cmd = NULL;
186
187 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
188
189 if (!list_empty(&instance->cmd_pool)) {
190 cmd = list_entry((&instance->cmd_pool)->next,
191 struct megasas_cmd, list);
192 list_del_init(&cmd->list);
193 } else {
194 printk(KERN_ERR "megasas: Command pool empty!\n");
195 }
196
197 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
198 return cmd;
199}
200
201/**
202 * megasas_return_cmd - Return a cmd to free command pool
203 * @instance: Adapter soft state
204 * @cmd: Command packet to be returned to free command pool
205 */
adam radford9c915a82010-12-21 13:34:31 -0800206inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400207megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
208{
209 unsigned long flags;
210
211 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
212
213 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800214 cmd->frame_count = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400215 list_add_tail(&cmd->list, &instance->cmd_pool);
216
217 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
218}
219
Sumant Patro1341c932006-01-25 12:02:40 -0800220
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400221/**
adam radford0d490162010-12-14 19:17:17 -0800222* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800223* (deviceid : 1064R, PERC5) controllers
224*/
225
226/**
227 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400228 * @regs: MFI register set
229 */
230static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800231megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400232{
bo yang39a98552010-09-22 22:36:29 -0400233 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400234
235 /* Dummy readl to force pci flush */
236 readl(&regs->outbound_intr_mask);
237}
238
239/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700240 * megasas_disable_intr_xscale -Disables interrupt
241 * @regs: MFI register set
242 */
243static inline void
244megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
245{
246 u32 mask = 0x1f;
247 writel(mask, &regs->outbound_intr_mask);
248 /* Dummy readl to force pci flush */
249 readl(&regs->outbound_intr_mask);
250}
251
252/**
Sumant Patro1341c932006-01-25 12:02:40 -0800253 * megasas_read_fw_status_reg_xscale - returns the current FW status value
254 * @regs: MFI register set
255 */
256static u32
257megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
258{
259 return readl(&(regs)->outbound_msg_0);
260}
261/**
262 * megasas_clear_interrupt_xscale - Check & clear interrupt
263 * @regs: MFI register set
264 */
adam radford0d490162010-12-14 19:17:17 -0800265static int
Sumant Patro1341c932006-01-25 12:02:40 -0800266megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
267{
268 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400269 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800270 /*
271 * Check if it is our interrupt
272 */
273 status = readl(&regs->outbound_intr_status);
274
bo yang39a98552010-09-22 22:36:29 -0400275 if (status & MFI_OB_INTR_STATUS_MASK)
276 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
277 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
278 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800279
280 /*
281 * Clear the interrupt by writing back the same value
282 */
bo yang39a98552010-09-22 22:36:29 -0400283 if (mfiStatus)
284 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800285
Yang, Bo06f579d2008-08-10 12:42:37 -0700286 /* Dummy readl to force pci flush */
287 readl(&regs->outbound_intr_status);
288
bo yang39a98552010-09-22 22:36:29 -0400289 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800290}
291
292/**
293 * megasas_fire_cmd_xscale - Sends command to the FW
294 * @frame_phys_addr : Physical address of cmd
295 * @frame_count : Number of frames for the command
296 * @regs : MFI register set
297 */
adam radford0d490162010-12-14 19:17:17 -0800298static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600299megasas_fire_cmd_xscale(struct megasas_instance *instance,
300 dma_addr_t frame_phys_addr,
301 u32 frame_count,
302 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800303{
bo yang39a98552010-09-22 22:36:29 -0400304 unsigned long flags;
305 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800306 writel((frame_phys_addr >> 3)|(frame_count),
307 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400308 spin_unlock_irqrestore(&instance->hba_lock, flags);
309}
310
311/**
312 * megasas_adp_reset_xscale - For controller reset
313 * @regs: MFI register set
314 */
315static int
316megasas_adp_reset_xscale(struct megasas_instance *instance,
317 struct megasas_register_set __iomem *regs)
318{
319 u32 i;
320 u32 pcidata;
321 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
322
323 for (i = 0; i < 3; i++)
324 msleep(1000); /* sleep for 3 secs */
325 pcidata = 0;
326 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
327 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
328 if (pcidata & 0x2) {
329 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
330 pcidata &= ~0x2;
331 pci_write_config_dword(instance->pdev,
332 MFI_1068_PCSR_OFFSET, pcidata);
333
334 for (i = 0; i < 2; i++)
335 msleep(1000); /* need to wait 2 secs again */
336
337 pcidata = 0;
338 pci_read_config_dword(instance->pdev,
339 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
340 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
341 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
342 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
343 pcidata = 0;
344 pci_write_config_dword(instance->pdev,
345 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
346 }
347 }
348 return 0;
349}
350
351/**
352 * megasas_check_reset_xscale - For controller reset check
353 * @regs: MFI register set
354 */
355static int
356megasas_check_reset_xscale(struct megasas_instance *instance,
357 struct megasas_register_set __iomem *regs)
358{
359 u32 consumer;
360 consumer = *instance->consumer;
361
362 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
363 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
364 return 1;
365 }
366 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800367}
368
369static struct megasas_instance_template megasas_instance_template_xscale = {
370
371 .fire_cmd = megasas_fire_cmd_xscale,
372 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700373 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800374 .clear_intr = megasas_clear_intr_xscale,
375 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400376 .adp_reset = megasas_adp_reset_xscale,
377 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800378 .service_isr = megasas_isr,
379 .tasklet = megasas_complete_cmd_dpc,
380 .init_adapter = megasas_init_adapter_mfi,
381 .build_and_issue_cmd = megasas_build_and_issue_cmd,
382 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800383};
384
385/**
adam radford0d490162010-12-14 19:17:17 -0800386* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800387* to xscale (deviceid : 1064R, PERC5) controllers
388*/
389
390/**
adam radford0d490162010-12-14 19:17:17 -0800391* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800392* controllers
393*/
394
395/**
396 * megasas_enable_intr_ppc - Enables interrupts
397 * @regs: MFI register set
398 */
399static inline void
400megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
401{
402 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800403
bo yang39a98552010-09-22 22:36:29 -0400404 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800405
406 /* Dummy readl to force pci flush */
407 readl(&regs->outbound_intr_mask);
408}
409
410/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700411 * megasas_disable_intr_ppc - Disable interrupt
412 * @regs: MFI register set
413 */
414static inline void
415megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
416{
417 u32 mask = 0xFFFFFFFF;
418 writel(mask, &regs->outbound_intr_mask);
419 /* Dummy readl to force pci flush */
420 readl(&regs->outbound_intr_mask);
421}
422
423/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800424 * megasas_read_fw_status_reg_ppc - returns the current FW status value
425 * @regs: MFI register set
426 */
427static u32
428megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
429{
430 return readl(&(regs)->outbound_scratch_pad);
431}
432
433/**
434 * megasas_clear_interrupt_ppc - Check & clear interrupt
435 * @regs: MFI register set
436 */
adam radford0d490162010-12-14 19:17:17 -0800437static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800438megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
439{
440 u32 status;
441 /*
442 * Check if it is our interrupt
443 */
444 status = readl(&regs->outbound_intr_status);
445
446 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
bo yang39a98552010-09-22 22:36:29 -0400447 return 0;
Sumant Patrof9876f02006-02-03 15:34:35 -0800448 }
449
450 /*
451 * Clear the interrupt by writing back the same value
452 */
453 writel(status, &regs->outbound_doorbell_clear);
454
Yang, Bo06f579d2008-08-10 12:42:37 -0700455 /* Dummy readl to force pci flush */
456 readl(&regs->outbound_doorbell_clear);
457
bo yang39a98552010-09-22 22:36:29 -0400458 return 1;
Sumant Patrof9876f02006-02-03 15:34:35 -0800459}
460/**
461 * megasas_fire_cmd_ppc - Sends command to the FW
462 * @frame_phys_addr : Physical address of cmd
463 * @frame_count : Number of frames for the command
464 * @regs : MFI register set
465 */
adam radford0d490162010-12-14 19:17:17 -0800466static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600467megasas_fire_cmd_ppc(struct megasas_instance *instance,
468 dma_addr_t frame_phys_addr,
469 u32 frame_count,
470 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800471{
bo yang39a98552010-09-22 22:36:29 -0400472 unsigned long flags;
473 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800474 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800475 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400476 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800477}
478
bo yang39a98552010-09-22 22:36:29 -0400479/**
480 * megasas_adp_reset_ppc - For controller reset
481 * @regs: MFI register set
482 */
483static int
484megasas_adp_reset_ppc(struct megasas_instance *instance,
485 struct megasas_register_set __iomem *regs)
486{
487 return 0;
488}
489
490/**
491 * megasas_check_reset_ppc - For controller reset check
492 * @regs: MFI register set
493 */
494static int
495megasas_check_reset_ppc(struct megasas_instance *instance,
496 struct megasas_register_set __iomem *regs)
497{
498 return 0;
499}
Sumant Patrof9876f02006-02-03 15:34:35 -0800500static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800501
Sumant Patrof9876f02006-02-03 15:34:35 -0800502 .fire_cmd = megasas_fire_cmd_ppc,
503 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700504 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800505 .clear_intr = megasas_clear_intr_ppc,
506 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
bo yang39a98552010-09-22 22:36:29 -0400507 .adp_reset = megasas_adp_reset_ppc,
508 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800509 .service_isr = megasas_isr,
510 .tasklet = megasas_complete_cmd_dpc,
511 .init_adapter = megasas_init_adapter_mfi,
512 .build_and_issue_cmd = megasas_build_and_issue_cmd,
513 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800514};
515
516/**
Yang, Bo87911122009-10-06 14:31:54 -0600517 * megasas_enable_intr_skinny - Enables interrupts
518 * @regs: MFI register set
519 */
520static inline void
521megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
522{
523 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
524
525 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
526
527 /* Dummy readl to force pci flush */
528 readl(&regs->outbound_intr_mask);
529}
530
531/**
532 * megasas_disable_intr_skinny - Disables interrupt
533 * @regs: MFI register set
534 */
535static inline void
536megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
537{
538 u32 mask = 0xFFFFFFFF;
539 writel(mask, &regs->outbound_intr_mask);
540 /* Dummy readl to force pci flush */
541 readl(&regs->outbound_intr_mask);
542}
543
544/**
545 * megasas_read_fw_status_reg_skinny - returns the current FW status value
546 * @regs: MFI register set
547 */
548static u32
549megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
550{
551 return readl(&(regs)->outbound_scratch_pad);
552}
553
554/**
555 * megasas_clear_interrupt_skinny - Check & clear interrupt
556 * @regs: MFI register set
557 */
558static int
559megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
560{
561 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800562 u32 mfiStatus = 0;
563
Yang, Bo87911122009-10-06 14:31:54 -0600564 /*
565 * Check if it is our interrupt
566 */
567 status = readl(&regs->outbound_intr_status);
568
569 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400570 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600571 }
572
573 /*
adam radfordebf054b2011-02-24 20:57:15 -0800574 * Check if it is our interrupt
575 */
576 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
577 MFI_STATE_FAULT) {
578 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
579 } else
580 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
581
582 /*
Yang, Bo87911122009-10-06 14:31:54 -0600583 * Clear the interrupt by writing back the same value
584 */
585 writel(status, &regs->outbound_intr_status);
586
587 /*
588 * dummy read to flush PCI
589 */
590 readl(&regs->outbound_intr_status);
591
adam radfordebf054b2011-02-24 20:57:15 -0800592 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600593}
594
595/**
596 * megasas_fire_cmd_skinny - Sends command to the FW
597 * @frame_phys_addr : Physical address of cmd
598 * @frame_count : Number of frames for the command
599 * @regs : MFI register set
600 */
601static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600602megasas_fire_cmd_skinny(struct megasas_instance *instance,
603 dma_addr_t frame_phys_addr,
604 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600605 struct megasas_register_set __iomem *regs)
606{
Yang, Bo0c79e682009-10-06 14:47:35 -0600607 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400608 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600609 writel(0, &(regs)->inbound_high_queue_port);
610 writel((frame_phys_addr | (frame_count<<1))|1,
611 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400612 spin_unlock_irqrestore(&instance->hba_lock, flags);
613}
614
615/**
bo yang39a98552010-09-22 22:36:29 -0400616 * megasas_check_reset_skinny - For controller reset check
617 * @regs: MFI register set
618 */
619static int
620megasas_check_reset_skinny(struct megasas_instance *instance,
621 struct megasas_register_set __iomem *regs)
622{
623 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600624}
625
626static struct megasas_instance_template megasas_instance_template_skinny = {
627
628 .fire_cmd = megasas_fire_cmd_skinny,
629 .enable_intr = megasas_enable_intr_skinny,
630 .disable_intr = megasas_disable_intr_skinny,
631 .clear_intr = megasas_clear_intr_skinny,
632 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800633 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400634 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800635 .service_isr = megasas_isr,
636 .tasklet = megasas_complete_cmd_dpc,
637 .init_adapter = megasas_init_adapter_mfi,
638 .build_and_issue_cmd = megasas_build_and_issue_cmd,
639 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600640};
641
642
643/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700644* The following functions are defined for gen2 (deviceid : 0x78 0x79)
645* controllers
646*/
647
648/**
649 * megasas_enable_intr_gen2 - Enables interrupts
650 * @regs: MFI register set
651 */
652static inline void
653megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
654{
655 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
656
657 /* write ~0x00000005 (4 & 1) to the intr mask*/
658 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
659
660 /* Dummy readl to force pci flush */
661 readl(&regs->outbound_intr_mask);
662}
663
664/**
665 * megasas_disable_intr_gen2 - Disables interrupt
666 * @regs: MFI register set
667 */
668static inline void
669megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
670{
671 u32 mask = 0xFFFFFFFF;
672 writel(mask, &regs->outbound_intr_mask);
673 /* Dummy readl to force pci flush */
674 readl(&regs->outbound_intr_mask);
675}
676
677/**
678 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
679 * @regs: MFI register set
680 */
681static u32
682megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
683{
684 return readl(&(regs)->outbound_scratch_pad);
685}
686
687/**
688 * megasas_clear_interrupt_gen2 - Check & clear interrupt
689 * @regs: MFI register set
690 */
691static int
692megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
693{
694 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400695 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700696 /*
697 * Check if it is our interrupt
698 */
699 status = readl(&regs->outbound_intr_status);
700
bo yang39a98552010-09-22 22:36:29 -0400701 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
702 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
703 }
704 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
705 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
706 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700707
708 /*
709 * Clear the interrupt by writing back the same value
710 */
bo yang39a98552010-09-22 22:36:29 -0400711 if (mfiStatus)
712 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700713
714 /* Dummy readl to force pci flush */
715 readl(&regs->outbound_intr_status);
716
bo yang39a98552010-09-22 22:36:29 -0400717 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700718}
719/**
720 * megasas_fire_cmd_gen2 - Sends command to the FW
721 * @frame_phys_addr : Physical address of cmd
722 * @frame_count : Number of frames for the command
723 * @regs : MFI register set
724 */
725static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600726megasas_fire_cmd_gen2(struct megasas_instance *instance,
727 dma_addr_t frame_phys_addr,
728 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700729 struct megasas_register_set __iomem *regs)
730{
bo yang39a98552010-09-22 22:36:29 -0400731 unsigned long flags;
732 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700733 writel((frame_phys_addr | (frame_count<<1))|1,
734 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400735 spin_unlock_irqrestore(&instance->hba_lock, flags);
736}
737
738/**
739 * megasas_adp_reset_gen2 - For controller reset
740 * @regs: MFI register set
741 */
742static int
743megasas_adp_reset_gen2(struct megasas_instance *instance,
744 struct megasas_register_set __iomem *reg_set)
745{
746 u32 retry = 0 ;
747 u32 HostDiag;
adam radfordebf054b2011-02-24 20:57:15 -0800748 u32 *seq_offset = &reg_set->seq_offset;
749 u32 *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400750
adam radfordebf054b2011-02-24 20:57:15 -0800751 if (instance->instancet == &megasas_instance_template_skinny) {
752 seq_offset = &reg_set->fusion_seq_offset;
753 hostdiag_offset = &reg_set->fusion_host_diag;
754 }
755
756 writel(0, seq_offset);
757 writel(4, seq_offset);
758 writel(0xb, seq_offset);
759 writel(2, seq_offset);
760 writel(7, seq_offset);
761 writel(0xd, seq_offset);
762
bo yang39a98552010-09-22 22:36:29 -0400763 msleep(1000);
764
adam radfordebf054b2011-02-24 20:57:15 -0800765 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400766
767 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
768 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800769 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400770 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
771 retry, HostDiag);
772
773 if (retry++ >= 100)
774 return 1;
775
776 }
777
778 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
779
adam radfordebf054b2011-02-24 20:57:15 -0800780 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400781
782 ssleep(10);
783
adam radfordebf054b2011-02-24 20:57:15 -0800784 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400785 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
786 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800787 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400788 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
789 retry, HostDiag);
790
791 if (retry++ >= 1000)
792 return 1;
793
794 }
795 return 0;
796}
797
798/**
799 * megasas_check_reset_gen2 - For controller reset check
800 * @regs: MFI register set
801 */
802static int
803megasas_check_reset_gen2(struct megasas_instance *instance,
804 struct megasas_register_set __iomem *regs)
805{
Yang, Bo707e09b2010-10-12 07:20:27 -0600806 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
807 return 1;
808 }
809
bo yang39a98552010-09-22 22:36:29 -0400810 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700811}
812
813static struct megasas_instance_template megasas_instance_template_gen2 = {
814
815 .fire_cmd = megasas_fire_cmd_gen2,
816 .enable_intr = megasas_enable_intr_gen2,
817 .disable_intr = megasas_disable_intr_gen2,
818 .clear_intr = megasas_clear_intr_gen2,
819 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400820 .adp_reset = megasas_adp_reset_gen2,
821 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800822 .service_isr = megasas_isr,
823 .tasklet = megasas_complete_cmd_dpc,
824 .init_adapter = megasas_init_adapter_mfi,
825 .build_and_issue_cmd = megasas_build_and_issue_cmd,
826 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700827};
828
829/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800830* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400831* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800832*/
833
adam radford9c915a82010-12-21 13:34:31 -0800834/*
835 * Template added for TB (Fusion)
836 */
837extern struct megasas_instance_template megasas_instance_template_fusion;
838
Sumant Patrof9876f02006-02-03 15:34:35 -0800839/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400840 * megasas_issue_polled - Issues a polling command
841 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800842 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400843 *
844 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
845 */
adam radford9c915a82010-12-21 13:34:31 -0800846int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400847megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
848{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400849
850 struct megasas_header *frame_hdr = &cmd->frame->hdr;
851
852 frame_hdr->cmd_status = 0xFF;
853 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
854
855 /*
856 * Issue the frame using inbound queue port
857 */
adam radford9c915a82010-12-21 13:34:31 -0800858 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400859
860 /*
861 * Wait for cmd_status to change
862 */
adam radford9c915a82010-12-21 13:34:31 -0800863 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400864}
865
866/**
867 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
868 * @instance: Adapter soft state
869 * @cmd: Command to be issued
870 *
871 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700872 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400873 * Used to issue ioctl commands.
874 */
875static int
876megasas_issue_blocked_cmd(struct megasas_instance *instance,
877 struct megasas_cmd *cmd)
878{
879 cmd->cmd_status = ENODATA;
880
adam radford9c915a82010-12-21 13:34:31 -0800881 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400882
bo yang39a98552010-09-22 22:36:29 -0400883 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400884
885 return 0;
886}
887
888/**
889 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
890 * @instance: Adapter soft state
891 * @cmd_to_abort: Previously issued cmd to be aborted
892 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +0100893 * MFI firmware can abort previously issued AEN command (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400894 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700895 * cmd and waits for return status.
896 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400897 */
898static int
899megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
900 struct megasas_cmd *cmd_to_abort)
901{
902 struct megasas_cmd *cmd;
903 struct megasas_abort_frame *abort_fr;
904
905 cmd = megasas_get_cmd(instance);
906
907 if (!cmd)
908 return -1;
909
910 abort_fr = &cmd->frame->abort;
911
912 /*
913 * Prepare and issue the abort frame
914 */
915 abort_fr->cmd = MFI_CMD_ABORT;
916 abort_fr->cmd_status = 0xFF;
917 abort_fr->flags = 0;
918 abort_fr->abort_context = cmd_to_abort->index;
919 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
920 abort_fr->abort_mfi_phys_addr_hi = 0;
921
922 cmd->sync_cmd = 1;
923 cmd->cmd_status = 0xFF;
924
adam radford9c915a82010-12-21 13:34:31 -0800925 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400926
927 /*
928 * Wait for this cmd to complete
929 */
bo yang39a98552010-09-22 22:36:29 -0400930 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
931 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400932
933 megasas_return_cmd(instance, cmd);
934 return 0;
935}
936
937/**
938 * megasas_make_sgl32 - Prepares 32-bit SGL
939 * @instance: Adapter soft state
940 * @scp: SCSI command from the mid-layer
941 * @mfi_sgl: SGL to be filled in
942 *
943 * If successful, this function returns the number of SG elements. Otherwise,
944 * it returnes -1.
945 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800946static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400947megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
948 union megasas_sgl *mfi_sgl)
949{
950 int i;
951 int sge_count;
952 struct scatterlist *os_sgl;
953
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900954 sge_count = scsi_dma_map(scp);
955 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400956
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900957 if (sge_count) {
958 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
959 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
960 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
961 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400962 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400963 return sge_count;
964}
965
966/**
967 * megasas_make_sgl64 - Prepares 64-bit SGL
968 * @instance: Adapter soft state
969 * @scp: SCSI command from the mid-layer
970 * @mfi_sgl: SGL to be filled in
971 *
972 * If successful, this function returns the number of SG elements. Otherwise,
973 * it returnes -1.
974 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800975static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400976megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
977 union megasas_sgl *mfi_sgl)
978{
979 int i;
980 int sge_count;
981 struct scatterlist *os_sgl;
982
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900983 sge_count = scsi_dma_map(scp);
984 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400985
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900986 if (sge_count) {
987 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
988 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
989 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
990 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400992 return sge_count;
993}
994
Yang, Bof4c9a132009-10-06 14:43:28 -0600995/**
996 * megasas_make_sgl_skinny - Prepares IEEE SGL
997 * @instance: Adapter soft state
998 * @scp: SCSI command from the mid-layer
999 * @mfi_sgl: SGL to be filled in
1000 *
1001 * If successful, this function returns the number of SG elements. Otherwise,
1002 * it returnes -1.
1003 */
1004static int
1005megasas_make_sgl_skinny(struct megasas_instance *instance,
1006 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1007{
1008 int i;
1009 int sge_count;
1010 struct scatterlist *os_sgl;
1011
1012 sge_count = scsi_dma_map(scp);
1013
1014 if (sge_count) {
1015 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1016 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1017 mfi_sgl->sge_skinny[i].phys_addr =
1018 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001019 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001020 }
1021 }
1022 return sge_count;
1023}
1024
Sumant Patrob1df99d2006-10-03 12:40:47 -07001025 /**
1026 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001027 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001028 * @sge_count : number of sg elements
1029 *
1030 * Returns the number of frames required for numnber of sge's (sge_count)
1031 */
1032
Yang, Bof4c9a132009-10-06 14:43:28 -06001033static u32 megasas_get_frame_count(struct megasas_instance *instance,
1034 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001035{
1036 int num_cnt;
1037 int sge_bytes;
1038 u32 sge_sz;
1039 u32 frame_count=0;
1040
1041 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1042 sizeof(struct megasas_sge32);
1043
Yang, Bof4c9a132009-10-06 14:43:28 -06001044 if (instance->flag_ieee) {
1045 sge_sz = sizeof(struct megasas_sge_skinny);
1046 }
1047
Sumant Patrob1df99d2006-10-03 12:40:47 -07001048 /*
bo yangd532dbe2008-03-17 03:36:43 -04001049 * Main frame can contain 2 SGEs for 64-bit SGLs and
1050 * 3 SGEs for 32-bit SGLs for ldio &
1051 * 1 SGEs for 64-bit SGLs and
1052 * 2 SGEs for 32-bit SGLs for pthru frame
1053 */
1054 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001055 if (instance->flag_ieee == 1) {
1056 num_cnt = sge_count - 1;
1057 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001058 num_cnt = sge_count - 1;
1059 else
1060 num_cnt = sge_count - 2;
1061 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001062 if (instance->flag_ieee == 1) {
1063 num_cnt = sge_count - 1;
1064 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001065 num_cnt = sge_count - 2;
1066 else
1067 num_cnt = sge_count - 3;
1068 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001069
1070 if(num_cnt>0){
1071 sge_bytes = sge_sz * num_cnt;
1072
1073 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1074 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1075 }
1076 /* Main frame */
1077 frame_count +=1;
1078
1079 if (frame_count > 7)
1080 frame_count = 8;
1081 return frame_count;
1082}
1083
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001084/**
1085 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1086 * @instance: Adapter soft state
1087 * @scp: SCSI command
1088 * @cmd: Command to be prepared in
1089 *
1090 * This function prepares CDB commands. These are typcially pass-through
1091 * commands to the devices.
1092 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001093static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001094megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1095 struct megasas_cmd *cmd)
1096{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001097 u32 is_logical;
1098 u32 device_id;
1099 u16 flags = 0;
1100 struct megasas_pthru_frame *pthru;
1101
1102 is_logical = MEGASAS_IS_LOGICAL(scp);
1103 device_id = MEGASAS_DEV_INDEX(instance, scp);
1104 pthru = (struct megasas_pthru_frame *)cmd->frame;
1105
1106 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1107 flags = MFI_FRAME_DIR_WRITE;
1108 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1109 flags = MFI_FRAME_DIR_READ;
1110 else if (scp->sc_data_direction == PCI_DMA_NONE)
1111 flags = MFI_FRAME_DIR_NONE;
1112
Yang, Bof4c9a132009-10-06 14:43:28 -06001113 if (instance->flag_ieee == 1) {
1114 flags |= MFI_FRAME_IEEE;
1115 }
1116
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001117 /*
1118 * Prepare the DCDB frame
1119 */
1120 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1121 pthru->cmd_status = 0x0;
1122 pthru->scsi_status = 0x0;
1123 pthru->target_id = device_id;
1124 pthru->lun = scp->device->lun;
1125 pthru->cdb_len = scp->cmd_len;
1126 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001127 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001128 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001129 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001130
1131 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1132
1133 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001134 * If the command is for the tape device, set the
1135 * pthru timeout to the os layer timeout value.
1136 */
1137 if (scp->device->type == TYPE_TAPE) {
1138 if ((scp->request->timeout / HZ) > 0xFFFF)
1139 pthru->timeout = 0xFFFF;
1140 else
1141 pthru->timeout = scp->request->timeout / HZ;
1142 }
1143
1144 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001145 * Construct SGL
1146 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001147 if (instance->flag_ieee == 1) {
1148 pthru->flags |= MFI_FRAME_SGL64;
1149 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1150 &pthru->sgl);
1151 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001152 pthru->flags |= MFI_FRAME_SGL64;
1153 pthru->sge_count = megasas_make_sgl64(instance, scp,
1154 &pthru->sgl);
1155 } else
1156 pthru->sge_count = megasas_make_sgl32(instance, scp,
1157 &pthru->sgl);
1158
Yang, Bobdc6fb82009-12-06 08:30:19 -07001159 if (pthru->sge_count > instance->max_num_sge) {
1160 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1161 pthru->sge_count);
1162 return 0;
1163 }
1164
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165 /*
1166 * Sense info specific
1167 */
1168 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1169 pthru->sense_buf_phys_addr_hi = 0;
1170 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1171
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001172 /*
1173 * Compute the total number of frames this command consumes. FW uses
1174 * this number to pull sufficient number of frames from host memory.
1175 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001176 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001177 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001178
1179 return cmd->frame_count;
1180}
1181
1182/**
1183 * megasas_build_ldio - Prepares IOs to logical devices
1184 * @instance: Adapter soft state
1185 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001186 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001187 *
1188 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1189 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001190static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001191megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1192 struct megasas_cmd *cmd)
1193{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001194 u32 device_id;
1195 u8 sc = scp->cmnd[0];
1196 u16 flags = 0;
1197 struct megasas_io_frame *ldio;
1198
1199 device_id = MEGASAS_DEV_INDEX(instance, scp);
1200 ldio = (struct megasas_io_frame *)cmd->frame;
1201
1202 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1203 flags = MFI_FRAME_DIR_WRITE;
1204 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1205 flags = MFI_FRAME_DIR_READ;
1206
Yang, Bof4c9a132009-10-06 14:43:28 -06001207 if (instance->flag_ieee == 1) {
1208 flags |= MFI_FRAME_IEEE;
1209 }
1210
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001211 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001212 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001213 */
1214 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1215 ldio->cmd_status = 0x0;
1216 ldio->scsi_status = 0x0;
1217 ldio->target_id = device_id;
1218 ldio->timeout = 0;
1219 ldio->reserved_0 = 0;
1220 ldio->pad_0 = 0;
1221 ldio->flags = flags;
1222 ldio->start_lba_hi = 0;
1223 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1224
1225 /*
1226 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1227 */
1228 if (scp->cmd_len == 6) {
1229 ldio->lba_count = (u32) scp->cmnd[4];
1230 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1231 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1232
1233 ldio->start_lba_lo &= 0x1FFFFF;
1234 }
1235
1236 /*
1237 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1238 */
1239 else if (scp->cmd_len == 10) {
1240 ldio->lba_count = (u32) scp->cmnd[8] |
1241 ((u32) scp->cmnd[7] << 8);
1242 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1243 ((u32) scp->cmnd[3] << 16) |
1244 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1245 }
1246
1247 /*
1248 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1249 */
1250 else if (scp->cmd_len == 12) {
1251 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1252 ((u32) scp->cmnd[7] << 16) |
1253 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1254
1255 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1256 ((u32) scp->cmnd[3] << 16) |
1257 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1258 }
1259
1260 /*
1261 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1262 */
1263 else if (scp->cmd_len == 16) {
1264 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1265 ((u32) scp->cmnd[11] << 16) |
1266 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1267
1268 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1269 ((u32) scp->cmnd[7] << 16) |
1270 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1271
1272 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1273 ((u32) scp->cmnd[3] << 16) |
1274 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1275
1276 }
1277
1278 /*
1279 * Construct SGL
1280 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001281 if (instance->flag_ieee) {
1282 ldio->flags |= MFI_FRAME_SGL64;
1283 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1284 &ldio->sgl);
1285 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001286 ldio->flags |= MFI_FRAME_SGL64;
1287 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1288 } else
1289 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1290
Yang, Bobdc6fb82009-12-06 08:30:19 -07001291 if (ldio->sge_count > instance->max_num_sge) {
1292 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1293 ldio->sge_count);
1294 return 0;
1295 }
1296
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001297 /*
1298 * Sense info specific
1299 */
1300 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1301 ldio->sense_buf_phys_addr_hi = 0;
1302 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1303
Sumant Patrob1df99d2006-10-03 12:40:47 -07001304 /*
1305 * Compute the total number of frames this command consumes. FW uses
1306 * this number to pull sufficient number of frames from host memory.
1307 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001308 cmd->frame_count = megasas_get_frame_count(instance,
1309 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001310
1311 return cmd->frame_count;
1312}
1313
1314/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001315 * megasas_is_ldio - Checks if the cmd is for logical drive
1316 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001317 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001318 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001319 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001320 */
adam radford9c915a82010-12-21 13:34:31 -08001321inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001322{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001323 if (!MEGASAS_IS_LOGICAL(cmd))
1324 return 0;
1325 switch (cmd->cmnd[0]) {
1326 case READ_10:
1327 case WRITE_10:
1328 case READ_12:
1329 case WRITE_12:
1330 case READ_6:
1331 case WRITE_6:
1332 case READ_16:
1333 case WRITE_16:
1334 return 1;
1335 default:
1336 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001337 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001338}
1339
Sumant Patro658dced2006-10-03 13:09:14 -07001340 /**
1341 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1342 * in FW
1343 * @instance: Adapter soft state
1344 */
1345static inline void
1346megasas_dump_pending_frames(struct megasas_instance *instance)
1347{
1348 struct megasas_cmd *cmd;
1349 int i,n;
1350 union megasas_sgl *mfi_sgl;
1351 struct megasas_io_frame *ldio;
1352 struct megasas_pthru_frame *pthru;
1353 u32 sgcount;
1354 u32 max_cmd = instance->max_fw_cmds;
1355
1356 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1357 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1358 if (IS_DMA64)
1359 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1360 else
1361 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1362
1363 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1364 for (i = 0; i < max_cmd; i++) {
1365 cmd = instance->cmd_list[i];
1366 if(!cmd->scmd)
1367 continue;
1368 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1369 if (megasas_is_ldio(cmd->scmd)){
1370 ldio = (struct megasas_io_frame *)cmd->frame;
1371 mfi_sgl = &ldio->sgl;
1372 sgcount = ldio->sge_count;
1373 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);
1374 }
1375 else {
1376 pthru = (struct megasas_pthru_frame *) cmd->frame;
1377 mfi_sgl = &pthru->sgl;
1378 sgcount = pthru->sge_count;
1379 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);
1380 }
1381 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1382 for (n = 0; n < sgcount; n++){
1383 if (IS_DMA64)
1384 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) ;
1385 else
1386 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1387 }
1388 }
1389 printk(KERN_ERR "\n");
1390 } /*for max_cmd*/
1391 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1392 for (i = 0; i < max_cmd; i++) {
1393
1394 cmd = instance->cmd_list[i];
1395
1396 if(cmd->sync_cmd == 1){
1397 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1398 }
1399 }
1400 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1401}
1402
adam radfordcd50ba82010-12-21 10:23:23 -08001403u32
1404megasas_build_and_issue_cmd(struct megasas_instance *instance,
1405 struct scsi_cmnd *scmd)
1406{
1407 struct megasas_cmd *cmd;
1408 u32 frame_count;
1409
1410 cmd = megasas_get_cmd(instance);
1411 if (!cmd)
1412 return SCSI_MLQUEUE_HOST_BUSY;
1413
1414 /*
1415 * Logical drive command
1416 */
1417 if (megasas_is_ldio(scmd))
1418 frame_count = megasas_build_ldio(instance, scmd, cmd);
1419 else
1420 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1421
1422 if (!frame_count)
1423 goto out_return_cmd;
1424
1425 cmd->scmd = scmd;
1426 scmd->SCp.ptr = (char *)cmd;
1427
1428 /*
1429 * Issue the command to the FW
1430 */
1431 atomic_inc(&instance->fw_outstanding);
1432
1433 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1434 cmd->frame_count-1, instance->reg_set);
1435 /*
1436 * Check if we have pend cmds to be completed
1437 */
1438 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1439 tasklet_schedule(&instance->isr_tasklet);
1440
1441 return 0;
1442out_return_cmd:
1443 megasas_return_cmd(instance, cmd);
1444 return 1;
1445}
1446
1447
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001448/**
1449 * megasas_queue_command - Queue entry point
1450 * @scmd: SCSI command to be queued
1451 * @done: Callback entry point
1452 */
1453static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001454megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001455{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001456 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001457 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001458
1459 instance = (struct megasas_instance *)
1460 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001461
bo yang39a98552010-09-22 22:36:29 -04001462 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001463 return SCSI_MLQUEUE_HOST_BUSY;
1464
bo yang39a98552010-09-22 22:36:29 -04001465 spin_lock_irqsave(&instance->hba_lock, flags);
1466 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1467 spin_unlock_irqrestore(&instance->hba_lock, flags);
1468 return SCSI_MLQUEUE_HOST_BUSY;
1469 }
1470
1471 spin_unlock_irqrestore(&instance->hba_lock, flags);
1472
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001473 scmd->scsi_done = done;
1474 scmd->result = 0;
1475
Sumant Patrocb59aa62006-01-25 11:53:25 -08001476 if (MEGASAS_IS_LOGICAL(scmd) &&
1477 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1478 scmd->result = DID_BAD_TARGET << 16;
1479 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001480 }
1481
Sumant Patro02b01e02007-02-14 13:00:55 -08001482 switch (scmd->cmnd[0]) {
1483 case SYNCHRONIZE_CACHE:
1484 /*
1485 * FW takes care of flush cache on its own
1486 * No need to send it down
1487 */
1488 scmd->result = DID_OK << 16;
1489 goto out_done;
1490 default:
1491 break;
1492 }
1493
adam radfordcd50ba82010-12-21 10:23:23 -08001494 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1495 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001496 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001497 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001498
1499 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001500
Sumant Patrocb59aa62006-01-25 11:53:25 -08001501 out_done:
1502 done(scmd);
1503 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001504}
1505
Jeff Garzikf2812332010-11-16 02:10:29 -05001506static DEF_SCSI_QCMD(megasas_queue_command)
1507
Yang, Bo044833b2009-10-06 14:33:06 -06001508static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1509{
1510 int i;
1511
1512 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1513
1514 if ((megasas_mgmt_info.instance[i]) &&
1515 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1516 return megasas_mgmt_info.instance[i];
1517 }
1518
1519 return NULL;
1520}
1521
Christoph Hellwig147aab62006-02-17 12:13:48 +01001522static int megasas_slave_configure(struct scsi_device *sdev)
1523{
Yang, Bo044833b2009-10-06 14:33:06 -06001524 u16 pd_index = 0;
1525 struct megasas_instance *instance ;
1526
1527 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001528
1529 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001530 * Don't export physical disk devices to the disk driver.
1531 *
1532 * FIXME: Currently we don't export them to the midlayer at all.
1533 * That will be fixed once LSI engineers have audited the
1534 * firmware for possible issues.
1535 */
1536 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1537 sdev->type == TYPE_DISK) {
1538 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1539 sdev->id;
1540 if (instance->pd_list[pd_index].driveState ==
1541 MR_PD_STATE_SYSTEM) {
1542 blk_queue_rq_timeout(sdev->request_queue,
1543 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1544 return 0;
1545 }
1546 return -ENXIO;
1547 }
1548
1549 /*
1550 * The RAID firmware may require extended timeouts.
1551 */
1552 blk_queue_rq_timeout(sdev->request_queue,
1553 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1554 return 0;
1555}
1556
1557static int megasas_slave_alloc(struct scsi_device *sdev)
1558{
1559 u16 pd_index = 0;
1560 struct megasas_instance *instance ;
1561 instance = megasas_lookup_instance(sdev->host->host_no);
1562 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1563 (sdev->type == TYPE_DISK)) {
1564 /*
1565 * Open the OS scan to the SYSTEM PD
1566 */
1567 pd_index =
1568 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1569 sdev->id;
1570 if ((instance->pd_list[pd_index].driveState ==
1571 MR_PD_STATE_SYSTEM) &&
1572 (instance->pd_list[pd_index].driveType ==
1573 TYPE_DISK)) {
1574 return 0;
1575 }
1576 return -ENXIO;
1577 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001578 return 0;
1579}
1580
adam radford9c915a82010-12-21 13:34:31 -08001581void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001582{
1583 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001584 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1585 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)) {
1586 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001587 } else {
adam radford9c915a82010-12-21 13:34:31 -08001588 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1589 }
1590}
1591
1592 /**
1593 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1594 * restored to max value
1595 * @instance: Adapter soft state
1596 *
1597 */
1598void
1599megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1600{
1601 unsigned long flags;
1602 if (instance->flag & MEGASAS_FW_BUSY
1603 && time_after(jiffies, instance->last_time + 5 * HZ)
1604 && atomic_read(&instance->fw_outstanding) < 17) {
1605
1606 spin_lock_irqsave(instance->host->host_lock, flags);
1607 instance->flag &= ~MEGASAS_FW_BUSY;
1608 if ((instance->pdev->device ==
1609 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1610 (instance->pdev->device ==
1611 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1612 instance->host->can_queue =
1613 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1614 } else
1615 instance->host->can_queue =
1616 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1617
1618 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001619 }
1620}
1621
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001622/**
bo yang7343eb62007-11-09 04:35:44 -05001623 * megasas_complete_cmd_dpc - Returns FW's controller structure
1624 * @instance_addr: Address of adapter soft state
1625 *
1626 * Tasklet to complete cmds
1627 */
1628static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1629{
1630 u32 producer;
1631 u32 consumer;
1632 u32 context;
1633 struct megasas_cmd *cmd;
1634 struct megasas_instance *instance =
1635 (struct megasas_instance *)instance_addr;
1636 unsigned long flags;
1637
1638 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001639 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001640 return;
1641
1642 spin_lock_irqsave(&instance->completion_lock, flags);
1643
1644 producer = *instance->producer;
1645 consumer = *instance->consumer;
1646
1647 while (consumer != producer) {
1648 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001649 if (context >= instance->max_fw_cmds) {
1650 printk(KERN_ERR "Unexpected context value %x\n",
1651 context);
1652 BUG();
1653 }
bo yang7343eb62007-11-09 04:35:44 -05001654
1655 cmd = instance->cmd_list[context];
1656
1657 megasas_complete_cmd(instance, cmd, DID_OK);
1658
1659 consumer++;
1660 if (consumer == (instance->max_fw_cmds + 1)) {
1661 consumer = 0;
1662 }
1663 }
1664
1665 *instance->consumer = producer;
1666
1667 spin_unlock_irqrestore(&instance->completion_lock, flags);
1668
1669 /*
1670 * Check if we can restore can_queue
1671 */
adam radford9c915a82010-12-21 13:34:31 -08001672 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001673}
1674
Yang, Bo707e09b2010-10-12 07:20:27 -06001675static void
1676megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1677
1678static void
1679process_fw_state_change_wq(struct work_struct *work);
1680
1681void megasas_do_ocr(struct megasas_instance *instance)
1682{
1683 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1684 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1685 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1686 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1687 }
1688 instance->instancet->disable_intr(instance->reg_set);
1689 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1690 instance->issuepend_done = 0;
1691
1692 atomic_set(&instance->fw_outstanding, 0);
1693 megasas_internal_reset_defer_cmds(instance);
1694 process_fw_state_change_wq(&instance->work_init);
1695}
1696
bo yang7343eb62007-11-09 04:35:44 -05001697/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001698 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1699 * @instance: Adapter soft state
1700 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001701 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001702 * complete all its outstanding commands. Returns error if one or more IOs
1703 * are pending after this time period. It also marks the controller dead.
1704 */
1705static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1706{
1707 int i;
bo yang39a98552010-09-22 22:36:29 -04001708 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001709 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001710 u8 adprecovery;
1711 unsigned long flags;
1712 struct list_head clist_local;
1713 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001714 u32 fw_state;
1715 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001716
1717 spin_lock_irqsave(&instance->hba_lock, flags);
1718 adprecovery = instance->adprecovery;
1719 spin_unlock_irqrestore(&instance->hba_lock, flags);
1720
1721 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1722
1723 INIT_LIST_HEAD(&clist_local);
1724 spin_lock_irqsave(&instance->hba_lock, flags);
1725 list_splice_init(&instance->internal_reset_pending_q,
1726 &clist_local);
1727 spin_unlock_irqrestore(&instance->hba_lock, flags);
1728
1729 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1730 for (i = 0; i < wait_time; i++) {
1731 msleep(1000);
1732 spin_lock_irqsave(&instance->hba_lock, flags);
1733 adprecovery = instance->adprecovery;
1734 spin_unlock_irqrestore(&instance->hba_lock, flags);
1735 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1736 break;
1737 }
1738
1739 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1740 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1741 spin_lock_irqsave(&instance->hba_lock, flags);
1742 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1743 spin_unlock_irqrestore(&instance->hba_lock, flags);
1744 return FAILED;
1745 }
1746
1747 reset_index = 0;
1748 while (!list_empty(&clist_local)) {
1749 reset_cmd = list_entry((&clist_local)->next,
1750 struct megasas_cmd, list);
1751 list_del_init(&reset_cmd->list);
1752 if (reset_cmd->scmd) {
1753 reset_cmd->scmd->result = DID_RESET << 16;
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001754 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04001755 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001756 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04001757
1758 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1759 megasas_return_cmd(instance, reset_cmd);
1760 } else if (reset_cmd->sync_cmd) {
1761 printk(KERN_NOTICE "megasas:%p synch cmds"
1762 "reset queue\n",
1763 reset_cmd);
1764
1765 reset_cmd->cmd_status = ENODATA;
1766 instance->instancet->fire_cmd(instance,
1767 reset_cmd->frame_phys_addr,
1768 0, instance->reg_set);
1769 } else {
1770 printk(KERN_NOTICE "megasas: %p unexpected"
1771 "cmds lst\n",
1772 reset_cmd);
1773 }
1774 reset_index++;
1775 }
1776
1777 return SUCCESS;
1778 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001779
1780 for (i = 0; i < wait_time; i++) {
1781
Sumant Patroe4a082c2006-05-30 12:03:37 -07001782 int outstanding = atomic_read(&instance->fw_outstanding);
1783
1784 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001785 break;
1786
1787 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1788 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001789 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001790 /*
1791 * Call cmd completion routine. Cmd to be
1792 * be completed directly without depending on isr.
1793 */
1794 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001795 }
1796
1797 msleep(1000);
1798 }
1799
Yang, Bo707e09b2010-10-12 07:20:27 -06001800 i = 0;
1801 kill_adapter_flag = 0;
1802 do {
1803 fw_state = instance->instancet->read_fw_status_reg(
1804 instance->reg_set) & MFI_STATE_MASK;
1805 if ((fw_state == MFI_STATE_FAULT) &&
1806 (instance->disableOnlineCtrlReset == 0)) {
1807 if (i == 3) {
1808 kill_adapter_flag = 2;
1809 break;
1810 }
1811 megasas_do_ocr(instance);
1812 kill_adapter_flag = 1;
1813
1814 /* wait for 1 secs to let FW finish the pending cmds */
1815 msleep(1000);
1816 }
1817 i++;
1818 } while (i <= 3);
1819
1820 if (atomic_read(&instance->fw_outstanding) &&
1821 !kill_adapter_flag) {
1822 if (instance->disableOnlineCtrlReset == 0) {
1823
1824 megasas_do_ocr(instance);
1825
1826 /* wait for 5 secs to let FW finish the pending cmds */
1827 for (i = 0; i < wait_time; i++) {
1828 int outstanding =
1829 atomic_read(&instance->fw_outstanding);
1830 if (!outstanding)
1831 return SUCCESS;
1832 msleep(1000);
1833 }
1834 }
1835 }
1836
1837 if (atomic_read(&instance->fw_outstanding) ||
1838 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001839 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001840 /*
1841 * Send signal to FW to stop processing any pending cmds.
1842 * The controller will be taken offline by the OS now.
1843 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001844 if ((instance->pdev->device ==
1845 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1846 (instance->pdev->device ==
1847 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1848 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001849 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001850 } else {
1851 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001852 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001853 }
Sumant Patro658dced2006-10-03 13:09:14 -07001854 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001855 spin_lock_irqsave(&instance->hba_lock, flags);
1856 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1857 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001858 return FAILED;
1859 }
1860
bo yang39a98552010-09-22 22:36:29 -04001861 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1862
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001863 return SUCCESS;
1864}
1865
1866/**
1867 * megasas_generic_reset - Generic reset routine
1868 * @scmd: Mid-layer SCSI command
1869 *
1870 * This routine implements a generic reset handler for device, bus and host
1871 * reset requests. Device, bus and host specific reset handlers can use this
1872 * function after they do their specific tasks.
1873 */
1874static int megasas_generic_reset(struct scsi_cmnd *scmd)
1875{
1876 int ret_val;
1877 struct megasas_instance *instance;
1878
1879 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1880
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001881 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1882 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001883
bo yang39a98552010-09-22 22:36:29 -04001884 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001885 printk(KERN_ERR "megasas: cannot recover from previous reset "
1886 "failures\n");
1887 return FAILED;
1888 }
1889
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001890 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001891 if (ret_val == SUCCESS)
1892 printk(KERN_NOTICE "megasas: reset successful \n");
1893 else
1894 printk(KERN_ERR "megasas: failed to do reset\n");
1895
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001896 return ret_val;
1897}
1898
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001899/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001900 * megasas_reset_timer - quiesce the adapter if required
1901 * @scmd: scsi cmnd
1902 *
1903 * Sets the FW busy flag and reduces the host->can_queue if the
1904 * cmd has not been completed within the timeout period.
1905 */
1906static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001907blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001908{
1909 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1910 struct megasas_instance *instance;
1911 unsigned long flags;
1912
1913 if (time_after(jiffies, scmd->jiffies_at_alloc +
1914 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001915 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001916 }
1917
1918 instance = cmd->instance;
1919 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1920 /* FW is busy, throttle IO */
1921 spin_lock_irqsave(instance->host->host_lock, flags);
1922
1923 instance->host->can_queue = 16;
1924 instance->last_time = jiffies;
1925 instance->flag |= MEGASAS_FW_BUSY;
1926
1927 spin_unlock_irqrestore(instance->host->host_lock, flags);
1928 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001929 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001930}
1931
1932/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001933 * megasas_reset_device - Device reset handler entry point
1934 */
1935static int megasas_reset_device(struct scsi_cmnd *scmd)
1936{
1937 int ret;
1938
1939 /*
1940 * First wait for all commands to complete
1941 */
1942 ret = megasas_generic_reset(scmd);
1943
1944 return ret;
1945}
1946
1947/**
1948 * megasas_reset_bus_host - Bus & host reset handler entry point
1949 */
1950static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1951{
1952 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001953 struct megasas_instance *instance;
1954 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001955
1956 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001957 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001958 */
adam radford9c915a82010-12-21 13:34:31 -08001959 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
1960 ret = megasas_reset_fusion(scmd->device->host);
1961 else
1962 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001963
1964 return ret;
1965}
1966
1967/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001968 * megasas_bios_param - Returns disk geometry for a disk
1969 * @sdev: device handle
1970 * @bdev: block device
1971 * @capacity: drive capacity
1972 * @geom: geometry parameters
1973 */
1974static int
1975megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1976 sector_t capacity, int geom[])
1977{
1978 int heads;
1979 int sectors;
1980 sector_t cylinders;
1981 unsigned long tmp;
1982 /* Default heads (64) & sectors (32) */
1983 heads = 64;
1984 sectors = 32;
1985
1986 tmp = heads * sectors;
1987 cylinders = capacity;
1988
1989 sector_div(cylinders, tmp);
1990
1991 /*
1992 * Handle extended translation size for logical drives > 1Gb
1993 */
1994
1995 if (capacity >= 0x200000) {
1996 heads = 255;
1997 sectors = 63;
1998 tmp = heads*sectors;
1999 cylinders = capacity;
2000 sector_div(cylinders, tmp);
2001 }
2002
2003 geom[0] = heads;
2004 geom[1] = sectors;
2005 geom[2] = cylinders;
2006
2007 return 0;
2008}
2009
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002010static void megasas_aen_polling(struct work_struct *work);
2011
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002012/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002013 * megasas_service_aen - Processes an event notification
2014 * @instance: Adapter soft state
2015 * @cmd: AEN command completed by the ISR
2016 *
2017 * For AEN, driver sends a command down to FW that is held by the FW till an
2018 * event occurs. When an event of interest occurs, FW completes the command
2019 * that it was previously holding.
2020 *
2021 * This routines sends SIGIO signal to processes that have registered with the
2022 * driver for AEN.
2023 */
2024static void
2025megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2026{
Yang, Boc3518832009-10-06 14:18:02 -06002027 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002028 /*
2029 * Don't signal app if it is just an aborted previously registered aen
2030 */
Yang, Boc3518832009-10-06 14:18:02 -06002031 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2032 spin_lock_irqsave(&poll_aen_lock, flags);
2033 megasas_poll_wait_aen = 1;
2034 spin_unlock_irqrestore(&poll_aen_lock, flags);
2035 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002036 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002037 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002038 else
2039 cmd->abort_aen = 0;
2040
2041 instance->aen_cmd = NULL;
2042 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002043
bo yang39a98552010-09-22 22:36:29 -04002044 if ((instance->unload == 0) &&
2045 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002046 struct megasas_aen_event *ev;
2047 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2048 if (!ev) {
2049 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2050 } else {
2051 ev->instance = instance;
2052 instance->ev = ev;
2053 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2054 schedule_delayed_work(
2055 (struct delayed_work *)&ev->hotplug_work, 0);
2056 }
2057 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002058}
2059
2060/*
2061 * Scsi host template for megaraid_sas driver
2062 */
2063static struct scsi_host_template megasas_template = {
2064
2065 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002066 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002067 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002068 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002069 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002070 .queuecommand = megasas_queue_command,
2071 .eh_device_reset_handler = megasas_reset_device,
2072 .eh_bus_reset_handler = megasas_reset_bus_host,
2073 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002074 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002075 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002076 .use_clustering = ENABLE_CLUSTERING,
2077};
2078
2079/**
2080 * megasas_complete_int_cmd - Completes an internal command
2081 * @instance: Adapter soft state
2082 * @cmd: Command to be completed
2083 *
2084 * The megasas_issue_blocked_cmd() function waits for a command to complete
2085 * after it issues a command. This function wakes up that waiting routine by
2086 * calling wake_up() on the wait queue.
2087 */
2088static void
2089megasas_complete_int_cmd(struct megasas_instance *instance,
2090 struct megasas_cmd *cmd)
2091{
2092 cmd->cmd_status = cmd->frame->io.cmd_status;
2093
2094 if (cmd->cmd_status == ENODATA) {
2095 cmd->cmd_status = 0;
2096 }
2097 wake_up(&instance->int_cmd_wait_q);
2098}
2099
2100/**
2101 * megasas_complete_abort - Completes aborting a command
2102 * @instance: Adapter soft state
2103 * @cmd: Cmd that was issued to abort another cmd
2104 *
adam radford0d490162010-12-14 19:17:17 -08002105 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2106 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002107 * wakes up all functions waiting on the same wait queue.
2108 */
2109static void
2110megasas_complete_abort(struct megasas_instance *instance,
2111 struct megasas_cmd *cmd)
2112{
2113 if (cmd->sync_cmd) {
2114 cmd->sync_cmd = 0;
2115 cmd->cmd_status = 0;
2116 wake_up(&instance->abort_cmd_wait_q);
2117 }
2118
2119 return;
2120}
2121
2122/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002123 * megasas_complete_cmd - Completes a command
2124 * @instance: Adapter soft state
2125 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002126 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002127 * SCSI mid-layer instead of the value returned
2128 * by the FW. This should be used if caller wants
2129 * an alternate status (as in the case of aborted
2130 * commands)
2131 */
adam radford9c915a82010-12-21 13:34:31 -08002132void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002133megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2134 u8 alt_status)
2135{
2136 int exception = 0;
2137 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002138 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002139 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002140
bo yang39a98552010-09-22 22:36:29 -04002141 /* flag for the retry reset */
2142 cmd->retry_for_fw_reset = 0;
2143
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002144 if (cmd->scmd)
2145 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002146
2147 switch (hdr->cmd) {
2148
2149 case MFI_CMD_PD_SCSI_IO:
2150 case MFI_CMD_LD_SCSI_IO:
2151
2152 /*
2153 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2154 * issued either through an IO path or an IOCTL path. If it
2155 * was via IOCTL, we will send it to internal completion.
2156 */
2157 if (cmd->sync_cmd) {
2158 cmd->sync_cmd = 0;
2159 megasas_complete_int_cmd(instance, cmd);
2160 break;
2161 }
2162
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002163 case MFI_CMD_LD_READ:
2164 case MFI_CMD_LD_WRITE:
2165
2166 if (alt_status) {
2167 cmd->scmd->result = alt_status << 16;
2168 exception = 1;
2169 }
2170
2171 if (exception) {
2172
Sumant Patroe4a082c2006-05-30 12:03:37 -07002173 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002174
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002175 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002176 cmd->scmd->scsi_done(cmd->scmd);
2177 megasas_return_cmd(instance, cmd);
2178
2179 break;
2180 }
2181
2182 switch (hdr->cmd_status) {
2183
2184 case MFI_STAT_OK:
2185 cmd->scmd->result = DID_OK << 16;
2186 break;
2187
2188 case MFI_STAT_SCSI_IO_FAILED:
2189 case MFI_STAT_LD_INIT_IN_PROGRESS:
2190 cmd->scmd->result =
2191 (DID_ERROR << 16) | hdr->scsi_status;
2192 break;
2193
2194 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2195
2196 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2197
2198 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2199 memset(cmd->scmd->sense_buffer, 0,
2200 SCSI_SENSE_BUFFERSIZE);
2201 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2202 hdr->sense_len);
2203
2204 cmd->scmd->result |= DRIVER_SENSE << 24;
2205 }
2206
2207 break;
2208
2209 case MFI_STAT_LD_OFFLINE:
2210 case MFI_STAT_DEVICE_NOT_FOUND:
2211 cmd->scmd->result = DID_BAD_TARGET << 16;
2212 break;
2213
2214 default:
2215 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2216 hdr->cmd_status);
2217 cmd->scmd->result = DID_ERROR << 16;
2218 break;
2219 }
2220
Sumant Patroe4a082c2006-05-30 12:03:37 -07002221 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002222
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002223 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002224 cmd->scmd->scsi_done(cmd->scmd);
2225 megasas_return_cmd(instance, cmd);
2226
2227 break;
2228
2229 case MFI_CMD_SMP:
2230 case MFI_CMD_STP:
2231 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002232 /* Check for LD map update */
2233 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2234 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2235 spin_lock_irqsave(instance->host->host_lock, flags);
2236 if (cmd->frame->hdr.cmd_status != 0) {
2237 if (cmd->frame->hdr.cmd_status !=
2238 MFI_STAT_NOT_FOUND)
2239 printk(KERN_WARNING "megasas: map sync"
2240 "failed, status = 0x%x.\n",
2241 cmd->frame->hdr.cmd_status);
2242 else {
2243 megasas_return_cmd(instance, cmd);
2244 spin_unlock_irqrestore(
2245 instance->host->host_lock,
2246 flags);
2247 break;
2248 }
2249 } else
2250 instance->map_id++;
2251 megasas_return_cmd(instance, cmd);
2252 if (MR_ValidateMapInfo(
2253 fusion->ld_map[(instance->map_id & 1)],
2254 fusion->load_balance_info))
2255 fusion->fast_path_io = 1;
2256 else
2257 fusion->fast_path_io = 0;
2258 megasas_sync_map_info(instance);
2259 spin_unlock_irqrestore(instance->host->host_lock,
2260 flags);
2261 break;
2262 }
Yang, Boc3518832009-10-06 14:18:02 -06002263 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2264 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2265 spin_lock_irqsave(&poll_aen_lock, flags);
2266 megasas_poll_wait_aen = 0;
2267 spin_unlock_irqrestore(&poll_aen_lock, flags);
2268 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002269
2270 /*
2271 * See if got an event notification
2272 */
2273 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2274 megasas_service_aen(instance, cmd);
2275 else
2276 megasas_complete_int_cmd(instance, cmd);
2277
2278 break;
2279
2280 case MFI_CMD_ABORT:
2281 /*
2282 * Cmd issued to abort another cmd returned
2283 */
2284 megasas_complete_abort(instance, cmd);
2285 break;
2286
2287 default:
2288 printk("megasas: Unknown command completed! [0x%X]\n",
2289 hdr->cmd);
2290 break;
2291 }
2292}
2293
2294/**
bo yang39a98552010-09-22 22:36:29 -04002295 * megasas_issue_pending_cmds_again - issue all pending cmds
2296 * in FW again because of the fw reset
2297 * @instance: Adapter soft state
2298 */
2299static inline void
2300megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2301{
2302 struct megasas_cmd *cmd;
2303 struct list_head clist_local;
2304 union megasas_evt_class_locale class_locale;
2305 unsigned long flags;
2306 u32 seq_num;
2307
2308 INIT_LIST_HEAD(&clist_local);
2309 spin_lock_irqsave(&instance->hba_lock, flags);
2310 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2311 spin_unlock_irqrestore(&instance->hba_lock, flags);
2312
2313 while (!list_empty(&clist_local)) {
2314 cmd = list_entry((&clist_local)->next,
2315 struct megasas_cmd, list);
2316 list_del_init(&cmd->list);
2317
2318 if (cmd->sync_cmd || cmd->scmd) {
2319 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2320 "detected to be pending while HBA reset.\n",
2321 cmd, cmd->scmd, cmd->sync_cmd);
2322
2323 cmd->retry_for_fw_reset++;
2324
2325 if (cmd->retry_for_fw_reset == 3) {
2326 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2327 "was tried multiple times during reset."
2328 "Shutting down the HBA\n",
2329 cmd, cmd->scmd, cmd->sync_cmd);
2330 megaraid_sas_kill_hba(instance);
2331
2332 instance->adprecovery =
2333 MEGASAS_HW_CRITICAL_ERROR;
2334 return;
2335 }
2336 }
2337
2338 if (cmd->sync_cmd == 1) {
2339 if (cmd->scmd) {
2340 printk(KERN_NOTICE "megaraid_sas: unexpected"
2341 "cmd attached to internal command!\n");
2342 }
2343 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2344 "on the internal reset queue,"
2345 "issue it again.\n", cmd);
2346 cmd->cmd_status = ENODATA;
2347 instance->instancet->fire_cmd(instance,
2348 cmd->frame_phys_addr ,
2349 0, instance->reg_set);
2350 } else if (cmd->scmd) {
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002351 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04002352 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002353 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002354
2355 atomic_inc(&instance->fw_outstanding);
2356 instance->instancet->fire_cmd(instance,
2357 cmd->frame_phys_addr,
2358 cmd->frame_count-1, instance->reg_set);
2359 } else {
2360 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2361 "internal reset defer list while re-issue!!\n",
2362 cmd);
2363 }
2364 }
2365
2366 if (instance->aen_cmd) {
2367 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2368 megasas_return_cmd(instance, instance->aen_cmd);
2369
2370 instance->aen_cmd = NULL;
2371 }
2372
2373 /*
2374 * Initiate AEN (Asynchronous Event Notification)
2375 */
2376 seq_num = instance->last_seq_num;
2377 class_locale.members.reserved = 0;
2378 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2379 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2380
2381 megasas_register_aen(instance, seq_num, class_locale.word);
2382}
2383
2384/**
2385 * Move the internal reset pending commands to a deferred queue.
2386 *
2387 * We move the commands pending at internal reset time to a
2388 * pending queue. This queue would be flushed after successful
2389 * completion of the internal reset sequence. if the internal reset
2390 * did not complete in time, the kernel reset handler would flush
2391 * these commands.
2392 **/
2393static void
2394megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2395{
2396 struct megasas_cmd *cmd;
2397 int i;
2398 u32 max_cmd = instance->max_fw_cmds;
2399 u32 defer_index;
2400 unsigned long flags;
2401
2402 defer_index = 0;
2403 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2404 for (i = 0; i < max_cmd; i++) {
2405 cmd = instance->cmd_list[i];
2406 if (cmd->sync_cmd == 1 || cmd->scmd) {
2407 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2408 "on the defer queue as internal\n",
2409 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2410
2411 if (!list_empty(&cmd->list)) {
2412 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2413 " moving this cmd:%p, %d %p, it was"
2414 "discovered on some list?\n",
2415 cmd, cmd->sync_cmd, cmd->scmd);
2416
2417 list_del_init(&cmd->list);
2418 }
2419 defer_index++;
2420 list_add_tail(&cmd->list,
2421 &instance->internal_reset_pending_q);
2422 }
2423 }
2424 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2425}
2426
2427
2428static void
2429process_fw_state_change_wq(struct work_struct *work)
2430{
2431 struct megasas_instance *instance =
2432 container_of(work, struct megasas_instance, work_init);
2433 u32 wait;
2434 unsigned long flags;
2435
2436 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2437 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2438 instance->adprecovery);
2439 return ;
2440 }
2441
2442 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2443 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2444 "state, restarting it...\n");
2445
2446 instance->instancet->disable_intr(instance->reg_set);
2447 atomic_set(&instance->fw_outstanding, 0);
2448
2449 atomic_set(&instance->fw_reset_no_pci_access, 1);
2450 instance->instancet->adp_reset(instance, instance->reg_set);
2451 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2452
2453 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2454 "initiating next stage...\n");
2455
2456 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2457 "state 2 starting...\n");
2458
2459 /*waitting for about 20 second before start the second init*/
2460 for (wait = 0; wait < 30; wait++) {
2461 msleep(1000);
2462 }
2463
2464 if (megasas_transition_to_ready(instance)) {
2465 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2466
2467 megaraid_sas_kill_hba(instance);
2468 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2469 return ;
2470 }
2471
2472 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2473 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2474 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2475 ) {
2476 *instance->consumer = *instance->producer;
2477 } else {
2478 *instance->consumer = 0;
2479 *instance->producer = 0;
2480 }
2481
2482 megasas_issue_init_mfi(instance);
2483
2484 spin_lock_irqsave(&instance->hba_lock, flags);
2485 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2486 spin_unlock_irqrestore(&instance->hba_lock, flags);
2487 instance->instancet->enable_intr(instance->reg_set);
2488
2489 megasas_issue_pending_cmds_again(instance);
2490 instance->issuepend_done = 1;
2491 }
2492 return ;
2493}
2494
2495/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002496 * megasas_deplete_reply_queue - Processes all completed commands
2497 * @instance: Adapter soft state
2498 * @alt_status: Alternate status to be returned to
2499 * SCSI mid-layer instead of the status
2500 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002501 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002502 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002503static int
bo yang39a98552010-09-22 22:36:29 -04002504megasas_deplete_reply_queue(struct megasas_instance *instance,
2505 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002506{
bo yang39a98552010-09-22 22:36:29 -04002507 u32 mfiStatus;
2508 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002509
bo yang39a98552010-09-22 22:36:29 -04002510 if ((mfiStatus = instance->instancet->check_reset(instance,
2511 instance->reg_set)) == 1) {
2512 return IRQ_HANDLED;
2513 }
2514
2515 if ((mfiStatus = instance->instancet->clear_intr(
2516 instance->reg_set)
2517 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002518 /* Hardware may not set outbound_intr_status in MSI-X mode */
2519 if (!instance->msi_flag)
2520 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002521 }
2522
2523 instance->mfiStatus = mfiStatus;
2524
2525 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2526 fw_state = instance->instancet->read_fw_status_reg(
2527 instance->reg_set) & MFI_STATE_MASK;
2528
2529 if (fw_state != MFI_STATE_FAULT) {
2530 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2531 fw_state);
2532 }
2533
2534 if ((fw_state == MFI_STATE_FAULT) &&
2535 (instance->disableOnlineCtrlReset == 0)) {
2536 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2537
2538 if ((instance->pdev->device ==
2539 PCI_DEVICE_ID_LSI_SAS1064R) ||
2540 (instance->pdev->device ==
2541 PCI_DEVICE_ID_DELL_PERC5) ||
2542 (instance->pdev->device ==
2543 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2544
2545 *instance->consumer =
2546 MEGASAS_ADPRESET_INPROG_SIGN;
2547 }
2548
2549
2550 instance->instancet->disable_intr(instance->reg_set);
2551 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2552 instance->issuepend_done = 0;
2553
2554 atomic_set(&instance->fw_outstanding, 0);
2555 megasas_internal_reset_defer_cmds(instance);
2556
2557 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2558 fw_state, instance->adprecovery);
2559
2560 schedule_work(&instance->work_init);
2561 return IRQ_HANDLED;
2562
2563 } else {
2564 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2565 fw_state, instance->disableOnlineCtrlReset);
2566 }
2567 }
2568
Sumant Patro5d018ad2006-10-03 13:13:18 -07002569 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002570 return IRQ_HANDLED;
2571}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002572/**
2573 * megasas_isr - isr entry point
2574 */
David Howells7d12e782006-10-05 14:55:46 +01002575static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002576{
bo yang39a98552010-09-22 22:36:29 -04002577 struct megasas_instance *instance;
2578 unsigned long flags;
2579 irqreturn_t rc;
2580
2581 if (atomic_read(
2582 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2583 return IRQ_HANDLED;
2584
2585 instance = (struct megasas_instance *)devp;
2586
2587 spin_lock_irqsave(&instance->hba_lock, flags);
2588 rc = megasas_deplete_reply_queue(instance, DID_OK);
2589 spin_unlock_irqrestore(&instance->hba_lock, flags);
2590
2591 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002592}
2593
2594/**
2595 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002596 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002597 *
2598 * During the initialization, FW passes can potentially be in any one of
2599 * several possible states. If the FW in operational, waiting-for-handshake
2600 * states, driver must take steps to bring it to ready state. Otherwise, it
2601 * has to wait for the ready state.
2602 */
adam radford9c915a82010-12-21 13:34:31 -08002603int
Sumant Patro1341c932006-01-25 12:02:40 -08002604megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002605{
2606 int i;
2607 u8 max_wait;
2608 u32 fw_state;
2609 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002610 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002611
Sumant Patro1341c932006-01-25 12:02:40 -08002612 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002613
Sumant Patroe3bbff92006-10-03 12:28:49 -07002614 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002615 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2616 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002617
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002618 while (fw_state != MFI_STATE_READY) {
2619
Yang, Bo7218df62009-10-06 14:52:20 -06002620 abs_state =
2621 instance->instancet->read_fw_status_reg(instance->reg_set);
2622
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002623 switch (fw_state) {
2624
2625 case MFI_STATE_FAULT:
2626
2627 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford1ac515e2011-02-24 20:57:04 -08002628 max_wait = MEGASAS_RESET_WAIT_TIME;
2629 cur_state = MFI_STATE_FAULT;
2630 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002631
2632 case MFI_STATE_WAIT_HANDSHAKE:
2633 /*
2634 * Set the CLR bit in inbound doorbell
2635 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002636 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002637 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2638 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002639 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2640 (instance->pdev->device ==
2641 PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002642 writel(
2643 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002644 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002645 } else {
2646 writel(
2647 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2648 &instance->reg_set->inbound_doorbell);
2649 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002650
Yang, Bo7218df62009-10-06 14:52:20 -06002651 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002652 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2653 break;
2654
Sumant Patroe3bbff92006-10-03 12:28:49 -07002655 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002656 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002657 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2658 (instance->pdev->device ==
2659 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2660 (instance->pdev->device ==
2661 PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002662 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002663 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002664 } else
2665 writel(MFI_INIT_HOTPLUG,
2666 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002667
Yang, Bo7218df62009-10-06 14:52:20 -06002668 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002669 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2670 break;
2671
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002672 case MFI_STATE_OPERATIONAL:
2673 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002674 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002675 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002676 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002677 if ((instance->pdev->device ==
2678 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2679 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002680 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2681 (instance->pdev->device
2682 == PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002683 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002684 &instance->reg_set->doorbell);
2685 if (instance->pdev->device ==
2686 PCI_DEVICE_ID_LSI_FUSION) {
2687 for (i = 0; i < (10 * 1000); i += 20) {
2688 if (readl(
2689 &instance->
2690 reg_set->
2691 doorbell) & 1)
2692 msleep(20);
2693 else
2694 break;
2695 }
2696 }
Yang, Bo87911122009-10-06 14:31:54 -06002697 } else
2698 writel(MFI_RESET_FLAGS,
2699 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002700
Yang, Bo7218df62009-10-06 14:52:20 -06002701 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002702 cur_state = MFI_STATE_OPERATIONAL;
2703 break;
2704
2705 case MFI_STATE_UNDEFINED:
2706 /*
2707 * This state should not last for more than 2 seconds
2708 */
Yang, Bo7218df62009-10-06 14:52:20 -06002709 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002710 cur_state = MFI_STATE_UNDEFINED;
2711 break;
2712
2713 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002714 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002715 cur_state = MFI_STATE_BB_INIT;
2716 break;
2717
2718 case MFI_STATE_FW_INIT:
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_FW_INIT;
2721 break;
2722
2723 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002724 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002725 cur_state = MFI_STATE_FW_INIT_2;
2726 break;
2727
2728 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002729 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002730 cur_state = MFI_STATE_DEVICE_SCAN;
2731 break;
2732
2733 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002734 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002735 cur_state = MFI_STATE_FLUSH_CACHE;
2736 break;
2737
2738 default:
2739 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2740 fw_state);
2741 return -ENODEV;
2742 }
2743
2744 /*
2745 * The cur_state should not last for more than max_wait secs
2746 */
2747 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002748 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002749 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002750 curr_abs_state =
2751 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002752
Yang, Bo7218df62009-10-06 14:52:20 -06002753 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002754 msleep(1);
2755 } else
2756 break;
2757 }
2758
2759 /*
2760 * Return error if fw_state hasn't changed after max_wait
2761 */
Yang, Bo7218df62009-10-06 14:52:20 -06002762 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002763 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2764 "in %d secs\n", fw_state, max_wait);
2765 return -ENODEV;
2766 }
bo yang39a98552010-09-22 22:36:29 -04002767 }
adam radford0d490162010-12-14 19:17:17 -08002768 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002769
2770 return 0;
2771}
2772
2773/**
2774 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2775 * @instance: Adapter soft state
2776 */
2777static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2778{
2779 int i;
adam radford9c915a82010-12-21 13:34:31 -08002780 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002781 struct megasas_cmd *cmd;
2782
2783 if (!instance->frame_dma_pool)
2784 return;
2785
2786 /*
2787 * Return all frames to pool
2788 */
2789 for (i = 0; i < max_cmd; i++) {
2790
2791 cmd = instance->cmd_list[i];
2792
2793 if (cmd->frame)
2794 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2795 cmd->frame_phys_addr);
2796
2797 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002798 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799 cmd->sense_phys_addr);
2800 }
2801
2802 /*
2803 * Now destroy the pool itself
2804 */
2805 pci_pool_destroy(instance->frame_dma_pool);
2806 pci_pool_destroy(instance->sense_dma_pool);
2807
2808 instance->frame_dma_pool = NULL;
2809 instance->sense_dma_pool = NULL;
2810}
2811
2812/**
2813 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2814 * @instance: Adapter soft state
2815 *
2816 * Each command packet has an embedded DMA memory buffer that is used for
2817 * filling MFI frame and the SG list that immediately follows the frame. This
2818 * function creates those DMA memory buffers for each command packet by using
2819 * PCI pool facility.
2820 */
2821static int megasas_create_frame_pool(struct megasas_instance *instance)
2822{
2823 int i;
2824 u32 max_cmd;
2825 u32 sge_sz;
2826 u32 sgl_sz;
2827 u32 total_sz;
2828 u32 frame_count;
2829 struct megasas_cmd *cmd;
2830
adam radford9c915a82010-12-21 13:34:31 -08002831 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002832
2833 /*
2834 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2835 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2836 */
2837 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2838 sizeof(struct megasas_sge32);
2839
Yang, Bof4c9a132009-10-06 14:43:28 -06002840 if (instance->flag_ieee) {
2841 sge_sz = sizeof(struct megasas_sge_skinny);
2842 }
2843
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002844 /*
2845 * Calculated the number of 64byte frames required for SGL
2846 */
2847 sgl_sz = sge_sz * instance->max_num_sge;
2848 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002849 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002850
2851 /*
2852 * We need one extra frame for the MFI command
2853 */
2854 frame_count++;
2855
2856 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2857 /*
2858 * Use DMA pool facility provided by PCI layer
2859 */
2860 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2861 instance->pdev, total_sz, 64,
2862 0);
2863
2864 if (!instance->frame_dma_pool) {
2865 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2866 return -ENOMEM;
2867 }
2868
2869 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2870 instance->pdev, 128, 4, 0);
2871
2872 if (!instance->sense_dma_pool) {
2873 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2874
2875 pci_pool_destroy(instance->frame_dma_pool);
2876 instance->frame_dma_pool = NULL;
2877
2878 return -ENOMEM;
2879 }
2880
2881 /*
2882 * Allocate and attach a frame to each of the commands in cmd_list.
2883 * By making cmd->index as the context instead of the &cmd, we can
2884 * always use 32bit context regardless of the architecture
2885 */
2886 for (i = 0; i < max_cmd; i++) {
2887
2888 cmd = instance->cmd_list[i];
2889
2890 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2891 GFP_KERNEL, &cmd->frame_phys_addr);
2892
2893 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2894 GFP_KERNEL, &cmd->sense_phys_addr);
2895
2896 /*
2897 * megasas_teardown_frame_pool() takes care of freeing
2898 * whatever has been allocated
2899 */
2900 if (!cmd->frame || !cmd->sense) {
2901 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2902 megasas_teardown_frame_pool(instance);
2903 return -ENOMEM;
2904 }
2905
Yang, Bo707e09b2010-10-12 07:20:27 -06002906 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002907 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002908 cmd->frame->io.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002909 }
2910
2911 return 0;
2912}
2913
2914/**
2915 * megasas_free_cmds - Free all the cmds in the free cmd pool
2916 * @instance: Adapter soft state
2917 */
adam radford9c915a82010-12-21 13:34:31 -08002918void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002919{
2920 int i;
2921 /* First free the MFI frame pool */
2922 megasas_teardown_frame_pool(instance);
2923
2924 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002925 for (i = 0; i < instance->max_mfi_cmds; i++)
2926
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002927 kfree(instance->cmd_list[i]);
2928
2929 /* Free the cmd_list buffer itself */
2930 kfree(instance->cmd_list);
2931 instance->cmd_list = NULL;
2932
2933 INIT_LIST_HEAD(&instance->cmd_pool);
2934}
2935
2936/**
2937 * megasas_alloc_cmds - Allocates the command packets
2938 * @instance: Adapter soft state
2939 *
2940 * Each command that is issued to the FW, whether IO commands from the OS or
2941 * internal commands like IOCTLs, are wrapped in local data structure called
2942 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2943 * the FW.
2944 *
2945 * Each frame has a 32-bit field called context (tag). This context is used
2946 * to get back the megasas_cmd from the frame when a frame gets completed in
2947 * the ISR. Typically the address of the megasas_cmd itself would be used as
2948 * the context. But we wanted to keep the differences between 32 and 64 bit
2949 * systems to the mininum. We always use 32 bit integers for the context. In
2950 * this driver, the 32 bit values are the indices into an array cmd_list.
2951 * This array is used only to look up the megasas_cmd given the context. The
2952 * free commands themselves are maintained in a linked list called cmd_pool.
2953 */
adam radford9c915a82010-12-21 13:34:31 -08002954int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002955{
2956 int i;
2957 int j;
2958 u32 max_cmd;
2959 struct megasas_cmd *cmd;
2960
adam radford9c915a82010-12-21 13:34:31 -08002961 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002962
2963 /*
2964 * instance->cmd_list is an array of struct megasas_cmd pointers.
2965 * Allocate the dynamic array first and then allocate individual
2966 * commands.
2967 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002968 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002969
2970 if (!instance->cmd_list) {
2971 printk(KERN_DEBUG "megasas: out of memory\n");
2972 return -ENOMEM;
2973 }
2974
adam radford9c915a82010-12-21 13:34:31 -08002975 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002976
2977 for (i = 0; i < max_cmd; i++) {
2978 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2979 GFP_KERNEL);
2980
2981 if (!instance->cmd_list[i]) {
2982
2983 for (j = 0; j < i; j++)
2984 kfree(instance->cmd_list[j]);
2985
2986 kfree(instance->cmd_list);
2987 instance->cmd_list = NULL;
2988
2989 return -ENOMEM;
2990 }
2991 }
2992
2993 /*
2994 * Add all the commands to command pool (instance->cmd_pool)
2995 */
2996 for (i = 0; i < max_cmd; i++) {
2997 cmd = instance->cmd_list[i];
2998 memset(cmd, 0, sizeof(struct megasas_cmd));
2999 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003000 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003001 cmd->instance = instance;
3002
3003 list_add_tail(&cmd->list, &instance->cmd_pool);
3004 }
3005
3006 /*
3007 * Create a frame pool and assign one frame to each cmd
3008 */
3009 if (megasas_create_frame_pool(instance)) {
3010 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3011 megasas_free_cmds(instance);
3012 }
3013
3014 return 0;
3015}
3016
Yang, Bo81e403c2009-10-06 14:27:54 -06003017/*
3018 * megasas_get_pd_list_info - Returns FW's pd_list structure
3019 * @instance: Adapter soft state
3020 * @pd_list: pd_list structure
3021 *
3022 * Issues an internal command (DCMD) to get the FW's controller PD
3023 * list structure. This information is mainly used to find out SYSTEM
3024 * supported by the FW.
3025 */
3026static int
3027megasas_get_pd_list(struct megasas_instance *instance)
3028{
3029 int ret = 0, pd_index = 0;
3030 struct megasas_cmd *cmd;
3031 struct megasas_dcmd_frame *dcmd;
3032 struct MR_PD_LIST *ci;
3033 struct MR_PD_ADDRESS *pd_addr;
3034 dma_addr_t ci_h = 0;
3035
3036 cmd = megasas_get_cmd(instance);
3037
3038 if (!cmd) {
3039 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3040 return -ENOMEM;
3041 }
3042
3043 dcmd = &cmd->frame->dcmd;
3044
3045 ci = pci_alloc_consistent(instance->pdev,
3046 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3047
3048 if (!ci) {
3049 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3050 megasas_return_cmd(instance, cmd);
3051 return -ENOMEM;
3052 }
3053
3054 memset(ci, 0, sizeof(*ci));
3055 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3056
3057 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3058 dcmd->mbox.b[1] = 0;
3059 dcmd->cmd = MFI_CMD_DCMD;
3060 dcmd->cmd_status = 0xFF;
3061 dcmd->sge_count = 1;
3062 dcmd->flags = MFI_FRAME_DIR_READ;
3063 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003064 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003065 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3066 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3067 dcmd->sgl.sge32[0].phys_addr = ci_h;
3068 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3069
3070 if (!megasas_issue_polled(instance, cmd)) {
3071 ret = 0;
3072 } else {
3073 ret = -1;
3074 }
3075
3076 /*
3077 * the following function will get the instance PD LIST.
3078 */
3079
3080 pd_addr = ci->addr;
3081
3082 if ( ret == 0 &&
3083 (ci->count <
3084 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3085
3086 memset(instance->pd_list, 0,
3087 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3088
3089 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3090
3091 instance->pd_list[pd_addr->deviceId].tid =
3092 pd_addr->deviceId;
3093 instance->pd_list[pd_addr->deviceId].driveType =
3094 pd_addr->scsiDevType;
3095 instance->pd_list[pd_addr->deviceId].driveState =
3096 MR_PD_STATE_SYSTEM;
3097 pd_addr++;
3098 }
3099 }
3100
3101 pci_free_consistent(instance->pdev,
3102 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3103 ci, ci_h);
3104 megasas_return_cmd(instance, cmd);
3105
3106 return ret;
3107}
3108
Yang, Bobdc6fb82009-12-06 08:30:19 -07003109/*
3110 * megasas_get_ld_list_info - Returns FW's ld_list structure
3111 * @instance: Adapter soft state
3112 * @ld_list: ld_list structure
3113 *
3114 * Issues an internal command (DCMD) to get the FW's controller PD
3115 * list structure. This information is mainly used to find out SYSTEM
3116 * supported by the FW.
3117 */
3118static int
3119megasas_get_ld_list(struct megasas_instance *instance)
3120{
3121 int ret = 0, ld_index = 0, ids = 0;
3122 struct megasas_cmd *cmd;
3123 struct megasas_dcmd_frame *dcmd;
3124 struct MR_LD_LIST *ci;
3125 dma_addr_t ci_h = 0;
3126
3127 cmd = megasas_get_cmd(instance);
3128
3129 if (!cmd) {
3130 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3131 return -ENOMEM;
3132 }
3133
3134 dcmd = &cmd->frame->dcmd;
3135
3136 ci = pci_alloc_consistent(instance->pdev,
3137 sizeof(struct MR_LD_LIST),
3138 &ci_h);
3139
3140 if (!ci) {
3141 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3142 megasas_return_cmd(instance, cmd);
3143 return -ENOMEM;
3144 }
3145
3146 memset(ci, 0, sizeof(*ci));
3147 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3148
3149 dcmd->cmd = MFI_CMD_DCMD;
3150 dcmd->cmd_status = 0xFF;
3151 dcmd->sge_count = 1;
3152 dcmd->flags = MFI_FRAME_DIR_READ;
3153 dcmd->timeout = 0;
3154 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3155 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3156 dcmd->sgl.sge32[0].phys_addr = ci_h;
3157 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3158 dcmd->pad_0 = 0;
3159
3160 if (!megasas_issue_polled(instance, cmd)) {
3161 ret = 0;
3162 } else {
3163 ret = -1;
3164 }
3165
3166 /* the following function will get the instance PD LIST */
3167
bo yang39a98552010-09-22 22:36:29 -04003168 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003169 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3170
3171 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3172 if (ci->ldList[ld_index].state != 0) {
3173 ids = ci->ldList[ld_index].ref.targetId;
3174 instance->ld_ids[ids] =
3175 ci->ldList[ld_index].ref.targetId;
3176 }
3177 }
3178 }
3179
3180 pci_free_consistent(instance->pdev,
3181 sizeof(struct MR_LD_LIST),
3182 ci,
3183 ci_h);
3184
3185 megasas_return_cmd(instance, cmd);
3186 return ret;
3187}
3188
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003189/**
3190 * megasas_get_controller_info - Returns FW's controller structure
3191 * @instance: Adapter soft state
3192 * @ctrl_info: Controller information structure
3193 *
3194 * Issues an internal command (DCMD) to get the FW's controller structure.
3195 * This information is mainly used to find out the maximum IO transfer per
3196 * command supported by the FW.
3197 */
3198static int
3199megasas_get_ctrl_info(struct megasas_instance *instance,
3200 struct megasas_ctrl_info *ctrl_info)
3201{
3202 int ret = 0;
3203 struct megasas_cmd *cmd;
3204 struct megasas_dcmd_frame *dcmd;
3205 struct megasas_ctrl_info *ci;
3206 dma_addr_t ci_h = 0;
3207
3208 cmd = megasas_get_cmd(instance);
3209
3210 if (!cmd) {
3211 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3212 return -ENOMEM;
3213 }
3214
3215 dcmd = &cmd->frame->dcmd;
3216
3217 ci = pci_alloc_consistent(instance->pdev,
3218 sizeof(struct megasas_ctrl_info), &ci_h);
3219
3220 if (!ci) {
3221 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3222 megasas_return_cmd(instance, cmd);
3223 return -ENOMEM;
3224 }
3225
3226 memset(ci, 0, sizeof(*ci));
3227 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3228
3229 dcmd->cmd = MFI_CMD_DCMD;
3230 dcmd->cmd_status = 0xFF;
3231 dcmd->sge_count = 1;
3232 dcmd->flags = MFI_FRAME_DIR_READ;
3233 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003234 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003235 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3236 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3237 dcmd->sgl.sge32[0].phys_addr = ci_h;
3238 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3239
3240 if (!megasas_issue_polled(instance, cmd)) {
3241 ret = 0;
3242 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3243 } else {
3244 ret = -1;
3245 }
3246
3247 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3248 ci, ci_h);
3249
3250 megasas_return_cmd(instance, cmd);
3251 return ret;
3252}
3253
3254/**
bo yang31ea7082007-11-07 12:09:50 -05003255 * megasas_issue_init_mfi - Initializes the FW
3256 * @instance: Adapter soft state
3257 *
3258 * Issues the INIT MFI cmd
3259 */
3260static int
3261megasas_issue_init_mfi(struct megasas_instance *instance)
3262{
3263 u32 context;
3264
3265 struct megasas_cmd *cmd;
3266
3267 struct megasas_init_frame *init_frame;
3268 struct megasas_init_queue_info *initq_info;
3269 dma_addr_t init_frame_h;
3270 dma_addr_t initq_info_h;
3271
3272 /*
3273 * Prepare a init frame. Note the init frame points to queue info
3274 * structure. Each frame has SGL allocated after first 64 bytes. For
3275 * this frame - since we don't need any SGL - we use SGL's space as
3276 * queue info structure
3277 *
3278 * We will not get a NULL command below. We just created the pool.
3279 */
3280 cmd = megasas_get_cmd(instance);
3281
3282 init_frame = (struct megasas_init_frame *)cmd->frame;
3283 initq_info = (struct megasas_init_queue_info *)
3284 ((unsigned long)init_frame + 64);
3285
3286 init_frame_h = cmd->frame_phys_addr;
3287 initq_info_h = init_frame_h + 64;
3288
3289 context = init_frame->context;
3290 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3291 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3292 init_frame->context = context;
3293
3294 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3295 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3296
3297 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3298 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3299
3300 init_frame->cmd = MFI_CMD_INIT;
3301 init_frame->cmd_status = 0xFF;
3302 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3303
3304 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3305
3306 /*
3307 * disable the intr before firing the init frame to FW
3308 */
3309 instance->instancet->disable_intr(instance->reg_set);
3310
3311 /*
3312 * Issue the init frame in polled mode
3313 */
3314
3315 if (megasas_issue_polled(instance, cmd)) {
3316 printk(KERN_ERR "megasas: Failed to init firmware\n");
3317 megasas_return_cmd(instance, cmd);
3318 goto fail_fw_init;
3319 }
3320
3321 megasas_return_cmd(instance, cmd);
3322
3323 return 0;
3324
3325fail_fw_init:
3326 return -EINVAL;
3327}
3328
3329/**
bo yangad84db22007-11-09 04:40:16 -05003330 * megasas_start_timer - Initializes a timer object
3331 * @instance: Adapter soft state
3332 * @timer: timer object to be initialized
3333 * @fn: timer function
3334 * @interval: time interval between timer function call
3335 */
3336static inline void
3337megasas_start_timer(struct megasas_instance *instance,
3338 struct timer_list *timer,
3339 void *fn, unsigned long interval)
3340{
3341 init_timer(timer);
3342 timer->expires = jiffies + interval;
3343 timer->data = (unsigned long)instance;
3344 timer->function = fn;
3345 add_timer(timer);
3346}
3347
3348/**
3349 * megasas_io_completion_timer - Timer fn
3350 * @instance_addr: Address of adapter soft state
3351 *
3352 * Schedules tasklet for cmd completion
3353 * if poll_mode_io is set
3354 */
3355static void
3356megasas_io_completion_timer(unsigned long instance_addr)
3357{
3358 struct megasas_instance *instance =
3359 (struct megasas_instance *)instance_addr;
3360
3361 if (atomic_read(&instance->fw_outstanding))
3362 tasklet_schedule(&instance->isr_tasklet);
3363
3364 /* Restart timer */
3365 if (poll_mode_io)
3366 mod_timer(&instance->io_completion_timer,
3367 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3368}
3369
adam radfordcd50ba82010-12-21 10:23:23 -08003370static u32
3371megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003372{
adam radfordcd50ba82010-12-21 10:23:23 -08003373 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003374 u32 context_sz;
3375 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003376
3377 reg_set = instance->reg_set;
3378
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003379 /*
3380 * Get various operational parameters from status register
3381 */
Sumant Patro1341c932006-01-25 12:02:40 -08003382 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003383 /*
3384 * Reduce the max supported cmds by 1. This is to ensure that the
3385 * reply_q_sz (1 more than the max cmd that driver may send)
3386 * does not exceed max cmds that the FW can support
3387 */
3388 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003389 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003390 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003391 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003392 /*
3393 * Create a pool of commands
3394 */
3395 if (megasas_alloc_cmds(instance))
3396 goto fail_alloc_cmds;
3397
3398 /*
3399 * Allocate memory for reply queue. Length of reply queue should
3400 * be _one_ more than the maximum commands handled by the firmware.
3401 *
3402 * Note: When FW completes commands, it places corresponding contex
3403 * values in this circular reply queue. This circular queue is a fairly
3404 * typical producer-consumer queue. FW is the producer (of completed
3405 * commands) and the driver is the consumer.
3406 */
3407 context_sz = sizeof(u32);
3408 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3409
3410 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3411 reply_q_sz,
3412 &instance->reply_queue_h);
3413
3414 if (!instance->reply_queue) {
3415 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3416 goto fail_reply_queue;
3417 }
3418
bo yang31ea7082007-11-07 12:09:50 -05003419 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003420 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003421
bo yang39a98552010-09-22 22:36:29 -04003422 instance->fw_support_ieee = 0;
3423 instance->fw_support_ieee =
3424 (instance->instancet->read_fw_status_reg(reg_set) &
3425 0x04000000);
3426
3427 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3428 instance->fw_support_ieee);
3429
3430 if (instance->fw_support_ieee)
3431 instance->flag_ieee = 1;
3432
adam radfordcd50ba82010-12-21 10:23:23 -08003433 return 0;
3434
3435fail_fw_init:
3436
3437 pci_free_consistent(instance->pdev, reply_q_sz,
3438 instance->reply_queue, instance->reply_queue_h);
3439fail_reply_queue:
3440 megasas_free_cmds(instance);
3441
3442fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003443 return 1;
3444}
3445
3446/**
3447 * megasas_init_fw - Initializes the FW
3448 * @instance: Adapter soft state
3449 *
3450 * This is the main function for initializing firmware
3451 */
3452
3453static int megasas_init_fw(struct megasas_instance *instance)
3454{
3455 u32 max_sectors_1;
3456 u32 max_sectors_2;
adam radford3f1abce2011-05-11 18:33:47 -07003457 u32 tmp_sectors, msix_enable;
adam radfordcd50ba82010-12-21 10:23:23 -08003458 struct megasas_register_set __iomem *reg_set;
3459 struct megasas_ctrl_info *ctrl_info;
3460 unsigned long bar_list;
3461
3462 /* Find first memory bar */
3463 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3464 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3465 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3466 if (pci_request_selected_regions(instance->pdev, instance->bar,
3467 "megasas: LSI")) {
3468 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3469 return -EBUSY;
3470 }
3471
3472 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3473
3474 if (!instance->reg_set) {
3475 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3476 goto fail_ioremap;
3477 }
3478
3479 reg_set = instance->reg_set;
3480
3481 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003482 case PCI_DEVICE_ID_LSI_FUSION:
3483 instance->instancet = &megasas_instance_template_fusion;
3484 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003485 case PCI_DEVICE_ID_LSI_SAS1078R:
3486 case PCI_DEVICE_ID_LSI_SAS1078DE:
3487 instance->instancet = &megasas_instance_template_ppc;
3488 break;
3489 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3490 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3491 instance->instancet = &megasas_instance_template_gen2;
3492 break;
3493 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3494 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3495 instance->instancet = &megasas_instance_template_skinny;
3496 break;
3497 case PCI_DEVICE_ID_LSI_SAS1064R:
3498 case PCI_DEVICE_ID_DELL_PERC5:
3499 default:
3500 instance->instancet = &megasas_instance_template_xscale;
3501 break;
3502 }
3503
3504 /*
3505 * We expect the FW state to be READY
3506 */
3507 if (megasas_transition_to_ready(instance))
3508 goto fail_ready_state;
3509
adam radford3f1abce2011-05-11 18:33:47 -07003510 /* Check if MSI-X is supported while in ready state */
3511 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3512 0x4000000) >> 0x1a;
3513 if (msix_enable && !msix_disable &&
3514 !pci_enable_msix(instance->pdev, &instance->msixentry, 1))
3515 instance->msi_flag = 1;
3516
adam radfordcd50ba82010-12-21 10:23:23 -08003517 /* Get operational params, sge flags, send init cmd to controller */
3518 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003519 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003520
3521 printk(KERN_ERR "megasas: INIT adapter done\n");
3522
bo yang39a98552010-09-22 22:36:29 -04003523 /** for passthrough
3524 * the following function will get the PD LIST.
3525 */
3526
Yang, Bo81e403c2009-10-06 14:27:54 -06003527 memset(instance->pd_list, 0 ,
3528 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3529 megasas_get_pd_list(instance);
3530
Yang, Bobdc6fb82009-12-06 08:30:19 -07003531 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3532 megasas_get_ld_list(instance);
3533
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003534 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3535
3536 /*
3537 * Compute the max allowed sectors per IO: The controller info has two
3538 * limits on max sectors. Driver should use the minimum of these two.
3539 *
3540 * 1 << stripe_sz_ops.min = max sectors per strip
3541 *
3542 * Note that older firmwares ( < FW ver 30) didn't report information
3543 * to calculate max_sectors_1. So the number ended up as zero always.
3544 */
bo yang14faea92007-11-09 04:14:00 -05003545 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003546 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3547
3548 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3549 ctrl_info->max_strips_per_io;
3550 max_sectors_2 = ctrl_info->max_request_size;
3551
bo yang14faea92007-11-09 04:14:00 -05003552 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003553 instance->disableOnlineCtrlReset =
3554 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003555 }
3556
3557 instance->max_sectors_per_req = instance->max_num_sge *
3558 PAGE_SIZE / 512;
3559 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3560 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003561
3562 kfree(ctrl_info);
3563
Sumant Patro5d018ad2006-10-03 13:13:18 -07003564 /*
3565 * Setup tasklet for cmd completion
3566 */
3567
adam radfordf86c54242011-02-24 20:57:00 -08003568 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003569 (unsigned long)instance);
3570
3571 /* Initialize the cmd completion timer */
3572 if (poll_mode_io)
3573 megasas_start_timer(instance, &instance->io_completion_timer,
3574 megasas_io_completion_timer,
3575 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003576 return 0;
3577
adam radfordeb1b1232011-02-24 20:55:56 -08003578fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003579fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003580 iounmap(instance->reg_set);
3581
3582 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003583 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003584
3585 return -EINVAL;
3586}
3587
3588/**
3589 * megasas_release_mfi - Reverses the FW initialization
3590 * @intance: Adapter soft state
3591 */
3592static void megasas_release_mfi(struct megasas_instance *instance)
3593{
adam radford9c915a82010-12-21 13:34:31 -08003594 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003595
adam radford9c915a82010-12-21 13:34:31 -08003596 if (instance->reply_queue)
3597 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003598 instance->reply_queue, instance->reply_queue_h);
3599
3600 megasas_free_cmds(instance);
3601
3602 iounmap(instance->reg_set);
3603
adam radfordb6d5d882010-12-14 18:56:07 -08003604 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003605}
3606
3607/**
3608 * megasas_get_seq_num - Gets latest event sequence numbers
3609 * @instance: Adapter soft state
3610 * @eli: FW event log sequence numbers information
3611 *
3612 * FW maintains a log of all events in a non-volatile area. Upper layers would
3613 * usually find out the latest sequence number of the events, the seq number at
3614 * the boot etc. They would "read" all the events below the latest seq number
3615 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3616 * number), they would subsribe to AEN (asynchronous event notification) and
3617 * wait for the events to happen.
3618 */
3619static int
3620megasas_get_seq_num(struct megasas_instance *instance,
3621 struct megasas_evt_log_info *eli)
3622{
3623 struct megasas_cmd *cmd;
3624 struct megasas_dcmd_frame *dcmd;
3625 struct megasas_evt_log_info *el_info;
3626 dma_addr_t el_info_h = 0;
3627
3628 cmd = megasas_get_cmd(instance);
3629
3630 if (!cmd) {
3631 return -ENOMEM;
3632 }
3633
3634 dcmd = &cmd->frame->dcmd;
3635 el_info = pci_alloc_consistent(instance->pdev,
3636 sizeof(struct megasas_evt_log_info),
3637 &el_info_h);
3638
3639 if (!el_info) {
3640 megasas_return_cmd(instance, cmd);
3641 return -ENOMEM;
3642 }
3643
3644 memset(el_info, 0, sizeof(*el_info));
3645 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3646
3647 dcmd->cmd = MFI_CMD_DCMD;
3648 dcmd->cmd_status = 0x0;
3649 dcmd->sge_count = 1;
3650 dcmd->flags = MFI_FRAME_DIR_READ;
3651 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003652 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003653 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3654 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3655 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3656 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3657
3658 megasas_issue_blocked_cmd(instance, cmd);
3659
3660 /*
3661 * Copy the data back into callers buffer
3662 */
3663 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3664
3665 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3666 el_info, el_info_h);
3667
3668 megasas_return_cmd(instance, cmd);
3669
3670 return 0;
3671}
3672
3673/**
3674 * megasas_register_aen - Registers for asynchronous event notification
3675 * @instance: Adapter soft state
3676 * @seq_num: The starting sequence number
3677 * @class_locale: Class of the event
3678 *
3679 * This function subscribes for AEN for events beyond the @seq_num. It requests
3680 * to be notified if and only if the event is of type @class_locale
3681 */
3682static int
3683megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3684 u32 class_locale_word)
3685{
3686 int ret_val;
3687 struct megasas_cmd *cmd;
3688 struct megasas_dcmd_frame *dcmd;
3689 union megasas_evt_class_locale curr_aen;
3690 union megasas_evt_class_locale prev_aen;
3691
3692 /*
3693 * If there an AEN pending already (aen_cmd), check if the
3694 * class_locale of that pending AEN is inclusive of the new
3695 * AEN request we currently have. If it is, then we don't have
3696 * to do anything. In other words, whichever events the current
3697 * AEN request is subscribing to, have already been subscribed
3698 * to.
3699 *
3700 * If the old_cmd is _not_ inclusive, then we have to abort
3701 * that command, form a class_locale that is superset of both
3702 * old and current and re-issue to the FW
3703 */
3704
3705 curr_aen.word = class_locale_word;
3706
3707 if (instance->aen_cmd) {
3708
3709 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3710
3711 /*
3712 * A class whose enum value is smaller is inclusive of all
3713 * higher values. If a PROGRESS (= -1) was previously
3714 * registered, then a new registration requests for higher
3715 * classes need not be sent to FW. They are automatically
3716 * included.
3717 *
3718 * Locale numbers don't have such hierarchy. They are bitmap
3719 * values
3720 */
3721 if ((prev_aen.members.class <= curr_aen.members.class) &&
3722 !((prev_aen.members.locale & curr_aen.members.locale) ^
3723 curr_aen.members.locale)) {
3724 /*
3725 * Previously issued event registration includes
3726 * current request. Nothing to do.
3727 */
3728 return 0;
3729 } else {
3730 curr_aen.members.locale |= prev_aen.members.locale;
3731
3732 if (prev_aen.members.class < curr_aen.members.class)
3733 curr_aen.members.class = prev_aen.members.class;
3734
3735 instance->aen_cmd->abort_aen = 1;
3736 ret_val = megasas_issue_blocked_abort_cmd(instance,
3737 instance->
3738 aen_cmd);
3739
3740 if (ret_val) {
3741 printk(KERN_DEBUG "megasas: Failed to abort "
3742 "previous AEN command\n");
3743 return ret_val;
3744 }
3745 }
3746 }
3747
3748 cmd = megasas_get_cmd(instance);
3749
3750 if (!cmd)
3751 return -ENOMEM;
3752
3753 dcmd = &cmd->frame->dcmd;
3754
3755 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3756
3757 /*
3758 * Prepare DCMD for aen registration
3759 */
3760 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3761
3762 dcmd->cmd = MFI_CMD_DCMD;
3763 dcmd->cmd_status = 0x0;
3764 dcmd->sge_count = 1;
3765 dcmd->flags = MFI_FRAME_DIR_READ;
3766 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003767 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003768 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003769 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3770 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3771 dcmd->mbox.w[0] = seq_num;
3772 dcmd->mbox.w[1] = curr_aen.word;
3773 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3774 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3775
Yang, Bof4c9a132009-10-06 14:43:28 -06003776 if (instance->aen_cmd != NULL) {
3777 megasas_return_cmd(instance, cmd);
3778 return 0;
3779 }
3780
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003781 /*
3782 * Store reference to the cmd used to register for AEN. When an
3783 * application wants us to register for AEN, we have to abort this
3784 * cmd and re-register with a new EVENT LOCALE supplied by that app
3785 */
3786 instance->aen_cmd = cmd;
3787
3788 /*
3789 * Issue the aen registration frame
3790 */
adam radford9c915a82010-12-21 13:34:31 -08003791 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003792
3793 return 0;
3794}
3795
3796/**
3797 * megasas_start_aen - Subscribes to AEN during driver load time
3798 * @instance: Adapter soft state
3799 */
3800static int megasas_start_aen(struct megasas_instance *instance)
3801{
3802 struct megasas_evt_log_info eli;
3803 union megasas_evt_class_locale class_locale;
3804
3805 /*
3806 * Get the latest sequence number from FW
3807 */
3808 memset(&eli, 0, sizeof(eli));
3809
3810 if (megasas_get_seq_num(instance, &eli))
3811 return -1;
3812
3813 /*
3814 * Register AEN with FW for latest sequence number plus 1
3815 */
3816 class_locale.members.reserved = 0;
3817 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3818 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3819
3820 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3821 class_locale.word);
3822}
3823
3824/**
3825 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3826 * @instance: Adapter soft state
3827 */
3828static int megasas_io_attach(struct megasas_instance *instance)
3829{
3830 struct Scsi_Host *host = instance->host;
3831
3832 /*
3833 * Export parameters required by SCSI mid-layer
3834 */
3835 host->irq = instance->pdev->irq;
3836 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003837 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3838 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3839 host->can_queue =
3840 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3841 } else
3842 host->can_queue =
3843 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003844 host->this_id = instance->init_id;
3845 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003846
3847 if (instance->fw_support_ieee)
3848 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3849
Yang, Bo1fd10682010-10-12 07:18:50 -06003850 /*
3851 * Check if the module parameter value for max_sectors can be used
3852 */
3853 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3854 instance->max_sectors_per_req = max_sectors;
3855 else {
3856 if (max_sectors) {
3857 if (((instance->pdev->device ==
3858 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3859 (instance->pdev->device ==
3860 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3861 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3862 instance->max_sectors_per_req = max_sectors;
3863 } else {
3864 printk(KERN_INFO "megasas: max_sectors should be > 0"
3865 "and <= %d (or < 1MB for GEN2 controller)\n",
3866 instance->max_sectors_per_req);
3867 }
3868 }
3869 }
3870
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003871 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003872 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003873 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3874 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3875 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003876 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003877
adam radford9c915a82010-12-21 13:34:31 -08003878 /* Fusion only supports host reset */
3879 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
3880 host->hostt->eh_device_reset_handler = NULL;
3881 host->hostt->eh_bus_reset_handler = NULL;
3882 }
3883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003884 /*
3885 * Notify the mid-layer about the new controller
3886 */
3887 if (scsi_add_host(host, &instance->pdev->dev)) {
3888 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3889 return -ENODEV;
3890 }
3891
3892 /*
3893 * Trigger SCSI to scan our drives
3894 */
3895 scsi_scan_host(host);
3896 return 0;
3897}
3898
bo yang31ea7082007-11-07 12:09:50 -05003899static int
3900megasas_set_dma_mask(struct pci_dev *pdev)
3901{
3902 /*
3903 * All our contollers are capable of performing 64-bit DMA
3904 */
3905 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003906 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003907
Yang Hongyang284901a2009-04-06 19:01:15 -07003908 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003909 goto fail_set_dma_mask;
3910 }
3911 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003912 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003913 goto fail_set_dma_mask;
3914 }
3915 return 0;
3916
3917fail_set_dma_mask:
3918 return 1;
3919}
3920
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003921/**
3922 * megasas_probe_one - PCI hotplug entry point
3923 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003924 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003925 */
3926static int __devinit
3927megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3928{
adam radford66192dfe2011-02-24 20:56:28 -08003929 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003930 struct Scsi_Host *host;
3931 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08003932 u16 control = 0;
3933
3934 /* Reset MSI-X in the kdump kernel */
3935 if (reset_devices) {
3936 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3937 if (pos) {
3938 pci_read_config_word(pdev, msi_control_reg(pos),
3939 &control);
3940 if (control & PCI_MSIX_FLAGS_ENABLE) {
3941 dev_info(&pdev->dev, "resetting MSI-X\n");
3942 pci_write_config_word(pdev,
3943 msi_control_reg(pos),
3944 control &
3945 ~PCI_MSIX_FLAGS_ENABLE);
3946 }
3947 }
3948 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003949
3950 /*
3951 * Announce PCI information
3952 */
3953 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3954 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3955 pdev->subsystem_device);
3956
3957 printk("bus %d:slot %d:func %d\n",
3958 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3959
3960 /*
3961 * PCI prepping: enable device set bus mastering and dma mask
3962 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003963 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003964
3965 if (rval) {
3966 return rval;
3967 }
3968
3969 pci_set_master(pdev);
3970
bo yang31ea7082007-11-07 12:09:50 -05003971 if (megasas_set_dma_mask(pdev))
3972 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003973
3974 host = scsi_host_alloc(&megasas_template,
3975 sizeof(struct megasas_instance));
3976
3977 if (!host) {
3978 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3979 goto fail_alloc_instance;
3980 }
3981
3982 instance = (struct megasas_instance *)host->hostdata;
3983 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04003984 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08003985 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003986
adam radford9c915a82010-12-21 13:34:31 -08003987 switch (instance->pdev->device) {
3988 case PCI_DEVICE_ID_LSI_FUSION:
3989 {
3990 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003991
adam radford9c915a82010-12-21 13:34:31 -08003992 instance->ctrl_context =
3993 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
3994 if (!instance->ctrl_context) {
3995 printk(KERN_DEBUG "megasas: Failed to allocate "
3996 "memory for Fusion context info\n");
3997 goto fail_alloc_dma_buf;
3998 }
3999 fusion = instance->ctrl_context;
4000 INIT_LIST_HEAD(&fusion->cmd_pool);
4001 spin_lock_init(&fusion->cmd_pool_lock);
4002 }
4003 break;
4004 default: /* For all other supported controllers */
4005
4006 instance->producer =
4007 pci_alloc_consistent(pdev, sizeof(u32),
4008 &instance->producer_h);
4009 instance->consumer =
4010 pci_alloc_consistent(pdev, sizeof(u32),
4011 &instance->consumer_h);
4012
4013 if (!instance->producer || !instance->consumer) {
4014 printk(KERN_DEBUG "megasas: Failed to allocate"
4015 "memory for producer, consumer\n");
4016 goto fail_alloc_dma_buf;
4017 }
4018
4019 *instance->producer = 0;
4020 *instance->consumer = 0;
4021 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004022 }
4023
Yang, Boc3518832009-10-06 14:18:02 -06004024 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004025 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004026 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004027 instance->issuepend_done = 1;
4028 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4029 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004030
4031 instance->evt_detail = pci_alloc_consistent(pdev,
4032 sizeof(struct
4033 megasas_evt_detail),
4034 &instance->evt_detail_h);
4035
4036 if (!instance->evt_detail) {
4037 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4038 "event detail structure\n");
4039 goto fail_alloc_dma_buf;
4040 }
4041
4042 /*
4043 * Initialize locks and queues
4044 */
4045 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004046 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004047
Sumant Patroe4a082c2006-05-30 12:03:37 -07004048 atomic_set(&instance->fw_outstanding,0);
4049
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004050 init_waitqueue_head(&instance->int_cmd_wait_q);
4051 init_waitqueue_head(&instance->abort_cmd_wait_q);
4052
4053 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004054 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004055 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06004056 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004057
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004058 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004059 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004060
4061 /*
4062 * Initialize PCI related and misc parameters
4063 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004064 instance->host = host;
4065 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4066 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4067
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004068 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4069 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004070 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004071 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4072 } else
4073 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4074
Sumant Patro658dced2006-10-03 13:09:14 -07004075 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004076 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004077 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004078 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004079 instance->disableOnlineCtrlReset = 1;
4080
adam radford9c915a82010-12-21 13:34:31 -08004081 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4082 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4083 else
4084 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004085
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004086 /*
adam radford0a770662011-02-24 20:56:12 -08004087 * Initialize MFI Firmware
4088 */
4089 if (megasas_init_fw(instance))
4090 goto fail_init_mfi;
4091
4092 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004093 * Register IRQ
4094 */
adam radford80d9da92010-12-21 10:17:40 -08004095 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004096 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004097 IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004098 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4099 goto fail_irq;
4100 }
4101
Sumant Patro1341c932006-01-25 12:02:40 -08004102 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004103
4104 /*
4105 * Store instance in PCI softstate
4106 */
4107 pci_set_drvdata(pdev, instance);
4108
4109 /*
4110 * Add this controller to megasas_mgmt_info structure so that it
4111 * can be exported to management applications
4112 */
4113 megasas_mgmt_info.count++;
4114 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4115 megasas_mgmt_info.max_index++;
4116
4117 /*
adam radford541f90b2011-05-11 18:34:29 -07004118 * Register with SCSI mid-layer
4119 */
4120 if (megasas_io_attach(instance))
4121 goto fail_io_attach;
4122
4123 instance->unload = 0;
4124
4125 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004126 * Initiate AEN (Asynchronous Event Notification)
4127 */
4128 if (megasas_start_aen(instance)) {
4129 printk(KERN_DEBUG "megasas: start aen failed\n");
4130 goto fail_start_aen;
4131 }
4132
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004133 return 0;
4134
4135 fail_start_aen:
4136 fail_io_attach:
4137 megasas_mgmt_info.count--;
4138 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4139 megasas_mgmt_info.max_index--;
4140
4141 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004142 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004143 free_irq(instance->msi_flag ? instance->msixentry.vector :
4144 instance->pdev->irq, instance);
adam radfordeb1b1232011-02-24 20:55:56 -08004145fail_irq:
adam radfordeb1b1232011-02-24 20:55:56 -08004146 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4147 megasas_release_fusion(instance);
4148 else
4149 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004150 fail_init_mfi:
adam radford0a770662011-02-24 20:56:12 -08004151 if (instance->msi_flag)
4152 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004153 fail_alloc_dma_buf:
4154 if (instance->evt_detail)
4155 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4156 instance->evt_detail,
4157 instance->evt_detail_h);
4158
adam radfordeb1b1232011-02-24 20:55:56 -08004159 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004160 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4161 instance->producer_h);
4162 if (instance->consumer)
4163 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4164 instance->consumer_h);
4165 scsi_host_put(host);
4166
4167 fail_alloc_instance:
4168 fail_set_dma_mask:
4169 pci_disable_device(pdev);
4170
4171 return -ENODEV;
4172}
4173
4174/**
4175 * megasas_flush_cache - Requests FW to flush all its caches
4176 * @instance: Adapter soft state
4177 */
4178static void megasas_flush_cache(struct megasas_instance *instance)
4179{
4180 struct megasas_cmd *cmd;
4181 struct megasas_dcmd_frame *dcmd;
4182
bo yang39a98552010-09-22 22:36:29 -04004183 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4184 return;
4185
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004186 cmd = megasas_get_cmd(instance);
4187
4188 if (!cmd)
4189 return;
4190
4191 dcmd = &cmd->frame->dcmd;
4192
4193 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4194
4195 dcmd->cmd = MFI_CMD_DCMD;
4196 dcmd->cmd_status = 0x0;
4197 dcmd->sge_count = 0;
4198 dcmd->flags = MFI_FRAME_DIR_NONE;
4199 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004200 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004201 dcmd->data_xfer_len = 0;
4202 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4203 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4204
4205 megasas_issue_blocked_cmd(instance, cmd);
4206
4207 megasas_return_cmd(instance, cmd);
4208
4209 return;
4210}
4211
4212/**
4213 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4214 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004215 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004216 */
bo yang31ea7082007-11-07 12:09:50 -05004217static void megasas_shutdown_controller(struct megasas_instance *instance,
4218 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004219{
4220 struct megasas_cmd *cmd;
4221 struct megasas_dcmd_frame *dcmd;
4222
bo yang39a98552010-09-22 22:36:29 -04004223 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4224 return;
4225
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004226 cmd = megasas_get_cmd(instance);
4227
4228 if (!cmd)
4229 return;
4230
4231 if (instance->aen_cmd)
4232 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004233 if (instance->map_update_cmd)
4234 megasas_issue_blocked_abort_cmd(instance,
4235 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004236 dcmd = &cmd->frame->dcmd;
4237
4238 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4239
4240 dcmd->cmd = MFI_CMD_DCMD;
4241 dcmd->cmd_status = 0x0;
4242 dcmd->sge_count = 0;
4243 dcmd->flags = MFI_FRAME_DIR_NONE;
4244 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004245 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004246 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004247 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004248
4249 megasas_issue_blocked_cmd(instance, cmd);
4250
4251 megasas_return_cmd(instance, cmd);
4252
4253 return;
4254}
4255
Jiri Slaby33139b22008-05-01 17:56:02 +02004256#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004257/**
bo yangad84db22007-11-09 04:40:16 -05004258 * megasas_suspend - driver suspend entry point
4259 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004260 * @state: PCI power state to suspend routine
4261 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004262static int
bo yang31ea7082007-11-07 12:09:50 -05004263megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4264{
4265 struct Scsi_Host *host;
4266 struct megasas_instance *instance;
4267
4268 instance = pci_get_drvdata(pdev);
4269 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004270 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004271
bo yangad84db22007-11-09 04:40:16 -05004272 if (poll_mode_io)
4273 del_timer_sync(&instance->io_completion_timer);
4274
bo yang31ea7082007-11-07 12:09:50 -05004275 megasas_flush_cache(instance);
4276 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004277
4278 /* cancel the delayed work if this work still in queue */
4279 if (instance->ev != NULL) {
4280 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004281 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004282 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004283 instance->ev = NULL;
4284 }
4285
bo yang31ea7082007-11-07 12:09:50 -05004286 tasklet_kill(&instance->isr_tasklet);
4287
4288 pci_set_drvdata(instance->pdev, instance);
4289 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004290 free_irq(instance->msi_flag ? instance->msixentry.vector :
4291 instance->pdev->irq, instance);
4292 if (instance->msi_flag)
4293 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004294
4295 pci_save_state(pdev);
4296 pci_disable_device(pdev);
4297
4298 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4299
4300 return 0;
4301}
4302
4303/**
4304 * megasas_resume- driver resume entry point
4305 * @pdev: PCI device structure
4306 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004307static int
bo yang31ea7082007-11-07 12:09:50 -05004308megasas_resume(struct pci_dev *pdev)
4309{
4310 int rval;
4311 struct Scsi_Host *host;
4312 struct megasas_instance *instance;
4313
4314 instance = pci_get_drvdata(pdev);
4315 host = instance->host;
4316 pci_set_power_state(pdev, PCI_D0);
4317 pci_enable_wake(pdev, PCI_D0, 0);
4318 pci_restore_state(pdev);
4319
4320 /*
4321 * PCI prepping: enable device set bus mastering and dma mask
4322 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004323 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004324
4325 if (rval) {
4326 printk(KERN_ERR "megasas: Enable device failed\n");
4327 return rval;
4328 }
4329
4330 pci_set_master(pdev);
4331
4332 if (megasas_set_dma_mask(pdev))
4333 goto fail_set_dma_mask;
4334
4335 /*
4336 * Initialize MFI Firmware
4337 */
4338
bo yang31ea7082007-11-07 12:09:50 -05004339 atomic_set(&instance->fw_outstanding, 0);
4340
4341 /*
4342 * We expect the FW state to be READY
4343 */
4344 if (megasas_transition_to_ready(instance))
4345 goto fail_ready_state;
4346
adam radford3f1abce2011-05-11 18:33:47 -07004347 /* Now re-enable MSI-X */
4348 if (instance->msi_flag)
4349 pci_enable_msix(instance->pdev, &instance->msixentry, 1);
4350
adam radford9c915a82010-12-21 13:34:31 -08004351 switch (instance->pdev->device) {
4352 case PCI_DEVICE_ID_LSI_FUSION:
4353 {
4354 megasas_reset_reply_desc(instance);
4355 if (megasas_ioc_init_fusion(instance)) {
4356 megasas_free_cmds(instance);
4357 megasas_free_cmds_fusion(instance);
4358 goto fail_init_mfi;
4359 }
4360 if (!megasas_get_map_info(instance))
4361 megasas_sync_map_info(instance);
4362 }
4363 break;
4364 default:
4365 *instance->producer = 0;
4366 *instance->consumer = 0;
4367 if (megasas_issue_init_mfi(instance))
4368 goto fail_init_mfi;
4369 break;
4370 }
bo yang31ea7082007-11-07 12:09:50 -05004371
adam radford9c915a82010-12-21 13:34:31 -08004372 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4373 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004374
4375 /*
4376 * Register IRQ
4377 */
adam radford80d9da92010-12-21 10:17:40 -08004378 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004379 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004380 IRQF_SHARED, "megasas", instance)) {
bo yang31ea7082007-11-07 12:09:50 -05004381 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4382 goto fail_irq;
4383 }
4384
4385 instance->instancet->enable_intr(instance->reg_set);
4386
bo yangad84db22007-11-09 04:40:16 -05004387 /* Initialize the cmd completion timer */
4388 if (poll_mode_io)
4389 megasas_start_timer(instance, &instance->io_completion_timer,
4390 megasas_io_completion_timer,
4391 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06004392 instance->unload = 0;
4393
adam radford541f90b2011-05-11 18:34:29 -07004394 /*
4395 * Initiate AEN (Asynchronous Event Notification)
4396 */
4397 if (megasas_start_aen(instance))
4398 printk(KERN_ERR "megasas: Start AEN failed\n");
4399
bo yang31ea7082007-11-07 12:09:50 -05004400 return 0;
4401
4402fail_irq:
4403fail_init_mfi:
4404 if (instance->evt_detail)
4405 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4406 instance->evt_detail,
4407 instance->evt_detail_h);
4408
4409 if (instance->producer)
4410 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4411 instance->producer_h);
4412 if (instance->consumer)
4413 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4414 instance->consumer_h);
4415 scsi_host_put(host);
4416
4417fail_set_dma_mask:
4418fail_ready_state:
4419
4420 pci_disable_device(pdev);
4421
4422 return -ENODEV;
4423}
Jiri Slaby33139b22008-05-01 17:56:02 +02004424#else
4425#define megasas_suspend NULL
4426#define megasas_resume NULL
4427#endif
bo yang31ea7082007-11-07 12:09:50 -05004428
4429/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004430 * megasas_detach_one - PCI hot"un"plug entry point
4431 * @pdev: PCI device structure
4432 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004433static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004434{
4435 int i;
4436 struct Scsi_Host *host;
4437 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004438 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004439
4440 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004441 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004442 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004443 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004444
bo yangad84db22007-11-09 04:40:16 -05004445 if (poll_mode_io)
4446 del_timer_sync(&instance->io_completion_timer);
4447
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004448 scsi_remove_host(instance->host);
4449 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004450 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004451
4452 /* cancel the delayed work if this work still in queue*/
4453 if (instance->ev != NULL) {
4454 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004455 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004456 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004457 instance->ev = NULL;
4458 }
4459
Sumant Patro5d018ad2006-10-03 13:13:18 -07004460 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004461
4462 /*
4463 * Take the instance off the instance array. Note that we will not
4464 * decrement the max_index. We let this array be sparse array
4465 */
4466 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4467 if (megasas_mgmt_info.instance[i] == instance) {
4468 megasas_mgmt_info.count--;
4469 megasas_mgmt_info.instance[i] = NULL;
4470
4471 break;
4472 }
4473 }
4474
4475 pci_set_drvdata(instance->pdev, NULL);
4476
Sumant Patrob274cab2006-10-03 12:52:12 -07004477 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004478
adam radford80d9da92010-12-21 10:17:40 -08004479 free_irq(instance->msi_flag ? instance->msixentry.vector :
4480 instance->pdev->irq, instance);
4481 if (instance->msi_flag)
4482 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004483
adam radford9c915a82010-12-21 13:34:31 -08004484 switch (instance->pdev->device) {
4485 case PCI_DEVICE_ID_LSI_FUSION:
4486 megasas_release_fusion(instance);
4487 for (i = 0; i < 2 ; i++)
4488 if (fusion->ld_map[i])
4489 dma_free_coherent(&instance->pdev->dev,
4490 fusion->map_sz,
4491 fusion->ld_map[i],
4492 fusion->
4493 ld_map_phys[i]);
4494 kfree(instance->ctrl_context);
4495 break;
4496 default:
4497 megasas_release_mfi(instance);
4498 pci_free_consistent(pdev,
4499 sizeof(struct megasas_evt_detail),
4500 instance->evt_detail,
4501 instance->evt_detail_h);
4502 pci_free_consistent(pdev, sizeof(u32),
4503 instance->producer,
4504 instance->producer_h);
4505 pci_free_consistent(pdev, sizeof(u32),
4506 instance->consumer,
4507 instance->consumer_h);
4508 break;
4509 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004510
4511 scsi_host_put(host);
4512
4513 pci_set_drvdata(pdev, NULL);
4514
4515 pci_disable_device(pdev);
4516
4517 return;
4518}
4519
4520/**
4521 * megasas_shutdown - Shutdown entry point
4522 * @device: Generic device structure
4523 */
4524static void megasas_shutdown(struct pci_dev *pdev)
4525{
4526 struct megasas_instance *instance = pci_get_drvdata(pdev);
Yang, Bo0c79e682009-10-06 14:47:35 -06004527 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004528 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004529 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
adam radford46fd2562011-05-11 18:34:17 -07004530 instance->instancet->disable_intr(instance->reg_set);
4531 free_irq(instance->msi_flag ? instance->msixentry.vector :
4532 instance->pdev->irq, instance);
4533 if (instance->msi_flag)
4534 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004535}
4536
4537/**
4538 * megasas_mgmt_open - char node "open" entry point
4539 */
4540static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4541{
4542 /*
4543 * Allow only those users with admin rights
4544 */
4545 if (!capable(CAP_SYS_ADMIN))
4546 return -EACCES;
4547
4548 return 0;
4549}
4550
4551/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004552 * megasas_mgmt_fasync - Async notifier registration from applications
4553 *
4554 * This function adds the calling process to a driver global queue. When an
4555 * event occurs, SIGIO will be sent to all processes in this queue.
4556 */
4557static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4558{
4559 int rc;
4560
Arjan van de Ven0b950672006-01-11 13:16:10 +01004561 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004562
4563 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4564
Arjan van de Ven0b950672006-01-11 13:16:10 +01004565 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004566
4567 if (rc >= 0) {
4568 /* For sanity check when we get ioctl */
4569 filep->private_data = filep;
4570 return 0;
4571 }
4572
4573 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4574
4575 return rc;
4576}
4577
4578/**
Yang, Boc3518832009-10-06 14:18:02 -06004579 * megasas_mgmt_poll - char node "poll" entry point
4580 * */
4581static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4582{
4583 unsigned int mask;
4584 unsigned long flags;
4585 poll_wait(file, &megasas_poll_wait, wait);
4586 spin_lock_irqsave(&poll_aen_lock, flags);
4587 if (megasas_poll_wait_aen)
4588 mask = (POLLIN | POLLRDNORM);
4589 else
4590 mask = 0;
4591 spin_unlock_irqrestore(&poll_aen_lock, flags);
4592 return mask;
4593}
4594
4595/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004596 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4597 * @instance: Adapter soft state
4598 * @argp: User's ioctl packet
4599 */
4600static int
4601megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4602 struct megasas_iocpacket __user * user_ioc,
4603 struct megasas_iocpacket *ioc)
4604{
4605 struct megasas_sge32 *kern_sge32;
4606 struct megasas_cmd *cmd;
4607 void *kbuff_arr[MAX_IOCTL_SGE];
4608 dma_addr_t buf_handle = 0;
4609 int error = 0, i;
4610 void *sense = NULL;
4611 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004612 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004613
4614 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4615
4616 if (ioc->sge_count > MAX_IOCTL_SGE) {
4617 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4618 ioc->sge_count, MAX_IOCTL_SGE);
4619 return -EINVAL;
4620 }
4621
4622 cmd = megasas_get_cmd(instance);
4623 if (!cmd) {
4624 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4625 return -ENOMEM;
4626 }
4627
4628 /*
4629 * User's IOCTL packet has 2 frames (maximum). Copy those two
4630 * frames into our cmd's frames. cmd->frame's context will get
4631 * overwritten when we copy from user's frames. So set that value
4632 * alone separately
4633 */
4634 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4635 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004636 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004637
4638 /*
4639 * The management interface between applications and the fw uses
4640 * MFI frames. E.g, RAID configuration changes, LD property changes
4641 * etc are accomplishes through different kinds of MFI frames. The
4642 * driver needs to care only about substituting user buffers with
4643 * kernel buffers in SGLs. The location of SGL is embedded in the
4644 * struct iocpacket itself.
4645 */
4646 kern_sge32 = (struct megasas_sge32 *)
4647 ((unsigned long)cmd->frame + ioc->sgl_off);
4648
4649 /*
4650 * For each user buffer, create a mirror buffer and copy in
4651 */
4652 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004653 if (!ioc->sgl[i].iov_len)
4654 continue;
4655
Sumant Patro9f35fa82007-02-14 12:55:45 -08004656 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004657 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004658 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004659 if (!kbuff_arr[i]) {
4660 printk(KERN_DEBUG "megasas: Failed to alloc "
4661 "kernel SGL buffer for IOCTL \n");
4662 error = -ENOMEM;
4663 goto out;
4664 }
4665
4666 /*
4667 * We don't change the dma_coherent_mask, so
4668 * pci_alloc_consistent only returns 32bit addresses
4669 */
4670 kern_sge32[i].phys_addr = (u32) buf_handle;
4671 kern_sge32[i].length = ioc->sgl[i].iov_len;
4672
4673 /*
4674 * We created a kernel buffer corresponding to the
4675 * user buffer. Now copy in from the user buffer
4676 */
4677 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4678 (u32) (ioc->sgl[i].iov_len))) {
4679 error = -EFAULT;
4680 goto out;
4681 }
4682 }
4683
4684 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004685 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4686 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004687 if (!sense) {
4688 error = -ENOMEM;
4689 goto out;
4690 }
4691
4692 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004693 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004694 *sense_ptr = sense_handle;
4695 }
4696
4697 /*
4698 * Set the sync_cmd flag so that the ISR knows not to complete this
4699 * cmd to the SCSI mid-layer
4700 */
4701 cmd->sync_cmd = 1;
4702 megasas_issue_blocked_cmd(instance, cmd);
4703 cmd->sync_cmd = 0;
4704
4705 /*
4706 * copy out the kernel buffers to user buffers
4707 */
4708 for (i = 0; i < ioc->sge_count; i++) {
4709 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4710 ioc->sgl[i].iov_len)) {
4711 error = -EFAULT;
4712 goto out;
4713 }
4714 }
4715
4716 /*
4717 * copy out the sense
4718 */
4719 if (ioc->sense_len) {
4720 /*
bo yangb70a41e2008-03-18 03:13:06 -04004721 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004722 * sense buffer address
4723 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004724 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4725 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004726
bo yangb70a41e2008-03-18 03:13:06 -04004727 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4728 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004729 printk(KERN_ERR "megasas: Failed to copy out to user "
4730 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004731 error = -EFAULT;
4732 goto out;
4733 }
4734 }
4735
4736 /*
4737 * copy the status codes returned by the fw
4738 */
4739 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4740 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4741 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4742 error = -EFAULT;
4743 }
4744
4745 out:
4746 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004747 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004748 sense, sense_handle);
4749 }
4750
4751 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004752 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004753 kern_sge32[i].length,
4754 kbuff_arr[i], kern_sge32[i].phys_addr);
4755 }
4756
4757 megasas_return_cmd(instance, cmd);
4758 return error;
4759}
4760
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004761static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4762{
4763 struct megasas_iocpacket __user *user_ioc =
4764 (struct megasas_iocpacket __user *)arg;
4765 struct megasas_iocpacket *ioc;
4766 struct megasas_instance *instance;
4767 int error;
bo yang39a98552010-09-22 22:36:29 -04004768 int i;
4769 unsigned long flags;
4770 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004771
4772 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4773 if (!ioc)
4774 return -ENOMEM;
4775
4776 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4777 error = -EFAULT;
4778 goto out_kfree_ioc;
4779 }
4780
4781 instance = megasas_lookup_instance(ioc->host_no);
4782 if (!instance) {
4783 error = -ENODEV;
4784 goto out_kfree_ioc;
4785 }
4786
bo yang39a98552010-09-22 22:36:29 -04004787 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4788 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004789 error = -ENODEV;
4790 goto out_kfree_ioc;
4791 }
4792
4793 if (instance->unload == 1) {
4794 error = -ENODEV;
4795 goto out_kfree_ioc;
4796 }
4797
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004798 /*
4799 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4800 */
4801 if (down_interruptible(&instance->ioctl_sem)) {
4802 error = -ERESTARTSYS;
4803 goto out_kfree_ioc;
4804 }
bo yang39a98552010-09-22 22:36:29 -04004805
4806 for (i = 0; i < wait_time; i++) {
4807
4808 spin_lock_irqsave(&instance->hba_lock, flags);
4809 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4810 spin_unlock_irqrestore(&instance->hba_lock, flags);
4811 break;
4812 }
4813 spin_unlock_irqrestore(&instance->hba_lock, flags);
4814
4815 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4816 printk(KERN_NOTICE "megasas: waiting"
4817 "for controller reset to finish\n");
4818 }
4819
4820 msleep(1000);
4821 }
4822
4823 spin_lock_irqsave(&instance->hba_lock, flags);
4824 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4825 spin_unlock_irqrestore(&instance->hba_lock, flags);
4826
4827 printk(KERN_ERR "megaraid_sas: timed out while"
4828 "waiting for HBA to recover\n");
4829 error = -ENODEV;
4830 goto out_kfree_ioc;
4831 }
4832 spin_unlock_irqrestore(&instance->hba_lock, flags);
4833
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004834 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4835 up(&instance->ioctl_sem);
4836
4837 out_kfree_ioc:
4838 kfree(ioc);
4839 return error;
4840}
4841
4842static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4843{
4844 struct megasas_instance *instance;
4845 struct megasas_aen aen;
4846 int error;
bo yang39a98552010-09-22 22:36:29 -04004847 int i;
4848 unsigned long flags;
4849 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004850
4851 if (file->private_data != file) {
4852 printk(KERN_DEBUG "megasas: fasync_helper was not "
4853 "called first\n");
4854 return -EINVAL;
4855 }
4856
4857 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4858 return -EFAULT;
4859
4860 instance = megasas_lookup_instance(aen.host_no);
4861
4862 if (!instance)
4863 return -ENODEV;
4864
bo yang39a98552010-09-22 22:36:29 -04004865 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4866 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004867 }
4868
4869 if (instance->unload == 1) {
4870 return -ENODEV;
4871 }
4872
bo yang39a98552010-09-22 22:36:29 -04004873 for (i = 0; i < wait_time; i++) {
4874
4875 spin_lock_irqsave(&instance->hba_lock, flags);
4876 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4877 spin_unlock_irqrestore(&instance->hba_lock,
4878 flags);
4879 break;
4880 }
4881
4882 spin_unlock_irqrestore(&instance->hba_lock, flags);
4883
4884 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4885 printk(KERN_NOTICE "megasas: waiting for"
4886 "controller reset to finish\n");
4887 }
4888
4889 msleep(1000);
4890 }
4891
4892 spin_lock_irqsave(&instance->hba_lock, flags);
4893 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4894 spin_unlock_irqrestore(&instance->hba_lock, flags);
4895 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4896 "for HBA to recover.\n");
4897 return -ENODEV;
4898 }
4899 spin_unlock_irqrestore(&instance->hba_lock, flags);
4900
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004901 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004902 error = megasas_register_aen(instance, aen.seq_num,
4903 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004904 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004905 return error;
4906}
4907
4908/**
4909 * megasas_mgmt_ioctl - char node ioctl entry point
4910 */
4911static long
4912megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4913{
4914 switch (cmd) {
4915 case MEGASAS_IOC_FIRMWARE:
4916 return megasas_mgmt_ioctl_fw(file, arg);
4917
4918 case MEGASAS_IOC_GET_AEN:
4919 return megasas_mgmt_ioctl_aen(file, arg);
4920 }
4921
4922 return -ENOTTY;
4923}
4924
4925#ifdef CONFIG_COMPAT
4926static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4927{
4928 struct compat_megasas_iocpacket __user *cioc =
4929 (struct compat_megasas_iocpacket __user *)arg;
4930 struct megasas_iocpacket __user *ioc =
4931 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4932 int i;
4933 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004934 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004935
Jeff Garzik83aabc12006-10-04 06:34:03 -04004936 if (clear_user(ioc, sizeof(*ioc)))
4937 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004938
4939 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4940 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4941 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4942 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4943 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4944 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4945 return -EFAULT;
4946
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004947 /*
4948 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4949 * sense_len is not null, so prepare the 64bit value under
4950 * the same condition.
4951 */
4952 if (ioc->sense_len) {
4953 void __user **sense_ioc_ptr =
4954 (void __user **)(ioc->frame.raw + ioc->sense_off);
4955 compat_uptr_t *sense_cioc_ptr =
4956 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4957 if (get_user(ptr, sense_cioc_ptr) ||
4958 put_user(compat_ptr(ptr), sense_ioc_ptr))
4959 return -EFAULT;
4960 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004961
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004962 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004963 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4964 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4965 copy_in_user(&ioc->sgl[i].iov_len,
4966 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4967 return -EFAULT;
4968 }
4969
4970 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4971
4972 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4973 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4974 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4975 return -EFAULT;
4976 }
4977 return error;
4978}
4979
4980static long
4981megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4982 unsigned long arg)
4983{
4984 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08004985 case MEGASAS_IOC_FIRMWARE32:
4986 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004987 case MEGASAS_IOC_GET_AEN:
4988 return megasas_mgmt_ioctl_aen(file, arg);
4989 }
4990
4991 return -ENOTTY;
4992}
4993#endif
4994
4995/*
4996 * File operations structure for management interface
4997 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08004998static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004999 .owner = THIS_MODULE,
5000 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005001 .fasync = megasas_mgmt_fasync,
5002 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06005003 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005004#ifdef CONFIG_COMPAT
5005 .compat_ioctl = megasas_mgmt_compat_ioctl,
5006#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02005007 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005008};
5009
5010/*
5011 * PCI hotplug support registration structure
5012 */
5013static struct pci_driver megasas_pci_driver = {
5014
5015 .name = "megaraid_sas",
5016 .id_table = megasas_pci_table,
5017 .probe = megasas_probe_one,
5018 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05005019 .suspend = megasas_suspend,
5020 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005021 .shutdown = megasas_shutdown,
5022};
5023
5024/*
5025 * Sysfs driver attributes
5026 */
5027static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5028{
5029 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5030 MEGASAS_VERSION);
5031}
5032
5033static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5034
5035static ssize_t
5036megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5037{
5038 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5039 MEGASAS_RELDATE);
5040}
5041
5042static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5043 NULL);
5044
Sumant Patro658dced2006-10-03 13:09:14 -07005045static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005046megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5047{
5048 return sprintf(buf, "%u\n", support_poll_for_event);
5049}
5050
5051static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5052 megasas_sysfs_show_support_poll_for_event, NULL);
5053
Yang, Bo837f5fe2010-10-11 06:59:20 -06005054 static ssize_t
5055megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5056{
5057 return sprintf(buf, "%u\n", support_device_change);
5058}
5059
5060static DRIVER_ATTR(support_device_change, S_IRUGO,
5061 megasas_sysfs_show_support_device_change, NULL);
5062
Yang, Bo72c4fd32009-10-06 14:20:59 -06005063static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005064megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5065{
bo yangad84db22007-11-09 04:40:16 -05005066 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005067}
5068
5069static ssize_t
5070megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5071{
5072 int retval = count;
5073 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5074 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5075 retval = -EINVAL;
5076 }
5077 return retval;
5078}
5079
Joe Malicki66dca9b2008-08-14 17:14:48 -04005080static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005081 megasas_sysfs_set_dbg_lvl);
5082
5083static ssize_t
5084megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
5085{
5086 return sprintf(buf, "%u\n", poll_mode_io);
5087}
5088
5089static ssize_t
5090megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
5091 const char *buf, size_t count)
5092{
5093 int retval = count;
5094 int tmp = poll_mode_io;
5095 int i;
5096 struct megasas_instance *instance;
5097
5098 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
5099 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
5100 retval = -EINVAL;
5101 }
5102
5103 /*
5104 * Check if poll_mode_io is already set or is same as previous value
5105 */
5106 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
5107 goto out;
5108
5109 if (poll_mode_io) {
5110 /*
5111 * Start timers for all adapters
5112 */
5113 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5114 instance = megasas_mgmt_info.instance[i];
5115 if (instance) {
5116 megasas_start_timer(instance,
5117 &instance->io_completion_timer,
5118 megasas_io_completion_timer,
5119 MEGASAS_COMPLETION_TIMER_INTERVAL);
5120 }
5121 }
5122 } else {
5123 /*
5124 * Delete timers for all adapters
5125 */
5126 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5127 instance = megasas_mgmt_info.instance[i];
5128 if (instance)
5129 del_timer_sync(&instance->io_completion_timer);
5130 }
5131 }
5132
5133out:
5134 return retval;
5135}
5136
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005137static void
5138megasas_aen_polling(struct work_struct *work)
5139{
5140 struct megasas_aen_event *ev =
5141 container_of(work, struct megasas_aen_event, hotplug_work);
5142 struct megasas_instance *instance = ev->instance;
5143 union megasas_evt_class_locale class_locale;
5144 struct Scsi_Host *host;
5145 struct scsi_device *sdev1;
5146 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005147 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005148 int i, j, doscan = 0;
5149 u32 seq_num;
5150 int error;
5151
5152 if (!instance) {
5153 printk(KERN_ERR "invalid instance!\n");
5154 kfree(ev);
5155 return;
5156 }
5157 instance->ev = NULL;
5158 host = instance->host;
5159 if (instance->evt_detail) {
5160
5161 switch (instance->evt_detail->code) {
5162 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005163 if (megasas_get_pd_list(instance) == 0) {
5164 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5165 for (j = 0;
5166 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5167 j++) {
5168
5169 pd_index =
5170 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5171
5172 sdev1 =
5173 scsi_device_lookup(host, i, j, 0);
5174
5175 if (instance->pd_list[pd_index].driveState
5176 == MR_PD_STATE_SYSTEM) {
5177 if (!sdev1) {
5178 scsi_add_device(host, i, j, 0);
5179 }
5180
5181 if (sdev1)
5182 scsi_device_put(sdev1);
5183 }
5184 }
5185 }
5186 }
5187 doscan = 0;
5188 break;
5189
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005190 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005191 if (megasas_get_pd_list(instance) == 0) {
5192 megasas_get_pd_list(instance);
5193 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5194 for (j = 0;
5195 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5196 j++) {
5197
5198 pd_index =
5199 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5200
5201 sdev1 =
5202 scsi_device_lookup(host, i, j, 0);
5203
5204 if (instance->pd_list[pd_index].driveState
5205 == MR_PD_STATE_SYSTEM) {
5206 if (sdev1) {
5207 scsi_device_put(sdev1);
5208 }
5209 } else {
5210 if (sdev1) {
5211 scsi_remove_device(sdev1);
5212 scsi_device_put(sdev1);
5213 }
5214 }
5215 }
5216 }
5217 }
5218 doscan = 0;
5219 break;
5220
5221 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005222 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005223 case MR_EVT_LD_DELETED:
5224 megasas_get_ld_list(instance);
5225 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5226 for (j = 0;
5227 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5228 j++) {
5229
5230 ld_index =
5231 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5232
5233 sdev1 = scsi_device_lookup(host,
5234 i + MEGASAS_MAX_LD_CHANNELS,
5235 j,
5236 0);
5237
5238 if (instance->ld_ids[ld_index] != 0xff) {
5239 if (sdev1) {
5240 scsi_device_put(sdev1);
5241 }
5242 } else {
5243 if (sdev1) {
5244 scsi_remove_device(sdev1);
5245 scsi_device_put(sdev1);
5246 }
5247 }
5248 }
5249 }
5250 doscan = 0;
5251 break;
5252 case MR_EVT_LD_CREATED:
5253 megasas_get_ld_list(instance);
5254 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5255 for (j = 0;
5256 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5257 j++) {
5258 ld_index =
5259 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5260
5261 sdev1 = scsi_device_lookup(host,
5262 i+MEGASAS_MAX_LD_CHANNELS,
5263 j, 0);
5264
5265 if (instance->ld_ids[ld_index] !=
5266 0xff) {
5267 if (!sdev1) {
5268 scsi_add_device(host,
5269 i + 2,
5270 j, 0);
5271 }
5272 }
5273 if (sdev1) {
5274 scsi_device_put(sdev1);
5275 }
5276 }
5277 }
5278 doscan = 0;
5279 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005280 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005281 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005282 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005283 doscan = 1;
5284 break;
5285 default:
5286 doscan = 0;
5287 break;
5288 }
5289 } else {
5290 printk(KERN_ERR "invalid evt_detail!\n");
5291 kfree(ev);
5292 return;
5293 }
5294
5295 if (doscan) {
5296 printk(KERN_INFO "scanning ...\n");
5297 megasas_get_pd_list(instance);
5298 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5299 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5300 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5301 sdev1 = scsi_device_lookup(host, i, j, 0);
5302 if (instance->pd_list[pd_index].driveState ==
5303 MR_PD_STATE_SYSTEM) {
5304 if (!sdev1) {
5305 scsi_add_device(host, i, j, 0);
5306 }
5307 if (sdev1)
5308 scsi_device_put(sdev1);
5309 } else {
5310 if (sdev1) {
5311 scsi_remove_device(sdev1);
5312 scsi_device_put(sdev1);
5313 }
5314 }
5315 }
5316 }
Yang, Boc9786842009-12-06 08:39:25 -07005317
5318 megasas_get_ld_list(instance);
5319 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5320 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5321 ld_index =
5322 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5323
5324 sdev1 = scsi_device_lookup(host,
5325 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5326 if (instance->ld_ids[ld_index] != 0xff) {
5327 if (!sdev1) {
5328 scsi_add_device(host,
5329 i+2,
5330 j, 0);
5331 } else {
5332 scsi_device_put(sdev1);
5333 }
5334 } else {
5335 if (sdev1) {
5336 scsi_remove_device(sdev1);
5337 scsi_device_put(sdev1);
5338 }
5339 }
5340 }
5341 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005342 }
5343
5344 if ( instance->aen_cmd != NULL ) {
5345 kfree(ev);
5346 return ;
5347 }
5348
5349 seq_num = instance->evt_detail->seq_num + 1;
5350
5351 /* Register AEN with FW for latest sequence number plus 1 */
5352 class_locale.members.reserved = 0;
5353 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5354 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5355 mutex_lock(&instance->aen_mutex);
5356 error = megasas_register_aen(instance, seq_num,
5357 class_locale.word);
5358 mutex_unlock(&instance->aen_mutex);
5359
5360 if (error)
5361 printk(KERN_ERR "register aen failed error %x\n", error);
5362
5363 kfree(ev);
5364}
5365
5366
Bryn M. Reevesbb7d3f22009-11-12 18:31:54 +00005367static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
bo yangad84db22007-11-09 04:40:16 -05005368 megasas_sysfs_show_poll_mode_io,
5369 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07005370
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005371/**
5372 * megasas_init - Driver load entry point
5373 */
5374static int __init megasas_init(void)
5375{
5376 int rval;
5377
5378 /*
5379 * Announce driver version and other information
5380 */
5381 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5382 MEGASAS_EXT_VERSION);
5383
Yang, Bo72c4fd32009-10-06 14:20:59 -06005384 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005385 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005386
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005387 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5388
5389 /*
5390 * Register character device node
5391 */
5392 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5393
5394 if (rval < 0) {
5395 printk(KERN_DEBUG "megasas: failed to open device node\n");
5396 return rval;
5397 }
5398
5399 megasas_mgmt_majorno = rval;
5400
5401 /*
5402 * Register ourselves as PCI hotplug module
5403 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005404 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005405
5406 if (rval) {
5407 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005408 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005409 }
5410
Jeff Garzik83aabc12006-10-04 06:34:03 -04005411 rval = driver_create_file(&megasas_pci_driver.driver,
5412 &driver_attr_version);
5413 if (rval)
5414 goto err_dcf_attr_ver;
5415 rval = driver_create_file(&megasas_pci_driver.driver,
5416 &driver_attr_release_date);
5417 if (rval)
5418 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005419
5420 rval = driver_create_file(&megasas_pci_driver.driver,
5421 &driver_attr_support_poll_for_event);
5422 if (rval)
5423 goto err_dcf_support_poll_for_event;
5424
Jeff Garzik83aabc12006-10-04 06:34:03 -04005425 rval = driver_create_file(&megasas_pci_driver.driver,
5426 &driver_attr_dbg_lvl);
5427 if (rval)
5428 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005429 rval = driver_create_file(&megasas_pci_driver.driver,
5430 &driver_attr_poll_mode_io);
5431 if (rval)
5432 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005433
Yang, Bo837f5fe2010-10-11 06:59:20 -06005434 rval = driver_create_file(&megasas_pci_driver.driver,
5435 &driver_attr_support_device_change);
5436 if (rval)
5437 goto err_dcf_support_device_change;
5438
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005439 return rval;
bo yangad84db22007-11-09 04:40:16 -05005440
Yang, Bo837f5fe2010-10-11 06:59:20 -06005441err_dcf_support_device_change:
5442 driver_remove_file(&megasas_pci_driver.driver,
5443 &driver_attr_poll_mode_io);
5444
bo yangad84db22007-11-09 04:40:16 -05005445err_dcf_poll_mode_io:
5446 driver_remove_file(&megasas_pci_driver.driver,
5447 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005448err_dcf_dbg_lvl:
5449 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005450 &driver_attr_support_poll_for_event);
5451
5452err_dcf_support_poll_for_event:
5453 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005454 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005455
Jeff Garzik83aabc12006-10-04 06:34:03 -04005456err_dcf_rel_date:
5457 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5458err_dcf_attr_ver:
5459 pci_unregister_driver(&megasas_pci_driver);
5460err_pcidrv:
5461 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005462 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005463}
5464
5465/**
5466 * megasas_exit - Driver unload entry point
5467 */
5468static void __exit megasas_exit(void)
5469{
Sumant Patro658dced2006-10-03 13:09:14 -07005470 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005471 &driver_attr_poll_mode_io);
5472 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07005473 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005474 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005475 &driver_attr_support_poll_for_event);
5476 driver_remove_file(&megasas_pci_driver.driver,
5477 &driver_attr_support_device_change);
5478 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005479 &driver_attr_release_date);
5480 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005481
5482 pci_unregister_driver(&megasas_pci_driver);
5483 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5484}
5485
5486module_init(megasas_init);
5487module_exit(megasas_exit);