blob: 960b88cb4ff678c92e7a8ed868d99510e09e0cbc [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
21 * Version : v00.00.05.29-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
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040027 *
adam radford3f1530c2010-12-14 18:51:48 -080028 * Send feedback to: <megaraidlinux@lsi.com>
29 *
30 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
31 * ATTN: Linuxraid
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040038#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
43#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040045#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010046#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040047#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080048#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010049#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060050#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040051
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_host.h>
adam radford9c915a82010-12-21 13:34:31 -080056#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040057#include "megaraid_sas.h"
58
bo yangad84db22007-11-09 04:40:16 -050059/*
60 * poll_mode_io:1- schedule complete completion from q cmd
61 */
62static unsigned int poll_mode_io;
63module_param_named(poll_mode_io, poll_mode_io, int, 0);
64MODULE_PARM_DESC(poll_mode_io,
65 "Complete cmds from IO path, (default=0)");
66
Yang, Bo1fd10682010-10-12 07:18:50 -060067/*
68 * Number of sectors per IO command
69 * Will be set in megasas_init_mfi if user does not provide
70 */
71static unsigned int max_sectors;
72module_param_named(max_sectors, max_sectors, int, 0);
73MODULE_PARM_DESC(max_sectors,
74 "Maximum number of sectors per IO command");
75
adam radford80d9da92010-12-21 10:17:40 -080076static int msix_disable;
77module_param(msix_disable, int, S_IRUGO);
78MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
79
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040080MODULE_LICENSE("GPL");
81MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080082MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050083MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040084
adam radford9c915a82010-12-21 13:34:31 -080085int megasas_transition_to_ready(struct megasas_instance *instance);
bo yang39a98552010-09-22 22:36:29 -040086static int megasas_get_pd_list(struct megasas_instance *instance);
87static int megasas_issue_init_mfi(struct megasas_instance *instance);
88static int megasas_register_aen(struct megasas_instance *instance,
89 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040090/*
91 * PCI ID table for all supported controllers
92 */
93static struct pci_device_id megasas_pci_table[] = {
94
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020095 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
96 /* xscale IOP */
97 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
98 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -040099 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
100 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700101 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
102 /* gen2*/
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
104 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
106 /* skinny*/
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
108 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
110 /* xscale IOP, vega */
111 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
112 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
114 /* Fusion */
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200115 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400116};
117
118MODULE_DEVICE_TABLE(pci, megasas_pci_table);
119
120static int megasas_mgmt_majorno;
121static struct megasas_mgmt_info megasas_mgmt_info;
122static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100123static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400124
Yang, Boc3518832009-10-06 14:18:02 -0600125static int megasas_poll_wait_aen;
126static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600127static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800128u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600129static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700130
Yang, Boc3518832009-10-06 14:18:02 -0600131/* define lock for aen poll */
132spinlock_t poll_aen_lock;
133
adam radford9c915a82010-12-21 13:34:31 -0800134void
bo yang7343eb62007-11-09 04:35:44 -0500135megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
136 u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -0800137
adam radfordcd50ba82010-12-21 10:23:23 -0800138static irqreturn_t megasas_isr(int irq, void *devp);
139static u32
140megasas_init_adapter_mfi(struct megasas_instance *instance);
141u32
142megasas_build_and_issue_cmd(struct megasas_instance *instance,
143 struct scsi_cmnd *scmd);
144static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800145void
146megasas_release_fusion(struct megasas_instance *instance);
147int
148megasas_ioc_init_fusion(struct megasas_instance *instance);
149void
150megasas_free_cmds_fusion(struct megasas_instance *instance);
151u8
152megasas_get_map_info(struct megasas_instance *instance);
153int
154megasas_sync_map_info(struct megasas_instance *instance);
155int
156wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
157void megasas_reset_reply_desc(struct megasas_instance *instance);
158u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
159 struct LD_LOAD_BALANCE_INFO *lbInfo);
160int megasas_reset_fusion(struct Scsi_Host *shost);
161void megasas_fusion_ocr_wq(struct work_struct *work);
adam radfordcd50ba82010-12-21 10:23:23 -0800162
163void
164megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
165{
166 instance->instancet->fire_cmd(instance,
167 cmd->frame_phys_addr, 0, instance->reg_set);
168}
bo yang7343eb62007-11-09 04:35:44 -0500169
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400170/**
171 * megasas_get_cmd - Get a command from the free pool
172 * @instance: Adapter soft state
173 *
174 * Returns a free command from the pool
175 */
adam radford9c915a82010-12-21 13:34:31 -0800176struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400177 *instance)
178{
179 unsigned long flags;
180 struct megasas_cmd *cmd = NULL;
181
182 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
183
184 if (!list_empty(&instance->cmd_pool)) {
185 cmd = list_entry((&instance->cmd_pool)->next,
186 struct megasas_cmd, list);
187 list_del_init(&cmd->list);
188 } else {
189 printk(KERN_ERR "megasas: Command pool empty!\n");
190 }
191
192 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
193 return cmd;
194}
195
196/**
197 * megasas_return_cmd - Return a cmd to free command pool
198 * @instance: Adapter soft state
199 * @cmd: Command packet to be returned to free command pool
200 */
adam radford9c915a82010-12-21 13:34:31 -0800201inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400202megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
203{
204 unsigned long flags;
205
206 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
207
208 cmd->scmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800209 cmd->frame_count = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400210 list_add_tail(&cmd->list, &instance->cmd_pool);
211
212 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
213}
214
Sumant Patro1341c932006-01-25 12:02:40 -0800215
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400216/**
adam radford0d490162010-12-14 19:17:17 -0800217* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800218* (deviceid : 1064R, PERC5) controllers
219*/
220
221/**
222 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400223 * @regs: MFI register set
224 */
225static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800226megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400227{
bo yang39a98552010-09-22 22:36:29 -0400228 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400229
230 /* Dummy readl to force pci flush */
231 readl(&regs->outbound_intr_mask);
232}
233
234/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700235 * megasas_disable_intr_xscale -Disables interrupt
236 * @regs: MFI register set
237 */
238static inline void
239megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
240{
241 u32 mask = 0x1f;
242 writel(mask, &regs->outbound_intr_mask);
243 /* Dummy readl to force pci flush */
244 readl(&regs->outbound_intr_mask);
245}
246
247/**
Sumant Patro1341c932006-01-25 12:02:40 -0800248 * megasas_read_fw_status_reg_xscale - returns the current FW status value
249 * @regs: MFI register set
250 */
251static u32
252megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
253{
254 return readl(&(regs)->outbound_msg_0);
255}
256/**
257 * megasas_clear_interrupt_xscale - Check & clear interrupt
258 * @regs: MFI register set
259 */
adam radford0d490162010-12-14 19:17:17 -0800260static int
Sumant Patro1341c932006-01-25 12:02:40 -0800261megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
262{
263 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400264 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800265 /*
266 * Check if it is our interrupt
267 */
268 status = readl(&regs->outbound_intr_status);
269
bo yang39a98552010-09-22 22:36:29 -0400270 if (status & MFI_OB_INTR_STATUS_MASK)
271 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
272 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
273 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800274
275 /*
276 * Clear the interrupt by writing back the same value
277 */
bo yang39a98552010-09-22 22:36:29 -0400278 if (mfiStatus)
279 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800280
Yang, Bo06f579d2008-08-10 12:42:37 -0700281 /* Dummy readl to force pci flush */
282 readl(&regs->outbound_intr_status);
283
bo yang39a98552010-09-22 22:36:29 -0400284 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800285}
286
287/**
288 * megasas_fire_cmd_xscale - Sends command to the FW
289 * @frame_phys_addr : Physical address of cmd
290 * @frame_count : Number of frames for the command
291 * @regs : MFI register set
292 */
adam radford0d490162010-12-14 19:17:17 -0800293static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600294megasas_fire_cmd_xscale(struct megasas_instance *instance,
295 dma_addr_t frame_phys_addr,
296 u32 frame_count,
297 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800298{
bo yang39a98552010-09-22 22:36:29 -0400299 unsigned long flags;
300 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800301 writel((frame_phys_addr >> 3)|(frame_count),
302 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400303 spin_unlock_irqrestore(&instance->hba_lock, flags);
304}
305
306/**
307 * megasas_adp_reset_xscale - For controller reset
308 * @regs: MFI register set
309 */
310static int
311megasas_adp_reset_xscale(struct megasas_instance *instance,
312 struct megasas_register_set __iomem *regs)
313{
314 u32 i;
315 u32 pcidata;
316 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
317
318 for (i = 0; i < 3; i++)
319 msleep(1000); /* sleep for 3 secs */
320 pcidata = 0;
321 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
322 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
323 if (pcidata & 0x2) {
324 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
325 pcidata &= ~0x2;
326 pci_write_config_dword(instance->pdev,
327 MFI_1068_PCSR_OFFSET, pcidata);
328
329 for (i = 0; i < 2; i++)
330 msleep(1000); /* need to wait 2 secs again */
331
332 pcidata = 0;
333 pci_read_config_dword(instance->pdev,
334 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
335 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
336 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
337 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
338 pcidata = 0;
339 pci_write_config_dword(instance->pdev,
340 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
341 }
342 }
343 return 0;
344}
345
346/**
347 * megasas_check_reset_xscale - For controller reset check
348 * @regs: MFI register set
349 */
350static int
351megasas_check_reset_xscale(struct megasas_instance *instance,
352 struct megasas_register_set __iomem *regs)
353{
354 u32 consumer;
355 consumer = *instance->consumer;
356
357 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
358 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
359 return 1;
360 }
361 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800362}
363
364static struct megasas_instance_template megasas_instance_template_xscale = {
365
366 .fire_cmd = megasas_fire_cmd_xscale,
367 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700368 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800369 .clear_intr = megasas_clear_intr_xscale,
370 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400371 .adp_reset = megasas_adp_reset_xscale,
372 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800373 .service_isr = megasas_isr,
374 .tasklet = megasas_complete_cmd_dpc,
375 .init_adapter = megasas_init_adapter_mfi,
376 .build_and_issue_cmd = megasas_build_and_issue_cmd,
377 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800378};
379
380/**
adam radford0d490162010-12-14 19:17:17 -0800381* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800382* to xscale (deviceid : 1064R, PERC5) controllers
383*/
384
385/**
adam radford0d490162010-12-14 19:17:17 -0800386* The following functions are defined for ppc (deviceid : 0x60)
Sumant Patrof9876f02006-02-03 15:34:35 -0800387* controllers
388*/
389
390/**
391 * megasas_enable_intr_ppc - Enables interrupts
392 * @regs: MFI register set
393 */
394static inline void
395megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
396{
397 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800398
bo yang39a98552010-09-22 22:36:29 -0400399 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800400
401 /* Dummy readl to force pci flush */
402 readl(&regs->outbound_intr_mask);
403}
404
405/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700406 * megasas_disable_intr_ppc - Disable interrupt
407 * @regs: MFI register set
408 */
409static inline void
410megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
411{
412 u32 mask = 0xFFFFFFFF;
413 writel(mask, &regs->outbound_intr_mask);
414 /* Dummy readl to force pci flush */
415 readl(&regs->outbound_intr_mask);
416}
417
418/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800419 * megasas_read_fw_status_reg_ppc - returns the current FW status value
420 * @regs: MFI register set
421 */
422static u32
423megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
424{
425 return readl(&(regs)->outbound_scratch_pad);
426}
427
428/**
429 * megasas_clear_interrupt_ppc - Check & clear interrupt
430 * @regs: MFI register set
431 */
adam radford0d490162010-12-14 19:17:17 -0800432static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800433megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
434{
435 u32 status;
436 /*
437 * Check if it is our interrupt
438 */
439 status = readl(&regs->outbound_intr_status);
440
441 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
bo yang39a98552010-09-22 22:36:29 -0400442 return 0;
Sumant Patrof9876f02006-02-03 15:34:35 -0800443 }
444
445 /*
446 * Clear the interrupt by writing back the same value
447 */
448 writel(status, &regs->outbound_doorbell_clear);
449
Yang, Bo06f579d2008-08-10 12:42:37 -0700450 /* Dummy readl to force pci flush */
451 readl(&regs->outbound_doorbell_clear);
452
bo yang39a98552010-09-22 22:36:29 -0400453 return 1;
Sumant Patrof9876f02006-02-03 15:34:35 -0800454}
455/**
456 * megasas_fire_cmd_ppc - Sends command to the FW
457 * @frame_phys_addr : Physical address of cmd
458 * @frame_count : Number of frames for the command
459 * @regs : MFI register set
460 */
adam radford0d490162010-12-14 19:17:17 -0800461static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600462megasas_fire_cmd_ppc(struct megasas_instance *instance,
463 dma_addr_t frame_phys_addr,
464 u32 frame_count,
465 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800466{
bo yang39a98552010-09-22 22:36:29 -0400467 unsigned long flags;
468 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800469 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800470 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400471 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800472}
473
bo yang39a98552010-09-22 22:36:29 -0400474/**
475 * megasas_adp_reset_ppc - For controller reset
476 * @regs: MFI register set
477 */
478static int
479megasas_adp_reset_ppc(struct megasas_instance *instance,
480 struct megasas_register_set __iomem *regs)
481{
482 return 0;
483}
484
485/**
486 * megasas_check_reset_ppc - For controller reset check
487 * @regs: MFI register set
488 */
489static int
490megasas_check_reset_ppc(struct megasas_instance *instance,
491 struct megasas_register_set __iomem *regs)
492{
493 return 0;
494}
Sumant Patrof9876f02006-02-03 15:34:35 -0800495static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800496
Sumant Patrof9876f02006-02-03 15:34:35 -0800497 .fire_cmd = megasas_fire_cmd_ppc,
498 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700499 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800500 .clear_intr = megasas_clear_intr_ppc,
501 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
bo yang39a98552010-09-22 22:36:29 -0400502 .adp_reset = megasas_adp_reset_ppc,
503 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800504 .service_isr = megasas_isr,
505 .tasklet = megasas_complete_cmd_dpc,
506 .init_adapter = megasas_init_adapter_mfi,
507 .build_and_issue_cmd = megasas_build_and_issue_cmd,
508 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800509};
510
511/**
Yang, Bo87911122009-10-06 14:31:54 -0600512 * megasas_enable_intr_skinny - Enables interrupts
513 * @regs: MFI register set
514 */
515static inline void
516megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
517{
518 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
519
520 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
521
522 /* Dummy readl to force pci flush */
523 readl(&regs->outbound_intr_mask);
524}
525
526/**
527 * megasas_disable_intr_skinny - Disables interrupt
528 * @regs: MFI register set
529 */
530static inline void
531megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
532{
533 u32 mask = 0xFFFFFFFF;
534 writel(mask, &regs->outbound_intr_mask);
535 /* Dummy readl to force pci flush */
536 readl(&regs->outbound_intr_mask);
537}
538
539/**
540 * megasas_read_fw_status_reg_skinny - returns the current FW status value
541 * @regs: MFI register set
542 */
543static u32
544megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
545{
546 return readl(&(regs)->outbound_scratch_pad);
547}
548
549/**
550 * megasas_clear_interrupt_skinny - Check & clear interrupt
551 * @regs: MFI register set
552 */
553static int
554megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
555{
556 u32 status;
557 /*
558 * Check if it is our interrupt
559 */
560 status = readl(&regs->outbound_intr_status);
561
562 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400563 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600564 }
565
566 /*
567 * Clear the interrupt by writing back the same value
568 */
569 writel(status, &regs->outbound_intr_status);
570
571 /*
572 * dummy read to flush PCI
573 */
574 readl(&regs->outbound_intr_status);
575
bo yang39a98552010-09-22 22:36:29 -0400576 return 1;
Yang, Bo87911122009-10-06 14:31:54 -0600577}
578
579/**
580 * megasas_fire_cmd_skinny - Sends command to the FW
581 * @frame_phys_addr : Physical address of cmd
582 * @frame_count : Number of frames for the command
583 * @regs : MFI register set
584 */
585static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600586megasas_fire_cmd_skinny(struct megasas_instance *instance,
587 dma_addr_t frame_phys_addr,
588 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600589 struct megasas_register_set __iomem *regs)
590{
Yang, Bo0c79e682009-10-06 14:47:35 -0600591 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400592 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600593 writel(0, &(regs)->inbound_high_queue_port);
594 writel((frame_phys_addr | (frame_count<<1))|1,
595 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400596 spin_unlock_irqrestore(&instance->hba_lock, flags);
597}
598
599/**
600 * megasas_adp_reset_skinny - For controller reset
601 * @regs: MFI register set
602 */
603static int
604megasas_adp_reset_skinny(struct megasas_instance *instance,
605 struct megasas_register_set __iomem *regs)
606{
607 return 0;
608}
609
610/**
611 * megasas_check_reset_skinny - For controller reset check
612 * @regs: MFI register set
613 */
614static int
615megasas_check_reset_skinny(struct megasas_instance *instance,
616 struct megasas_register_set __iomem *regs)
617{
618 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600619}
620
621static struct megasas_instance_template megasas_instance_template_skinny = {
622
623 .fire_cmd = megasas_fire_cmd_skinny,
624 .enable_intr = megasas_enable_intr_skinny,
625 .disable_intr = megasas_disable_intr_skinny,
626 .clear_intr = megasas_clear_intr_skinny,
627 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
bo yang39a98552010-09-22 22:36:29 -0400628 .adp_reset = megasas_adp_reset_skinny,
629 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800630 .service_isr = megasas_isr,
631 .tasklet = megasas_complete_cmd_dpc,
632 .init_adapter = megasas_init_adapter_mfi,
633 .build_and_issue_cmd = megasas_build_and_issue_cmd,
634 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600635};
636
637
638/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700639* The following functions are defined for gen2 (deviceid : 0x78 0x79)
640* controllers
641*/
642
643/**
644 * megasas_enable_intr_gen2 - Enables interrupts
645 * @regs: MFI register set
646 */
647static inline void
648megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
649{
650 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
651
652 /* write ~0x00000005 (4 & 1) to the intr mask*/
653 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
654
655 /* Dummy readl to force pci flush */
656 readl(&regs->outbound_intr_mask);
657}
658
659/**
660 * megasas_disable_intr_gen2 - Disables interrupt
661 * @regs: MFI register set
662 */
663static inline void
664megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
665{
666 u32 mask = 0xFFFFFFFF;
667 writel(mask, &regs->outbound_intr_mask);
668 /* Dummy readl to force pci flush */
669 readl(&regs->outbound_intr_mask);
670}
671
672/**
673 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
674 * @regs: MFI register set
675 */
676static u32
677megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
678{
679 return readl(&(regs)->outbound_scratch_pad);
680}
681
682/**
683 * megasas_clear_interrupt_gen2 - Check & clear interrupt
684 * @regs: MFI register set
685 */
686static int
687megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
688{
689 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400690 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700691 /*
692 * Check if it is our interrupt
693 */
694 status = readl(&regs->outbound_intr_status);
695
bo yang39a98552010-09-22 22:36:29 -0400696 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
697 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
698 }
699 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
700 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
701 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700702
703 /*
704 * Clear the interrupt by writing back the same value
705 */
bo yang39a98552010-09-22 22:36:29 -0400706 if (mfiStatus)
707 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700708
709 /* Dummy readl to force pci flush */
710 readl(&regs->outbound_intr_status);
711
bo yang39a98552010-09-22 22:36:29 -0400712 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700713}
714/**
715 * megasas_fire_cmd_gen2 - Sends command to the FW
716 * @frame_phys_addr : Physical address of cmd
717 * @frame_count : Number of frames for the command
718 * @regs : MFI register set
719 */
720static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600721megasas_fire_cmd_gen2(struct megasas_instance *instance,
722 dma_addr_t frame_phys_addr,
723 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700724 struct megasas_register_set __iomem *regs)
725{
bo yang39a98552010-09-22 22:36:29 -0400726 unsigned long flags;
727 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700728 writel((frame_phys_addr | (frame_count<<1))|1,
729 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400730 spin_unlock_irqrestore(&instance->hba_lock, flags);
731}
732
733/**
734 * megasas_adp_reset_gen2 - For controller reset
735 * @regs: MFI register set
736 */
737static int
738megasas_adp_reset_gen2(struct megasas_instance *instance,
739 struct megasas_register_set __iomem *reg_set)
740{
741 u32 retry = 0 ;
742 u32 HostDiag;
743
744 writel(0, &reg_set->seq_offset);
745 writel(4, &reg_set->seq_offset);
746 writel(0xb, &reg_set->seq_offset);
747 writel(2, &reg_set->seq_offset);
748 writel(7, &reg_set->seq_offset);
749 writel(0xd, &reg_set->seq_offset);
750 msleep(1000);
751
752 HostDiag = (u32)readl(&reg_set->host_diag);
753
754 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
755 msleep(100);
756 HostDiag = (u32)readl(&reg_set->host_diag);
757 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
758 retry, HostDiag);
759
760 if (retry++ >= 100)
761 return 1;
762
763 }
764
765 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
766
767 writel((HostDiag | DIAG_RESET_ADAPTER), &reg_set->host_diag);
768
769 ssleep(10);
770
771 HostDiag = (u32)readl(&reg_set->host_diag);
772 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
773 msleep(100);
774 HostDiag = (u32)readl(&reg_set->host_diag);
775 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
776 retry, HostDiag);
777
778 if (retry++ >= 1000)
779 return 1;
780
781 }
782 return 0;
783}
784
785/**
786 * megasas_check_reset_gen2 - For controller reset check
787 * @regs: MFI register set
788 */
789static int
790megasas_check_reset_gen2(struct megasas_instance *instance,
791 struct megasas_register_set __iomem *regs)
792{
Yang, Bo707e09b2010-10-12 07:20:27 -0600793 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
794 return 1;
795 }
796
bo yang39a98552010-09-22 22:36:29 -0400797 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700798}
799
800static struct megasas_instance_template megasas_instance_template_gen2 = {
801
802 .fire_cmd = megasas_fire_cmd_gen2,
803 .enable_intr = megasas_enable_intr_gen2,
804 .disable_intr = megasas_disable_intr_gen2,
805 .clear_intr = megasas_clear_intr_gen2,
806 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400807 .adp_reset = megasas_adp_reset_gen2,
808 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800809 .service_isr = megasas_isr,
810 .tasklet = megasas_complete_cmd_dpc,
811 .init_adapter = megasas_init_adapter_mfi,
812 .build_and_issue_cmd = megasas_build_and_issue_cmd,
813 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700814};
815
816/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800817* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400818* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800819*/
820
adam radford9c915a82010-12-21 13:34:31 -0800821/*
822 * Template added for TB (Fusion)
823 */
824extern struct megasas_instance_template megasas_instance_template_fusion;
825
Sumant Patrof9876f02006-02-03 15:34:35 -0800826/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400827 * megasas_issue_polled - Issues a polling command
828 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800829 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400830 *
831 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
832 */
adam radford9c915a82010-12-21 13:34:31 -0800833int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400834megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
835{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400836
837 struct megasas_header *frame_hdr = &cmd->frame->hdr;
838
839 frame_hdr->cmd_status = 0xFF;
840 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
841
842 /*
843 * Issue the frame using inbound queue port
844 */
adam radford9c915a82010-12-21 13:34:31 -0800845 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400846
847 /*
848 * Wait for cmd_status to change
849 */
adam radford9c915a82010-12-21 13:34:31 -0800850 return wait_and_poll(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400851}
852
853/**
854 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
855 * @instance: Adapter soft state
856 * @cmd: Command to be issued
857 *
858 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700859 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400860 * Used to issue ioctl commands.
861 */
862static int
863megasas_issue_blocked_cmd(struct megasas_instance *instance,
864 struct megasas_cmd *cmd)
865{
866 cmd->cmd_status = ENODATA;
867
adam radford9c915a82010-12-21 13:34:31 -0800868 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400869
bo yang39a98552010-09-22 22:36:29 -0400870 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400871
872 return 0;
873}
874
875/**
876 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
877 * @instance: Adapter soft state
878 * @cmd_to_abort: Previously issued cmd to be aborted
879 *
880 * MFI firmware can abort previously issued AEN comamnd (automatic event
881 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700882 * cmd and waits for return status.
883 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400884 */
885static int
886megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
887 struct megasas_cmd *cmd_to_abort)
888{
889 struct megasas_cmd *cmd;
890 struct megasas_abort_frame *abort_fr;
891
892 cmd = megasas_get_cmd(instance);
893
894 if (!cmd)
895 return -1;
896
897 abort_fr = &cmd->frame->abort;
898
899 /*
900 * Prepare and issue the abort frame
901 */
902 abort_fr->cmd = MFI_CMD_ABORT;
903 abort_fr->cmd_status = 0xFF;
904 abort_fr->flags = 0;
905 abort_fr->abort_context = cmd_to_abort->index;
906 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
907 abort_fr->abort_mfi_phys_addr_hi = 0;
908
909 cmd->sync_cmd = 1;
910 cmd->cmd_status = 0xFF;
911
adam radford9c915a82010-12-21 13:34:31 -0800912 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400913
914 /*
915 * Wait for this cmd to complete
916 */
bo yang39a98552010-09-22 22:36:29 -0400917 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
918 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400919
920 megasas_return_cmd(instance, cmd);
921 return 0;
922}
923
924/**
925 * megasas_make_sgl32 - Prepares 32-bit SGL
926 * @instance: Adapter soft state
927 * @scp: SCSI command from the mid-layer
928 * @mfi_sgl: SGL to be filled in
929 *
930 * If successful, this function returns the number of SG elements. Otherwise,
931 * it returnes -1.
932 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800933static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400934megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
935 union megasas_sgl *mfi_sgl)
936{
937 int i;
938 int sge_count;
939 struct scatterlist *os_sgl;
940
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900941 sge_count = scsi_dma_map(scp);
942 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400943
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900944 if (sge_count) {
945 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
946 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
947 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
948 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400949 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400950 return sge_count;
951}
952
953/**
954 * megasas_make_sgl64 - Prepares 64-bit SGL
955 * @instance: Adapter soft state
956 * @scp: SCSI command from the mid-layer
957 * @mfi_sgl: SGL to be filled in
958 *
959 * If successful, this function returns the number of SG elements. Otherwise,
960 * it returnes -1.
961 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800962static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400963megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
964 union megasas_sgl *mfi_sgl)
965{
966 int i;
967 int sge_count;
968 struct scatterlist *os_sgl;
969
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900970 sge_count = scsi_dma_map(scp);
971 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400972
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900973 if (sge_count) {
974 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
975 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
976 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
977 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400978 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400979 return sge_count;
980}
981
Yang, Bof4c9a132009-10-06 14:43:28 -0600982/**
983 * megasas_make_sgl_skinny - Prepares IEEE SGL
984 * @instance: Adapter soft state
985 * @scp: SCSI command from the mid-layer
986 * @mfi_sgl: SGL to be filled in
987 *
988 * If successful, this function returns the number of SG elements. Otherwise,
989 * it returnes -1.
990 */
991static int
992megasas_make_sgl_skinny(struct megasas_instance *instance,
993 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
994{
995 int i;
996 int sge_count;
997 struct scatterlist *os_sgl;
998
999 sge_count = scsi_dma_map(scp);
1000
1001 if (sge_count) {
1002 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1003 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1004 mfi_sgl->sge_skinny[i].phys_addr =
1005 sg_dma_address(os_sgl);
Yang, Bo707e09b2010-10-12 07:20:27 -06001006 mfi_sgl->sge_skinny[i].flag = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06001007 }
1008 }
1009 return sge_count;
1010}
1011
Sumant Patrob1df99d2006-10-03 12:40:47 -07001012 /**
1013 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001014 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001015 * @sge_count : number of sg elements
1016 *
1017 * Returns the number of frames required for numnber of sge's (sge_count)
1018 */
1019
Yang, Bof4c9a132009-10-06 14:43:28 -06001020static u32 megasas_get_frame_count(struct megasas_instance *instance,
1021 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001022{
1023 int num_cnt;
1024 int sge_bytes;
1025 u32 sge_sz;
1026 u32 frame_count=0;
1027
1028 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1029 sizeof(struct megasas_sge32);
1030
Yang, Bof4c9a132009-10-06 14:43:28 -06001031 if (instance->flag_ieee) {
1032 sge_sz = sizeof(struct megasas_sge_skinny);
1033 }
1034
Sumant Patrob1df99d2006-10-03 12:40:47 -07001035 /*
bo yangd532dbe2008-03-17 03:36:43 -04001036 * Main frame can contain 2 SGEs for 64-bit SGLs and
1037 * 3 SGEs for 32-bit SGLs for ldio &
1038 * 1 SGEs for 64-bit SGLs and
1039 * 2 SGEs for 32-bit SGLs for pthru frame
1040 */
1041 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001042 if (instance->flag_ieee == 1) {
1043 num_cnt = sge_count - 1;
1044 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001045 num_cnt = sge_count - 1;
1046 else
1047 num_cnt = sge_count - 2;
1048 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001049 if (instance->flag_ieee == 1) {
1050 num_cnt = sge_count - 1;
1051 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001052 num_cnt = sge_count - 2;
1053 else
1054 num_cnt = sge_count - 3;
1055 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001056
1057 if(num_cnt>0){
1058 sge_bytes = sge_sz * num_cnt;
1059
1060 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1061 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1062 }
1063 /* Main frame */
1064 frame_count +=1;
1065
1066 if (frame_count > 7)
1067 frame_count = 8;
1068 return frame_count;
1069}
1070
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001071/**
1072 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1073 * @instance: Adapter soft state
1074 * @scp: SCSI command
1075 * @cmd: Command to be prepared in
1076 *
1077 * This function prepares CDB commands. These are typcially pass-through
1078 * commands to the devices.
1079 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001080static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001081megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1082 struct megasas_cmd *cmd)
1083{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001084 u32 is_logical;
1085 u32 device_id;
1086 u16 flags = 0;
1087 struct megasas_pthru_frame *pthru;
1088
1089 is_logical = MEGASAS_IS_LOGICAL(scp);
1090 device_id = MEGASAS_DEV_INDEX(instance, scp);
1091 pthru = (struct megasas_pthru_frame *)cmd->frame;
1092
1093 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1094 flags = MFI_FRAME_DIR_WRITE;
1095 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1096 flags = MFI_FRAME_DIR_READ;
1097 else if (scp->sc_data_direction == PCI_DMA_NONE)
1098 flags = MFI_FRAME_DIR_NONE;
1099
Yang, Bof4c9a132009-10-06 14:43:28 -06001100 if (instance->flag_ieee == 1) {
1101 flags |= MFI_FRAME_IEEE;
1102 }
1103
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001104 /*
1105 * Prepare the DCDB frame
1106 */
1107 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1108 pthru->cmd_status = 0x0;
1109 pthru->scsi_status = 0x0;
1110 pthru->target_id = device_id;
1111 pthru->lun = scp->device->lun;
1112 pthru->cdb_len = scp->cmd_len;
1113 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001114 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001115 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001116 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001117
1118 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1119
1120 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001121 * If the command is for the tape device, set the
1122 * pthru timeout to the os layer timeout value.
1123 */
1124 if (scp->device->type == TYPE_TAPE) {
1125 if ((scp->request->timeout / HZ) > 0xFFFF)
1126 pthru->timeout = 0xFFFF;
1127 else
1128 pthru->timeout = scp->request->timeout / HZ;
1129 }
1130
1131 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001132 * Construct SGL
1133 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001134 if (instance->flag_ieee == 1) {
1135 pthru->flags |= MFI_FRAME_SGL64;
1136 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1137 &pthru->sgl);
1138 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001139 pthru->flags |= MFI_FRAME_SGL64;
1140 pthru->sge_count = megasas_make_sgl64(instance, scp,
1141 &pthru->sgl);
1142 } else
1143 pthru->sge_count = megasas_make_sgl32(instance, scp,
1144 &pthru->sgl);
1145
Yang, Bobdc6fb82009-12-06 08:30:19 -07001146 if (pthru->sge_count > instance->max_num_sge) {
1147 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1148 pthru->sge_count);
1149 return 0;
1150 }
1151
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001152 /*
1153 * Sense info specific
1154 */
1155 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1156 pthru->sense_buf_phys_addr_hi = 0;
1157 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1158
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001159 /*
1160 * Compute the total number of frames this command consumes. FW uses
1161 * this number to pull sufficient number of frames from host memory.
1162 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001163 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001164 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165
1166 return cmd->frame_count;
1167}
1168
1169/**
1170 * megasas_build_ldio - Prepares IOs to logical devices
1171 * @instance: Adapter soft state
1172 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001173 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001174 *
1175 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1176 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001177static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001178megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1179 struct megasas_cmd *cmd)
1180{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001181 u32 device_id;
1182 u8 sc = scp->cmnd[0];
1183 u16 flags = 0;
1184 struct megasas_io_frame *ldio;
1185
1186 device_id = MEGASAS_DEV_INDEX(instance, scp);
1187 ldio = (struct megasas_io_frame *)cmd->frame;
1188
1189 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1190 flags = MFI_FRAME_DIR_WRITE;
1191 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1192 flags = MFI_FRAME_DIR_READ;
1193
Yang, Bof4c9a132009-10-06 14:43:28 -06001194 if (instance->flag_ieee == 1) {
1195 flags |= MFI_FRAME_IEEE;
1196 }
1197
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001198 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001199 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001200 */
1201 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1202 ldio->cmd_status = 0x0;
1203 ldio->scsi_status = 0x0;
1204 ldio->target_id = device_id;
1205 ldio->timeout = 0;
1206 ldio->reserved_0 = 0;
1207 ldio->pad_0 = 0;
1208 ldio->flags = flags;
1209 ldio->start_lba_hi = 0;
1210 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1211
1212 /*
1213 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1214 */
1215 if (scp->cmd_len == 6) {
1216 ldio->lba_count = (u32) scp->cmnd[4];
1217 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1218 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1219
1220 ldio->start_lba_lo &= 0x1FFFFF;
1221 }
1222
1223 /*
1224 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1225 */
1226 else if (scp->cmd_len == 10) {
1227 ldio->lba_count = (u32) scp->cmnd[8] |
1228 ((u32) scp->cmnd[7] << 8);
1229 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1230 ((u32) scp->cmnd[3] << 16) |
1231 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1232 }
1233
1234 /*
1235 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1236 */
1237 else if (scp->cmd_len == 12) {
1238 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1239 ((u32) scp->cmnd[7] << 16) |
1240 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1241
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 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1249 */
1250 else if (scp->cmd_len == 16) {
1251 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1252 ((u32) scp->cmnd[11] << 16) |
1253 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1254
1255 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1256 ((u32) scp->cmnd[7] << 16) |
1257 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1258
1259 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1260 ((u32) scp->cmnd[3] << 16) |
1261 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1262
1263 }
1264
1265 /*
1266 * Construct SGL
1267 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001268 if (instance->flag_ieee) {
1269 ldio->flags |= MFI_FRAME_SGL64;
1270 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1271 &ldio->sgl);
1272 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001273 ldio->flags |= MFI_FRAME_SGL64;
1274 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1275 } else
1276 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1277
Yang, Bobdc6fb82009-12-06 08:30:19 -07001278 if (ldio->sge_count > instance->max_num_sge) {
1279 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1280 ldio->sge_count);
1281 return 0;
1282 }
1283
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001284 /*
1285 * Sense info specific
1286 */
1287 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1288 ldio->sense_buf_phys_addr_hi = 0;
1289 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1290
Sumant Patrob1df99d2006-10-03 12:40:47 -07001291 /*
1292 * Compute the total number of frames this command consumes. FW uses
1293 * this number to pull sufficient number of frames from host memory.
1294 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001295 cmd->frame_count = megasas_get_frame_count(instance,
1296 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001297
1298 return cmd->frame_count;
1299}
1300
1301/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001302 * megasas_is_ldio - Checks if the cmd is for logical drive
1303 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001304 *
Sumant Patrocb59aa62006-01-25 11:53:25 -08001305 * Called by megasas_queue_command to find out if the command to be queued
adam radford0d490162010-12-14 19:17:17 -08001306 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001307 */
adam radford9c915a82010-12-21 13:34:31 -08001308inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001309{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001310 if (!MEGASAS_IS_LOGICAL(cmd))
1311 return 0;
1312 switch (cmd->cmnd[0]) {
1313 case READ_10:
1314 case WRITE_10:
1315 case READ_12:
1316 case WRITE_12:
1317 case READ_6:
1318 case WRITE_6:
1319 case READ_16:
1320 case WRITE_16:
1321 return 1;
1322 default:
1323 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001324 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001325}
1326
Sumant Patro658dced2006-10-03 13:09:14 -07001327 /**
1328 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1329 * in FW
1330 * @instance: Adapter soft state
1331 */
1332static inline void
1333megasas_dump_pending_frames(struct megasas_instance *instance)
1334{
1335 struct megasas_cmd *cmd;
1336 int i,n;
1337 union megasas_sgl *mfi_sgl;
1338 struct megasas_io_frame *ldio;
1339 struct megasas_pthru_frame *pthru;
1340 u32 sgcount;
1341 u32 max_cmd = instance->max_fw_cmds;
1342
1343 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1344 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1345 if (IS_DMA64)
1346 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1347 else
1348 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1349
1350 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1351 for (i = 0; i < max_cmd; i++) {
1352 cmd = instance->cmd_list[i];
1353 if(!cmd->scmd)
1354 continue;
1355 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1356 if (megasas_is_ldio(cmd->scmd)){
1357 ldio = (struct megasas_io_frame *)cmd->frame;
1358 mfi_sgl = &ldio->sgl;
1359 sgcount = ldio->sge_count;
1360 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);
1361 }
1362 else {
1363 pthru = (struct megasas_pthru_frame *) cmd->frame;
1364 mfi_sgl = &pthru->sgl;
1365 sgcount = pthru->sge_count;
1366 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);
1367 }
1368 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1369 for (n = 0; n < sgcount; n++){
1370 if (IS_DMA64)
1371 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) ;
1372 else
1373 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1374 }
1375 }
1376 printk(KERN_ERR "\n");
1377 } /*for max_cmd*/
1378 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1379 for (i = 0; i < max_cmd; i++) {
1380
1381 cmd = instance->cmd_list[i];
1382
1383 if(cmd->sync_cmd == 1){
1384 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1385 }
1386 }
1387 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1388}
1389
adam radfordcd50ba82010-12-21 10:23:23 -08001390u32
1391megasas_build_and_issue_cmd(struct megasas_instance *instance,
1392 struct scsi_cmnd *scmd)
1393{
1394 struct megasas_cmd *cmd;
1395 u32 frame_count;
1396
1397 cmd = megasas_get_cmd(instance);
1398 if (!cmd)
1399 return SCSI_MLQUEUE_HOST_BUSY;
1400
1401 /*
1402 * Logical drive command
1403 */
1404 if (megasas_is_ldio(scmd))
1405 frame_count = megasas_build_ldio(instance, scmd, cmd);
1406 else
1407 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1408
1409 if (!frame_count)
1410 goto out_return_cmd;
1411
1412 cmd->scmd = scmd;
1413 scmd->SCp.ptr = (char *)cmd;
1414
1415 /*
1416 * Issue the command to the FW
1417 */
1418 atomic_inc(&instance->fw_outstanding);
1419
1420 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1421 cmd->frame_count-1, instance->reg_set);
1422 /*
1423 * Check if we have pend cmds to be completed
1424 */
1425 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1426 tasklet_schedule(&instance->isr_tasklet);
1427
1428 return 0;
1429out_return_cmd:
1430 megasas_return_cmd(instance, cmd);
1431 return 1;
1432}
1433
1434
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001435/**
1436 * megasas_queue_command - Queue entry point
1437 * @scmd: SCSI command to be queued
1438 * @done: Callback entry point
1439 */
1440static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001441megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001442{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001443 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001444 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001445
1446 instance = (struct megasas_instance *)
1447 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001448
bo yang39a98552010-09-22 22:36:29 -04001449 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001450 return SCSI_MLQUEUE_HOST_BUSY;
1451
bo yang39a98552010-09-22 22:36:29 -04001452 spin_lock_irqsave(&instance->hba_lock, flags);
1453 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1454 spin_unlock_irqrestore(&instance->hba_lock, flags);
1455 return SCSI_MLQUEUE_HOST_BUSY;
1456 }
1457
1458 spin_unlock_irqrestore(&instance->hba_lock, flags);
1459
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001460 scmd->scsi_done = done;
1461 scmd->result = 0;
1462
Sumant Patrocb59aa62006-01-25 11:53:25 -08001463 if (MEGASAS_IS_LOGICAL(scmd) &&
1464 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1465 scmd->result = DID_BAD_TARGET << 16;
1466 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001467 }
1468
Sumant Patro02b01e02007-02-14 13:00:55 -08001469 switch (scmd->cmnd[0]) {
1470 case SYNCHRONIZE_CACHE:
1471 /*
1472 * FW takes care of flush cache on its own
1473 * No need to send it down
1474 */
1475 scmd->result = DID_OK << 16;
1476 goto out_done;
1477 default:
1478 break;
1479 }
1480
adam radfordcd50ba82010-12-21 10:23:23 -08001481 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1482 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
Sumant Patrocb59aa62006-01-25 11:53:25 -08001483 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001484 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001485
1486 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001487
Sumant Patrocb59aa62006-01-25 11:53:25 -08001488 out_done:
1489 done(scmd);
1490 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001491}
1492
Jeff Garzikf2812332010-11-16 02:10:29 -05001493static DEF_SCSI_QCMD(megasas_queue_command)
1494
Yang, Bo044833b2009-10-06 14:33:06 -06001495static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1496{
1497 int i;
1498
1499 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1500
1501 if ((megasas_mgmt_info.instance[i]) &&
1502 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1503 return megasas_mgmt_info.instance[i];
1504 }
1505
1506 return NULL;
1507}
1508
Christoph Hellwig147aab62006-02-17 12:13:48 +01001509static int megasas_slave_configure(struct scsi_device *sdev)
1510{
Yang, Bo044833b2009-10-06 14:33:06 -06001511 u16 pd_index = 0;
1512 struct megasas_instance *instance ;
1513
1514 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001515
1516 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001517 * Don't export physical disk devices to the disk driver.
1518 *
1519 * FIXME: Currently we don't export them to the midlayer at all.
1520 * That will be fixed once LSI engineers have audited the
1521 * firmware for possible issues.
1522 */
1523 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1524 sdev->type == TYPE_DISK) {
1525 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1526 sdev->id;
1527 if (instance->pd_list[pd_index].driveState ==
1528 MR_PD_STATE_SYSTEM) {
1529 blk_queue_rq_timeout(sdev->request_queue,
1530 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1531 return 0;
1532 }
1533 return -ENXIO;
1534 }
1535
1536 /*
1537 * The RAID firmware may require extended timeouts.
1538 */
1539 blk_queue_rq_timeout(sdev->request_queue,
1540 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1541 return 0;
1542}
1543
1544static int megasas_slave_alloc(struct scsi_device *sdev)
1545{
1546 u16 pd_index = 0;
1547 struct megasas_instance *instance ;
1548 instance = megasas_lookup_instance(sdev->host->host_no);
1549 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1550 (sdev->type == TYPE_DISK)) {
1551 /*
1552 * Open the OS scan to the SYSTEM PD
1553 */
1554 pd_index =
1555 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1556 sdev->id;
1557 if ((instance->pd_list[pd_index].driveState ==
1558 MR_PD_STATE_SYSTEM) &&
1559 (instance->pd_list[pd_index].driveType ==
1560 TYPE_DISK)) {
1561 return 0;
1562 }
1563 return -ENXIO;
1564 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001565 return 0;
1566}
1567
adam radford9c915a82010-12-21 13:34:31 -08001568void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001569{
1570 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
adam radford9c915a82010-12-21 13:34:31 -08001571 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1572 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)) {
1573 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
bo yang39a98552010-09-22 22:36:29 -04001574 } else {
adam radford9c915a82010-12-21 13:34:31 -08001575 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1576 }
1577}
1578
1579 /**
1580 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1581 * restored to max value
1582 * @instance: Adapter soft state
1583 *
1584 */
1585void
1586megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1587{
1588 unsigned long flags;
1589 if (instance->flag & MEGASAS_FW_BUSY
1590 && time_after(jiffies, instance->last_time + 5 * HZ)
1591 && atomic_read(&instance->fw_outstanding) < 17) {
1592
1593 spin_lock_irqsave(instance->host->host_lock, flags);
1594 instance->flag &= ~MEGASAS_FW_BUSY;
1595 if ((instance->pdev->device ==
1596 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1597 (instance->pdev->device ==
1598 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1599 instance->host->can_queue =
1600 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1601 } else
1602 instance->host->can_queue =
1603 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1604
1605 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001606 }
1607}
1608
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001609/**
bo yang7343eb62007-11-09 04:35:44 -05001610 * megasas_complete_cmd_dpc - Returns FW's controller structure
1611 * @instance_addr: Address of adapter soft state
1612 *
1613 * Tasklet to complete cmds
1614 */
1615static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1616{
1617 u32 producer;
1618 u32 consumer;
1619 u32 context;
1620 struct megasas_cmd *cmd;
1621 struct megasas_instance *instance =
1622 (struct megasas_instance *)instance_addr;
1623 unsigned long flags;
1624
1625 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001626 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001627 return;
1628
1629 spin_lock_irqsave(&instance->completion_lock, flags);
1630
1631 producer = *instance->producer;
1632 consumer = *instance->consumer;
1633
1634 while (consumer != producer) {
1635 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001636 if (context >= instance->max_fw_cmds) {
1637 printk(KERN_ERR "Unexpected context value %x\n",
1638 context);
1639 BUG();
1640 }
bo yang7343eb62007-11-09 04:35:44 -05001641
1642 cmd = instance->cmd_list[context];
1643
1644 megasas_complete_cmd(instance, cmd, DID_OK);
1645
1646 consumer++;
1647 if (consumer == (instance->max_fw_cmds + 1)) {
1648 consumer = 0;
1649 }
1650 }
1651
1652 *instance->consumer = producer;
1653
1654 spin_unlock_irqrestore(&instance->completion_lock, flags);
1655
1656 /*
1657 * Check if we can restore can_queue
1658 */
adam radford9c915a82010-12-21 13:34:31 -08001659 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05001660}
1661
Yang, Bo707e09b2010-10-12 07:20:27 -06001662static void
1663megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1664
1665static void
1666process_fw_state_change_wq(struct work_struct *work);
1667
1668void megasas_do_ocr(struct megasas_instance *instance)
1669{
1670 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1671 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1672 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1673 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1674 }
1675 instance->instancet->disable_intr(instance->reg_set);
1676 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1677 instance->issuepend_done = 0;
1678
1679 atomic_set(&instance->fw_outstanding, 0);
1680 megasas_internal_reset_defer_cmds(instance);
1681 process_fw_state_change_wq(&instance->work_init);
1682}
1683
bo yang7343eb62007-11-09 04:35:44 -05001684/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001685 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1686 * @instance: Adapter soft state
1687 *
1688 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1689 * complete all its outstanding commands. Returns error if one or more IOs
1690 * are pending after this time period. It also marks the controller dead.
1691 */
1692static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1693{
1694 int i;
bo yang39a98552010-09-22 22:36:29 -04001695 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001696 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001697 u8 adprecovery;
1698 unsigned long flags;
1699 struct list_head clist_local;
1700 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06001701 u32 fw_state;
1702 u8 kill_adapter_flag;
bo yang39a98552010-09-22 22:36:29 -04001703
1704 spin_lock_irqsave(&instance->hba_lock, flags);
1705 adprecovery = instance->adprecovery;
1706 spin_unlock_irqrestore(&instance->hba_lock, flags);
1707
1708 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1709
1710 INIT_LIST_HEAD(&clist_local);
1711 spin_lock_irqsave(&instance->hba_lock, flags);
1712 list_splice_init(&instance->internal_reset_pending_q,
1713 &clist_local);
1714 spin_unlock_irqrestore(&instance->hba_lock, flags);
1715
1716 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1717 for (i = 0; i < wait_time; i++) {
1718 msleep(1000);
1719 spin_lock_irqsave(&instance->hba_lock, flags);
1720 adprecovery = instance->adprecovery;
1721 spin_unlock_irqrestore(&instance->hba_lock, flags);
1722 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1723 break;
1724 }
1725
1726 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1727 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1728 spin_lock_irqsave(&instance->hba_lock, flags);
1729 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1730 spin_unlock_irqrestore(&instance->hba_lock, flags);
1731 return FAILED;
1732 }
1733
1734 reset_index = 0;
1735 while (!list_empty(&clist_local)) {
1736 reset_cmd = list_entry((&clist_local)->next,
1737 struct megasas_cmd, list);
1738 list_del_init(&reset_cmd->list);
1739 if (reset_cmd->scmd) {
1740 reset_cmd->scmd->result = DID_RESET << 16;
1741 printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
1742 reset_index, reset_cmd,
1743 reset_cmd->scmd->cmnd[0],
1744 reset_cmd->scmd->serial_number);
1745
1746 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1747 megasas_return_cmd(instance, reset_cmd);
1748 } else if (reset_cmd->sync_cmd) {
1749 printk(KERN_NOTICE "megasas:%p synch cmds"
1750 "reset queue\n",
1751 reset_cmd);
1752
1753 reset_cmd->cmd_status = ENODATA;
1754 instance->instancet->fire_cmd(instance,
1755 reset_cmd->frame_phys_addr,
1756 0, instance->reg_set);
1757 } else {
1758 printk(KERN_NOTICE "megasas: %p unexpected"
1759 "cmds lst\n",
1760 reset_cmd);
1761 }
1762 reset_index++;
1763 }
1764
1765 return SUCCESS;
1766 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001767
1768 for (i = 0; i < wait_time; i++) {
1769
Sumant Patroe4a082c2006-05-30 12:03:37 -07001770 int outstanding = atomic_read(&instance->fw_outstanding);
1771
1772 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001773 break;
1774
1775 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1776 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001777 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001778 /*
1779 * Call cmd completion routine. Cmd to be
1780 * be completed directly without depending on isr.
1781 */
1782 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001783 }
1784
1785 msleep(1000);
1786 }
1787
Yang, Bo707e09b2010-10-12 07:20:27 -06001788 i = 0;
1789 kill_adapter_flag = 0;
1790 do {
1791 fw_state = instance->instancet->read_fw_status_reg(
1792 instance->reg_set) & MFI_STATE_MASK;
1793 if ((fw_state == MFI_STATE_FAULT) &&
1794 (instance->disableOnlineCtrlReset == 0)) {
1795 if (i == 3) {
1796 kill_adapter_flag = 2;
1797 break;
1798 }
1799 megasas_do_ocr(instance);
1800 kill_adapter_flag = 1;
1801
1802 /* wait for 1 secs to let FW finish the pending cmds */
1803 msleep(1000);
1804 }
1805 i++;
1806 } while (i <= 3);
1807
1808 if (atomic_read(&instance->fw_outstanding) &&
1809 !kill_adapter_flag) {
1810 if (instance->disableOnlineCtrlReset == 0) {
1811
1812 megasas_do_ocr(instance);
1813
1814 /* wait for 5 secs to let FW finish the pending cmds */
1815 for (i = 0; i < wait_time; i++) {
1816 int outstanding =
1817 atomic_read(&instance->fw_outstanding);
1818 if (!outstanding)
1819 return SUCCESS;
1820 msleep(1000);
1821 }
1822 }
1823 }
1824
1825 if (atomic_read(&instance->fw_outstanding) ||
1826 (kill_adapter_flag == 2)) {
bo yang39a98552010-09-22 22:36:29 -04001827 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001828 /*
1829 * Send signal to FW to stop processing any pending cmds.
1830 * The controller will be taken offline by the OS now.
1831 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001832 if ((instance->pdev->device ==
1833 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1834 (instance->pdev->device ==
1835 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1836 writel(MFI_STOP_ADP,
adam radford9c915a82010-12-21 13:34:31 -08001837 &instance->reg_set->doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001838 } else {
1839 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001840 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001841 }
Sumant Patro658dced2006-10-03 13:09:14 -07001842 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001843 spin_lock_irqsave(&instance->hba_lock, flags);
1844 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1845 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001846 return FAILED;
1847 }
1848
bo yang39a98552010-09-22 22:36:29 -04001849 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1850
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001851 return SUCCESS;
1852}
1853
1854/**
1855 * megasas_generic_reset - Generic reset routine
1856 * @scmd: Mid-layer SCSI command
1857 *
1858 * This routine implements a generic reset handler for device, bus and host
1859 * reset requests. Device, bus and host specific reset handlers can use this
1860 * function after they do their specific tasks.
1861 */
1862static int megasas_generic_reset(struct scsi_cmnd *scmd)
1863{
1864 int ret_val;
1865 struct megasas_instance *instance;
1866
1867 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1868
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001869 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1870 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001871
bo yang39a98552010-09-22 22:36:29 -04001872 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001873 printk(KERN_ERR "megasas: cannot recover from previous reset "
1874 "failures\n");
1875 return FAILED;
1876 }
1877
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001878 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001879 if (ret_val == SUCCESS)
1880 printk(KERN_NOTICE "megasas: reset successful \n");
1881 else
1882 printk(KERN_ERR "megasas: failed to do reset\n");
1883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001884 return ret_val;
1885}
1886
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001887/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001888 * megasas_reset_timer - quiesce the adapter if required
1889 * @scmd: scsi cmnd
1890 *
1891 * Sets the FW busy flag and reduces the host->can_queue if the
1892 * cmd has not been completed within the timeout period.
1893 */
1894static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001895blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001896{
1897 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1898 struct megasas_instance *instance;
1899 unsigned long flags;
1900
1901 if (time_after(jiffies, scmd->jiffies_at_alloc +
1902 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001903 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001904 }
1905
1906 instance = cmd->instance;
1907 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1908 /* FW is busy, throttle IO */
1909 spin_lock_irqsave(instance->host->host_lock, flags);
1910
1911 instance->host->can_queue = 16;
1912 instance->last_time = jiffies;
1913 instance->flag |= MEGASAS_FW_BUSY;
1914
1915 spin_unlock_irqrestore(instance->host->host_lock, flags);
1916 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001917 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001918}
1919
1920/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001921 * megasas_reset_device - Device reset handler entry point
1922 */
1923static int megasas_reset_device(struct scsi_cmnd *scmd)
1924{
1925 int ret;
1926
1927 /*
1928 * First wait for all commands to complete
1929 */
1930 ret = megasas_generic_reset(scmd);
1931
1932 return ret;
1933}
1934
1935/**
1936 * megasas_reset_bus_host - Bus & host reset handler entry point
1937 */
1938static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1939{
1940 int ret;
adam radford9c915a82010-12-21 13:34:31 -08001941 struct megasas_instance *instance;
1942 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001943
1944 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001945 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001946 */
adam radford9c915a82010-12-21 13:34:31 -08001947 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
1948 ret = megasas_reset_fusion(scmd->device->host);
1949 else
1950 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001951
1952 return ret;
1953}
1954
1955/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001956 * megasas_bios_param - Returns disk geometry for a disk
1957 * @sdev: device handle
1958 * @bdev: block device
1959 * @capacity: drive capacity
1960 * @geom: geometry parameters
1961 */
1962static int
1963megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1964 sector_t capacity, int geom[])
1965{
1966 int heads;
1967 int sectors;
1968 sector_t cylinders;
1969 unsigned long tmp;
1970 /* Default heads (64) & sectors (32) */
1971 heads = 64;
1972 sectors = 32;
1973
1974 tmp = heads * sectors;
1975 cylinders = capacity;
1976
1977 sector_div(cylinders, tmp);
1978
1979 /*
1980 * Handle extended translation size for logical drives > 1Gb
1981 */
1982
1983 if (capacity >= 0x200000) {
1984 heads = 255;
1985 sectors = 63;
1986 tmp = heads*sectors;
1987 cylinders = capacity;
1988 sector_div(cylinders, tmp);
1989 }
1990
1991 geom[0] = heads;
1992 geom[1] = sectors;
1993 geom[2] = cylinders;
1994
1995 return 0;
1996}
1997
Yang, Bo7e8a75f2009-10-06 14:50:17 -06001998static void megasas_aen_polling(struct work_struct *work);
1999
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002000/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002001 * megasas_service_aen - Processes an event notification
2002 * @instance: Adapter soft state
2003 * @cmd: AEN command completed by the ISR
2004 *
2005 * For AEN, driver sends a command down to FW that is held by the FW till an
2006 * event occurs. When an event of interest occurs, FW completes the command
2007 * that it was previously holding.
2008 *
2009 * This routines sends SIGIO signal to processes that have registered with the
2010 * driver for AEN.
2011 */
2012static void
2013megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2014{
Yang, Boc3518832009-10-06 14:18:02 -06002015 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002016 /*
2017 * Don't signal app if it is just an aborted previously registered aen
2018 */
Yang, Boc3518832009-10-06 14:18:02 -06002019 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2020 spin_lock_irqsave(&poll_aen_lock, flags);
2021 megasas_poll_wait_aen = 1;
2022 spin_unlock_irqrestore(&poll_aen_lock, flags);
2023 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002024 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002025 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002026 else
2027 cmd->abort_aen = 0;
2028
2029 instance->aen_cmd = NULL;
2030 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002031
bo yang39a98552010-09-22 22:36:29 -04002032 if ((instance->unload == 0) &&
2033 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002034 struct megasas_aen_event *ev;
2035 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2036 if (!ev) {
2037 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2038 } else {
2039 ev->instance = instance;
2040 instance->ev = ev;
2041 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2042 schedule_delayed_work(
2043 (struct delayed_work *)&ev->hotplug_work, 0);
2044 }
2045 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002046}
2047
2048/*
2049 * Scsi host template for megaraid_sas driver
2050 */
2051static struct scsi_host_template megasas_template = {
2052
2053 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05002054 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002055 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002056 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002057 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002058 .queuecommand = megasas_queue_command,
2059 .eh_device_reset_handler = megasas_reset_device,
2060 .eh_bus_reset_handler = megasas_reset_bus_host,
2061 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002062 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002063 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002064 .use_clustering = ENABLE_CLUSTERING,
2065};
2066
2067/**
2068 * megasas_complete_int_cmd - Completes an internal command
2069 * @instance: Adapter soft state
2070 * @cmd: Command to be completed
2071 *
2072 * The megasas_issue_blocked_cmd() function waits for a command to complete
2073 * after it issues a command. This function wakes up that waiting routine by
2074 * calling wake_up() on the wait queue.
2075 */
2076static void
2077megasas_complete_int_cmd(struct megasas_instance *instance,
2078 struct megasas_cmd *cmd)
2079{
2080 cmd->cmd_status = cmd->frame->io.cmd_status;
2081
2082 if (cmd->cmd_status == ENODATA) {
2083 cmd->cmd_status = 0;
2084 }
2085 wake_up(&instance->int_cmd_wait_q);
2086}
2087
2088/**
2089 * megasas_complete_abort - Completes aborting a command
2090 * @instance: Adapter soft state
2091 * @cmd: Cmd that was issued to abort another cmd
2092 *
adam radford0d490162010-12-14 19:17:17 -08002093 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2094 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002095 * wakes up all functions waiting on the same wait queue.
2096 */
2097static void
2098megasas_complete_abort(struct megasas_instance *instance,
2099 struct megasas_cmd *cmd)
2100{
2101 if (cmd->sync_cmd) {
2102 cmd->sync_cmd = 0;
2103 cmd->cmd_status = 0;
2104 wake_up(&instance->abort_cmd_wait_q);
2105 }
2106
2107 return;
2108}
2109
2110/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002111 * megasas_complete_cmd - Completes a command
2112 * @instance: Adapter soft state
2113 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08002114 * @alt_status: If non-zero, use this value as status to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002115 * SCSI mid-layer instead of the value returned
2116 * by the FW. This should be used if caller wants
2117 * an alternate status (as in the case of aborted
2118 * commands)
2119 */
adam radford9c915a82010-12-21 13:34:31 -08002120void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002121megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2122 u8 alt_status)
2123{
2124 int exception = 0;
2125 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06002126 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08002127 struct fusion_context *fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002128
bo yang39a98552010-09-22 22:36:29 -04002129 /* flag for the retry reset */
2130 cmd->retry_for_fw_reset = 0;
2131
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002132 if (cmd->scmd)
2133 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002134
2135 switch (hdr->cmd) {
2136
2137 case MFI_CMD_PD_SCSI_IO:
2138 case MFI_CMD_LD_SCSI_IO:
2139
2140 /*
2141 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2142 * issued either through an IO path or an IOCTL path. If it
2143 * was via IOCTL, we will send it to internal completion.
2144 */
2145 if (cmd->sync_cmd) {
2146 cmd->sync_cmd = 0;
2147 megasas_complete_int_cmd(instance, cmd);
2148 break;
2149 }
2150
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002151 case MFI_CMD_LD_READ:
2152 case MFI_CMD_LD_WRITE:
2153
2154 if (alt_status) {
2155 cmd->scmd->result = alt_status << 16;
2156 exception = 1;
2157 }
2158
2159 if (exception) {
2160
Sumant Patroe4a082c2006-05-30 12:03:37 -07002161 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002162
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002163 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002164 cmd->scmd->scsi_done(cmd->scmd);
2165 megasas_return_cmd(instance, cmd);
2166
2167 break;
2168 }
2169
2170 switch (hdr->cmd_status) {
2171
2172 case MFI_STAT_OK:
2173 cmd->scmd->result = DID_OK << 16;
2174 break;
2175
2176 case MFI_STAT_SCSI_IO_FAILED:
2177 case MFI_STAT_LD_INIT_IN_PROGRESS:
2178 cmd->scmd->result =
2179 (DID_ERROR << 16) | hdr->scsi_status;
2180 break;
2181
2182 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2183
2184 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2185
2186 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2187 memset(cmd->scmd->sense_buffer, 0,
2188 SCSI_SENSE_BUFFERSIZE);
2189 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2190 hdr->sense_len);
2191
2192 cmd->scmd->result |= DRIVER_SENSE << 24;
2193 }
2194
2195 break;
2196
2197 case MFI_STAT_LD_OFFLINE:
2198 case MFI_STAT_DEVICE_NOT_FOUND:
2199 cmd->scmd->result = DID_BAD_TARGET << 16;
2200 break;
2201
2202 default:
2203 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2204 hdr->cmd_status);
2205 cmd->scmd->result = DID_ERROR << 16;
2206 break;
2207 }
2208
Sumant Patroe4a082c2006-05-30 12:03:37 -07002209 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002210
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002211 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002212 cmd->scmd->scsi_done(cmd->scmd);
2213 megasas_return_cmd(instance, cmd);
2214
2215 break;
2216
2217 case MFI_CMD_SMP:
2218 case MFI_CMD_STP:
2219 case MFI_CMD_DCMD:
adam radford9c915a82010-12-21 13:34:31 -08002220 /* Check for LD map update */
2221 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2222 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2223 spin_lock_irqsave(instance->host->host_lock, flags);
2224 if (cmd->frame->hdr.cmd_status != 0) {
2225 if (cmd->frame->hdr.cmd_status !=
2226 MFI_STAT_NOT_FOUND)
2227 printk(KERN_WARNING "megasas: map sync"
2228 "failed, status = 0x%x.\n",
2229 cmd->frame->hdr.cmd_status);
2230 else {
2231 megasas_return_cmd(instance, cmd);
2232 spin_unlock_irqrestore(
2233 instance->host->host_lock,
2234 flags);
2235 break;
2236 }
2237 } else
2238 instance->map_id++;
2239 megasas_return_cmd(instance, cmd);
2240 if (MR_ValidateMapInfo(
2241 fusion->ld_map[(instance->map_id & 1)],
2242 fusion->load_balance_info))
2243 fusion->fast_path_io = 1;
2244 else
2245 fusion->fast_path_io = 0;
2246 megasas_sync_map_info(instance);
2247 spin_unlock_irqrestore(instance->host->host_lock,
2248 flags);
2249 break;
2250 }
Yang, Boc3518832009-10-06 14:18:02 -06002251 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2252 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2253 spin_lock_irqsave(&poll_aen_lock, flags);
2254 megasas_poll_wait_aen = 0;
2255 spin_unlock_irqrestore(&poll_aen_lock, flags);
2256 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002257
2258 /*
2259 * See if got an event notification
2260 */
2261 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2262 megasas_service_aen(instance, cmd);
2263 else
2264 megasas_complete_int_cmd(instance, cmd);
2265
2266 break;
2267
2268 case MFI_CMD_ABORT:
2269 /*
2270 * Cmd issued to abort another cmd returned
2271 */
2272 megasas_complete_abort(instance, cmd);
2273 break;
2274
2275 default:
2276 printk("megasas: Unknown command completed! [0x%X]\n",
2277 hdr->cmd);
2278 break;
2279 }
2280}
2281
2282/**
bo yang39a98552010-09-22 22:36:29 -04002283 * megasas_issue_pending_cmds_again - issue all pending cmds
2284 * in FW again because of the fw reset
2285 * @instance: Adapter soft state
2286 */
2287static inline void
2288megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2289{
2290 struct megasas_cmd *cmd;
2291 struct list_head clist_local;
2292 union megasas_evt_class_locale class_locale;
2293 unsigned long flags;
2294 u32 seq_num;
2295
2296 INIT_LIST_HEAD(&clist_local);
2297 spin_lock_irqsave(&instance->hba_lock, flags);
2298 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2299 spin_unlock_irqrestore(&instance->hba_lock, flags);
2300
2301 while (!list_empty(&clist_local)) {
2302 cmd = list_entry((&clist_local)->next,
2303 struct megasas_cmd, list);
2304 list_del_init(&cmd->list);
2305
2306 if (cmd->sync_cmd || cmd->scmd) {
2307 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2308 "detected to be pending while HBA reset.\n",
2309 cmd, cmd->scmd, cmd->sync_cmd);
2310
2311 cmd->retry_for_fw_reset++;
2312
2313 if (cmd->retry_for_fw_reset == 3) {
2314 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2315 "was tried multiple times during reset."
2316 "Shutting down the HBA\n",
2317 cmd, cmd->scmd, cmd->sync_cmd);
2318 megaraid_sas_kill_hba(instance);
2319
2320 instance->adprecovery =
2321 MEGASAS_HW_CRITICAL_ERROR;
2322 return;
2323 }
2324 }
2325
2326 if (cmd->sync_cmd == 1) {
2327 if (cmd->scmd) {
2328 printk(KERN_NOTICE "megaraid_sas: unexpected"
2329 "cmd attached to internal command!\n");
2330 }
2331 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2332 "on the internal reset queue,"
2333 "issue it again.\n", cmd);
2334 cmd->cmd_status = ENODATA;
2335 instance->instancet->fire_cmd(instance,
2336 cmd->frame_phys_addr ,
2337 0, instance->reg_set);
2338 } else if (cmd->scmd) {
2339 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
2340 "detected on the internal queue, issue again.\n",
2341 cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
2342
2343 atomic_inc(&instance->fw_outstanding);
2344 instance->instancet->fire_cmd(instance,
2345 cmd->frame_phys_addr,
2346 cmd->frame_count-1, instance->reg_set);
2347 } else {
2348 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2349 "internal reset defer list while re-issue!!\n",
2350 cmd);
2351 }
2352 }
2353
2354 if (instance->aen_cmd) {
2355 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2356 megasas_return_cmd(instance, instance->aen_cmd);
2357
2358 instance->aen_cmd = NULL;
2359 }
2360
2361 /*
2362 * Initiate AEN (Asynchronous Event Notification)
2363 */
2364 seq_num = instance->last_seq_num;
2365 class_locale.members.reserved = 0;
2366 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2367 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2368
2369 megasas_register_aen(instance, seq_num, class_locale.word);
2370}
2371
2372/**
2373 * Move the internal reset pending commands to a deferred queue.
2374 *
2375 * We move the commands pending at internal reset time to a
2376 * pending queue. This queue would be flushed after successful
2377 * completion of the internal reset sequence. if the internal reset
2378 * did not complete in time, the kernel reset handler would flush
2379 * these commands.
2380 **/
2381static void
2382megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2383{
2384 struct megasas_cmd *cmd;
2385 int i;
2386 u32 max_cmd = instance->max_fw_cmds;
2387 u32 defer_index;
2388 unsigned long flags;
2389
2390 defer_index = 0;
2391 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2392 for (i = 0; i < max_cmd; i++) {
2393 cmd = instance->cmd_list[i];
2394 if (cmd->sync_cmd == 1 || cmd->scmd) {
2395 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2396 "on the defer queue as internal\n",
2397 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2398
2399 if (!list_empty(&cmd->list)) {
2400 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2401 " moving this cmd:%p, %d %p, it was"
2402 "discovered on some list?\n",
2403 cmd, cmd->sync_cmd, cmd->scmd);
2404
2405 list_del_init(&cmd->list);
2406 }
2407 defer_index++;
2408 list_add_tail(&cmd->list,
2409 &instance->internal_reset_pending_q);
2410 }
2411 }
2412 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2413}
2414
2415
2416static void
2417process_fw_state_change_wq(struct work_struct *work)
2418{
2419 struct megasas_instance *instance =
2420 container_of(work, struct megasas_instance, work_init);
2421 u32 wait;
2422 unsigned long flags;
2423
2424 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2425 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2426 instance->adprecovery);
2427 return ;
2428 }
2429
2430 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2431 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2432 "state, restarting it...\n");
2433
2434 instance->instancet->disable_intr(instance->reg_set);
2435 atomic_set(&instance->fw_outstanding, 0);
2436
2437 atomic_set(&instance->fw_reset_no_pci_access, 1);
2438 instance->instancet->adp_reset(instance, instance->reg_set);
2439 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2440
2441 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2442 "initiating next stage...\n");
2443
2444 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2445 "state 2 starting...\n");
2446
2447 /*waitting for about 20 second before start the second init*/
2448 for (wait = 0; wait < 30; wait++) {
2449 msleep(1000);
2450 }
2451
2452 if (megasas_transition_to_ready(instance)) {
2453 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2454
2455 megaraid_sas_kill_hba(instance);
2456 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2457 return ;
2458 }
2459
2460 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2461 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2462 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2463 ) {
2464 *instance->consumer = *instance->producer;
2465 } else {
2466 *instance->consumer = 0;
2467 *instance->producer = 0;
2468 }
2469
2470 megasas_issue_init_mfi(instance);
2471
2472 spin_lock_irqsave(&instance->hba_lock, flags);
2473 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2474 spin_unlock_irqrestore(&instance->hba_lock, flags);
2475 instance->instancet->enable_intr(instance->reg_set);
2476
2477 megasas_issue_pending_cmds_again(instance);
2478 instance->issuepend_done = 1;
2479 }
2480 return ;
2481}
2482
2483/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002484 * megasas_deplete_reply_queue - Processes all completed commands
2485 * @instance: Adapter soft state
2486 * @alt_status: Alternate status to be returned to
2487 * SCSI mid-layer instead of the status
2488 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002489 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002490 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002491static int
bo yang39a98552010-09-22 22:36:29 -04002492megasas_deplete_reply_queue(struct megasas_instance *instance,
2493 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002494{
bo yang39a98552010-09-22 22:36:29 -04002495 u32 mfiStatus;
2496 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002497
bo yang39a98552010-09-22 22:36:29 -04002498 if ((mfiStatus = instance->instancet->check_reset(instance,
2499 instance->reg_set)) == 1) {
2500 return IRQ_HANDLED;
2501 }
2502
2503 if ((mfiStatus = instance->instancet->clear_intr(
2504 instance->reg_set)
2505 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08002506 /* Hardware may not set outbound_intr_status in MSI-X mode */
2507 if (!instance->msi_flag)
2508 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04002509 }
2510
2511 instance->mfiStatus = mfiStatus;
2512
2513 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2514 fw_state = instance->instancet->read_fw_status_reg(
2515 instance->reg_set) & MFI_STATE_MASK;
2516
2517 if (fw_state != MFI_STATE_FAULT) {
2518 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2519 fw_state);
2520 }
2521
2522 if ((fw_state == MFI_STATE_FAULT) &&
2523 (instance->disableOnlineCtrlReset == 0)) {
2524 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2525
2526 if ((instance->pdev->device ==
2527 PCI_DEVICE_ID_LSI_SAS1064R) ||
2528 (instance->pdev->device ==
2529 PCI_DEVICE_ID_DELL_PERC5) ||
2530 (instance->pdev->device ==
2531 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2532
2533 *instance->consumer =
2534 MEGASAS_ADPRESET_INPROG_SIGN;
2535 }
2536
2537
2538 instance->instancet->disable_intr(instance->reg_set);
2539 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2540 instance->issuepend_done = 0;
2541
2542 atomic_set(&instance->fw_outstanding, 0);
2543 megasas_internal_reset_defer_cmds(instance);
2544
2545 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2546 fw_state, instance->adprecovery);
2547
2548 schedule_work(&instance->work_init);
2549 return IRQ_HANDLED;
2550
2551 } else {
2552 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2553 fw_state, instance->disableOnlineCtrlReset);
2554 }
2555 }
2556
Sumant Patro5d018ad2006-10-03 13:13:18 -07002557 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002558 return IRQ_HANDLED;
2559}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002560/**
2561 * megasas_isr - isr entry point
2562 */
David Howells7d12e782006-10-05 14:55:46 +01002563static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002564{
bo yang39a98552010-09-22 22:36:29 -04002565 struct megasas_instance *instance;
2566 unsigned long flags;
2567 irqreturn_t rc;
2568
2569 if (atomic_read(
2570 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2571 return IRQ_HANDLED;
2572
2573 instance = (struct megasas_instance *)devp;
2574
2575 spin_lock_irqsave(&instance->hba_lock, flags);
2576 rc = megasas_deplete_reply_queue(instance, DID_OK);
2577 spin_unlock_irqrestore(&instance->hba_lock, flags);
2578
2579 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002580}
2581
2582/**
2583 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002584 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002585 *
2586 * During the initialization, FW passes can potentially be in any one of
2587 * several possible states. If the FW in operational, waiting-for-handshake
2588 * states, driver must take steps to bring it to ready state. Otherwise, it
2589 * has to wait for the ready state.
2590 */
adam radford9c915a82010-12-21 13:34:31 -08002591int
Sumant Patro1341c932006-01-25 12:02:40 -08002592megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002593{
2594 int i;
2595 u8 max_wait;
2596 u32 fw_state;
2597 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002598 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002599
Sumant Patro1341c932006-01-25 12:02:40 -08002600 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002601
Sumant Patroe3bbff92006-10-03 12:28:49 -07002602 if (fw_state != MFI_STATE_READY)
adam radford0d490162010-12-14 19:17:17 -08002603 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2604 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07002605
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002606 while (fw_state != MFI_STATE_READY) {
2607
Yang, Bo7218df62009-10-06 14:52:20 -06002608 abs_state =
2609 instance->instancet->read_fw_status_reg(instance->reg_set);
2610
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002611 switch (fw_state) {
2612
2613 case MFI_STATE_FAULT:
2614
2615 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
adam radford1ac515e2011-02-24 20:57:04 -08002616 max_wait = MEGASAS_RESET_WAIT_TIME;
2617 cur_state = MFI_STATE_FAULT;
2618 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002619
2620 case MFI_STATE_WAIT_HANDSHAKE:
2621 /*
2622 * Set the CLR bit in inbound doorbell
2623 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002624 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002625 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2626 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002627 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2628 (instance->pdev->device ==
2629 PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002630 writel(
2631 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002632 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002633 } else {
2634 writel(
2635 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2636 &instance->reg_set->inbound_doorbell);
2637 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002638
Yang, Bo7218df62009-10-06 14:52:20 -06002639 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002640 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2641 break;
2642
Sumant Patroe3bbff92006-10-03 12:28:49 -07002643 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002644 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002645 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2646 (instance->pdev->device ==
2647 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2648 (instance->pdev->device ==
2649 PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002650 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08002651 &instance->reg_set->doorbell);
Yang, Bo87911122009-10-06 14:31:54 -06002652 } else
2653 writel(MFI_INIT_HOTPLUG,
2654 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002655
Yang, Bo7218df62009-10-06 14:52:20 -06002656 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002657 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2658 break;
2659
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002660 case MFI_STATE_OPERATIONAL:
2661 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002662 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002663 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002664 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002665 if ((instance->pdev->device ==
2666 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2667 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08002668 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2669 (instance->pdev->device
2670 == PCI_DEVICE_ID_LSI_FUSION)) {
Yang, Bo87911122009-10-06 14:31:54 -06002671 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08002672 &instance->reg_set->doorbell);
2673 if (instance->pdev->device ==
2674 PCI_DEVICE_ID_LSI_FUSION) {
2675 for (i = 0; i < (10 * 1000); i += 20) {
2676 if (readl(
2677 &instance->
2678 reg_set->
2679 doorbell) & 1)
2680 msleep(20);
2681 else
2682 break;
2683 }
2684 }
Yang, Bo87911122009-10-06 14:31:54 -06002685 } else
2686 writel(MFI_RESET_FLAGS,
2687 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002688
Yang, Bo7218df62009-10-06 14:52:20 -06002689 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002690 cur_state = MFI_STATE_OPERATIONAL;
2691 break;
2692
2693 case MFI_STATE_UNDEFINED:
2694 /*
2695 * This state should not last for more than 2 seconds
2696 */
Yang, Bo7218df62009-10-06 14:52:20 -06002697 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002698 cur_state = MFI_STATE_UNDEFINED;
2699 break;
2700
2701 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002702 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002703 cur_state = MFI_STATE_BB_INIT;
2704 break;
2705
2706 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002707 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002708 cur_state = MFI_STATE_FW_INIT;
2709 break;
2710
2711 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002712 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002713 cur_state = MFI_STATE_FW_INIT_2;
2714 break;
2715
2716 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002717 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002718 cur_state = MFI_STATE_DEVICE_SCAN;
2719 break;
2720
2721 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002722 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002723 cur_state = MFI_STATE_FLUSH_CACHE;
2724 break;
2725
2726 default:
2727 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2728 fw_state);
2729 return -ENODEV;
2730 }
2731
2732 /*
2733 * The cur_state should not last for more than max_wait secs
2734 */
2735 for (i = 0; i < (max_wait * 1000); i++) {
adam radford0d490162010-12-14 19:17:17 -08002736 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
Sumant Patro1341c932006-01-25 12:02:40 -08002737 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002738 curr_abs_state =
2739 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002740
Yang, Bo7218df62009-10-06 14:52:20 -06002741 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002742 msleep(1);
2743 } else
2744 break;
2745 }
2746
2747 /*
2748 * Return error if fw_state hasn't changed after max_wait
2749 */
Yang, Bo7218df62009-10-06 14:52:20 -06002750 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002751 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2752 "in %d secs\n", fw_state, max_wait);
2753 return -ENODEV;
2754 }
bo yang39a98552010-09-22 22:36:29 -04002755 }
adam radford0d490162010-12-14 19:17:17 -08002756 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002757
2758 return 0;
2759}
2760
2761/**
2762 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2763 * @instance: Adapter soft state
2764 */
2765static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2766{
2767 int i;
adam radford9c915a82010-12-21 13:34:31 -08002768 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002769 struct megasas_cmd *cmd;
2770
2771 if (!instance->frame_dma_pool)
2772 return;
2773
2774 /*
2775 * Return all frames to pool
2776 */
2777 for (i = 0; i < max_cmd; i++) {
2778
2779 cmd = instance->cmd_list[i];
2780
2781 if (cmd->frame)
2782 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2783 cmd->frame_phys_addr);
2784
2785 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002786 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002787 cmd->sense_phys_addr);
2788 }
2789
2790 /*
2791 * Now destroy the pool itself
2792 */
2793 pci_pool_destroy(instance->frame_dma_pool);
2794 pci_pool_destroy(instance->sense_dma_pool);
2795
2796 instance->frame_dma_pool = NULL;
2797 instance->sense_dma_pool = NULL;
2798}
2799
2800/**
2801 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2802 * @instance: Adapter soft state
2803 *
2804 * Each command packet has an embedded DMA memory buffer that is used for
2805 * filling MFI frame and the SG list that immediately follows the frame. This
2806 * function creates those DMA memory buffers for each command packet by using
2807 * PCI pool facility.
2808 */
2809static int megasas_create_frame_pool(struct megasas_instance *instance)
2810{
2811 int i;
2812 u32 max_cmd;
2813 u32 sge_sz;
2814 u32 sgl_sz;
2815 u32 total_sz;
2816 u32 frame_count;
2817 struct megasas_cmd *cmd;
2818
adam radford9c915a82010-12-21 13:34:31 -08002819 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002820
2821 /*
2822 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2823 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2824 */
2825 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2826 sizeof(struct megasas_sge32);
2827
Yang, Bof4c9a132009-10-06 14:43:28 -06002828 if (instance->flag_ieee) {
2829 sge_sz = sizeof(struct megasas_sge_skinny);
2830 }
2831
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002832 /*
2833 * Calculated the number of 64byte frames required for SGL
2834 */
2835 sgl_sz = sge_sz * instance->max_num_sge;
2836 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002837 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002838
2839 /*
2840 * We need one extra frame for the MFI command
2841 */
2842 frame_count++;
2843
2844 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2845 /*
2846 * Use DMA pool facility provided by PCI layer
2847 */
2848 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2849 instance->pdev, total_sz, 64,
2850 0);
2851
2852 if (!instance->frame_dma_pool) {
2853 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2854 return -ENOMEM;
2855 }
2856
2857 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2858 instance->pdev, 128, 4, 0);
2859
2860 if (!instance->sense_dma_pool) {
2861 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2862
2863 pci_pool_destroy(instance->frame_dma_pool);
2864 instance->frame_dma_pool = NULL;
2865
2866 return -ENOMEM;
2867 }
2868
2869 /*
2870 * Allocate and attach a frame to each of the commands in cmd_list.
2871 * By making cmd->index as the context instead of the &cmd, we can
2872 * always use 32bit context regardless of the architecture
2873 */
2874 for (i = 0; i < max_cmd; i++) {
2875
2876 cmd = instance->cmd_list[i];
2877
2878 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2879 GFP_KERNEL, &cmd->frame_phys_addr);
2880
2881 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2882 GFP_KERNEL, &cmd->sense_phys_addr);
2883
2884 /*
2885 * megasas_teardown_frame_pool() takes care of freeing
2886 * whatever has been allocated
2887 */
2888 if (!cmd->frame || !cmd->sense) {
2889 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2890 megasas_teardown_frame_pool(instance);
2891 return -ENOMEM;
2892 }
2893
Yang, Bo707e09b2010-10-12 07:20:27 -06002894 memset(cmd->frame, 0, total_sz);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002895 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002896 cmd->frame->io.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002897 }
2898
2899 return 0;
2900}
2901
2902/**
2903 * megasas_free_cmds - Free all the cmds in the free cmd pool
2904 * @instance: Adapter soft state
2905 */
adam radford9c915a82010-12-21 13:34:31 -08002906void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002907{
2908 int i;
2909 /* First free the MFI frame pool */
2910 megasas_teardown_frame_pool(instance);
2911
2912 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08002913 for (i = 0; i < instance->max_mfi_cmds; i++)
2914
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002915 kfree(instance->cmd_list[i]);
2916
2917 /* Free the cmd_list buffer itself */
2918 kfree(instance->cmd_list);
2919 instance->cmd_list = NULL;
2920
2921 INIT_LIST_HEAD(&instance->cmd_pool);
2922}
2923
2924/**
2925 * megasas_alloc_cmds - Allocates the command packets
2926 * @instance: Adapter soft state
2927 *
2928 * Each command that is issued to the FW, whether IO commands from the OS or
2929 * internal commands like IOCTLs, are wrapped in local data structure called
2930 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2931 * the FW.
2932 *
2933 * Each frame has a 32-bit field called context (tag). This context is used
2934 * to get back the megasas_cmd from the frame when a frame gets completed in
2935 * the ISR. Typically the address of the megasas_cmd itself would be used as
2936 * the context. But we wanted to keep the differences between 32 and 64 bit
2937 * systems to the mininum. We always use 32 bit integers for the context. In
2938 * this driver, the 32 bit values are the indices into an array cmd_list.
2939 * This array is used only to look up the megasas_cmd given the context. The
2940 * free commands themselves are maintained in a linked list called cmd_pool.
2941 */
adam radford9c915a82010-12-21 13:34:31 -08002942int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002943{
2944 int i;
2945 int j;
2946 u32 max_cmd;
2947 struct megasas_cmd *cmd;
2948
adam radford9c915a82010-12-21 13:34:31 -08002949 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002950
2951 /*
2952 * instance->cmd_list is an array of struct megasas_cmd pointers.
2953 * Allocate the dynamic array first and then allocate individual
2954 * commands.
2955 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002956 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002957
2958 if (!instance->cmd_list) {
2959 printk(KERN_DEBUG "megasas: out of memory\n");
2960 return -ENOMEM;
2961 }
2962
adam radford9c915a82010-12-21 13:34:31 -08002963 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002964
2965 for (i = 0; i < max_cmd; i++) {
2966 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2967 GFP_KERNEL);
2968
2969 if (!instance->cmd_list[i]) {
2970
2971 for (j = 0; j < i; j++)
2972 kfree(instance->cmd_list[j]);
2973
2974 kfree(instance->cmd_list);
2975 instance->cmd_list = NULL;
2976
2977 return -ENOMEM;
2978 }
2979 }
2980
2981 /*
2982 * Add all the commands to command pool (instance->cmd_pool)
2983 */
2984 for (i = 0; i < max_cmd; i++) {
2985 cmd = instance->cmd_list[i];
2986 memset(cmd, 0, sizeof(struct megasas_cmd));
2987 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04002988 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002989 cmd->instance = instance;
2990
2991 list_add_tail(&cmd->list, &instance->cmd_pool);
2992 }
2993
2994 /*
2995 * Create a frame pool and assign one frame to each cmd
2996 */
2997 if (megasas_create_frame_pool(instance)) {
2998 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2999 megasas_free_cmds(instance);
3000 }
3001
3002 return 0;
3003}
3004
Yang, Bo81e403c2009-10-06 14:27:54 -06003005/*
3006 * megasas_get_pd_list_info - Returns FW's pd_list structure
3007 * @instance: Adapter soft state
3008 * @pd_list: pd_list structure
3009 *
3010 * Issues an internal command (DCMD) to get the FW's controller PD
3011 * list structure. This information is mainly used to find out SYSTEM
3012 * supported by the FW.
3013 */
3014static int
3015megasas_get_pd_list(struct megasas_instance *instance)
3016{
3017 int ret = 0, pd_index = 0;
3018 struct megasas_cmd *cmd;
3019 struct megasas_dcmd_frame *dcmd;
3020 struct MR_PD_LIST *ci;
3021 struct MR_PD_ADDRESS *pd_addr;
3022 dma_addr_t ci_h = 0;
3023
3024 cmd = megasas_get_cmd(instance);
3025
3026 if (!cmd) {
3027 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3028 return -ENOMEM;
3029 }
3030
3031 dcmd = &cmd->frame->dcmd;
3032
3033 ci = pci_alloc_consistent(instance->pdev,
3034 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3035
3036 if (!ci) {
3037 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3038 megasas_return_cmd(instance, cmd);
3039 return -ENOMEM;
3040 }
3041
3042 memset(ci, 0, sizeof(*ci));
3043 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3044
3045 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3046 dcmd->mbox.b[1] = 0;
3047 dcmd->cmd = MFI_CMD_DCMD;
3048 dcmd->cmd_status = 0xFF;
3049 dcmd->sge_count = 1;
3050 dcmd->flags = MFI_FRAME_DIR_READ;
3051 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003052 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06003053 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3054 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3055 dcmd->sgl.sge32[0].phys_addr = ci_h;
3056 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3057
3058 if (!megasas_issue_polled(instance, cmd)) {
3059 ret = 0;
3060 } else {
3061 ret = -1;
3062 }
3063
3064 /*
3065 * the following function will get the instance PD LIST.
3066 */
3067
3068 pd_addr = ci->addr;
3069
3070 if ( ret == 0 &&
3071 (ci->count <
3072 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3073
3074 memset(instance->pd_list, 0,
3075 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3076
3077 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3078
3079 instance->pd_list[pd_addr->deviceId].tid =
3080 pd_addr->deviceId;
3081 instance->pd_list[pd_addr->deviceId].driveType =
3082 pd_addr->scsiDevType;
3083 instance->pd_list[pd_addr->deviceId].driveState =
3084 MR_PD_STATE_SYSTEM;
3085 pd_addr++;
3086 }
3087 }
3088
3089 pci_free_consistent(instance->pdev,
3090 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3091 ci, ci_h);
3092 megasas_return_cmd(instance, cmd);
3093
3094 return ret;
3095}
3096
Yang, Bobdc6fb82009-12-06 08:30:19 -07003097/*
3098 * megasas_get_ld_list_info - Returns FW's ld_list structure
3099 * @instance: Adapter soft state
3100 * @ld_list: ld_list structure
3101 *
3102 * Issues an internal command (DCMD) to get the FW's controller PD
3103 * list structure. This information is mainly used to find out SYSTEM
3104 * supported by the FW.
3105 */
3106static int
3107megasas_get_ld_list(struct megasas_instance *instance)
3108{
3109 int ret = 0, ld_index = 0, ids = 0;
3110 struct megasas_cmd *cmd;
3111 struct megasas_dcmd_frame *dcmd;
3112 struct MR_LD_LIST *ci;
3113 dma_addr_t ci_h = 0;
3114
3115 cmd = megasas_get_cmd(instance);
3116
3117 if (!cmd) {
3118 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3119 return -ENOMEM;
3120 }
3121
3122 dcmd = &cmd->frame->dcmd;
3123
3124 ci = pci_alloc_consistent(instance->pdev,
3125 sizeof(struct MR_LD_LIST),
3126 &ci_h);
3127
3128 if (!ci) {
3129 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3130 megasas_return_cmd(instance, cmd);
3131 return -ENOMEM;
3132 }
3133
3134 memset(ci, 0, sizeof(*ci));
3135 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3136
3137 dcmd->cmd = MFI_CMD_DCMD;
3138 dcmd->cmd_status = 0xFF;
3139 dcmd->sge_count = 1;
3140 dcmd->flags = MFI_FRAME_DIR_READ;
3141 dcmd->timeout = 0;
3142 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3143 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3144 dcmd->sgl.sge32[0].phys_addr = ci_h;
3145 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3146 dcmd->pad_0 = 0;
3147
3148 if (!megasas_issue_polled(instance, cmd)) {
3149 ret = 0;
3150 } else {
3151 ret = -1;
3152 }
3153
3154 /* the following function will get the instance PD LIST */
3155
bo yang39a98552010-09-22 22:36:29 -04003156 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07003157 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3158
3159 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3160 if (ci->ldList[ld_index].state != 0) {
3161 ids = ci->ldList[ld_index].ref.targetId;
3162 instance->ld_ids[ids] =
3163 ci->ldList[ld_index].ref.targetId;
3164 }
3165 }
3166 }
3167
3168 pci_free_consistent(instance->pdev,
3169 sizeof(struct MR_LD_LIST),
3170 ci,
3171 ci_h);
3172
3173 megasas_return_cmd(instance, cmd);
3174 return ret;
3175}
3176
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003177/**
3178 * megasas_get_controller_info - Returns FW's controller structure
3179 * @instance: Adapter soft state
3180 * @ctrl_info: Controller information structure
3181 *
3182 * Issues an internal command (DCMD) to get the FW's controller structure.
3183 * This information is mainly used to find out the maximum IO transfer per
3184 * command supported by the FW.
3185 */
3186static int
3187megasas_get_ctrl_info(struct megasas_instance *instance,
3188 struct megasas_ctrl_info *ctrl_info)
3189{
3190 int ret = 0;
3191 struct megasas_cmd *cmd;
3192 struct megasas_dcmd_frame *dcmd;
3193 struct megasas_ctrl_info *ci;
3194 dma_addr_t ci_h = 0;
3195
3196 cmd = megasas_get_cmd(instance);
3197
3198 if (!cmd) {
3199 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3200 return -ENOMEM;
3201 }
3202
3203 dcmd = &cmd->frame->dcmd;
3204
3205 ci = pci_alloc_consistent(instance->pdev,
3206 sizeof(struct megasas_ctrl_info), &ci_h);
3207
3208 if (!ci) {
3209 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3210 megasas_return_cmd(instance, cmd);
3211 return -ENOMEM;
3212 }
3213
3214 memset(ci, 0, sizeof(*ci));
3215 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3216
3217 dcmd->cmd = MFI_CMD_DCMD;
3218 dcmd->cmd_status = 0xFF;
3219 dcmd->sge_count = 1;
3220 dcmd->flags = MFI_FRAME_DIR_READ;
3221 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003222 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003223 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3224 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3225 dcmd->sgl.sge32[0].phys_addr = ci_h;
3226 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3227
3228 if (!megasas_issue_polled(instance, cmd)) {
3229 ret = 0;
3230 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3231 } else {
3232 ret = -1;
3233 }
3234
3235 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3236 ci, ci_h);
3237
3238 megasas_return_cmd(instance, cmd);
3239 return ret;
3240}
3241
3242/**
bo yang31ea7082007-11-07 12:09:50 -05003243 * megasas_issue_init_mfi - Initializes the FW
3244 * @instance: Adapter soft state
3245 *
3246 * Issues the INIT MFI cmd
3247 */
3248static int
3249megasas_issue_init_mfi(struct megasas_instance *instance)
3250{
3251 u32 context;
3252
3253 struct megasas_cmd *cmd;
3254
3255 struct megasas_init_frame *init_frame;
3256 struct megasas_init_queue_info *initq_info;
3257 dma_addr_t init_frame_h;
3258 dma_addr_t initq_info_h;
3259
3260 /*
3261 * Prepare a init frame. Note the init frame points to queue info
3262 * structure. Each frame has SGL allocated after first 64 bytes. For
3263 * this frame - since we don't need any SGL - we use SGL's space as
3264 * queue info structure
3265 *
3266 * We will not get a NULL command below. We just created the pool.
3267 */
3268 cmd = megasas_get_cmd(instance);
3269
3270 init_frame = (struct megasas_init_frame *)cmd->frame;
3271 initq_info = (struct megasas_init_queue_info *)
3272 ((unsigned long)init_frame + 64);
3273
3274 init_frame_h = cmd->frame_phys_addr;
3275 initq_info_h = init_frame_h + 64;
3276
3277 context = init_frame->context;
3278 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3279 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3280 init_frame->context = context;
3281
3282 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3283 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3284
3285 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3286 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3287
3288 init_frame->cmd = MFI_CMD_INIT;
3289 init_frame->cmd_status = 0xFF;
3290 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3291
3292 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3293
3294 /*
3295 * disable the intr before firing the init frame to FW
3296 */
3297 instance->instancet->disable_intr(instance->reg_set);
3298
3299 /*
3300 * Issue the init frame in polled mode
3301 */
3302
3303 if (megasas_issue_polled(instance, cmd)) {
3304 printk(KERN_ERR "megasas: Failed to init firmware\n");
3305 megasas_return_cmd(instance, cmd);
3306 goto fail_fw_init;
3307 }
3308
3309 megasas_return_cmd(instance, cmd);
3310
3311 return 0;
3312
3313fail_fw_init:
3314 return -EINVAL;
3315}
3316
3317/**
bo yangad84db22007-11-09 04:40:16 -05003318 * megasas_start_timer - Initializes a timer object
3319 * @instance: Adapter soft state
3320 * @timer: timer object to be initialized
3321 * @fn: timer function
3322 * @interval: time interval between timer function call
3323 */
3324static inline void
3325megasas_start_timer(struct megasas_instance *instance,
3326 struct timer_list *timer,
3327 void *fn, unsigned long interval)
3328{
3329 init_timer(timer);
3330 timer->expires = jiffies + interval;
3331 timer->data = (unsigned long)instance;
3332 timer->function = fn;
3333 add_timer(timer);
3334}
3335
3336/**
3337 * megasas_io_completion_timer - Timer fn
3338 * @instance_addr: Address of adapter soft state
3339 *
3340 * Schedules tasklet for cmd completion
3341 * if poll_mode_io is set
3342 */
3343static void
3344megasas_io_completion_timer(unsigned long instance_addr)
3345{
3346 struct megasas_instance *instance =
3347 (struct megasas_instance *)instance_addr;
3348
3349 if (atomic_read(&instance->fw_outstanding))
3350 tasklet_schedule(&instance->isr_tasklet);
3351
3352 /* Restart timer */
3353 if (poll_mode_io)
3354 mod_timer(&instance->io_completion_timer,
3355 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3356}
3357
adam radfordcd50ba82010-12-21 10:23:23 -08003358static u32
3359megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003360{
adam radfordcd50ba82010-12-21 10:23:23 -08003361 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003362 u32 context_sz;
3363 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003364
3365 reg_set = instance->reg_set;
3366
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003367 /*
3368 * Get various operational parameters from status register
3369 */
Sumant Patro1341c932006-01-25 12:02:40 -08003370 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003371 /*
3372 * Reduce the max supported cmds by 1. This is to ensure that the
3373 * reply_q_sz (1 more than the max cmd that driver may send)
3374 * does not exceed max cmds that the FW can support
3375 */
3376 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08003377 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08003378 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08003379 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003380 /*
3381 * Create a pool of commands
3382 */
3383 if (megasas_alloc_cmds(instance))
3384 goto fail_alloc_cmds;
3385
3386 /*
3387 * Allocate memory for reply queue. Length of reply queue should
3388 * be _one_ more than the maximum commands handled by the firmware.
3389 *
3390 * Note: When FW completes commands, it places corresponding contex
3391 * values in this circular reply queue. This circular queue is a fairly
3392 * typical producer-consumer queue. FW is the producer (of completed
3393 * commands) and the driver is the consumer.
3394 */
3395 context_sz = sizeof(u32);
3396 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3397
3398 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3399 reply_q_sz,
3400 &instance->reply_queue_h);
3401
3402 if (!instance->reply_queue) {
3403 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3404 goto fail_reply_queue;
3405 }
3406
bo yang31ea7082007-11-07 12:09:50 -05003407 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003408 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003409
bo yang39a98552010-09-22 22:36:29 -04003410 instance->fw_support_ieee = 0;
3411 instance->fw_support_ieee =
3412 (instance->instancet->read_fw_status_reg(reg_set) &
3413 0x04000000);
3414
3415 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3416 instance->fw_support_ieee);
3417
3418 if (instance->fw_support_ieee)
3419 instance->flag_ieee = 1;
3420
adam radfordcd50ba82010-12-21 10:23:23 -08003421 return 0;
3422
3423fail_fw_init:
3424
3425 pci_free_consistent(instance->pdev, reply_q_sz,
3426 instance->reply_queue, instance->reply_queue_h);
3427fail_reply_queue:
3428 megasas_free_cmds(instance);
3429
3430fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08003431 return 1;
3432}
3433
3434/**
3435 * megasas_init_fw - Initializes the FW
3436 * @instance: Adapter soft state
3437 *
3438 * This is the main function for initializing firmware
3439 */
3440
3441static int megasas_init_fw(struct megasas_instance *instance)
3442{
3443 u32 max_sectors_1;
3444 u32 max_sectors_2;
3445 u32 tmp_sectors;
3446 struct megasas_register_set __iomem *reg_set;
3447 struct megasas_ctrl_info *ctrl_info;
3448 unsigned long bar_list;
3449
3450 /* Find first memory bar */
3451 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3452 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3453 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3454 if (pci_request_selected_regions(instance->pdev, instance->bar,
3455 "megasas: LSI")) {
3456 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3457 return -EBUSY;
3458 }
3459
3460 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3461
3462 if (!instance->reg_set) {
3463 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3464 goto fail_ioremap;
3465 }
3466
3467 reg_set = instance->reg_set;
3468
3469 switch (instance->pdev->device) {
adam radford9c915a82010-12-21 13:34:31 -08003470 case PCI_DEVICE_ID_LSI_FUSION:
3471 instance->instancet = &megasas_instance_template_fusion;
3472 break;
adam radfordcd50ba82010-12-21 10:23:23 -08003473 case PCI_DEVICE_ID_LSI_SAS1078R:
3474 case PCI_DEVICE_ID_LSI_SAS1078DE:
3475 instance->instancet = &megasas_instance_template_ppc;
3476 break;
3477 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3478 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3479 instance->instancet = &megasas_instance_template_gen2;
3480 break;
3481 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3482 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3483 instance->instancet = &megasas_instance_template_skinny;
3484 break;
3485 case PCI_DEVICE_ID_LSI_SAS1064R:
3486 case PCI_DEVICE_ID_DELL_PERC5:
3487 default:
3488 instance->instancet = &megasas_instance_template_xscale;
3489 break;
3490 }
3491
3492 /*
3493 * We expect the FW state to be READY
3494 */
3495 if (megasas_transition_to_ready(instance))
3496 goto fail_ready_state;
3497
3498 /* Get operational params, sge flags, send init cmd to controller */
3499 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08003500 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08003501
3502 printk(KERN_ERR "megasas: INIT adapter done\n");
3503
bo yang39a98552010-09-22 22:36:29 -04003504 /** for passthrough
3505 * the following function will get the PD LIST.
3506 */
3507
Yang, Bo81e403c2009-10-06 14:27:54 -06003508 memset(instance->pd_list, 0 ,
3509 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3510 megasas_get_pd_list(instance);
3511
Yang, Bobdc6fb82009-12-06 08:30:19 -07003512 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3513 megasas_get_ld_list(instance);
3514
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003515 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3516
3517 /*
3518 * Compute the max allowed sectors per IO: The controller info has two
3519 * limits on max sectors. Driver should use the minimum of these two.
3520 *
3521 * 1 << stripe_sz_ops.min = max sectors per strip
3522 *
3523 * Note that older firmwares ( < FW ver 30) didn't report information
3524 * to calculate max_sectors_1. So the number ended up as zero always.
3525 */
bo yang14faea92007-11-09 04:14:00 -05003526 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003527 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3528
3529 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3530 ctrl_info->max_strips_per_io;
3531 max_sectors_2 = ctrl_info->max_request_size;
3532
bo yang14faea92007-11-09 04:14:00 -05003533 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003534 instance->disableOnlineCtrlReset =
3535 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003536 }
3537
3538 instance->max_sectors_per_req = instance->max_num_sge *
3539 PAGE_SIZE / 512;
3540 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3541 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003542
3543 kfree(ctrl_info);
3544
Sumant Patro5d018ad2006-10-03 13:13:18 -07003545 /*
3546 * Setup tasklet for cmd completion
3547 */
3548
adam radfordf86c5422011-02-24 20:57:00 -08003549 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
bo yangad84db22007-11-09 04:40:16 -05003550 (unsigned long)instance);
3551
3552 /* Initialize the cmd completion timer */
3553 if (poll_mode_io)
3554 megasas_start_timer(instance, &instance->io_completion_timer,
3555 megasas_io_completion_timer,
3556 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003557 return 0;
3558
adam radfordeb1b1232011-02-24 20:55:56 -08003559fail_init_adapter:
adam radfordcd50ba82010-12-21 10:23:23 -08003560fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003561 iounmap(instance->reg_set);
3562
3563 fail_ioremap:
adam radfordb6d5d882010-12-14 18:56:07 -08003564 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565
3566 return -EINVAL;
3567}
3568
3569/**
3570 * megasas_release_mfi - Reverses the FW initialization
3571 * @intance: Adapter soft state
3572 */
3573static void megasas_release_mfi(struct megasas_instance *instance)
3574{
adam radford9c915a82010-12-21 13:34:31 -08003575 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003576
adam radford9c915a82010-12-21 13:34:31 -08003577 if (instance->reply_queue)
3578 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003579 instance->reply_queue, instance->reply_queue_h);
3580
3581 megasas_free_cmds(instance);
3582
3583 iounmap(instance->reg_set);
3584
adam radfordb6d5d882010-12-14 18:56:07 -08003585 pci_release_selected_regions(instance->pdev, instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003586}
3587
3588/**
3589 * megasas_get_seq_num - Gets latest event sequence numbers
3590 * @instance: Adapter soft state
3591 * @eli: FW event log sequence numbers information
3592 *
3593 * FW maintains a log of all events in a non-volatile area. Upper layers would
3594 * usually find out the latest sequence number of the events, the seq number at
3595 * the boot etc. They would "read" all the events below the latest seq number
3596 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3597 * number), they would subsribe to AEN (asynchronous event notification) and
3598 * wait for the events to happen.
3599 */
3600static int
3601megasas_get_seq_num(struct megasas_instance *instance,
3602 struct megasas_evt_log_info *eli)
3603{
3604 struct megasas_cmd *cmd;
3605 struct megasas_dcmd_frame *dcmd;
3606 struct megasas_evt_log_info *el_info;
3607 dma_addr_t el_info_h = 0;
3608
3609 cmd = megasas_get_cmd(instance);
3610
3611 if (!cmd) {
3612 return -ENOMEM;
3613 }
3614
3615 dcmd = &cmd->frame->dcmd;
3616 el_info = pci_alloc_consistent(instance->pdev,
3617 sizeof(struct megasas_evt_log_info),
3618 &el_info_h);
3619
3620 if (!el_info) {
3621 megasas_return_cmd(instance, cmd);
3622 return -ENOMEM;
3623 }
3624
3625 memset(el_info, 0, sizeof(*el_info));
3626 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3627
3628 dcmd->cmd = MFI_CMD_DCMD;
3629 dcmd->cmd_status = 0x0;
3630 dcmd->sge_count = 1;
3631 dcmd->flags = MFI_FRAME_DIR_READ;
3632 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003633 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003634 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3635 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3636 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3637 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3638
3639 megasas_issue_blocked_cmd(instance, cmd);
3640
3641 /*
3642 * Copy the data back into callers buffer
3643 */
3644 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3645
3646 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3647 el_info, el_info_h);
3648
3649 megasas_return_cmd(instance, cmd);
3650
3651 return 0;
3652}
3653
3654/**
3655 * megasas_register_aen - Registers for asynchronous event notification
3656 * @instance: Adapter soft state
3657 * @seq_num: The starting sequence number
3658 * @class_locale: Class of the event
3659 *
3660 * This function subscribes for AEN for events beyond the @seq_num. It requests
3661 * to be notified if and only if the event is of type @class_locale
3662 */
3663static int
3664megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3665 u32 class_locale_word)
3666{
3667 int ret_val;
3668 struct megasas_cmd *cmd;
3669 struct megasas_dcmd_frame *dcmd;
3670 union megasas_evt_class_locale curr_aen;
3671 union megasas_evt_class_locale prev_aen;
3672
3673 /*
3674 * If there an AEN pending already (aen_cmd), check if the
3675 * class_locale of that pending AEN is inclusive of the new
3676 * AEN request we currently have. If it is, then we don't have
3677 * to do anything. In other words, whichever events the current
3678 * AEN request is subscribing to, have already been subscribed
3679 * to.
3680 *
3681 * If the old_cmd is _not_ inclusive, then we have to abort
3682 * that command, form a class_locale that is superset of both
3683 * old and current and re-issue to the FW
3684 */
3685
3686 curr_aen.word = class_locale_word;
3687
3688 if (instance->aen_cmd) {
3689
3690 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3691
3692 /*
3693 * A class whose enum value is smaller is inclusive of all
3694 * higher values. If a PROGRESS (= -1) was previously
3695 * registered, then a new registration requests for higher
3696 * classes need not be sent to FW. They are automatically
3697 * included.
3698 *
3699 * Locale numbers don't have such hierarchy. They are bitmap
3700 * values
3701 */
3702 if ((prev_aen.members.class <= curr_aen.members.class) &&
3703 !((prev_aen.members.locale & curr_aen.members.locale) ^
3704 curr_aen.members.locale)) {
3705 /*
3706 * Previously issued event registration includes
3707 * current request. Nothing to do.
3708 */
3709 return 0;
3710 } else {
3711 curr_aen.members.locale |= prev_aen.members.locale;
3712
3713 if (prev_aen.members.class < curr_aen.members.class)
3714 curr_aen.members.class = prev_aen.members.class;
3715
3716 instance->aen_cmd->abort_aen = 1;
3717 ret_val = megasas_issue_blocked_abort_cmd(instance,
3718 instance->
3719 aen_cmd);
3720
3721 if (ret_val) {
3722 printk(KERN_DEBUG "megasas: Failed to abort "
3723 "previous AEN command\n");
3724 return ret_val;
3725 }
3726 }
3727 }
3728
3729 cmd = megasas_get_cmd(instance);
3730
3731 if (!cmd)
3732 return -ENOMEM;
3733
3734 dcmd = &cmd->frame->dcmd;
3735
3736 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3737
3738 /*
3739 * Prepare DCMD for aen registration
3740 */
3741 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3742
3743 dcmd->cmd = MFI_CMD_DCMD;
3744 dcmd->cmd_status = 0x0;
3745 dcmd->sge_count = 1;
3746 dcmd->flags = MFI_FRAME_DIR_READ;
3747 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003748 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003749 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003750 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3751 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3752 dcmd->mbox.w[0] = seq_num;
3753 dcmd->mbox.w[1] = curr_aen.word;
3754 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3755 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3756
Yang, Bof4c9a132009-10-06 14:43:28 -06003757 if (instance->aen_cmd != NULL) {
3758 megasas_return_cmd(instance, cmd);
3759 return 0;
3760 }
3761
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003762 /*
3763 * Store reference to the cmd used to register for AEN. When an
3764 * application wants us to register for AEN, we have to abort this
3765 * cmd and re-register with a new EVENT LOCALE supplied by that app
3766 */
3767 instance->aen_cmd = cmd;
3768
3769 /*
3770 * Issue the aen registration frame
3771 */
adam radford9c915a82010-12-21 13:34:31 -08003772 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003773
3774 return 0;
3775}
3776
3777/**
3778 * megasas_start_aen - Subscribes to AEN during driver load time
3779 * @instance: Adapter soft state
3780 */
3781static int megasas_start_aen(struct megasas_instance *instance)
3782{
3783 struct megasas_evt_log_info eli;
3784 union megasas_evt_class_locale class_locale;
3785
3786 /*
3787 * Get the latest sequence number from FW
3788 */
3789 memset(&eli, 0, sizeof(eli));
3790
3791 if (megasas_get_seq_num(instance, &eli))
3792 return -1;
3793
3794 /*
3795 * Register AEN with FW for latest sequence number plus 1
3796 */
3797 class_locale.members.reserved = 0;
3798 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3799 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3800
3801 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3802 class_locale.word);
3803}
3804
3805/**
3806 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3807 * @instance: Adapter soft state
3808 */
3809static int megasas_io_attach(struct megasas_instance *instance)
3810{
3811 struct Scsi_Host *host = instance->host;
3812
3813 /*
3814 * Export parameters required by SCSI mid-layer
3815 */
3816 host->irq = instance->pdev->irq;
3817 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003818 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3819 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3820 host->can_queue =
3821 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3822 } else
3823 host->can_queue =
3824 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003825 host->this_id = instance->init_id;
3826 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08003827
3828 if (instance->fw_support_ieee)
3829 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3830
Yang, Bo1fd10682010-10-12 07:18:50 -06003831 /*
3832 * Check if the module parameter value for max_sectors can be used
3833 */
3834 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3835 instance->max_sectors_per_req = max_sectors;
3836 else {
3837 if (max_sectors) {
3838 if (((instance->pdev->device ==
3839 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3840 (instance->pdev->device ==
3841 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3842 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3843 instance->max_sectors_per_req = max_sectors;
3844 } else {
3845 printk(KERN_INFO "megasas: max_sectors should be > 0"
3846 "and <= %d (or < 1MB for GEN2 controller)\n",
3847 instance->max_sectors_per_req);
3848 }
3849 }
3850 }
3851
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003852 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08003853 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003854 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3855 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3856 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003857 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003858
adam radford9c915a82010-12-21 13:34:31 -08003859 /* Fusion only supports host reset */
3860 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
3861 host->hostt->eh_device_reset_handler = NULL;
3862 host->hostt->eh_bus_reset_handler = NULL;
3863 }
3864
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003865 /*
3866 * Notify the mid-layer about the new controller
3867 */
3868 if (scsi_add_host(host, &instance->pdev->dev)) {
3869 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3870 return -ENODEV;
3871 }
3872
3873 /*
3874 * Trigger SCSI to scan our drives
3875 */
3876 scsi_scan_host(host);
3877 return 0;
3878}
3879
bo yang31ea7082007-11-07 12:09:50 -05003880static int
3881megasas_set_dma_mask(struct pci_dev *pdev)
3882{
3883 /*
3884 * All our contollers are capable of performing 64-bit DMA
3885 */
3886 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003887 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003888
Yang Hongyang284901a2009-04-06 19:01:15 -07003889 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003890 goto fail_set_dma_mask;
3891 }
3892 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003893 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003894 goto fail_set_dma_mask;
3895 }
3896 return 0;
3897
3898fail_set_dma_mask:
3899 return 1;
3900}
3901
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003902/**
3903 * megasas_probe_one - PCI hotplug entry point
3904 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08003905 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003906 */
3907static int __devinit
3908megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3909{
adam radford66192dfe2011-02-24 20:56:28 -08003910 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003911 struct Scsi_Host *host;
3912 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08003913 u16 control = 0;
3914
3915 /* Reset MSI-X in the kdump kernel */
3916 if (reset_devices) {
3917 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3918 if (pos) {
3919 pci_read_config_word(pdev, msi_control_reg(pos),
3920 &control);
3921 if (control & PCI_MSIX_FLAGS_ENABLE) {
3922 dev_info(&pdev->dev, "resetting MSI-X\n");
3923 pci_write_config_word(pdev,
3924 msi_control_reg(pos),
3925 control &
3926 ~PCI_MSIX_FLAGS_ENABLE);
3927 }
3928 }
3929 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003930
3931 /*
3932 * Announce PCI information
3933 */
3934 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3935 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3936 pdev->subsystem_device);
3937
3938 printk("bus %d:slot %d:func %d\n",
3939 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3940
3941 /*
3942 * PCI prepping: enable device set bus mastering and dma mask
3943 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003944 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003945
3946 if (rval) {
3947 return rval;
3948 }
3949
3950 pci_set_master(pdev);
3951
bo yang31ea7082007-11-07 12:09:50 -05003952 if (megasas_set_dma_mask(pdev))
3953 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003954
3955 host = scsi_host_alloc(&megasas_template,
3956 sizeof(struct megasas_instance));
3957
3958 if (!host) {
3959 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3960 goto fail_alloc_instance;
3961 }
3962
3963 instance = (struct megasas_instance *)host->hostdata;
3964 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04003965 atomic_set( &instance->fw_reset_no_pci_access, 0 );
adam radford9c915a82010-12-21 13:34:31 -08003966 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003967
adam radford9c915a82010-12-21 13:34:31 -08003968 switch (instance->pdev->device) {
3969 case PCI_DEVICE_ID_LSI_FUSION:
3970 {
3971 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003972
adam radford9c915a82010-12-21 13:34:31 -08003973 instance->ctrl_context =
3974 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
3975 if (!instance->ctrl_context) {
3976 printk(KERN_DEBUG "megasas: Failed to allocate "
3977 "memory for Fusion context info\n");
3978 goto fail_alloc_dma_buf;
3979 }
3980 fusion = instance->ctrl_context;
3981 INIT_LIST_HEAD(&fusion->cmd_pool);
3982 spin_lock_init(&fusion->cmd_pool_lock);
3983 }
3984 break;
3985 default: /* For all other supported controllers */
3986
3987 instance->producer =
3988 pci_alloc_consistent(pdev, sizeof(u32),
3989 &instance->producer_h);
3990 instance->consumer =
3991 pci_alloc_consistent(pdev, sizeof(u32),
3992 &instance->consumer_h);
3993
3994 if (!instance->producer || !instance->consumer) {
3995 printk(KERN_DEBUG "megasas: Failed to allocate"
3996 "memory for producer, consumer\n");
3997 goto fail_alloc_dma_buf;
3998 }
3999
4000 *instance->producer = 0;
4001 *instance->consumer = 0;
4002 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004003 }
4004
Yang, Boc3518832009-10-06 14:18:02 -06004005 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06004006 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004007 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04004008 instance->issuepend_done = 1;
4009 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4010 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004011
4012 instance->evt_detail = pci_alloc_consistent(pdev,
4013 sizeof(struct
4014 megasas_evt_detail),
4015 &instance->evt_detail_h);
4016
4017 if (!instance->evt_detail) {
4018 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4019 "event detail structure\n");
4020 goto fail_alloc_dma_buf;
4021 }
4022
4023 /*
4024 * Initialize locks and queues
4025 */
4026 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04004027 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004028
Sumant Patroe4a082c2006-05-30 12:03:37 -07004029 atomic_set(&instance->fw_outstanding,0);
4030
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004031 init_waitqueue_head(&instance->int_cmd_wait_q);
4032 init_waitqueue_head(&instance->abort_cmd_wait_q);
4033
4034 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04004035 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05004036 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06004037 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004038
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004039 mutex_init(&instance->aen_mutex);
adam radford9c915a82010-12-21 13:34:31 -08004040 mutex_init(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004041
4042 /*
4043 * Initialize PCI related and misc parameters
4044 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004045 instance->host = host;
4046 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4047 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4048
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004049 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4050 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06004051 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06004052 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4053 } else
4054 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4055
Sumant Patro658dced2006-10-03 13:09:14 -07004056 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004057 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06004058 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07004059 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04004060 instance->disableOnlineCtrlReset = 1;
4061
adam radford9c915a82010-12-21 13:34:31 -08004062 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4063 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4064 else
4065 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07004066
adam radford80d9da92010-12-21 10:17:40 -08004067 /* Try to enable MSI-X */
4068 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078R) &&
4069 (instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078DE) &&
4070 (instance->pdev->device != PCI_DEVICE_ID_LSI_VERDE_ZCR) &&
4071 !msix_disable && !pci_enable_msix(instance->pdev,
4072 &instance->msixentry, 1))
4073 instance->msi_flag = 1;
4074
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004075 /*
adam radford0a770662011-02-24 20:56:12 -08004076 * Initialize MFI Firmware
4077 */
4078 if (megasas_init_fw(instance))
4079 goto fail_init_mfi;
4080
4081 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004082 * Register IRQ
4083 */
adam radford80d9da92010-12-21 10:17:40 -08004084 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004085 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004086 IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004087 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4088 goto fail_irq;
4089 }
4090
Sumant Patro1341c932006-01-25 12:02:40 -08004091 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004092
4093 /*
4094 * Store instance in PCI softstate
4095 */
4096 pci_set_drvdata(pdev, instance);
4097
4098 /*
4099 * Add this controller to megasas_mgmt_info structure so that it
4100 * can be exported to management applications
4101 */
4102 megasas_mgmt_info.count++;
4103 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4104 megasas_mgmt_info.max_index++;
4105
4106 /*
4107 * Initiate AEN (Asynchronous Event Notification)
4108 */
4109 if (megasas_start_aen(instance)) {
4110 printk(KERN_DEBUG "megasas: start aen failed\n");
4111 goto fail_start_aen;
4112 }
4113
4114 /*
4115 * Register with SCSI mid-layer
4116 */
4117 if (megasas_io_attach(instance))
4118 goto fail_io_attach;
4119
Yang, Bo0c79e682009-10-06 14:47:35 -06004120 instance->unload = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004121 return 0;
4122
4123 fail_start_aen:
4124 fail_io_attach:
4125 megasas_mgmt_info.count--;
4126 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4127 megasas_mgmt_info.max_index--;
4128
4129 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07004130 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004131 free_irq(instance->msi_flag ? instance->msixentry.vector :
4132 instance->pdev->irq, instance);
adam radfordeb1b1232011-02-24 20:55:56 -08004133fail_irq:
adam radfordeb1b1232011-02-24 20:55:56 -08004134 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4135 megasas_release_fusion(instance);
4136 else
4137 megasas_release_mfi(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004138 fail_init_mfi:
adam radford0a770662011-02-24 20:56:12 -08004139 if (instance->msi_flag)
4140 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004141 fail_alloc_dma_buf:
4142 if (instance->evt_detail)
4143 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4144 instance->evt_detail,
4145 instance->evt_detail_h);
4146
adam radfordeb1b1232011-02-24 20:55:56 -08004147 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004148 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4149 instance->producer_h);
4150 if (instance->consumer)
4151 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4152 instance->consumer_h);
4153 scsi_host_put(host);
4154
4155 fail_alloc_instance:
4156 fail_set_dma_mask:
4157 pci_disable_device(pdev);
4158
4159 return -ENODEV;
4160}
4161
4162/**
4163 * megasas_flush_cache - Requests FW to flush all its caches
4164 * @instance: Adapter soft state
4165 */
4166static void megasas_flush_cache(struct megasas_instance *instance)
4167{
4168 struct megasas_cmd *cmd;
4169 struct megasas_dcmd_frame *dcmd;
4170
bo yang39a98552010-09-22 22:36:29 -04004171 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4172 return;
4173
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004174 cmd = megasas_get_cmd(instance);
4175
4176 if (!cmd)
4177 return;
4178
4179 dcmd = &cmd->frame->dcmd;
4180
4181 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4182
4183 dcmd->cmd = MFI_CMD_DCMD;
4184 dcmd->cmd_status = 0x0;
4185 dcmd->sge_count = 0;
4186 dcmd->flags = MFI_FRAME_DIR_NONE;
4187 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004188 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004189 dcmd->data_xfer_len = 0;
4190 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4191 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4192
4193 megasas_issue_blocked_cmd(instance, cmd);
4194
4195 megasas_return_cmd(instance, cmd);
4196
4197 return;
4198}
4199
4200/**
4201 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4202 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05004203 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004204 */
bo yang31ea7082007-11-07 12:09:50 -05004205static void megasas_shutdown_controller(struct megasas_instance *instance,
4206 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004207{
4208 struct megasas_cmd *cmd;
4209 struct megasas_dcmd_frame *dcmd;
4210
bo yang39a98552010-09-22 22:36:29 -04004211 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4212 return;
4213
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004214 cmd = megasas_get_cmd(instance);
4215
4216 if (!cmd)
4217 return;
4218
4219 if (instance->aen_cmd)
4220 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
adam radford9c915a82010-12-21 13:34:31 -08004221 if (instance->map_update_cmd)
4222 megasas_issue_blocked_abort_cmd(instance,
4223 instance->map_update_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004224 dcmd = &cmd->frame->dcmd;
4225
4226 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4227
4228 dcmd->cmd = MFI_CMD_DCMD;
4229 dcmd->cmd_status = 0x0;
4230 dcmd->sge_count = 0;
4231 dcmd->flags = MFI_FRAME_DIR_NONE;
4232 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004233 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004234 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05004235 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004236
4237 megasas_issue_blocked_cmd(instance, cmd);
4238
4239 megasas_return_cmd(instance, cmd);
4240
4241 return;
4242}
4243
Jiri Slaby33139b22008-05-01 17:56:02 +02004244#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004245/**
bo yangad84db22007-11-09 04:40:16 -05004246 * megasas_suspend - driver suspend entry point
4247 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05004248 * @state: PCI power state to suspend routine
4249 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004250static int
bo yang31ea7082007-11-07 12:09:50 -05004251megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4252{
4253 struct Scsi_Host *host;
4254 struct megasas_instance *instance;
4255
4256 instance = pci_get_drvdata(pdev);
4257 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06004258 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05004259
bo yangad84db22007-11-09 04:40:16 -05004260 if (poll_mode_io)
4261 del_timer_sync(&instance->io_completion_timer);
4262
bo yang31ea7082007-11-07 12:09:50 -05004263 megasas_flush_cache(instance);
4264 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004265
4266 /* cancel the delayed work if this work still in queue */
4267 if (instance->ev != NULL) {
4268 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004269 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004270 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004271 instance->ev = NULL;
4272 }
4273
bo yang31ea7082007-11-07 12:09:50 -05004274 tasklet_kill(&instance->isr_tasklet);
4275
4276 pci_set_drvdata(instance->pdev, instance);
4277 instance->instancet->disable_intr(instance->reg_set);
adam radford80d9da92010-12-21 10:17:40 -08004278 free_irq(instance->msi_flag ? instance->msixentry.vector :
4279 instance->pdev->irq, instance);
4280 if (instance->msi_flag)
4281 pci_disable_msix(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05004282
4283 pci_save_state(pdev);
4284 pci_disable_device(pdev);
4285
4286 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4287
4288 return 0;
4289}
4290
4291/**
4292 * megasas_resume- driver resume entry point
4293 * @pdev: PCI device structure
4294 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004295static int
bo yang31ea7082007-11-07 12:09:50 -05004296megasas_resume(struct pci_dev *pdev)
4297{
4298 int rval;
4299 struct Scsi_Host *host;
4300 struct megasas_instance *instance;
4301
4302 instance = pci_get_drvdata(pdev);
4303 host = instance->host;
4304 pci_set_power_state(pdev, PCI_D0);
4305 pci_enable_wake(pdev, PCI_D0, 0);
4306 pci_restore_state(pdev);
4307
4308 /*
4309 * PCI prepping: enable device set bus mastering and dma mask
4310 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004311 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004312
4313 if (rval) {
4314 printk(KERN_ERR "megasas: Enable device failed\n");
4315 return rval;
4316 }
4317
4318 pci_set_master(pdev);
4319
4320 if (megasas_set_dma_mask(pdev))
4321 goto fail_set_dma_mask;
4322
adam radford0a770662011-02-24 20:56:12 -08004323 /* Now re-enable MSI-X */
4324 if (instance->msi_flag)
4325 pci_enable_msix(instance->pdev, &instance->msixentry, 1);
4326
bo yang31ea7082007-11-07 12:09:50 -05004327 /*
4328 * Initialize MFI Firmware
4329 */
4330
bo yang31ea7082007-11-07 12:09:50 -05004331 atomic_set(&instance->fw_outstanding, 0);
4332
4333 /*
4334 * We expect the FW state to be READY
4335 */
4336 if (megasas_transition_to_ready(instance))
4337 goto fail_ready_state;
4338
adam radford9c915a82010-12-21 13:34:31 -08004339 switch (instance->pdev->device) {
4340 case PCI_DEVICE_ID_LSI_FUSION:
4341 {
4342 megasas_reset_reply_desc(instance);
4343 if (megasas_ioc_init_fusion(instance)) {
4344 megasas_free_cmds(instance);
4345 megasas_free_cmds_fusion(instance);
4346 goto fail_init_mfi;
4347 }
4348 if (!megasas_get_map_info(instance))
4349 megasas_sync_map_info(instance);
4350 }
4351 break;
4352 default:
4353 *instance->producer = 0;
4354 *instance->consumer = 0;
4355 if (megasas_issue_init_mfi(instance))
4356 goto fail_init_mfi;
4357 break;
4358 }
bo yang31ea7082007-11-07 12:09:50 -05004359
adam radford9c915a82010-12-21 13:34:31 -08004360 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4361 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05004362
4363 /*
4364 * Register IRQ
4365 */
adam radford80d9da92010-12-21 10:17:40 -08004366 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
adam radfordcd50ba82010-12-21 10:23:23 -08004367 pdev->irq, instance->instancet->service_isr,
adam radford80d9da92010-12-21 10:17:40 -08004368 IRQF_SHARED, "megasas", instance)) {
bo yang31ea7082007-11-07 12:09:50 -05004369 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4370 goto fail_irq;
4371 }
4372
4373 instance->instancet->enable_intr(instance->reg_set);
4374
4375 /*
4376 * Initiate AEN (Asynchronous Event Notification)
4377 */
4378 if (megasas_start_aen(instance))
4379 printk(KERN_ERR "megasas: Start AEN failed\n");
4380
bo yangad84db22007-11-09 04:40:16 -05004381 /* Initialize the cmd completion timer */
4382 if (poll_mode_io)
4383 megasas_start_timer(instance, &instance->io_completion_timer,
4384 megasas_io_completion_timer,
4385 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06004386 instance->unload = 0;
4387
bo yang31ea7082007-11-07 12:09:50 -05004388 return 0;
4389
4390fail_irq:
4391fail_init_mfi:
4392 if (instance->evt_detail)
4393 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4394 instance->evt_detail,
4395 instance->evt_detail_h);
4396
4397 if (instance->producer)
4398 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4399 instance->producer_h);
4400 if (instance->consumer)
4401 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4402 instance->consumer_h);
4403 scsi_host_put(host);
4404
4405fail_set_dma_mask:
4406fail_ready_state:
4407
4408 pci_disable_device(pdev);
4409
4410 return -ENODEV;
4411}
Jiri Slaby33139b22008-05-01 17:56:02 +02004412#else
4413#define megasas_suspend NULL
4414#define megasas_resume NULL
4415#endif
bo yang31ea7082007-11-07 12:09:50 -05004416
4417/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004418 * megasas_detach_one - PCI hot"un"plug entry point
4419 * @pdev: PCI device structure
4420 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004421static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004422{
4423 int i;
4424 struct Scsi_Host *host;
4425 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08004426 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004427
4428 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004429 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004430 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08004431 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004432
bo yangad84db22007-11-09 04:40:16 -05004433 if (poll_mode_io)
4434 del_timer_sync(&instance->io_completion_timer);
4435
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004436 scsi_remove_host(instance->host);
4437 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004438 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004439
4440 /* cancel the delayed work if this work still in queue*/
4441 if (instance->ev != NULL) {
4442 struct megasas_aen_event *ev = instance->ev;
Tejun Heoa684b8d2011-01-24 14:57:28 +01004443 cancel_delayed_work_sync(
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004444 (struct delayed_work *)&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004445 instance->ev = NULL;
4446 }
4447
Sumant Patro5d018ad2006-10-03 13:13:18 -07004448 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004449
4450 /*
4451 * Take the instance off the instance array. Note that we will not
4452 * decrement the max_index. We let this array be sparse array
4453 */
4454 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4455 if (megasas_mgmt_info.instance[i] == instance) {
4456 megasas_mgmt_info.count--;
4457 megasas_mgmt_info.instance[i] = NULL;
4458
4459 break;
4460 }
4461 }
4462
4463 pci_set_drvdata(instance->pdev, NULL);
4464
Sumant Patrob274cab2006-10-03 12:52:12 -07004465 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004466
adam radford80d9da92010-12-21 10:17:40 -08004467 free_irq(instance->msi_flag ? instance->msixentry.vector :
4468 instance->pdev->irq, instance);
4469 if (instance->msi_flag)
4470 pci_disable_msix(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004471
adam radford9c915a82010-12-21 13:34:31 -08004472 switch (instance->pdev->device) {
4473 case PCI_DEVICE_ID_LSI_FUSION:
4474 megasas_release_fusion(instance);
4475 for (i = 0; i < 2 ; i++)
4476 if (fusion->ld_map[i])
4477 dma_free_coherent(&instance->pdev->dev,
4478 fusion->map_sz,
4479 fusion->ld_map[i],
4480 fusion->
4481 ld_map_phys[i]);
4482 kfree(instance->ctrl_context);
4483 break;
4484 default:
4485 megasas_release_mfi(instance);
4486 pci_free_consistent(pdev,
4487 sizeof(struct megasas_evt_detail),
4488 instance->evt_detail,
4489 instance->evt_detail_h);
4490 pci_free_consistent(pdev, sizeof(u32),
4491 instance->producer,
4492 instance->producer_h);
4493 pci_free_consistent(pdev, sizeof(u32),
4494 instance->consumer,
4495 instance->consumer_h);
4496 break;
4497 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004498
4499 scsi_host_put(host);
4500
4501 pci_set_drvdata(pdev, NULL);
4502
4503 pci_disable_device(pdev);
4504
4505 return;
4506}
4507
4508/**
4509 * megasas_shutdown - Shutdown entry point
4510 * @device: Generic device structure
4511 */
4512static void megasas_shutdown(struct pci_dev *pdev)
4513{
4514 struct megasas_instance *instance = pci_get_drvdata(pdev);
Yang, Bo0c79e682009-10-06 14:47:35 -06004515 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004516 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004517 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004518}
4519
4520/**
4521 * megasas_mgmt_open - char node "open" entry point
4522 */
4523static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4524{
4525 /*
4526 * Allow only those users with admin rights
4527 */
4528 if (!capable(CAP_SYS_ADMIN))
4529 return -EACCES;
4530
4531 return 0;
4532}
4533
4534/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004535 * megasas_mgmt_fasync - Async notifier registration from applications
4536 *
4537 * This function adds the calling process to a driver global queue. When an
4538 * event occurs, SIGIO will be sent to all processes in this queue.
4539 */
4540static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4541{
4542 int rc;
4543
Arjan van de Ven0b950672006-01-11 13:16:10 +01004544 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004545
4546 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4547
Arjan van de Ven0b950672006-01-11 13:16:10 +01004548 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004549
4550 if (rc >= 0) {
4551 /* For sanity check when we get ioctl */
4552 filep->private_data = filep;
4553 return 0;
4554 }
4555
4556 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4557
4558 return rc;
4559}
4560
4561/**
Yang, Boc3518832009-10-06 14:18:02 -06004562 * megasas_mgmt_poll - char node "poll" entry point
4563 * */
4564static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4565{
4566 unsigned int mask;
4567 unsigned long flags;
4568 poll_wait(file, &megasas_poll_wait, wait);
4569 spin_lock_irqsave(&poll_aen_lock, flags);
4570 if (megasas_poll_wait_aen)
4571 mask = (POLLIN | POLLRDNORM);
4572 else
4573 mask = 0;
4574 spin_unlock_irqrestore(&poll_aen_lock, flags);
4575 return mask;
4576}
4577
4578/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004579 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4580 * @instance: Adapter soft state
4581 * @argp: User's ioctl packet
4582 */
4583static int
4584megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4585 struct megasas_iocpacket __user * user_ioc,
4586 struct megasas_iocpacket *ioc)
4587{
4588 struct megasas_sge32 *kern_sge32;
4589 struct megasas_cmd *cmd;
4590 void *kbuff_arr[MAX_IOCTL_SGE];
4591 dma_addr_t buf_handle = 0;
4592 int error = 0, i;
4593 void *sense = NULL;
4594 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004595 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004596
4597 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4598
4599 if (ioc->sge_count > MAX_IOCTL_SGE) {
4600 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4601 ioc->sge_count, MAX_IOCTL_SGE);
4602 return -EINVAL;
4603 }
4604
4605 cmd = megasas_get_cmd(instance);
4606 if (!cmd) {
4607 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4608 return -ENOMEM;
4609 }
4610
4611 /*
4612 * User's IOCTL packet has 2 frames (maximum). Copy those two
4613 * frames into our cmd's frames. cmd->frame's context will get
4614 * overwritten when we copy from user's frames. So set that value
4615 * alone separately
4616 */
4617 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4618 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004619 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004620
4621 /*
4622 * The management interface between applications and the fw uses
4623 * MFI frames. E.g, RAID configuration changes, LD property changes
4624 * etc are accomplishes through different kinds of MFI frames. The
4625 * driver needs to care only about substituting user buffers with
4626 * kernel buffers in SGLs. The location of SGL is embedded in the
4627 * struct iocpacket itself.
4628 */
4629 kern_sge32 = (struct megasas_sge32 *)
4630 ((unsigned long)cmd->frame + ioc->sgl_off);
4631
4632 /*
4633 * For each user buffer, create a mirror buffer and copy in
4634 */
4635 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01004636 if (!ioc->sgl[i].iov_len)
4637 continue;
4638
Sumant Patro9f35fa82007-02-14 12:55:45 -08004639 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004640 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004641 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004642 if (!kbuff_arr[i]) {
4643 printk(KERN_DEBUG "megasas: Failed to alloc "
4644 "kernel SGL buffer for IOCTL \n");
4645 error = -ENOMEM;
4646 goto out;
4647 }
4648
4649 /*
4650 * We don't change the dma_coherent_mask, so
4651 * pci_alloc_consistent only returns 32bit addresses
4652 */
4653 kern_sge32[i].phys_addr = (u32) buf_handle;
4654 kern_sge32[i].length = ioc->sgl[i].iov_len;
4655
4656 /*
4657 * We created a kernel buffer corresponding to the
4658 * user buffer. Now copy in from the user buffer
4659 */
4660 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4661 (u32) (ioc->sgl[i].iov_len))) {
4662 error = -EFAULT;
4663 goto out;
4664 }
4665 }
4666
4667 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004668 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4669 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004670 if (!sense) {
4671 error = -ENOMEM;
4672 goto out;
4673 }
4674
4675 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004676 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004677 *sense_ptr = sense_handle;
4678 }
4679
4680 /*
4681 * Set the sync_cmd flag so that the ISR knows not to complete this
4682 * cmd to the SCSI mid-layer
4683 */
4684 cmd->sync_cmd = 1;
4685 megasas_issue_blocked_cmd(instance, cmd);
4686 cmd->sync_cmd = 0;
4687
4688 /*
4689 * copy out the kernel buffers to user buffers
4690 */
4691 for (i = 0; i < ioc->sge_count; i++) {
4692 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4693 ioc->sgl[i].iov_len)) {
4694 error = -EFAULT;
4695 goto out;
4696 }
4697 }
4698
4699 /*
4700 * copy out the sense
4701 */
4702 if (ioc->sense_len) {
4703 /*
bo yangb70a41e2008-03-18 03:13:06 -04004704 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004705 * sense buffer address
4706 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004707 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4708 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004709
bo yangb70a41e2008-03-18 03:13:06 -04004710 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4711 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004712 printk(KERN_ERR "megasas: Failed to copy out to user "
4713 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004714 error = -EFAULT;
4715 goto out;
4716 }
4717 }
4718
4719 /*
4720 * copy the status codes returned by the fw
4721 */
4722 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4723 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4724 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4725 error = -EFAULT;
4726 }
4727
4728 out:
4729 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004730 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004731 sense, sense_handle);
4732 }
4733
4734 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004735 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004736 kern_sge32[i].length,
4737 kbuff_arr[i], kern_sge32[i].phys_addr);
4738 }
4739
4740 megasas_return_cmd(instance, cmd);
4741 return error;
4742}
4743
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004744static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4745{
4746 struct megasas_iocpacket __user *user_ioc =
4747 (struct megasas_iocpacket __user *)arg;
4748 struct megasas_iocpacket *ioc;
4749 struct megasas_instance *instance;
4750 int error;
bo yang39a98552010-09-22 22:36:29 -04004751 int i;
4752 unsigned long flags;
4753 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004754
4755 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4756 if (!ioc)
4757 return -ENOMEM;
4758
4759 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4760 error = -EFAULT;
4761 goto out_kfree_ioc;
4762 }
4763
4764 instance = megasas_lookup_instance(ioc->host_no);
4765 if (!instance) {
4766 error = -ENODEV;
4767 goto out_kfree_ioc;
4768 }
4769
bo yang39a98552010-09-22 22:36:29 -04004770 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4771 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004772 error = -ENODEV;
4773 goto out_kfree_ioc;
4774 }
4775
4776 if (instance->unload == 1) {
4777 error = -ENODEV;
4778 goto out_kfree_ioc;
4779 }
4780
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004781 /*
4782 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4783 */
4784 if (down_interruptible(&instance->ioctl_sem)) {
4785 error = -ERESTARTSYS;
4786 goto out_kfree_ioc;
4787 }
bo yang39a98552010-09-22 22:36:29 -04004788
4789 for (i = 0; i < wait_time; i++) {
4790
4791 spin_lock_irqsave(&instance->hba_lock, flags);
4792 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4793 spin_unlock_irqrestore(&instance->hba_lock, flags);
4794 break;
4795 }
4796 spin_unlock_irqrestore(&instance->hba_lock, flags);
4797
4798 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4799 printk(KERN_NOTICE "megasas: waiting"
4800 "for controller reset to finish\n");
4801 }
4802
4803 msleep(1000);
4804 }
4805
4806 spin_lock_irqsave(&instance->hba_lock, flags);
4807 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4808 spin_unlock_irqrestore(&instance->hba_lock, flags);
4809
4810 printk(KERN_ERR "megaraid_sas: timed out while"
4811 "waiting for HBA to recover\n");
4812 error = -ENODEV;
4813 goto out_kfree_ioc;
4814 }
4815 spin_unlock_irqrestore(&instance->hba_lock, flags);
4816
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004817 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4818 up(&instance->ioctl_sem);
4819
4820 out_kfree_ioc:
4821 kfree(ioc);
4822 return error;
4823}
4824
4825static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4826{
4827 struct megasas_instance *instance;
4828 struct megasas_aen aen;
4829 int error;
bo yang39a98552010-09-22 22:36:29 -04004830 int i;
4831 unsigned long flags;
4832 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004833
4834 if (file->private_data != file) {
4835 printk(KERN_DEBUG "megasas: fasync_helper was not "
4836 "called first\n");
4837 return -EINVAL;
4838 }
4839
4840 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4841 return -EFAULT;
4842
4843 instance = megasas_lookup_instance(aen.host_no);
4844
4845 if (!instance)
4846 return -ENODEV;
4847
bo yang39a98552010-09-22 22:36:29 -04004848 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4849 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004850 }
4851
4852 if (instance->unload == 1) {
4853 return -ENODEV;
4854 }
4855
bo yang39a98552010-09-22 22:36:29 -04004856 for (i = 0; i < wait_time; i++) {
4857
4858 spin_lock_irqsave(&instance->hba_lock, flags);
4859 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4860 spin_unlock_irqrestore(&instance->hba_lock,
4861 flags);
4862 break;
4863 }
4864
4865 spin_unlock_irqrestore(&instance->hba_lock, flags);
4866
4867 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4868 printk(KERN_NOTICE "megasas: waiting for"
4869 "controller reset to finish\n");
4870 }
4871
4872 msleep(1000);
4873 }
4874
4875 spin_lock_irqsave(&instance->hba_lock, flags);
4876 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4877 spin_unlock_irqrestore(&instance->hba_lock, flags);
4878 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4879 "for HBA to recover.\n");
4880 return -ENODEV;
4881 }
4882 spin_unlock_irqrestore(&instance->hba_lock, flags);
4883
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004884 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004885 error = megasas_register_aen(instance, aen.seq_num,
4886 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004887 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004888 return error;
4889}
4890
4891/**
4892 * megasas_mgmt_ioctl - char node ioctl entry point
4893 */
4894static long
4895megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4896{
4897 switch (cmd) {
4898 case MEGASAS_IOC_FIRMWARE:
4899 return megasas_mgmt_ioctl_fw(file, arg);
4900
4901 case MEGASAS_IOC_GET_AEN:
4902 return megasas_mgmt_ioctl_aen(file, arg);
4903 }
4904
4905 return -ENOTTY;
4906}
4907
4908#ifdef CONFIG_COMPAT
4909static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4910{
4911 struct compat_megasas_iocpacket __user *cioc =
4912 (struct compat_megasas_iocpacket __user *)arg;
4913 struct megasas_iocpacket __user *ioc =
4914 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4915 int i;
4916 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004917 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004918
Jeff Garzik83aabc12006-10-04 06:34:03 -04004919 if (clear_user(ioc, sizeof(*ioc)))
4920 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004921
4922 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4923 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4924 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4925 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4926 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4927 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4928 return -EFAULT;
4929
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004930 /*
4931 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4932 * sense_len is not null, so prepare the 64bit value under
4933 * the same condition.
4934 */
4935 if (ioc->sense_len) {
4936 void __user **sense_ioc_ptr =
4937 (void __user **)(ioc->frame.raw + ioc->sense_off);
4938 compat_uptr_t *sense_cioc_ptr =
4939 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4940 if (get_user(ptr, sense_cioc_ptr) ||
4941 put_user(compat_ptr(ptr), sense_ioc_ptr))
4942 return -EFAULT;
4943 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004944
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004945 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004946 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4947 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4948 copy_in_user(&ioc->sgl[i].iov_len,
4949 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4950 return -EFAULT;
4951 }
4952
4953 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4954
4955 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4956 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4957 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4958 return -EFAULT;
4959 }
4960 return error;
4961}
4962
4963static long
4964megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4965 unsigned long arg)
4966{
4967 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08004968 case MEGASAS_IOC_FIRMWARE32:
4969 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004970 case MEGASAS_IOC_GET_AEN:
4971 return megasas_mgmt_ioctl_aen(file, arg);
4972 }
4973
4974 return -ENOTTY;
4975}
4976#endif
4977
4978/*
4979 * File operations structure for management interface
4980 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08004981static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004982 .owner = THIS_MODULE,
4983 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004984 .fasync = megasas_mgmt_fasync,
4985 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06004986 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004987#ifdef CONFIG_COMPAT
4988 .compat_ioctl = megasas_mgmt_compat_ioctl,
4989#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02004990 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004991};
4992
4993/*
4994 * PCI hotplug support registration structure
4995 */
4996static struct pci_driver megasas_pci_driver = {
4997
4998 .name = "megaraid_sas",
4999 .id_table = megasas_pci_table,
5000 .probe = megasas_probe_one,
5001 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05005002 .suspend = megasas_suspend,
5003 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005004 .shutdown = megasas_shutdown,
5005};
5006
5007/*
5008 * Sysfs driver attributes
5009 */
5010static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5011{
5012 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5013 MEGASAS_VERSION);
5014}
5015
5016static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5017
5018static ssize_t
5019megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5020{
5021 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5022 MEGASAS_RELDATE);
5023}
5024
5025static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5026 NULL);
5027
Sumant Patro658dced2006-10-03 13:09:14 -07005028static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06005029megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5030{
5031 return sprintf(buf, "%u\n", support_poll_for_event);
5032}
5033
5034static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5035 megasas_sysfs_show_support_poll_for_event, NULL);
5036
Yang, Bo837f5fe2010-10-11 06:59:20 -06005037 static ssize_t
5038megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5039{
5040 return sprintf(buf, "%u\n", support_device_change);
5041}
5042
5043static DRIVER_ATTR(support_device_change, S_IRUGO,
5044 megasas_sysfs_show_support_device_change, NULL);
5045
Yang, Bo72c4fd32009-10-06 14:20:59 -06005046static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07005047megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5048{
bo yangad84db22007-11-09 04:40:16 -05005049 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07005050}
5051
5052static ssize_t
5053megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5054{
5055 int retval = count;
5056 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5057 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5058 retval = -EINVAL;
5059 }
5060 return retval;
5061}
5062
Joe Malicki66dca9b2008-08-14 17:14:48 -04005063static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05005064 megasas_sysfs_set_dbg_lvl);
5065
5066static ssize_t
5067megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
5068{
5069 return sprintf(buf, "%u\n", poll_mode_io);
5070}
5071
5072static ssize_t
5073megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
5074 const char *buf, size_t count)
5075{
5076 int retval = count;
5077 int tmp = poll_mode_io;
5078 int i;
5079 struct megasas_instance *instance;
5080
5081 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
5082 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
5083 retval = -EINVAL;
5084 }
5085
5086 /*
5087 * Check if poll_mode_io is already set or is same as previous value
5088 */
5089 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
5090 goto out;
5091
5092 if (poll_mode_io) {
5093 /*
5094 * Start timers for all adapters
5095 */
5096 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5097 instance = megasas_mgmt_info.instance[i];
5098 if (instance) {
5099 megasas_start_timer(instance,
5100 &instance->io_completion_timer,
5101 megasas_io_completion_timer,
5102 MEGASAS_COMPLETION_TIMER_INTERVAL);
5103 }
5104 }
5105 } else {
5106 /*
5107 * Delete timers for all adapters
5108 */
5109 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5110 instance = megasas_mgmt_info.instance[i];
5111 if (instance)
5112 del_timer_sync(&instance->io_completion_timer);
5113 }
5114 }
5115
5116out:
5117 return retval;
5118}
5119
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005120static void
5121megasas_aen_polling(struct work_struct *work)
5122{
5123 struct megasas_aen_event *ev =
5124 container_of(work, struct megasas_aen_event, hotplug_work);
5125 struct megasas_instance *instance = ev->instance;
5126 union megasas_evt_class_locale class_locale;
5127 struct Scsi_Host *host;
5128 struct scsi_device *sdev1;
5129 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07005130 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005131 int i, j, doscan = 0;
5132 u32 seq_num;
5133 int error;
5134
5135 if (!instance) {
5136 printk(KERN_ERR "invalid instance!\n");
5137 kfree(ev);
5138 return;
5139 }
5140 instance->ev = NULL;
5141 host = instance->host;
5142 if (instance->evt_detail) {
5143
5144 switch (instance->evt_detail->code) {
5145 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07005146 if (megasas_get_pd_list(instance) == 0) {
5147 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5148 for (j = 0;
5149 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5150 j++) {
5151
5152 pd_index =
5153 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5154
5155 sdev1 =
5156 scsi_device_lookup(host, i, j, 0);
5157
5158 if (instance->pd_list[pd_index].driveState
5159 == MR_PD_STATE_SYSTEM) {
5160 if (!sdev1) {
5161 scsi_add_device(host, i, j, 0);
5162 }
5163
5164 if (sdev1)
5165 scsi_device_put(sdev1);
5166 }
5167 }
5168 }
5169 }
5170 doscan = 0;
5171 break;
5172
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005173 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07005174 if (megasas_get_pd_list(instance) == 0) {
5175 megasas_get_pd_list(instance);
5176 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5177 for (j = 0;
5178 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5179 j++) {
5180
5181 pd_index =
5182 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5183
5184 sdev1 =
5185 scsi_device_lookup(host, i, j, 0);
5186
5187 if (instance->pd_list[pd_index].driveState
5188 == MR_PD_STATE_SYSTEM) {
5189 if (sdev1) {
5190 scsi_device_put(sdev1);
5191 }
5192 } else {
5193 if (sdev1) {
5194 scsi_remove_device(sdev1);
5195 scsi_device_put(sdev1);
5196 }
5197 }
5198 }
5199 }
5200 }
5201 doscan = 0;
5202 break;
5203
5204 case MR_EVT_LD_OFFLINE:
adam radford4c598b22011-02-24 20:56:53 -08005205 case MR_EVT_CFG_CLEARED:
Yang, Boc9786842009-12-06 08:39:25 -07005206 case MR_EVT_LD_DELETED:
5207 megasas_get_ld_list(instance);
5208 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5209 for (j = 0;
5210 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5211 j++) {
5212
5213 ld_index =
5214 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5215
5216 sdev1 = scsi_device_lookup(host,
5217 i + MEGASAS_MAX_LD_CHANNELS,
5218 j,
5219 0);
5220
5221 if (instance->ld_ids[ld_index] != 0xff) {
5222 if (sdev1) {
5223 scsi_device_put(sdev1);
5224 }
5225 } else {
5226 if (sdev1) {
5227 scsi_remove_device(sdev1);
5228 scsi_device_put(sdev1);
5229 }
5230 }
5231 }
5232 }
5233 doscan = 0;
5234 break;
5235 case MR_EVT_LD_CREATED:
5236 megasas_get_ld_list(instance);
5237 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5238 for (j = 0;
5239 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5240 j++) {
5241 ld_index =
5242 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5243
5244 sdev1 = scsi_device_lookup(host,
5245 i+MEGASAS_MAX_LD_CHANNELS,
5246 j, 0);
5247
5248 if (instance->ld_ids[ld_index] !=
5249 0xff) {
5250 if (!sdev1) {
5251 scsi_add_device(host,
5252 i + 2,
5253 j, 0);
5254 }
5255 }
5256 if (sdev1) {
5257 scsi_device_put(sdev1);
5258 }
5259 }
5260 }
5261 doscan = 0;
5262 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005263 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07005264 case MR_EVT_FOREIGN_CFG_IMPORTED:
adam radford9c915a82010-12-21 13:34:31 -08005265 case MR_EVT_LD_STATE_CHANGE:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005266 doscan = 1;
5267 break;
5268 default:
5269 doscan = 0;
5270 break;
5271 }
5272 } else {
5273 printk(KERN_ERR "invalid evt_detail!\n");
5274 kfree(ev);
5275 return;
5276 }
5277
5278 if (doscan) {
5279 printk(KERN_INFO "scanning ...\n");
5280 megasas_get_pd_list(instance);
5281 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5282 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5283 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5284 sdev1 = scsi_device_lookup(host, i, j, 0);
5285 if (instance->pd_list[pd_index].driveState ==
5286 MR_PD_STATE_SYSTEM) {
5287 if (!sdev1) {
5288 scsi_add_device(host, i, j, 0);
5289 }
5290 if (sdev1)
5291 scsi_device_put(sdev1);
5292 } else {
5293 if (sdev1) {
5294 scsi_remove_device(sdev1);
5295 scsi_device_put(sdev1);
5296 }
5297 }
5298 }
5299 }
Yang, Boc9786842009-12-06 08:39:25 -07005300
5301 megasas_get_ld_list(instance);
5302 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5303 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5304 ld_index =
5305 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5306
5307 sdev1 = scsi_device_lookup(host,
5308 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5309 if (instance->ld_ids[ld_index] != 0xff) {
5310 if (!sdev1) {
5311 scsi_add_device(host,
5312 i+2,
5313 j, 0);
5314 } else {
5315 scsi_device_put(sdev1);
5316 }
5317 } else {
5318 if (sdev1) {
5319 scsi_remove_device(sdev1);
5320 scsi_device_put(sdev1);
5321 }
5322 }
5323 }
5324 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005325 }
5326
5327 if ( instance->aen_cmd != NULL ) {
5328 kfree(ev);
5329 return ;
5330 }
5331
5332 seq_num = instance->evt_detail->seq_num + 1;
5333
5334 /* Register AEN with FW for latest sequence number plus 1 */
5335 class_locale.members.reserved = 0;
5336 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5337 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5338 mutex_lock(&instance->aen_mutex);
5339 error = megasas_register_aen(instance, seq_num,
5340 class_locale.word);
5341 mutex_unlock(&instance->aen_mutex);
5342
5343 if (error)
5344 printk(KERN_ERR "register aen failed error %x\n", error);
5345
5346 kfree(ev);
5347}
5348
5349
Bryn M. Reevesbb7d3f22009-11-12 18:31:54 +00005350static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
bo yangad84db22007-11-09 04:40:16 -05005351 megasas_sysfs_show_poll_mode_io,
5352 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07005353
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005354/**
5355 * megasas_init - Driver load entry point
5356 */
5357static int __init megasas_init(void)
5358{
5359 int rval;
5360
5361 /*
5362 * Announce driver version and other information
5363 */
5364 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5365 MEGASAS_EXT_VERSION);
5366
Yang, Bo72c4fd32009-10-06 14:20:59 -06005367 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005368 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005369
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005370 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5371
5372 /*
5373 * Register character device node
5374 */
5375 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5376
5377 if (rval < 0) {
5378 printk(KERN_DEBUG "megasas: failed to open device node\n");
5379 return rval;
5380 }
5381
5382 megasas_mgmt_majorno = rval;
5383
5384 /*
5385 * Register ourselves as PCI hotplug module
5386 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005387 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005388
5389 if (rval) {
5390 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005391 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005392 }
5393
Jeff Garzik83aabc12006-10-04 06:34:03 -04005394 rval = driver_create_file(&megasas_pci_driver.driver,
5395 &driver_attr_version);
5396 if (rval)
5397 goto err_dcf_attr_ver;
5398 rval = driver_create_file(&megasas_pci_driver.driver,
5399 &driver_attr_release_date);
5400 if (rval)
5401 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005402
5403 rval = driver_create_file(&megasas_pci_driver.driver,
5404 &driver_attr_support_poll_for_event);
5405 if (rval)
5406 goto err_dcf_support_poll_for_event;
5407
Jeff Garzik83aabc12006-10-04 06:34:03 -04005408 rval = driver_create_file(&megasas_pci_driver.driver,
5409 &driver_attr_dbg_lvl);
5410 if (rval)
5411 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005412 rval = driver_create_file(&megasas_pci_driver.driver,
5413 &driver_attr_poll_mode_io);
5414 if (rval)
5415 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005416
Yang, Bo837f5fe2010-10-11 06:59:20 -06005417 rval = driver_create_file(&megasas_pci_driver.driver,
5418 &driver_attr_support_device_change);
5419 if (rval)
5420 goto err_dcf_support_device_change;
5421
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005422 return rval;
bo yangad84db22007-11-09 04:40:16 -05005423
Yang, Bo837f5fe2010-10-11 06:59:20 -06005424err_dcf_support_device_change:
5425 driver_remove_file(&megasas_pci_driver.driver,
5426 &driver_attr_poll_mode_io);
5427
bo yangad84db22007-11-09 04:40:16 -05005428err_dcf_poll_mode_io:
5429 driver_remove_file(&megasas_pci_driver.driver,
5430 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005431err_dcf_dbg_lvl:
5432 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005433 &driver_attr_support_poll_for_event);
5434
5435err_dcf_support_poll_for_event:
5436 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005437 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005438
Jeff Garzik83aabc12006-10-04 06:34:03 -04005439err_dcf_rel_date:
5440 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5441err_dcf_attr_ver:
5442 pci_unregister_driver(&megasas_pci_driver);
5443err_pcidrv:
5444 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08005445 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005446}
5447
5448/**
5449 * megasas_exit - Driver unload entry point
5450 */
5451static void __exit megasas_exit(void)
5452{
Sumant Patro658dced2006-10-03 13:09:14 -07005453 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005454 &driver_attr_poll_mode_io);
5455 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07005456 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005457 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005458 &driver_attr_support_poll_for_event);
5459 driver_remove_file(&megasas_pci_driver.driver,
5460 &driver_attr_support_device_change);
5461 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005462 &driver_attr_release_date);
5463 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005464
5465 pci_unregister_driver(&megasas_pci_driver);
5466 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5467}
5468
5469module_init(megasas_init);
5470module_exit(megasas_exit);