blob: 41ffc205724f13da93e97a75cd00802a200ea638 [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
2 *
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
bo yangf28cd7c2007-11-09 04:44:56 -05005 * Copyright (c) 2003-2005 LSI Corporation.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * FILE : megaraid_sas.c
Yang, Bo63bad452009-12-06 08:42:28 -070013 * Version : v00.00.04.17.1-rc1
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040014 *
15 * Authors:
Sumant Patrocc5968c2007-02-14 13:05:42 -080016 * (email-id : megaraidlinux@lsi.com)
17 * Sreenivas Bagalkote
18 * Sumant Patro
19 * Bo Yang
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040020 *
21 * List of supported controllers
22 *
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
25 */
26
27#include <linux/kernel.h>
28#include <linux/types.h>
29#include <linux/pci.h>
30#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040031#include <linux/moduleparam.h>
32#include <linux/module.h>
33#include <linux/spinlock.h>
34#include <linux/interrupt.h>
35#include <linux/delay.h>
36#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040038#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010039#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040040#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080041#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010042#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060043#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040044
45#include <scsi/scsi.h>
46#include <scsi/scsi_cmnd.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_host.h>
49#include "megaraid_sas.h"
50
bo yangad84db22007-11-09 04:40:16 -050051/*
52 * poll_mode_io:1- schedule complete completion from q cmd
53 */
54static unsigned int poll_mode_io;
55module_param_named(poll_mode_io, poll_mode_io, int, 0);
56MODULE_PARM_DESC(poll_mode_io,
57 "Complete cmds from IO path, (default=0)");
58
Yang, Bo1fd10682010-10-12 07:18:50 -060059/*
60 * Number of sectors per IO command
61 * Will be set in megasas_init_mfi if user does not provide
62 */
63static unsigned int max_sectors;
64module_param_named(max_sectors, max_sectors, int, 0);
65MODULE_PARM_DESC(max_sectors,
66 "Maximum number of sectors per IO command");
67
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040068MODULE_LICENSE("GPL");
69MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080070MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050071MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040072
bo yang39a98552010-09-22 22:36:29 -040073static int megasas_transition_to_ready(struct megasas_instance *instance);
74static int megasas_get_pd_list(struct megasas_instance *instance);
75static int megasas_issue_init_mfi(struct megasas_instance *instance);
76static int megasas_register_aen(struct megasas_instance *instance,
77 u32 seq_num, u32 class_locale_word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040078/*
79 * PCI ID table for all supported controllers
80 */
81static struct pci_device_id megasas_pci_table[] = {
82
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020083 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
84 /* xscale IOP */
85 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
86 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -040087 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
88 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -070089 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
90 /* gen2*/
91 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
92 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -060093 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
94 /* skinny*/
95 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
96 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020097 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
98 /* xscale IOP, vega */
99 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
100 /* xscale IOP */
101 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400102};
103
104MODULE_DEVICE_TABLE(pci, megasas_pci_table);
105
106static int megasas_mgmt_majorno;
107static struct megasas_mgmt_info megasas_mgmt_info;
108static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100109static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400110
Yang, Boc3518832009-10-06 14:18:02 -0600111static int megasas_poll_wait_aen;
112static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600113static u32 support_poll_for_event;
Sumant Patro658dced2006-10-03 13:09:14 -0700114static u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600115static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700116
Yang, Boc3518832009-10-06 14:18:02 -0600117/* define lock for aen poll */
118spinlock_t poll_aen_lock;
119
bo yang7343eb62007-11-09 04:35:44 -0500120static void
121megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
122 u8 alt_status);
123
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400124/**
125 * megasas_get_cmd - Get a command from the free pool
126 * @instance: Adapter soft state
127 *
128 * Returns a free command from the pool
129 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800130static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400131 *instance)
132{
133 unsigned long flags;
134 struct megasas_cmd *cmd = NULL;
135
136 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
137
138 if (!list_empty(&instance->cmd_pool)) {
139 cmd = list_entry((&instance->cmd_pool)->next,
140 struct megasas_cmd, list);
141 list_del_init(&cmd->list);
142 } else {
143 printk(KERN_ERR "megasas: Command pool empty!\n");
144 }
145
146 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
147 return cmd;
148}
149
150/**
151 * megasas_return_cmd - Return a cmd to free command pool
152 * @instance: Adapter soft state
153 * @cmd: Command packet to be returned to free command pool
154 */
155static inline void
156megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
157{
158 unsigned long flags;
159
160 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
161
162 cmd->scmd = NULL;
163 list_add_tail(&cmd->list, &instance->cmd_pool);
164
165 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
166}
167
Sumant Patro1341c932006-01-25 12:02:40 -0800168
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400169/**
Sumant Patro1341c932006-01-25 12:02:40 -0800170* The following functions are defined for xscale
171* (deviceid : 1064R, PERC5) controllers
172*/
173
174/**
175 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400176 * @regs: MFI register set
177 */
178static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800179megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400180{
bo yang39a98552010-09-22 22:36:29 -0400181 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400182
183 /* Dummy readl to force pci flush */
184 readl(&regs->outbound_intr_mask);
185}
186
187/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700188 * megasas_disable_intr_xscale -Disables interrupt
189 * @regs: MFI register set
190 */
191static inline void
192megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
193{
194 u32 mask = 0x1f;
195 writel(mask, &regs->outbound_intr_mask);
196 /* Dummy readl to force pci flush */
197 readl(&regs->outbound_intr_mask);
198}
199
200/**
Sumant Patro1341c932006-01-25 12:02:40 -0800201 * megasas_read_fw_status_reg_xscale - returns the current FW status value
202 * @regs: MFI register set
203 */
204static u32
205megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
206{
207 return readl(&(regs)->outbound_msg_0);
208}
209/**
210 * megasas_clear_interrupt_xscale - Check & clear interrupt
211 * @regs: MFI register set
212 */
213static int
214megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
215{
216 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400217 u32 mfiStatus = 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800218 /*
219 * Check if it is our interrupt
220 */
221 status = readl(&regs->outbound_intr_status);
222
bo yang39a98552010-09-22 22:36:29 -0400223 if (status & MFI_OB_INTR_STATUS_MASK)
224 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
225 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
226 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800227
228 /*
229 * Clear the interrupt by writing back the same value
230 */
bo yang39a98552010-09-22 22:36:29 -0400231 if (mfiStatus)
232 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800233
Yang, Bo06f579d2008-08-10 12:42:37 -0700234 /* Dummy readl to force pci flush */
235 readl(&regs->outbound_intr_status);
236
bo yang39a98552010-09-22 22:36:29 -0400237 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800238}
239
240/**
241 * megasas_fire_cmd_xscale - Sends command to the FW
242 * @frame_phys_addr : Physical address of cmd
243 * @frame_count : Number of frames for the command
244 * @regs : MFI register set
245 */
246static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600247megasas_fire_cmd_xscale(struct megasas_instance *instance,
248 dma_addr_t frame_phys_addr,
249 u32 frame_count,
250 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800251{
bo yang39a98552010-09-22 22:36:29 -0400252 unsigned long flags;
253 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800254 writel((frame_phys_addr >> 3)|(frame_count),
255 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400256 spin_unlock_irqrestore(&instance->hba_lock, flags);
257}
258
259/**
260 * megasas_adp_reset_xscale - For controller reset
261 * @regs: MFI register set
262 */
263static int
264megasas_adp_reset_xscale(struct megasas_instance *instance,
265 struct megasas_register_set __iomem *regs)
266{
267 u32 i;
268 u32 pcidata;
269 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
270
271 for (i = 0; i < 3; i++)
272 msleep(1000); /* sleep for 3 secs */
273 pcidata = 0;
274 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
275 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
276 if (pcidata & 0x2) {
277 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
278 pcidata &= ~0x2;
279 pci_write_config_dword(instance->pdev,
280 MFI_1068_PCSR_OFFSET, pcidata);
281
282 for (i = 0; i < 2; i++)
283 msleep(1000); /* need to wait 2 secs again */
284
285 pcidata = 0;
286 pci_read_config_dword(instance->pdev,
287 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
288 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
289 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
290 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
291 pcidata = 0;
292 pci_write_config_dword(instance->pdev,
293 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
294 }
295 }
296 return 0;
297}
298
299/**
300 * megasas_check_reset_xscale - For controller reset check
301 * @regs: MFI register set
302 */
303static int
304megasas_check_reset_xscale(struct megasas_instance *instance,
305 struct megasas_register_set __iomem *regs)
306{
307 u32 consumer;
308 consumer = *instance->consumer;
309
310 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
311 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
312 return 1;
313 }
314 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800315}
316
317static struct megasas_instance_template megasas_instance_template_xscale = {
318
319 .fire_cmd = megasas_fire_cmd_xscale,
320 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700321 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800322 .clear_intr = megasas_clear_intr_xscale,
323 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400324 .adp_reset = megasas_adp_reset_xscale,
325 .check_reset = megasas_check_reset_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800326};
327
328/**
329* This is the end of set of functions & definitions specific
330* to xscale (deviceid : 1064R, PERC5) controllers
331*/
332
333/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800334* The following functions are defined for ppc (deviceid : 0x60)
335* controllers
336*/
337
338/**
339 * megasas_enable_intr_ppc - Enables interrupts
340 * @regs: MFI register set
341 */
342static inline void
343megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
344{
345 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
346
bo yang39a98552010-09-22 22:36:29 -0400347 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800348
349 /* Dummy readl to force pci flush */
350 readl(&regs->outbound_intr_mask);
351}
352
353/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700354 * megasas_disable_intr_ppc - Disable interrupt
355 * @regs: MFI register set
356 */
357static inline void
358megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
359{
360 u32 mask = 0xFFFFFFFF;
361 writel(mask, &regs->outbound_intr_mask);
362 /* Dummy readl to force pci flush */
363 readl(&regs->outbound_intr_mask);
364}
365
366/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800367 * megasas_read_fw_status_reg_ppc - returns the current FW status value
368 * @regs: MFI register set
369 */
370static u32
371megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
372{
373 return readl(&(regs)->outbound_scratch_pad);
374}
375
376/**
377 * megasas_clear_interrupt_ppc - Check & clear interrupt
378 * @regs: MFI register set
379 */
380static int
381megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
382{
383 u32 status;
384 /*
385 * Check if it is our interrupt
386 */
387 status = readl(&regs->outbound_intr_status);
388
389 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
bo yang39a98552010-09-22 22:36:29 -0400390 return 0;
Sumant Patrof9876f02006-02-03 15:34:35 -0800391 }
392
393 /*
394 * Clear the interrupt by writing back the same value
395 */
396 writel(status, &regs->outbound_doorbell_clear);
397
Yang, Bo06f579d2008-08-10 12:42:37 -0700398 /* Dummy readl to force pci flush */
399 readl(&regs->outbound_doorbell_clear);
400
bo yang39a98552010-09-22 22:36:29 -0400401 return 1;
Sumant Patrof9876f02006-02-03 15:34:35 -0800402}
403/**
404 * megasas_fire_cmd_ppc - Sends command to the FW
405 * @frame_phys_addr : Physical address of cmd
406 * @frame_count : Number of frames for the command
407 * @regs : MFI register set
408 */
409static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600410megasas_fire_cmd_ppc(struct megasas_instance *instance,
411 dma_addr_t frame_phys_addr,
412 u32 frame_count,
413 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800414{
bo yang39a98552010-09-22 22:36:29 -0400415 unsigned long flags;
416 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800417 writel((frame_phys_addr | (frame_count<<1))|1,
418 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400419 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800420}
421
bo yang39a98552010-09-22 22:36:29 -0400422/**
423 * megasas_adp_reset_ppc - For controller reset
424 * @regs: MFI register set
425 */
426static int
427megasas_adp_reset_ppc(struct megasas_instance *instance,
428 struct megasas_register_set __iomem *regs)
429{
430 return 0;
431}
432
433/**
434 * megasas_check_reset_ppc - For controller reset check
435 * @regs: MFI register set
436 */
437static int
438megasas_check_reset_ppc(struct megasas_instance *instance,
439 struct megasas_register_set __iomem *regs)
440{
441 return 0;
442}
Sumant Patrof9876f02006-02-03 15:34:35 -0800443static struct megasas_instance_template megasas_instance_template_ppc = {
444
445 .fire_cmd = megasas_fire_cmd_ppc,
446 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700447 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800448 .clear_intr = megasas_clear_intr_ppc,
449 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
bo yang39a98552010-09-22 22:36:29 -0400450 .adp_reset = megasas_adp_reset_ppc,
451 .check_reset = megasas_check_reset_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800452};
453
454/**
Yang, Bo87911122009-10-06 14:31:54 -0600455 * megasas_enable_intr_skinny - Enables interrupts
456 * @regs: MFI register set
457 */
458static inline void
459megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
460{
461 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
462
463 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
464
465 /* Dummy readl to force pci flush */
466 readl(&regs->outbound_intr_mask);
467}
468
469/**
470 * megasas_disable_intr_skinny - Disables interrupt
471 * @regs: MFI register set
472 */
473static inline void
474megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
475{
476 u32 mask = 0xFFFFFFFF;
477 writel(mask, &regs->outbound_intr_mask);
478 /* Dummy readl to force pci flush */
479 readl(&regs->outbound_intr_mask);
480}
481
482/**
483 * megasas_read_fw_status_reg_skinny - returns the current FW status value
484 * @regs: MFI register set
485 */
486static u32
487megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
488{
489 return readl(&(regs)->outbound_scratch_pad);
490}
491
492/**
493 * megasas_clear_interrupt_skinny - Check & clear interrupt
494 * @regs: MFI register set
495 */
496static int
497megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
498{
499 u32 status;
500 /*
501 * Check if it is our interrupt
502 */
503 status = readl(&regs->outbound_intr_status);
504
505 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400506 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600507 }
508
509 /*
510 * Clear the interrupt by writing back the same value
511 */
512 writel(status, &regs->outbound_intr_status);
513
514 /*
515 * dummy read to flush PCI
516 */
517 readl(&regs->outbound_intr_status);
518
bo yang39a98552010-09-22 22:36:29 -0400519 return 1;
Yang, Bo87911122009-10-06 14:31:54 -0600520}
521
522/**
523 * megasas_fire_cmd_skinny - Sends command to the FW
524 * @frame_phys_addr : Physical address of cmd
525 * @frame_count : Number of frames for the command
526 * @regs : MFI register set
527 */
528static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600529megasas_fire_cmd_skinny(struct megasas_instance *instance,
530 dma_addr_t frame_phys_addr,
531 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600532 struct megasas_register_set __iomem *regs)
533{
Yang, Bo0c79e682009-10-06 14:47:35 -0600534 unsigned long flags;
bo yang39a98552010-09-22 22:36:29 -0400535 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600536 writel(0, &(regs)->inbound_high_queue_port);
537 writel((frame_phys_addr | (frame_count<<1))|1,
538 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400539 spin_unlock_irqrestore(&instance->hba_lock, flags);
540}
541
542/**
543 * megasas_adp_reset_skinny - For controller reset
544 * @regs: MFI register set
545 */
546static int
547megasas_adp_reset_skinny(struct megasas_instance *instance,
548 struct megasas_register_set __iomem *regs)
549{
550 return 0;
551}
552
553/**
554 * megasas_check_reset_skinny - For controller reset check
555 * @regs: MFI register set
556 */
557static int
558megasas_check_reset_skinny(struct megasas_instance *instance,
559 struct megasas_register_set __iomem *regs)
560{
561 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600562}
563
564static struct megasas_instance_template megasas_instance_template_skinny = {
565
566 .fire_cmd = megasas_fire_cmd_skinny,
567 .enable_intr = megasas_enable_intr_skinny,
568 .disable_intr = megasas_disable_intr_skinny,
569 .clear_intr = megasas_clear_intr_skinny,
570 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
bo yang39a98552010-09-22 22:36:29 -0400571 .adp_reset = megasas_adp_reset_skinny,
572 .check_reset = megasas_check_reset_skinny,
Yang, Bo87911122009-10-06 14:31:54 -0600573};
574
575
576/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700577* The following functions are defined for gen2 (deviceid : 0x78 0x79)
578* controllers
579*/
580
581/**
582 * megasas_enable_intr_gen2 - Enables interrupts
583 * @regs: MFI register set
584 */
585static inline void
586megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
587{
588 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
589
590 /* write ~0x00000005 (4 & 1) to the intr mask*/
591 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
592
593 /* Dummy readl to force pci flush */
594 readl(&regs->outbound_intr_mask);
595}
596
597/**
598 * megasas_disable_intr_gen2 - Disables interrupt
599 * @regs: MFI register set
600 */
601static inline void
602megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
603{
604 u32 mask = 0xFFFFFFFF;
605 writel(mask, &regs->outbound_intr_mask);
606 /* Dummy readl to force pci flush */
607 readl(&regs->outbound_intr_mask);
608}
609
610/**
611 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
612 * @regs: MFI register set
613 */
614static u32
615megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
616{
617 return readl(&(regs)->outbound_scratch_pad);
618}
619
620/**
621 * megasas_clear_interrupt_gen2 - Check & clear interrupt
622 * @regs: MFI register set
623 */
624static int
625megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
626{
627 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400628 u32 mfiStatus = 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700629 /*
630 * Check if it is our interrupt
631 */
632 status = readl(&regs->outbound_intr_status);
633
bo yang39a98552010-09-22 22:36:29 -0400634 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
635 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
636 }
637 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
638 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
639 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700640
641 /*
642 * Clear the interrupt by writing back the same value
643 */
bo yang39a98552010-09-22 22:36:29 -0400644 if (mfiStatus)
645 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700646
647 /* Dummy readl to force pci flush */
648 readl(&regs->outbound_intr_status);
649
bo yang39a98552010-09-22 22:36:29 -0400650 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700651}
652/**
653 * megasas_fire_cmd_gen2 - Sends command to the FW
654 * @frame_phys_addr : Physical address of cmd
655 * @frame_count : Number of frames for the command
656 * @regs : MFI register set
657 */
658static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600659megasas_fire_cmd_gen2(struct megasas_instance *instance,
660 dma_addr_t frame_phys_addr,
661 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700662 struct megasas_register_set __iomem *regs)
663{
bo yang39a98552010-09-22 22:36:29 -0400664 unsigned long flags;
665 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700666 writel((frame_phys_addr | (frame_count<<1))|1,
667 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400668 spin_unlock_irqrestore(&instance->hba_lock, flags);
669}
670
671/**
672 * megasas_adp_reset_gen2 - For controller reset
673 * @regs: MFI register set
674 */
675static int
676megasas_adp_reset_gen2(struct megasas_instance *instance,
677 struct megasas_register_set __iomem *reg_set)
678{
679 u32 retry = 0 ;
680 u32 HostDiag;
681
682 writel(0, &reg_set->seq_offset);
683 writel(4, &reg_set->seq_offset);
684 writel(0xb, &reg_set->seq_offset);
685 writel(2, &reg_set->seq_offset);
686 writel(7, &reg_set->seq_offset);
687 writel(0xd, &reg_set->seq_offset);
688 msleep(1000);
689
690 HostDiag = (u32)readl(&reg_set->host_diag);
691
692 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
693 msleep(100);
694 HostDiag = (u32)readl(&reg_set->host_diag);
695 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
696 retry, HostDiag);
697
698 if (retry++ >= 100)
699 return 1;
700
701 }
702
703 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
704
705 writel((HostDiag | DIAG_RESET_ADAPTER), &reg_set->host_diag);
706
707 ssleep(10);
708
709 HostDiag = (u32)readl(&reg_set->host_diag);
710 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
711 msleep(100);
712 HostDiag = (u32)readl(&reg_set->host_diag);
713 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
714 retry, HostDiag);
715
716 if (retry++ >= 1000)
717 return 1;
718
719 }
720 return 0;
721}
722
723/**
724 * megasas_check_reset_gen2 - For controller reset check
725 * @regs: MFI register set
726 */
727static int
728megasas_check_reset_gen2(struct megasas_instance *instance,
729 struct megasas_register_set __iomem *regs)
730{
731 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700732}
733
734static struct megasas_instance_template megasas_instance_template_gen2 = {
735
736 .fire_cmd = megasas_fire_cmd_gen2,
737 .enable_intr = megasas_enable_intr_gen2,
738 .disable_intr = megasas_disable_intr_gen2,
739 .clear_intr = megasas_clear_intr_gen2,
740 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400741 .adp_reset = megasas_adp_reset_gen2,
742 .check_reset = megasas_check_reset_gen2,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700743};
744
745/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800746* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400747* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800748*/
749
750/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400751 * megasas_issue_polled - Issues a polling command
752 * @instance: Adapter soft state
753 * @cmd: Command packet to be issued
754 *
755 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
756 */
757static int
758megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
759{
760 int i;
761 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
762
763 struct megasas_header *frame_hdr = &cmd->frame->hdr;
764
765 frame_hdr->cmd_status = 0xFF;
766 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
767
768 /*
769 * Issue the frame using inbound queue port
770 */
Yang, Bo0c79e682009-10-06 14:47:35 -0600771 instance->instancet->fire_cmd(instance,
772 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400773
774 /*
775 * Wait for cmd_status to change
776 */
777 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
778 rmb();
779 msleep(1);
780 }
781
782 if (frame_hdr->cmd_status == 0xff)
783 return -ETIME;
784
785 return 0;
786}
787
788/**
789 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
790 * @instance: Adapter soft state
791 * @cmd: Command to be issued
792 *
793 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700794 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400795 * Used to issue ioctl commands.
796 */
797static int
798megasas_issue_blocked_cmd(struct megasas_instance *instance,
799 struct megasas_cmd *cmd)
800{
801 cmd->cmd_status = ENODATA;
802
Yang, Bo0c79e682009-10-06 14:47:35 -0600803 instance->instancet->fire_cmd(instance,
804 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400805
bo yang39a98552010-09-22 22:36:29 -0400806 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400807
808 return 0;
809}
810
811/**
812 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
813 * @instance: Adapter soft state
814 * @cmd_to_abort: Previously issued cmd to be aborted
815 *
816 * MFI firmware can abort previously issued AEN comamnd (automatic event
817 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700818 * cmd and waits for return status.
819 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400820 */
821static int
822megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
823 struct megasas_cmd *cmd_to_abort)
824{
825 struct megasas_cmd *cmd;
826 struct megasas_abort_frame *abort_fr;
827
828 cmd = megasas_get_cmd(instance);
829
830 if (!cmd)
831 return -1;
832
833 abort_fr = &cmd->frame->abort;
834
835 /*
836 * Prepare and issue the abort frame
837 */
838 abort_fr->cmd = MFI_CMD_ABORT;
839 abort_fr->cmd_status = 0xFF;
840 abort_fr->flags = 0;
841 abort_fr->abort_context = cmd_to_abort->index;
842 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
843 abort_fr->abort_mfi_phys_addr_hi = 0;
844
845 cmd->sync_cmd = 1;
846 cmd->cmd_status = 0xFF;
847
Yang, Bo0c79e682009-10-06 14:47:35 -0600848 instance->instancet->fire_cmd(instance,
849 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400850
851 /*
852 * Wait for this cmd to complete
853 */
bo yang39a98552010-09-22 22:36:29 -0400854 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
855 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400856
857 megasas_return_cmd(instance, cmd);
858 return 0;
859}
860
861/**
862 * megasas_make_sgl32 - Prepares 32-bit SGL
863 * @instance: Adapter soft state
864 * @scp: SCSI command from the mid-layer
865 * @mfi_sgl: SGL to be filled in
866 *
867 * If successful, this function returns the number of SG elements. Otherwise,
868 * it returnes -1.
869 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800870static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400871megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
872 union megasas_sgl *mfi_sgl)
873{
874 int i;
875 int sge_count;
876 struct scatterlist *os_sgl;
877
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900878 sge_count = scsi_dma_map(scp);
879 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400880
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900881 if (sge_count) {
882 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
883 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
884 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
885 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400886 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400887 return sge_count;
888}
889
890/**
891 * megasas_make_sgl64 - Prepares 64-bit SGL
892 * @instance: Adapter soft state
893 * @scp: SCSI command from the mid-layer
894 * @mfi_sgl: SGL to be filled in
895 *
896 * If successful, this function returns the number of SG elements. Otherwise,
897 * it returnes -1.
898 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800899static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400900megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
901 union megasas_sgl *mfi_sgl)
902{
903 int i;
904 int sge_count;
905 struct scatterlist *os_sgl;
906
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900907 sge_count = scsi_dma_map(scp);
908 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400909
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900910 if (sge_count) {
911 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
912 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
913 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
914 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400915 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400916 return sge_count;
917}
918
Yang, Bof4c9a132009-10-06 14:43:28 -0600919/**
920 * megasas_make_sgl_skinny - Prepares IEEE SGL
921 * @instance: Adapter soft state
922 * @scp: SCSI command from the mid-layer
923 * @mfi_sgl: SGL to be filled in
924 *
925 * If successful, this function returns the number of SG elements. Otherwise,
926 * it returnes -1.
927 */
928static int
929megasas_make_sgl_skinny(struct megasas_instance *instance,
930 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
931{
932 int i;
933 int sge_count;
934 struct scatterlist *os_sgl;
935
936 sge_count = scsi_dma_map(scp);
937
938 if (sge_count) {
939 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
940 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
941 mfi_sgl->sge_skinny[i].phys_addr =
942 sg_dma_address(os_sgl);
943 }
944 }
945 return sge_count;
946}
947
Sumant Patrob1df99d2006-10-03 12:40:47 -0700948 /**
949 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -0400950 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -0700951 * @sge_count : number of sg elements
952 *
953 * Returns the number of frames required for numnber of sge's (sge_count)
954 */
955
Yang, Bof4c9a132009-10-06 14:43:28 -0600956static u32 megasas_get_frame_count(struct megasas_instance *instance,
957 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700958{
959 int num_cnt;
960 int sge_bytes;
961 u32 sge_sz;
962 u32 frame_count=0;
963
964 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
965 sizeof(struct megasas_sge32);
966
Yang, Bof4c9a132009-10-06 14:43:28 -0600967 if (instance->flag_ieee) {
968 sge_sz = sizeof(struct megasas_sge_skinny);
969 }
970
Sumant Patrob1df99d2006-10-03 12:40:47 -0700971 /*
bo yangd532dbe2008-03-17 03:36:43 -0400972 * Main frame can contain 2 SGEs for 64-bit SGLs and
973 * 3 SGEs for 32-bit SGLs for ldio &
974 * 1 SGEs for 64-bit SGLs and
975 * 2 SGEs for 32-bit SGLs for pthru frame
976 */
977 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -0600978 if (instance->flag_ieee == 1) {
979 num_cnt = sge_count - 1;
980 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -0400981 num_cnt = sge_count - 1;
982 else
983 num_cnt = sge_count - 2;
984 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -0600985 if (instance->flag_ieee == 1) {
986 num_cnt = sge_count - 1;
987 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -0400988 num_cnt = sge_count - 2;
989 else
990 num_cnt = sge_count - 3;
991 }
Sumant Patrob1df99d2006-10-03 12:40:47 -0700992
993 if(num_cnt>0){
994 sge_bytes = sge_sz * num_cnt;
995
996 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
997 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
998 }
999 /* Main frame */
1000 frame_count +=1;
1001
1002 if (frame_count > 7)
1003 frame_count = 8;
1004 return frame_count;
1005}
1006
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001007/**
1008 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1009 * @instance: Adapter soft state
1010 * @scp: SCSI command
1011 * @cmd: Command to be prepared in
1012 *
1013 * This function prepares CDB commands. These are typcially pass-through
1014 * commands to the devices.
1015 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001016static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001017megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1018 struct megasas_cmd *cmd)
1019{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001020 u32 is_logical;
1021 u32 device_id;
1022 u16 flags = 0;
1023 struct megasas_pthru_frame *pthru;
1024
1025 is_logical = MEGASAS_IS_LOGICAL(scp);
1026 device_id = MEGASAS_DEV_INDEX(instance, scp);
1027 pthru = (struct megasas_pthru_frame *)cmd->frame;
1028
1029 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1030 flags = MFI_FRAME_DIR_WRITE;
1031 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1032 flags = MFI_FRAME_DIR_READ;
1033 else if (scp->sc_data_direction == PCI_DMA_NONE)
1034 flags = MFI_FRAME_DIR_NONE;
1035
Yang, Bof4c9a132009-10-06 14:43:28 -06001036 if (instance->flag_ieee == 1) {
1037 flags |= MFI_FRAME_IEEE;
1038 }
1039
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001040 /*
1041 * Prepare the DCDB frame
1042 */
1043 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1044 pthru->cmd_status = 0x0;
1045 pthru->scsi_status = 0x0;
1046 pthru->target_id = device_id;
1047 pthru->lun = scp->device->lun;
1048 pthru->cdb_len = scp->cmd_len;
1049 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001050 pthru->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001051 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001052 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001053
1054 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1055
1056 /*
Yang, Bo8d568252009-10-06 14:12:21 -06001057 * If the command is for the tape device, set the
1058 * pthru timeout to the os layer timeout value.
1059 */
1060 if (scp->device->type == TYPE_TAPE) {
1061 if ((scp->request->timeout / HZ) > 0xFFFF)
1062 pthru->timeout = 0xFFFF;
1063 else
1064 pthru->timeout = scp->request->timeout / HZ;
1065 }
1066
1067 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001068 * Construct SGL
1069 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001070 if (instance->flag_ieee == 1) {
1071 pthru->flags |= MFI_FRAME_SGL64;
1072 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1073 &pthru->sgl);
1074 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001075 pthru->flags |= MFI_FRAME_SGL64;
1076 pthru->sge_count = megasas_make_sgl64(instance, scp,
1077 &pthru->sgl);
1078 } else
1079 pthru->sge_count = megasas_make_sgl32(instance, scp,
1080 &pthru->sgl);
1081
Yang, Bobdc6fb82009-12-06 08:30:19 -07001082 if (pthru->sge_count > instance->max_num_sge) {
1083 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1084 pthru->sge_count);
1085 return 0;
1086 }
1087
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001088 /*
1089 * Sense info specific
1090 */
1091 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1092 pthru->sense_buf_phys_addr_hi = 0;
1093 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1094
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001095 /*
1096 * Compute the total number of frames this command consumes. FW uses
1097 * this number to pull sufficient number of frames from host memory.
1098 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001099 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001100 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001101
1102 return cmd->frame_count;
1103}
1104
1105/**
1106 * megasas_build_ldio - Prepares IOs to logical devices
1107 * @instance: Adapter soft state
1108 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001109 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001110 *
1111 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1112 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001113static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001114megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1115 struct megasas_cmd *cmd)
1116{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001117 u32 device_id;
1118 u8 sc = scp->cmnd[0];
1119 u16 flags = 0;
1120 struct megasas_io_frame *ldio;
1121
1122 device_id = MEGASAS_DEV_INDEX(instance, scp);
1123 ldio = (struct megasas_io_frame *)cmd->frame;
1124
1125 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1126 flags = MFI_FRAME_DIR_WRITE;
1127 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1128 flags = MFI_FRAME_DIR_READ;
1129
Yang, Bof4c9a132009-10-06 14:43:28 -06001130 if (instance->flag_ieee == 1) {
1131 flags |= MFI_FRAME_IEEE;
1132 }
1133
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001134 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001135 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001136 */
1137 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1138 ldio->cmd_status = 0x0;
1139 ldio->scsi_status = 0x0;
1140 ldio->target_id = device_id;
1141 ldio->timeout = 0;
1142 ldio->reserved_0 = 0;
1143 ldio->pad_0 = 0;
1144 ldio->flags = flags;
1145 ldio->start_lba_hi = 0;
1146 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1147
1148 /*
1149 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1150 */
1151 if (scp->cmd_len == 6) {
1152 ldio->lba_count = (u32) scp->cmnd[4];
1153 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1154 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1155
1156 ldio->start_lba_lo &= 0x1FFFFF;
1157 }
1158
1159 /*
1160 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1161 */
1162 else if (scp->cmd_len == 10) {
1163 ldio->lba_count = (u32) scp->cmnd[8] |
1164 ((u32) scp->cmnd[7] << 8);
1165 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1166 ((u32) scp->cmnd[3] << 16) |
1167 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1168 }
1169
1170 /*
1171 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1172 */
1173 else if (scp->cmd_len == 12) {
1174 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1175 ((u32) scp->cmnd[7] << 16) |
1176 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1177
1178 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1179 ((u32) scp->cmnd[3] << 16) |
1180 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1181 }
1182
1183 /*
1184 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1185 */
1186 else if (scp->cmd_len == 16) {
1187 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1188 ((u32) scp->cmnd[11] << 16) |
1189 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1190
1191 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1192 ((u32) scp->cmnd[7] << 16) |
1193 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1194
1195 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1196 ((u32) scp->cmnd[3] << 16) |
1197 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1198
1199 }
1200
1201 /*
1202 * Construct SGL
1203 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001204 if (instance->flag_ieee) {
1205 ldio->flags |= MFI_FRAME_SGL64;
1206 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1207 &ldio->sgl);
1208 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001209 ldio->flags |= MFI_FRAME_SGL64;
1210 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1211 } else
1212 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1213
Yang, Bobdc6fb82009-12-06 08:30:19 -07001214 if (ldio->sge_count > instance->max_num_sge) {
1215 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1216 ldio->sge_count);
1217 return 0;
1218 }
1219
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001220 /*
1221 * Sense info specific
1222 */
1223 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1224 ldio->sense_buf_phys_addr_hi = 0;
1225 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1226
Sumant Patrob1df99d2006-10-03 12:40:47 -07001227 /*
1228 * Compute the total number of frames this command consumes. FW uses
1229 * this number to pull sufficient number of frames from host memory.
1230 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001231 cmd->frame_count = megasas_get_frame_count(instance,
1232 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001233
1234 return cmd->frame_count;
1235}
1236
1237/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001238 * megasas_is_ldio - Checks if the cmd is for logical drive
1239 * @scmd: SCSI command
1240 *
1241 * Called by megasas_queue_command to find out if the command to be queued
1242 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001243 */
Sumant Patrocb59aa62006-01-25 11:53:25 -08001244static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001245{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001246 if (!MEGASAS_IS_LOGICAL(cmd))
1247 return 0;
1248 switch (cmd->cmnd[0]) {
1249 case READ_10:
1250 case WRITE_10:
1251 case READ_12:
1252 case WRITE_12:
1253 case READ_6:
1254 case WRITE_6:
1255 case READ_16:
1256 case WRITE_16:
1257 return 1;
1258 default:
1259 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001260 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001261}
1262
Sumant Patro658dced2006-10-03 13:09:14 -07001263 /**
1264 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1265 * in FW
1266 * @instance: Adapter soft state
1267 */
1268static inline void
1269megasas_dump_pending_frames(struct megasas_instance *instance)
1270{
1271 struct megasas_cmd *cmd;
1272 int i,n;
1273 union megasas_sgl *mfi_sgl;
1274 struct megasas_io_frame *ldio;
1275 struct megasas_pthru_frame *pthru;
1276 u32 sgcount;
1277 u32 max_cmd = instance->max_fw_cmds;
1278
1279 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1280 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1281 if (IS_DMA64)
1282 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1283 else
1284 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1285
1286 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1287 for (i = 0; i < max_cmd; i++) {
1288 cmd = instance->cmd_list[i];
1289 if(!cmd->scmd)
1290 continue;
1291 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1292 if (megasas_is_ldio(cmd->scmd)){
1293 ldio = (struct megasas_io_frame *)cmd->frame;
1294 mfi_sgl = &ldio->sgl;
1295 sgcount = ldio->sge_count;
1296 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);
1297 }
1298 else {
1299 pthru = (struct megasas_pthru_frame *) cmd->frame;
1300 mfi_sgl = &pthru->sgl;
1301 sgcount = pthru->sge_count;
1302 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);
1303 }
1304 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1305 for (n = 0; n < sgcount; n++){
1306 if (IS_DMA64)
1307 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) ;
1308 else
1309 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1310 }
1311 }
1312 printk(KERN_ERR "\n");
1313 } /*for max_cmd*/
1314 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1315 for (i = 0; i < max_cmd; i++) {
1316
1317 cmd = instance->cmd_list[i];
1318
1319 if(cmd->sync_cmd == 1){
1320 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1321 }
1322 }
1323 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1324}
1325
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001326/**
1327 * megasas_queue_command - Queue entry point
1328 * @scmd: SCSI command to be queued
1329 * @done: Callback entry point
1330 */
1331static int
1332megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1333{
1334 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001335 struct megasas_cmd *cmd;
1336 struct megasas_instance *instance;
bo yang39a98552010-09-22 22:36:29 -04001337 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001338
1339 instance = (struct megasas_instance *)
1340 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001341
bo yang39a98552010-09-22 22:36:29 -04001342 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001343 return SCSI_MLQUEUE_HOST_BUSY;
1344
bo yang39a98552010-09-22 22:36:29 -04001345 spin_lock_irqsave(&instance->hba_lock, flags);
1346 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1347 spin_unlock_irqrestore(&instance->hba_lock, flags);
1348 return SCSI_MLQUEUE_HOST_BUSY;
1349 }
1350
1351 spin_unlock_irqrestore(&instance->hba_lock, flags);
1352
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001353 scmd->scsi_done = done;
1354 scmd->result = 0;
1355
Sumant Patrocb59aa62006-01-25 11:53:25 -08001356 if (MEGASAS_IS_LOGICAL(scmd) &&
1357 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1358 scmd->result = DID_BAD_TARGET << 16;
1359 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001360 }
1361
Sumant Patro02b01e02007-02-14 13:00:55 -08001362 switch (scmd->cmnd[0]) {
1363 case SYNCHRONIZE_CACHE:
1364 /*
1365 * FW takes care of flush cache on its own
1366 * No need to send it down
1367 */
1368 scmd->result = DID_OK << 16;
1369 goto out_done;
1370 default:
1371 break;
1372 }
1373
Sumant Patrocb59aa62006-01-25 11:53:25 -08001374 cmd = megasas_get_cmd(instance);
1375 if (!cmd)
1376 return SCSI_MLQUEUE_HOST_BUSY;
1377
1378 /*
1379 * Logical drive command
1380 */
1381 if (megasas_is_ldio(scmd))
1382 frame_count = megasas_build_ldio(instance, scmd, cmd);
1383 else
1384 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1385
1386 if (!frame_count)
1387 goto out_return_cmd;
1388
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001389 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001390 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001391
1392 /*
1393 * Issue the command to the FW
1394 */
Sumant Patroe4a082c2006-05-30 12:03:37 -07001395 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001396
Yang, Bo0c79e682009-10-06 14:47:35 -06001397 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1398 cmd->frame_count-1, instance->reg_set);
bo yangad84db22007-11-09 04:40:16 -05001399 /*
1400 * Check if we have pend cmds to be completed
1401 */
1402 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1403 tasklet_schedule(&instance->isr_tasklet);
1404
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001405
1406 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001407
1408 out_return_cmd:
1409 megasas_return_cmd(instance, cmd);
1410 out_done:
1411 done(scmd);
1412 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001413}
1414
Yang, Bo044833b2009-10-06 14:33:06 -06001415static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1416{
1417 int i;
1418
1419 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1420
1421 if ((megasas_mgmt_info.instance[i]) &&
1422 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1423 return megasas_mgmt_info.instance[i];
1424 }
1425
1426 return NULL;
1427}
1428
Christoph Hellwig147aab62006-02-17 12:13:48 +01001429static int megasas_slave_configure(struct scsi_device *sdev)
1430{
Yang, Bo044833b2009-10-06 14:33:06 -06001431 u16 pd_index = 0;
1432 struct megasas_instance *instance ;
1433
1434 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001435
1436 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001437 * Don't export physical disk devices to the disk driver.
1438 *
1439 * FIXME: Currently we don't export them to the midlayer at all.
1440 * That will be fixed once LSI engineers have audited the
1441 * firmware for possible issues.
1442 */
1443 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1444 sdev->type == TYPE_DISK) {
1445 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1446 sdev->id;
1447 if (instance->pd_list[pd_index].driveState ==
1448 MR_PD_STATE_SYSTEM) {
1449 blk_queue_rq_timeout(sdev->request_queue,
1450 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1451 return 0;
1452 }
1453 return -ENXIO;
1454 }
1455
1456 /*
1457 * The RAID firmware may require extended timeouts.
1458 */
1459 blk_queue_rq_timeout(sdev->request_queue,
1460 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1461 return 0;
1462}
1463
1464static int megasas_slave_alloc(struct scsi_device *sdev)
1465{
1466 u16 pd_index = 0;
1467 struct megasas_instance *instance ;
1468 instance = megasas_lookup_instance(sdev->host->host_no);
1469 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1470 (sdev->type == TYPE_DISK)) {
1471 /*
1472 * Open the OS scan to the SYSTEM PD
1473 */
1474 pd_index =
1475 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1476 sdev->id;
1477 if ((instance->pd_list[pd_index].driveState ==
1478 MR_PD_STATE_SYSTEM) &&
1479 (instance->pd_list[pd_index].driveType ==
1480 TYPE_DISK)) {
1481 return 0;
1482 }
1483 return -ENXIO;
1484 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001485 return 0;
1486}
1487
bo yang39a98552010-09-22 22:36:29 -04001488static void megaraid_sas_kill_hba(struct megasas_instance *instance)
1489{
1490 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1491 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1492 writel(MFI_STOP_ADP,
1493 &instance->reg_set->reserved_0[0]);
1494 } else {
1495 writel(MFI_STOP_ADP,
1496 &instance->reg_set->inbound_doorbell);
1497 }
1498}
1499
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001500/**
bo yang7343eb62007-11-09 04:35:44 -05001501 * megasas_complete_cmd_dpc - Returns FW's controller structure
1502 * @instance_addr: Address of adapter soft state
1503 *
1504 * Tasklet to complete cmds
1505 */
1506static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1507{
1508 u32 producer;
1509 u32 consumer;
1510 u32 context;
1511 struct megasas_cmd *cmd;
1512 struct megasas_instance *instance =
1513 (struct megasas_instance *)instance_addr;
1514 unsigned long flags;
1515
1516 /* If we have already declared adapter dead, donot complete cmds */
bo yang39a98552010-09-22 22:36:29 -04001517 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
bo yang7343eb62007-11-09 04:35:44 -05001518 return;
1519
1520 spin_lock_irqsave(&instance->completion_lock, flags);
1521
1522 producer = *instance->producer;
1523 consumer = *instance->consumer;
1524
1525 while (consumer != producer) {
1526 context = instance->reply_queue[consumer];
bo yang39a98552010-09-22 22:36:29 -04001527 if (context >= instance->max_fw_cmds) {
1528 printk(KERN_ERR "Unexpected context value %x\n",
1529 context);
1530 BUG();
1531 }
bo yang7343eb62007-11-09 04:35:44 -05001532
1533 cmd = instance->cmd_list[context];
1534
1535 megasas_complete_cmd(instance, cmd, DID_OK);
1536
1537 consumer++;
1538 if (consumer == (instance->max_fw_cmds + 1)) {
1539 consumer = 0;
1540 }
1541 }
1542
1543 *instance->consumer = producer;
1544
1545 spin_unlock_irqrestore(&instance->completion_lock, flags);
1546
1547 /*
1548 * Check if we can restore can_queue
1549 */
1550 if (instance->flag & MEGASAS_FW_BUSY
1551 && time_after(jiffies, instance->last_time + 5 * HZ)
1552 && atomic_read(&instance->fw_outstanding) < 17) {
1553
1554 spin_lock_irqsave(instance->host->host_lock, flags);
1555 instance->flag &= ~MEGASAS_FW_BUSY;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06001556 if ((instance->pdev->device ==
1557 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1558 (instance->pdev->device ==
1559 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1560 instance->host->can_queue =
1561 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1562 } else
1563 instance->host->can_queue =
bo yang7343eb62007-11-09 04:35:44 -05001564 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1565
1566 spin_unlock_irqrestore(instance->host->host_lock, flags);
1567 }
1568}
1569
1570/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001571 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1572 * @instance: Adapter soft state
1573 *
1574 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1575 * complete all its outstanding commands. Returns error if one or more IOs
1576 * are pending after this time period. It also marks the controller dead.
1577 */
1578static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1579{
1580 int i;
bo yang39a98552010-09-22 22:36:29 -04001581 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001582 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04001583 u8 adprecovery;
1584 unsigned long flags;
1585 struct list_head clist_local;
1586 struct megasas_cmd *reset_cmd;
1587
1588 spin_lock_irqsave(&instance->hba_lock, flags);
1589 adprecovery = instance->adprecovery;
1590 spin_unlock_irqrestore(&instance->hba_lock, flags);
1591
1592 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1593
1594 INIT_LIST_HEAD(&clist_local);
1595 spin_lock_irqsave(&instance->hba_lock, flags);
1596 list_splice_init(&instance->internal_reset_pending_q,
1597 &clist_local);
1598 spin_unlock_irqrestore(&instance->hba_lock, flags);
1599
1600 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1601 for (i = 0; i < wait_time; i++) {
1602 msleep(1000);
1603 spin_lock_irqsave(&instance->hba_lock, flags);
1604 adprecovery = instance->adprecovery;
1605 spin_unlock_irqrestore(&instance->hba_lock, flags);
1606 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1607 break;
1608 }
1609
1610 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1611 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1612 spin_lock_irqsave(&instance->hba_lock, flags);
1613 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1614 spin_unlock_irqrestore(&instance->hba_lock, flags);
1615 return FAILED;
1616 }
1617
1618 reset_index = 0;
1619 while (!list_empty(&clist_local)) {
1620 reset_cmd = list_entry((&clist_local)->next,
1621 struct megasas_cmd, list);
1622 list_del_init(&reset_cmd->list);
1623 if (reset_cmd->scmd) {
1624 reset_cmd->scmd->result = DID_RESET << 16;
1625 printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
1626 reset_index, reset_cmd,
1627 reset_cmd->scmd->cmnd[0],
1628 reset_cmd->scmd->serial_number);
1629
1630 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1631 megasas_return_cmd(instance, reset_cmd);
1632 } else if (reset_cmd->sync_cmd) {
1633 printk(KERN_NOTICE "megasas:%p synch cmds"
1634 "reset queue\n",
1635 reset_cmd);
1636
1637 reset_cmd->cmd_status = ENODATA;
1638 instance->instancet->fire_cmd(instance,
1639 reset_cmd->frame_phys_addr,
1640 0, instance->reg_set);
1641 } else {
1642 printk(KERN_NOTICE "megasas: %p unexpected"
1643 "cmds lst\n",
1644 reset_cmd);
1645 }
1646 reset_index++;
1647 }
1648
1649 return SUCCESS;
1650 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001651
1652 for (i = 0; i < wait_time; i++) {
1653
Sumant Patroe4a082c2006-05-30 12:03:37 -07001654 int outstanding = atomic_read(&instance->fw_outstanding);
1655
1656 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001657 break;
1658
1659 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1660 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001661 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001662 /*
1663 * Call cmd completion routine. Cmd to be
1664 * be completed directly without depending on isr.
1665 */
1666 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001667 }
1668
1669 msleep(1000);
1670 }
1671
Sumant Patroe4a082c2006-05-30 12:03:37 -07001672 if (atomic_read(&instance->fw_outstanding)) {
bo yang39a98552010-09-22 22:36:29 -04001673 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07001674 /*
1675 * Send signal to FW to stop processing any pending cmds.
1676 * The controller will be taken offline by the OS now.
1677 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001678 if ((instance->pdev->device ==
1679 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1680 (instance->pdev->device ==
1681 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1682 writel(MFI_STOP_ADP,
1683 &instance->reg_set->reserved_0[0]);
1684 } else {
1685 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001686 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001687 }
Sumant Patro658dced2006-10-03 13:09:14 -07001688 megasas_dump_pending_frames(instance);
bo yang39a98552010-09-22 22:36:29 -04001689 spin_lock_irqsave(&instance->hba_lock, flags);
1690 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1691 spin_unlock_irqrestore(&instance->hba_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001692 return FAILED;
1693 }
1694
bo yang39a98552010-09-22 22:36:29 -04001695 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1696
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001697 return SUCCESS;
1698}
1699
1700/**
1701 * megasas_generic_reset - Generic reset routine
1702 * @scmd: Mid-layer SCSI command
1703 *
1704 * This routine implements a generic reset handler for device, bus and host
1705 * reset requests. Device, bus and host specific reset handlers can use this
1706 * function after they do their specific tasks.
1707 */
1708static int megasas_generic_reset(struct scsi_cmnd *scmd)
1709{
1710 int ret_val;
1711 struct megasas_instance *instance;
1712
1713 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1714
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001715 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1716 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001717
bo yang39a98552010-09-22 22:36:29 -04001718 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001719 printk(KERN_ERR "megasas: cannot recover from previous reset "
1720 "failures\n");
1721 return FAILED;
1722 }
1723
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001724 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001725 if (ret_val == SUCCESS)
1726 printk(KERN_NOTICE "megasas: reset successful \n");
1727 else
1728 printk(KERN_ERR "megasas: failed to do reset\n");
1729
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001730 return ret_val;
1731}
1732
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001733/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001734 * megasas_reset_timer - quiesce the adapter if required
1735 * @scmd: scsi cmnd
1736 *
1737 * Sets the FW busy flag and reduces the host->can_queue if the
1738 * cmd has not been completed within the timeout period.
1739 */
1740static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001741blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001742{
1743 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1744 struct megasas_instance *instance;
1745 unsigned long flags;
1746
1747 if (time_after(jiffies, scmd->jiffies_at_alloc +
1748 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001749 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001750 }
1751
1752 instance = cmd->instance;
1753 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1754 /* FW is busy, throttle IO */
1755 spin_lock_irqsave(instance->host->host_lock, flags);
1756
1757 instance->host->can_queue = 16;
1758 instance->last_time = jiffies;
1759 instance->flag |= MEGASAS_FW_BUSY;
1760
1761 spin_unlock_irqrestore(instance->host->host_lock, flags);
1762 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001763 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001764}
1765
1766/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001767 * megasas_reset_device - Device reset handler entry point
1768 */
1769static int megasas_reset_device(struct scsi_cmnd *scmd)
1770{
1771 int ret;
1772
1773 /*
1774 * First wait for all commands to complete
1775 */
1776 ret = megasas_generic_reset(scmd);
1777
1778 return ret;
1779}
1780
1781/**
1782 * megasas_reset_bus_host - Bus & host reset handler entry point
1783 */
1784static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1785{
1786 int ret;
1787
1788 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001789 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001790 */
1791 ret = megasas_generic_reset(scmd);
1792
1793 return ret;
1794}
1795
1796/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001797 * megasas_bios_param - Returns disk geometry for a disk
1798 * @sdev: device handle
1799 * @bdev: block device
1800 * @capacity: drive capacity
1801 * @geom: geometry parameters
1802 */
1803static int
1804megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1805 sector_t capacity, int geom[])
1806{
1807 int heads;
1808 int sectors;
1809 sector_t cylinders;
1810 unsigned long tmp;
1811 /* Default heads (64) & sectors (32) */
1812 heads = 64;
1813 sectors = 32;
1814
1815 tmp = heads * sectors;
1816 cylinders = capacity;
1817
1818 sector_div(cylinders, tmp);
1819
1820 /*
1821 * Handle extended translation size for logical drives > 1Gb
1822 */
1823
1824 if (capacity >= 0x200000) {
1825 heads = 255;
1826 sectors = 63;
1827 tmp = heads*sectors;
1828 cylinders = capacity;
1829 sector_div(cylinders, tmp);
1830 }
1831
1832 geom[0] = heads;
1833 geom[1] = sectors;
1834 geom[2] = cylinders;
1835
1836 return 0;
1837}
1838
Yang, Bo7e8a75f2009-10-06 14:50:17 -06001839static void megasas_aen_polling(struct work_struct *work);
1840
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001841/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001842 * megasas_service_aen - Processes an event notification
1843 * @instance: Adapter soft state
1844 * @cmd: AEN command completed by the ISR
1845 *
1846 * For AEN, driver sends a command down to FW that is held by the FW till an
1847 * event occurs. When an event of interest occurs, FW completes the command
1848 * that it was previously holding.
1849 *
1850 * This routines sends SIGIO signal to processes that have registered with the
1851 * driver for AEN.
1852 */
1853static void
1854megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1855{
Yang, Boc3518832009-10-06 14:18:02 -06001856 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001857 /*
1858 * Don't signal app if it is just an aborted previously registered aen
1859 */
Yang, Boc3518832009-10-06 14:18:02 -06001860 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1861 spin_lock_irqsave(&poll_aen_lock, flags);
1862 megasas_poll_wait_aen = 1;
1863 spin_unlock_irqrestore(&poll_aen_lock, flags);
1864 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001865 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06001866 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001867 else
1868 cmd->abort_aen = 0;
1869
1870 instance->aen_cmd = NULL;
1871 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06001872
bo yang39a98552010-09-22 22:36:29 -04001873 if ((instance->unload == 0) &&
1874 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06001875 struct megasas_aen_event *ev;
1876 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1877 if (!ev) {
1878 printk(KERN_ERR "megasas_service_aen: out of memory\n");
1879 } else {
1880 ev->instance = instance;
1881 instance->ev = ev;
1882 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
1883 schedule_delayed_work(
1884 (struct delayed_work *)&ev->hotplug_work, 0);
1885 }
1886 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001887}
1888
1889/*
1890 * Scsi host template for megaraid_sas driver
1891 */
1892static struct scsi_host_template megasas_template = {
1893
1894 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001895 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001896 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001897 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06001898 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001899 .queuecommand = megasas_queue_command,
1900 .eh_device_reset_handler = megasas_reset_device,
1901 .eh_bus_reset_handler = megasas_reset_bus_host,
1902 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001903 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001904 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001905 .use_clustering = ENABLE_CLUSTERING,
1906};
1907
1908/**
1909 * megasas_complete_int_cmd - Completes an internal command
1910 * @instance: Adapter soft state
1911 * @cmd: Command to be completed
1912 *
1913 * The megasas_issue_blocked_cmd() function waits for a command to complete
1914 * after it issues a command. This function wakes up that waiting routine by
1915 * calling wake_up() on the wait queue.
1916 */
1917static void
1918megasas_complete_int_cmd(struct megasas_instance *instance,
1919 struct megasas_cmd *cmd)
1920{
1921 cmd->cmd_status = cmd->frame->io.cmd_status;
1922
1923 if (cmd->cmd_status == ENODATA) {
1924 cmd->cmd_status = 0;
1925 }
1926 wake_up(&instance->int_cmd_wait_q);
1927}
1928
1929/**
1930 * megasas_complete_abort - Completes aborting a command
1931 * @instance: Adapter soft state
1932 * @cmd: Cmd that was issued to abort another cmd
1933 *
1934 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1935 * after it issues an abort on a previously issued command. This function
1936 * wakes up all functions waiting on the same wait queue.
1937 */
1938static void
1939megasas_complete_abort(struct megasas_instance *instance,
1940 struct megasas_cmd *cmd)
1941{
1942 if (cmd->sync_cmd) {
1943 cmd->sync_cmd = 0;
1944 cmd->cmd_status = 0;
1945 wake_up(&instance->abort_cmd_wait_q);
1946 }
1947
1948 return;
1949}
1950
1951/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001952 * megasas_complete_cmd - Completes a command
1953 * @instance: Adapter soft state
1954 * @cmd: Command to be completed
1955 * @alt_status: If non-zero, use this value as status to
1956 * SCSI mid-layer instead of the value returned
1957 * by the FW. This should be used if caller wants
1958 * an alternate status (as in the case of aborted
1959 * commands)
1960 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001961static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001962megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1963 u8 alt_status)
1964{
1965 int exception = 0;
1966 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06001967 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001968
bo yang39a98552010-09-22 22:36:29 -04001969 /* flag for the retry reset */
1970 cmd->retry_for_fw_reset = 0;
1971
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001972 if (cmd->scmd)
1973 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001974
1975 switch (hdr->cmd) {
1976
1977 case MFI_CMD_PD_SCSI_IO:
1978 case MFI_CMD_LD_SCSI_IO:
1979
1980 /*
1981 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1982 * issued either through an IO path or an IOCTL path. If it
1983 * was via IOCTL, we will send it to internal completion.
1984 */
1985 if (cmd->sync_cmd) {
1986 cmd->sync_cmd = 0;
1987 megasas_complete_int_cmd(instance, cmd);
1988 break;
1989 }
1990
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001991 case MFI_CMD_LD_READ:
1992 case MFI_CMD_LD_WRITE:
1993
1994 if (alt_status) {
1995 cmd->scmd->result = alt_status << 16;
1996 exception = 1;
1997 }
1998
1999 if (exception) {
2000
Sumant Patroe4a082c2006-05-30 12:03:37 -07002001 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002002
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002003 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002004 cmd->scmd->scsi_done(cmd->scmd);
2005 megasas_return_cmd(instance, cmd);
2006
2007 break;
2008 }
2009
2010 switch (hdr->cmd_status) {
2011
2012 case MFI_STAT_OK:
2013 cmd->scmd->result = DID_OK << 16;
2014 break;
2015
2016 case MFI_STAT_SCSI_IO_FAILED:
2017 case MFI_STAT_LD_INIT_IN_PROGRESS:
2018 cmd->scmd->result =
2019 (DID_ERROR << 16) | hdr->scsi_status;
2020 break;
2021
2022 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2023
2024 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2025
2026 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2027 memset(cmd->scmd->sense_buffer, 0,
2028 SCSI_SENSE_BUFFERSIZE);
2029 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2030 hdr->sense_len);
2031
2032 cmd->scmd->result |= DRIVER_SENSE << 24;
2033 }
2034
2035 break;
2036
2037 case MFI_STAT_LD_OFFLINE:
2038 case MFI_STAT_DEVICE_NOT_FOUND:
2039 cmd->scmd->result = DID_BAD_TARGET << 16;
2040 break;
2041
2042 default:
2043 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2044 hdr->cmd_status);
2045 cmd->scmd->result = DID_ERROR << 16;
2046 break;
2047 }
2048
Sumant Patroe4a082c2006-05-30 12:03:37 -07002049 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002050
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09002051 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002052 cmd->scmd->scsi_done(cmd->scmd);
2053 megasas_return_cmd(instance, cmd);
2054
2055 break;
2056
2057 case MFI_CMD_SMP:
2058 case MFI_CMD_STP:
2059 case MFI_CMD_DCMD:
Yang, Boc3518832009-10-06 14:18:02 -06002060 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2061 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2062 spin_lock_irqsave(&poll_aen_lock, flags);
2063 megasas_poll_wait_aen = 0;
2064 spin_unlock_irqrestore(&poll_aen_lock, flags);
2065 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002066
2067 /*
2068 * See if got an event notification
2069 */
2070 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2071 megasas_service_aen(instance, cmd);
2072 else
2073 megasas_complete_int_cmd(instance, cmd);
2074
2075 break;
2076
2077 case MFI_CMD_ABORT:
2078 /*
2079 * Cmd issued to abort another cmd returned
2080 */
2081 megasas_complete_abort(instance, cmd);
2082 break;
2083
2084 default:
2085 printk("megasas: Unknown command completed! [0x%X]\n",
2086 hdr->cmd);
2087 break;
2088 }
2089}
2090
2091/**
bo yang39a98552010-09-22 22:36:29 -04002092 * megasas_issue_pending_cmds_again - issue all pending cmds
2093 * in FW again because of the fw reset
2094 * @instance: Adapter soft state
2095 */
2096static inline void
2097megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2098{
2099 struct megasas_cmd *cmd;
2100 struct list_head clist_local;
2101 union megasas_evt_class_locale class_locale;
2102 unsigned long flags;
2103 u32 seq_num;
2104
2105 INIT_LIST_HEAD(&clist_local);
2106 spin_lock_irqsave(&instance->hba_lock, flags);
2107 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2108 spin_unlock_irqrestore(&instance->hba_lock, flags);
2109
2110 while (!list_empty(&clist_local)) {
2111 cmd = list_entry((&clist_local)->next,
2112 struct megasas_cmd, list);
2113 list_del_init(&cmd->list);
2114
2115 if (cmd->sync_cmd || cmd->scmd) {
2116 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2117 "detected to be pending while HBA reset.\n",
2118 cmd, cmd->scmd, cmd->sync_cmd);
2119
2120 cmd->retry_for_fw_reset++;
2121
2122 if (cmd->retry_for_fw_reset == 3) {
2123 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2124 "was tried multiple times during reset."
2125 "Shutting down the HBA\n",
2126 cmd, cmd->scmd, cmd->sync_cmd);
2127 megaraid_sas_kill_hba(instance);
2128
2129 instance->adprecovery =
2130 MEGASAS_HW_CRITICAL_ERROR;
2131 return;
2132 }
2133 }
2134
2135 if (cmd->sync_cmd == 1) {
2136 if (cmd->scmd) {
2137 printk(KERN_NOTICE "megaraid_sas: unexpected"
2138 "cmd attached to internal command!\n");
2139 }
2140 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2141 "on the internal reset queue,"
2142 "issue it again.\n", cmd);
2143 cmd->cmd_status = ENODATA;
2144 instance->instancet->fire_cmd(instance,
2145 cmd->frame_phys_addr ,
2146 0, instance->reg_set);
2147 } else if (cmd->scmd) {
2148 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
2149 "detected on the internal queue, issue again.\n",
2150 cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
2151
2152 atomic_inc(&instance->fw_outstanding);
2153 instance->instancet->fire_cmd(instance,
2154 cmd->frame_phys_addr,
2155 cmd->frame_count-1, instance->reg_set);
2156 } else {
2157 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2158 "internal reset defer list while re-issue!!\n",
2159 cmd);
2160 }
2161 }
2162
2163 if (instance->aen_cmd) {
2164 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2165 megasas_return_cmd(instance, instance->aen_cmd);
2166
2167 instance->aen_cmd = NULL;
2168 }
2169
2170 /*
2171 * Initiate AEN (Asynchronous Event Notification)
2172 */
2173 seq_num = instance->last_seq_num;
2174 class_locale.members.reserved = 0;
2175 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2176 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2177
2178 megasas_register_aen(instance, seq_num, class_locale.word);
2179}
2180
2181/**
2182 * Move the internal reset pending commands to a deferred queue.
2183 *
2184 * We move the commands pending at internal reset time to a
2185 * pending queue. This queue would be flushed after successful
2186 * completion of the internal reset sequence. if the internal reset
2187 * did not complete in time, the kernel reset handler would flush
2188 * these commands.
2189 **/
2190static void
2191megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2192{
2193 struct megasas_cmd *cmd;
2194 int i;
2195 u32 max_cmd = instance->max_fw_cmds;
2196 u32 defer_index;
2197 unsigned long flags;
2198
2199 defer_index = 0;
2200 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2201 for (i = 0; i < max_cmd; i++) {
2202 cmd = instance->cmd_list[i];
2203 if (cmd->sync_cmd == 1 || cmd->scmd) {
2204 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2205 "on the defer queue as internal\n",
2206 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2207
2208 if (!list_empty(&cmd->list)) {
2209 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2210 " moving this cmd:%p, %d %p, it was"
2211 "discovered on some list?\n",
2212 cmd, cmd->sync_cmd, cmd->scmd);
2213
2214 list_del_init(&cmd->list);
2215 }
2216 defer_index++;
2217 list_add_tail(&cmd->list,
2218 &instance->internal_reset_pending_q);
2219 }
2220 }
2221 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2222}
2223
2224
2225static void
2226process_fw_state_change_wq(struct work_struct *work)
2227{
2228 struct megasas_instance *instance =
2229 container_of(work, struct megasas_instance, work_init);
2230 u32 wait;
2231 unsigned long flags;
2232
2233 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2234 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2235 instance->adprecovery);
2236 return ;
2237 }
2238
2239 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2240 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2241 "state, restarting it...\n");
2242
2243 instance->instancet->disable_intr(instance->reg_set);
2244 atomic_set(&instance->fw_outstanding, 0);
2245
2246 atomic_set(&instance->fw_reset_no_pci_access, 1);
2247 instance->instancet->adp_reset(instance, instance->reg_set);
2248 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2249
2250 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2251 "initiating next stage...\n");
2252
2253 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2254 "state 2 starting...\n");
2255
2256 /*waitting for about 20 second before start the second init*/
2257 for (wait = 0; wait < 30; wait++) {
2258 msleep(1000);
2259 }
2260
2261 if (megasas_transition_to_ready(instance)) {
2262 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2263
2264 megaraid_sas_kill_hba(instance);
2265 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2266 return ;
2267 }
2268
2269 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2270 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2271 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2272 ) {
2273 *instance->consumer = *instance->producer;
2274 } else {
2275 *instance->consumer = 0;
2276 *instance->producer = 0;
2277 }
2278
2279 megasas_issue_init_mfi(instance);
2280
2281 spin_lock_irqsave(&instance->hba_lock, flags);
2282 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2283 spin_unlock_irqrestore(&instance->hba_lock, flags);
2284 instance->instancet->enable_intr(instance->reg_set);
2285
2286 megasas_issue_pending_cmds_again(instance);
2287 instance->issuepend_done = 1;
2288 }
2289 return ;
2290}
2291
2292/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002293 * megasas_deplete_reply_queue - Processes all completed commands
2294 * @instance: Adapter soft state
2295 * @alt_status: Alternate status to be returned to
2296 * SCSI mid-layer instead of the status
2297 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04002298 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002299 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002300static int
bo yang39a98552010-09-22 22:36:29 -04002301megasas_deplete_reply_queue(struct megasas_instance *instance,
2302 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002303{
bo yang39a98552010-09-22 22:36:29 -04002304 u32 mfiStatus;
2305 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002306
bo yang39a98552010-09-22 22:36:29 -04002307 if ((mfiStatus = instance->instancet->check_reset(instance,
2308 instance->reg_set)) == 1) {
2309 return IRQ_HANDLED;
2310 }
2311
2312 if ((mfiStatus = instance->instancet->clear_intr(
2313 instance->reg_set)
2314 ) == 0) {
2315 return IRQ_NONE;
2316 }
2317
2318 instance->mfiStatus = mfiStatus;
2319
2320 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2321 fw_state = instance->instancet->read_fw_status_reg(
2322 instance->reg_set) & MFI_STATE_MASK;
2323
2324 if (fw_state != MFI_STATE_FAULT) {
2325 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2326 fw_state);
2327 }
2328
2329 if ((fw_state == MFI_STATE_FAULT) &&
2330 (instance->disableOnlineCtrlReset == 0)) {
2331 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2332
2333 if ((instance->pdev->device ==
2334 PCI_DEVICE_ID_LSI_SAS1064R) ||
2335 (instance->pdev->device ==
2336 PCI_DEVICE_ID_DELL_PERC5) ||
2337 (instance->pdev->device ==
2338 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2339
2340 *instance->consumer =
2341 MEGASAS_ADPRESET_INPROG_SIGN;
2342 }
2343
2344
2345 instance->instancet->disable_intr(instance->reg_set);
2346 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2347 instance->issuepend_done = 0;
2348
2349 atomic_set(&instance->fw_outstanding, 0);
2350 megasas_internal_reset_defer_cmds(instance);
2351
2352 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2353 fw_state, instance->adprecovery);
2354
2355 schedule_work(&instance->work_init);
2356 return IRQ_HANDLED;
2357
2358 } else {
2359 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2360 fw_state, instance->disableOnlineCtrlReset);
2361 }
2362 }
2363
Sumant Patro5d018ad2006-10-03 13:13:18 -07002364 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002365 return IRQ_HANDLED;
2366}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002367/**
2368 * megasas_isr - isr entry point
2369 */
David Howells7d12e782006-10-05 14:55:46 +01002370static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002371{
bo yang39a98552010-09-22 22:36:29 -04002372 struct megasas_instance *instance;
2373 unsigned long flags;
2374 irqreturn_t rc;
2375
2376 if (atomic_read(
2377 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2378 return IRQ_HANDLED;
2379
2380 instance = (struct megasas_instance *)devp;
2381
2382 spin_lock_irqsave(&instance->hba_lock, flags);
2383 rc = megasas_deplete_reply_queue(instance, DID_OK);
2384 spin_unlock_irqrestore(&instance->hba_lock, flags);
2385
2386 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002387}
2388
2389/**
2390 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08002391 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002392 *
2393 * During the initialization, FW passes can potentially be in any one of
2394 * several possible states. If the FW in operational, waiting-for-handshake
2395 * states, driver must take steps to bring it to ready state. Otherwise, it
2396 * has to wait for the ready state.
2397 */
2398static int
Sumant Patro1341c932006-01-25 12:02:40 -08002399megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002400{
2401 int i;
2402 u8 max_wait;
2403 u32 fw_state;
2404 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06002405 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002406
Sumant Patro1341c932006-01-25 12:02:40 -08002407 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002408
Sumant Patroe3bbff92006-10-03 12:28:49 -07002409 if (fw_state != MFI_STATE_READY)
2410 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2411 " state\n");
2412
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002413 while (fw_state != MFI_STATE_READY) {
2414
Yang, Bo7218df62009-10-06 14:52:20 -06002415 abs_state =
2416 instance->instancet->read_fw_status_reg(instance->reg_set);
2417
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002418 switch (fw_state) {
2419
2420 case MFI_STATE_FAULT:
2421
2422 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2423 return -ENODEV;
2424
2425 case MFI_STATE_WAIT_HANDSHAKE:
2426 /*
2427 * Set the CLR bit in inbound doorbell
2428 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002429 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06002430 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2431 (instance->pdev->device ==
2432 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2433
2434 writel(
2435 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2436 &instance->reg_set->reserved_0[0]);
2437 } else {
2438 writel(
2439 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2440 &instance->reg_set->inbound_doorbell);
2441 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002442
Yang, Bo7218df62009-10-06 14:52:20 -06002443 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002444 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2445 break;
2446
Sumant Patroe3bbff92006-10-03 12:28:49 -07002447 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06002448 if ((instance->pdev->device ==
2449 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2450 (instance->pdev->device ==
2451 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2452 writel(MFI_INIT_HOTPLUG,
2453 &instance->reg_set->reserved_0[0]);
2454 } else
2455 writel(MFI_INIT_HOTPLUG,
2456 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07002457
Yang, Bo7218df62009-10-06 14:52:20 -06002458 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002459 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2460 break;
2461
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002462 case MFI_STATE_OPERATIONAL:
2463 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07002464 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002465 */
Sumant Patrob274cab2006-10-03 12:52:12 -07002466 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06002467 if ((instance->pdev->device ==
2468 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2469 (instance->pdev->device ==
2470 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2471 writel(MFI_RESET_FLAGS,
2472 &instance->reg_set->reserved_0[0]);
2473 } else
2474 writel(MFI_RESET_FLAGS,
2475 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002476
Yang, Bo7218df62009-10-06 14:52:20 -06002477 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002478 cur_state = MFI_STATE_OPERATIONAL;
2479 break;
2480
2481 case MFI_STATE_UNDEFINED:
2482 /*
2483 * This state should not last for more than 2 seconds
2484 */
Yang, Bo7218df62009-10-06 14:52:20 -06002485 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002486 cur_state = MFI_STATE_UNDEFINED;
2487 break;
2488
2489 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002490 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002491 cur_state = MFI_STATE_BB_INIT;
2492 break;
2493
2494 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06002495 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002496 cur_state = MFI_STATE_FW_INIT;
2497 break;
2498
2499 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06002500 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002501 cur_state = MFI_STATE_FW_INIT_2;
2502 break;
2503
2504 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06002505 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002506 cur_state = MFI_STATE_DEVICE_SCAN;
2507 break;
2508
2509 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06002510 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002511 cur_state = MFI_STATE_FLUSH_CACHE;
2512 break;
2513
2514 default:
2515 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2516 fw_state);
2517 return -ENODEV;
2518 }
2519
2520 /*
2521 * The cur_state should not last for more than max_wait secs
2522 */
2523 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08002524 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2525 MFI_STATE_MASK ;
Yang, Bo7218df62009-10-06 14:52:20 -06002526 curr_abs_state =
2527 instance->instancet->read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002528
Yang, Bo7218df62009-10-06 14:52:20 -06002529 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002530 msleep(1);
2531 } else
2532 break;
2533 }
2534
2535 /*
2536 * Return error if fw_state hasn't changed after max_wait
2537 */
Yang, Bo7218df62009-10-06 14:52:20 -06002538 if (curr_abs_state == abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002539 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2540 "in %d secs\n", fw_state, max_wait);
2541 return -ENODEV;
2542 }
bo yang39a98552010-09-22 22:36:29 -04002543 }
Sumant Patroe3bbff92006-10-03 12:28:49 -07002544 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002545
2546 return 0;
2547}
2548
2549/**
2550 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2551 * @instance: Adapter soft state
2552 */
2553static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2554{
2555 int i;
2556 u32 max_cmd = instance->max_fw_cmds;
2557 struct megasas_cmd *cmd;
2558
2559 if (!instance->frame_dma_pool)
2560 return;
2561
2562 /*
2563 * Return all frames to pool
2564 */
2565 for (i = 0; i < max_cmd; i++) {
2566
2567 cmd = instance->cmd_list[i];
2568
2569 if (cmd->frame)
2570 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2571 cmd->frame_phys_addr);
2572
2573 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07002574 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002575 cmd->sense_phys_addr);
2576 }
2577
2578 /*
2579 * Now destroy the pool itself
2580 */
2581 pci_pool_destroy(instance->frame_dma_pool);
2582 pci_pool_destroy(instance->sense_dma_pool);
2583
2584 instance->frame_dma_pool = NULL;
2585 instance->sense_dma_pool = NULL;
2586}
2587
2588/**
2589 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2590 * @instance: Adapter soft state
2591 *
2592 * Each command packet has an embedded DMA memory buffer that is used for
2593 * filling MFI frame and the SG list that immediately follows the frame. This
2594 * function creates those DMA memory buffers for each command packet by using
2595 * PCI pool facility.
2596 */
2597static int megasas_create_frame_pool(struct megasas_instance *instance)
2598{
2599 int i;
2600 u32 max_cmd;
2601 u32 sge_sz;
2602 u32 sgl_sz;
2603 u32 total_sz;
2604 u32 frame_count;
2605 struct megasas_cmd *cmd;
2606
2607 max_cmd = instance->max_fw_cmds;
2608
2609 /*
2610 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2611 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2612 */
2613 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2614 sizeof(struct megasas_sge32);
2615
Yang, Bof4c9a132009-10-06 14:43:28 -06002616 if (instance->flag_ieee) {
2617 sge_sz = sizeof(struct megasas_sge_skinny);
2618 }
2619
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002620 /*
2621 * Calculated the number of 64byte frames required for SGL
2622 */
2623 sgl_sz = sge_sz * instance->max_num_sge;
2624 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
bo yang39a98552010-09-22 22:36:29 -04002625 frame_count = 15;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002626
2627 /*
2628 * We need one extra frame for the MFI command
2629 */
2630 frame_count++;
2631
2632 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2633 /*
2634 * Use DMA pool facility provided by PCI layer
2635 */
2636 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2637 instance->pdev, total_sz, 64,
2638 0);
2639
2640 if (!instance->frame_dma_pool) {
2641 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2642 return -ENOMEM;
2643 }
2644
2645 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2646 instance->pdev, 128, 4, 0);
2647
2648 if (!instance->sense_dma_pool) {
2649 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2650
2651 pci_pool_destroy(instance->frame_dma_pool);
2652 instance->frame_dma_pool = NULL;
2653
2654 return -ENOMEM;
2655 }
2656
2657 /*
2658 * Allocate and attach a frame to each of the commands in cmd_list.
2659 * By making cmd->index as the context instead of the &cmd, we can
2660 * always use 32bit context regardless of the architecture
2661 */
2662 for (i = 0; i < max_cmd; i++) {
2663
2664 cmd = instance->cmd_list[i];
2665
2666 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2667 GFP_KERNEL, &cmd->frame_phys_addr);
2668
2669 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2670 GFP_KERNEL, &cmd->sense_phys_addr);
2671
2672 /*
2673 * megasas_teardown_frame_pool() takes care of freeing
2674 * whatever has been allocated
2675 */
2676 if (!cmd->frame || !cmd->sense) {
2677 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2678 megasas_teardown_frame_pool(instance);
2679 return -ENOMEM;
2680 }
2681
2682 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002683 cmd->frame->io.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002684 }
2685
2686 return 0;
2687}
2688
2689/**
2690 * megasas_free_cmds - Free all the cmds in the free cmd pool
2691 * @instance: Adapter soft state
2692 */
2693static void megasas_free_cmds(struct megasas_instance *instance)
2694{
2695 int i;
2696 /* First free the MFI frame pool */
2697 megasas_teardown_frame_pool(instance);
2698
2699 /* Free all the commands in the cmd_list */
2700 for (i = 0; i < instance->max_fw_cmds; i++)
2701 kfree(instance->cmd_list[i]);
2702
2703 /* Free the cmd_list buffer itself */
2704 kfree(instance->cmd_list);
2705 instance->cmd_list = NULL;
2706
2707 INIT_LIST_HEAD(&instance->cmd_pool);
2708}
2709
2710/**
2711 * megasas_alloc_cmds - Allocates the command packets
2712 * @instance: Adapter soft state
2713 *
2714 * Each command that is issued to the FW, whether IO commands from the OS or
2715 * internal commands like IOCTLs, are wrapped in local data structure called
2716 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2717 * the FW.
2718 *
2719 * Each frame has a 32-bit field called context (tag). This context is used
2720 * to get back the megasas_cmd from the frame when a frame gets completed in
2721 * the ISR. Typically the address of the megasas_cmd itself would be used as
2722 * the context. But we wanted to keep the differences between 32 and 64 bit
2723 * systems to the mininum. We always use 32 bit integers for the context. In
2724 * this driver, the 32 bit values are the indices into an array cmd_list.
2725 * This array is used only to look up the megasas_cmd given the context. The
2726 * free commands themselves are maintained in a linked list called cmd_pool.
2727 */
2728static int megasas_alloc_cmds(struct megasas_instance *instance)
2729{
2730 int i;
2731 int j;
2732 u32 max_cmd;
2733 struct megasas_cmd *cmd;
2734
2735 max_cmd = instance->max_fw_cmds;
2736
2737 /*
2738 * instance->cmd_list is an array of struct megasas_cmd pointers.
2739 * Allocate the dynamic array first and then allocate individual
2740 * commands.
2741 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002742 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002743
2744 if (!instance->cmd_list) {
2745 printk(KERN_DEBUG "megasas: out of memory\n");
2746 return -ENOMEM;
2747 }
2748
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002749
2750 for (i = 0; i < max_cmd; i++) {
2751 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2752 GFP_KERNEL);
2753
2754 if (!instance->cmd_list[i]) {
2755
2756 for (j = 0; j < i; j++)
2757 kfree(instance->cmd_list[j]);
2758
2759 kfree(instance->cmd_list);
2760 instance->cmd_list = NULL;
2761
2762 return -ENOMEM;
2763 }
2764 }
2765
2766 /*
2767 * Add all the commands to command pool (instance->cmd_pool)
2768 */
2769 for (i = 0; i < max_cmd; i++) {
2770 cmd = instance->cmd_list[i];
2771 memset(cmd, 0, sizeof(struct megasas_cmd));
2772 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04002773 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002774 cmd->instance = instance;
2775
2776 list_add_tail(&cmd->list, &instance->cmd_pool);
2777 }
2778
2779 /*
2780 * Create a frame pool and assign one frame to each cmd
2781 */
2782 if (megasas_create_frame_pool(instance)) {
2783 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2784 megasas_free_cmds(instance);
2785 }
2786
2787 return 0;
2788}
2789
Yang, Bo81e403c2009-10-06 14:27:54 -06002790/*
2791 * megasas_get_pd_list_info - Returns FW's pd_list structure
2792 * @instance: Adapter soft state
2793 * @pd_list: pd_list structure
2794 *
2795 * Issues an internal command (DCMD) to get the FW's controller PD
2796 * list structure. This information is mainly used to find out SYSTEM
2797 * supported by the FW.
2798 */
2799static int
2800megasas_get_pd_list(struct megasas_instance *instance)
2801{
2802 int ret = 0, pd_index = 0;
2803 struct megasas_cmd *cmd;
2804 struct megasas_dcmd_frame *dcmd;
2805 struct MR_PD_LIST *ci;
2806 struct MR_PD_ADDRESS *pd_addr;
2807 dma_addr_t ci_h = 0;
2808
2809 cmd = megasas_get_cmd(instance);
2810
2811 if (!cmd) {
2812 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2813 return -ENOMEM;
2814 }
2815
2816 dcmd = &cmd->frame->dcmd;
2817
2818 ci = pci_alloc_consistent(instance->pdev,
2819 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2820
2821 if (!ci) {
2822 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2823 megasas_return_cmd(instance, cmd);
2824 return -ENOMEM;
2825 }
2826
2827 memset(ci, 0, sizeof(*ci));
2828 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2829
2830 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2831 dcmd->mbox.b[1] = 0;
2832 dcmd->cmd = MFI_CMD_DCMD;
2833 dcmd->cmd_status = 0xFF;
2834 dcmd->sge_count = 1;
2835 dcmd->flags = MFI_FRAME_DIR_READ;
2836 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07002837 dcmd->pad_0 = 0;
Yang, Bo81e403c2009-10-06 14:27:54 -06002838 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2839 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2840 dcmd->sgl.sge32[0].phys_addr = ci_h;
2841 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2842
2843 if (!megasas_issue_polled(instance, cmd)) {
2844 ret = 0;
2845 } else {
2846 ret = -1;
2847 }
2848
2849 /*
2850 * the following function will get the instance PD LIST.
2851 */
2852
2853 pd_addr = ci->addr;
2854
2855 if ( ret == 0 &&
2856 (ci->count <
2857 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2858
2859 memset(instance->pd_list, 0,
2860 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2861
2862 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2863
2864 instance->pd_list[pd_addr->deviceId].tid =
2865 pd_addr->deviceId;
2866 instance->pd_list[pd_addr->deviceId].driveType =
2867 pd_addr->scsiDevType;
2868 instance->pd_list[pd_addr->deviceId].driveState =
2869 MR_PD_STATE_SYSTEM;
2870 pd_addr++;
2871 }
2872 }
2873
2874 pci_free_consistent(instance->pdev,
2875 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2876 ci, ci_h);
2877 megasas_return_cmd(instance, cmd);
2878
2879 return ret;
2880}
2881
Yang, Bobdc6fb82009-12-06 08:30:19 -07002882/*
2883 * megasas_get_ld_list_info - Returns FW's ld_list structure
2884 * @instance: Adapter soft state
2885 * @ld_list: ld_list structure
2886 *
2887 * Issues an internal command (DCMD) to get the FW's controller PD
2888 * list structure. This information is mainly used to find out SYSTEM
2889 * supported by the FW.
2890 */
2891static int
2892megasas_get_ld_list(struct megasas_instance *instance)
2893{
2894 int ret = 0, ld_index = 0, ids = 0;
2895 struct megasas_cmd *cmd;
2896 struct megasas_dcmd_frame *dcmd;
2897 struct MR_LD_LIST *ci;
2898 dma_addr_t ci_h = 0;
2899
2900 cmd = megasas_get_cmd(instance);
2901
2902 if (!cmd) {
2903 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
2904 return -ENOMEM;
2905 }
2906
2907 dcmd = &cmd->frame->dcmd;
2908
2909 ci = pci_alloc_consistent(instance->pdev,
2910 sizeof(struct MR_LD_LIST),
2911 &ci_h);
2912
2913 if (!ci) {
2914 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
2915 megasas_return_cmd(instance, cmd);
2916 return -ENOMEM;
2917 }
2918
2919 memset(ci, 0, sizeof(*ci));
2920 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2921
2922 dcmd->cmd = MFI_CMD_DCMD;
2923 dcmd->cmd_status = 0xFF;
2924 dcmd->sge_count = 1;
2925 dcmd->flags = MFI_FRAME_DIR_READ;
2926 dcmd->timeout = 0;
2927 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
2928 dcmd->opcode = MR_DCMD_LD_GET_LIST;
2929 dcmd->sgl.sge32[0].phys_addr = ci_h;
2930 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
2931 dcmd->pad_0 = 0;
2932
2933 if (!megasas_issue_polled(instance, cmd)) {
2934 ret = 0;
2935 } else {
2936 ret = -1;
2937 }
2938
2939 /* the following function will get the instance PD LIST */
2940
bo yang39a98552010-09-22 22:36:29 -04002941 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07002942 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2943
2944 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
2945 if (ci->ldList[ld_index].state != 0) {
2946 ids = ci->ldList[ld_index].ref.targetId;
2947 instance->ld_ids[ids] =
2948 ci->ldList[ld_index].ref.targetId;
2949 }
2950 }
2951 }
2952
2953 pci_free_consistent(instance->pdev,
2954 sizeof(struct MR_LD_LIST),
2955 ci,
2956 ci_h);
2957
2958 megasas_return_cmd(instance, cmd);
2959 return ret;
2960}
2961
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002962/**
2963 * megasas_get_controller_info - Returns FW's controller structure
2964 * @instance: Adapter soft state
2965 * @ctrl_info: Controller information structure
2966 *
2967 * Issues an internal command (DCMD) to get the FW's controller structure.
2968 * This information is mainly used to find out the maximum IO transfer per
2969 * command supported by the FW.
2970 */
2971static int
2972megasas_get_ctrl_info(struct megasas_instance *instance,
2973 struct megasas_ctrl_info *ctrl_info)
2974{
2975 int ret = 0;
2976 struct megasas_cmd *cmd;
2977 struct megasas_dcmd_frame *dcmd;
2978 struct megasas_ctrl_info *ci;
2979 dma_addr_t ci_h = 0;
2980
2981 cmd = megasas_get_cmd(instance);
2982
2983 if (!cmd) {
2984 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2985 return -ENOMEM;
2986 }
2987
2988 dcmd = &cmd->frame->dcmd;
2989
2990 ci = pci_alloc_consistent(instance->pdev,
2991 sizeof(struct megasas_ctrl_info), &ci_h);
2992
2993 if (!ci) {
2994 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2995 megasas_return_cmd(instance, cmd);
2996 return -ENOMEM;
2997 }
2998
2999 memset(ci, 0, sizeof(*ci));
3000 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3001
3002 dcmd->cmd = MFI_CMD_DCMD;
3003 dcmd->cmd_status = 0xFF;
3004 dcmd->sge_count = 1;
3005 dcmd->flags = MFI_FRAME_DIR_READ;
3006 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003007 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003008 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3009 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3010 dcmd->sgl.sge32[0].phys_addr = ci_h;
3011 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3012
3013 if (!megasas_issue_polled(instance, cmd)) {
3014 ret = 0;
3015 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3016 } else {
3017 ret = -1;
3018 }
3019
3020 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3021 ci, ci_h);
3022
3023 megasas_return_cmd(instance, cmd);
3024 return ret;
3025}
3026
3027/**
bo yang31ea7082007-11-07 12:09:50 -05003028 * megasas_issue_init_mfi - Initializes the FW
3029 * @instance: Adapter soft state
3030 *
3031 * Issues the INIT MFI cmd
3032 */
3033static int
3034megasas_issue_init_mfi(struct megasas_instance *instance)
3035{
3036 u32 context;
3037
3038 struct megasas_cmd *cmd;
3039
3040 struct megasas_init_frame *init_frame;
3041 struct megasas_init_queue_info *initq_info;
3042 dma_addr_t init_frame_h;
3043 dma_addr_t initq_info_h;
3044
3045 /*
3046 * Prepare a init frame. Note the init frame points to queue info
3047 * structure. Each frame has SGL allocated after first 64 bytes. For
3048 * this frame - since we don't need any SGL - we use SGL's space as
3049 * queue info structure
3050 *
3051 * We will not get a NULL command below. We just created the pool.
3052 */
3053 cmd = megasas_get_cmd(instance);
3054
3055 init_frame = (struct megasas_init_frame *)cmd->frame;
3056 initq_info = (struct megasas_init_queue_info *)
3057 ((unsigned long)init_frame + 64);
3058
3059 init_frame_h = cmd->frame_phys_addr;
3060 initq_info_h = init_frame_h + 64;
3061
3062 context = init_frame->context;
3063 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3064 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3065 init_frame->context = context;
3066
3067 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3068 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3069
3070 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3071 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3072
3073 init_frame->cmd = MFI_CMD_INIT;
3074 init_frame->cmd_status = 0xFF;
3075 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3076
3077 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3078
3079 /*
3080 * disable the intr before firing the init frame to FW
3081 */
3082 instance->instancet->disable_intr(instance->reg_set);
3083
3084 /*
3085 * Issue the init frame in polled mode
3086 */
3087
3088 if (megasas_issue_polled(instance, cmd)) {
3089 printk(KERN_ERR "megasas: Failed to init firmware\n");
3090 megasas_return_cmd(instance, cmd);
3091 goto fail_fw_init;
3092 }
3093
3094 megasas_return_cmd(instance, cmd);
3095
3096 return 0;
3097
3098fail_fw_init:
3099 return -EINVAL;
3100}
3101
3102/**
bo yangad84db22007-11-09 04:40:16 -05003103 * megasas_start_timer - Initializes a timer object
3104 * @instance: Adapter soft state
3105 * @timer: timer object to be initialized
3106 * @fn: timer function
3107 * @interval: time interval between timer function call
3108 */
3109static inline void
3110megasas_start_timer(struct megasas_instance *instance,
3111 struct timer_list *timer,
3112 void *fn, unsigned long interval)
3113{
3114 init_timer(timer);
3115 timer->expires = jiffies + interval;
3116 timer->data = (unsigned long)instance;
3117 timer->function = fn;
3118 add_timer(timer);
3119}
3120
3121/**
3122 * megasas_io_completion_timer - Timer fn
3123 * @instance_addr: Address of adapter soft state
3124 *
3125 * Schedules tasklet for cmd completion
3126 * if poll_mode_io is set
3127 */
3128static void
3129megasas_io_completion_timer(unsigned long instance_addr)
3130{
3131 struct megasas_instance *instance =
3132 (struct megasas_instance *)instance_addr;
3133
3134 if (atomic_read(&instance->fw_outstanding))
3135 tasklet_schedule(&instance->isr_tasklet);
3136
3137 /* Restart timer */
3138 if (poll_mode_io)
3139 mod_timer(&instance->io_completion_timer,
3140 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3141}
3142
3143/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003144 * megasas_init_mfi - Initializes the FW
3145 * @instance: Adapter soft state
3146 *
3147 * This is the main function for initializing MFI firmware.
3148 */
3149static int megasas_init_mfi(struct megasas_instance *instance)
3150{
3151 u32 context_sz;
3152 u32 reply_q_sz;
3153 u32 max_sectors_1;
3154 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05003155 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003156 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003157 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003158 /*
3159 * Map the message registers
3160 */
Yang, Bo6610a6b2008-08-10 12:42:38 -07003161 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
Yang, Bo87911122009-10-06 14:31:54 -06003162 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3163 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Yang, Bo6610a6b2008-08-10 12:42:38 -07003164 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
3165 instance->base_addr = pci_resource_start(instance->pdev, 1);
3166 } else {
3167 instance->base_addr = pci_resource_start(instance->pdev, 0);
3168 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003169
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003170 if (pci_request_selected_regions(instance->pdev,
3171 pci_select_bars(instance->pdev, IORESOURCE_MEM),
3172 "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003173 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3174 return -EBUSY;
3175 }
3176
3177 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3178
3179 if (!instance->reg_set) {
3180 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3181 goto fail_ioremap;
3182 }
3183
3184 reg_set = instance->reg_set;
3185
Sumant Patrof9876f02006-02-03 15:34:35 -08003186 switch(instance->pdev->device)
3187 {
bo yangaf7a5642008-03-17 04:13:07 -04003188 case PCI_DEVICE_ID_LSI_SAS1078R:
3189 case PCI_DEVICE_ID_LSI_SAS1078DE:
Sumant Patrof9876f02006-02-03 15:34:35 -08003190 instance->instancet = &megasas_instance_template_ppc;
3191 break;
Yang, Bo6610a6b2008-08-10 12:42:38 -07003192 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3193 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3194 instance->instancet = &megasas_instance_template_gen2;
3195 break;
Yang, Bo87911122009-10-06 14:31:54 -06003196 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3197 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3198 instance->instancet = &megasas_instance_template_skinny;
3199 break;
Sumant Patrof9876f02006-02-03 15:34:35 -08003200 case PCI_DEVICE_ID_LSI_SAS1064R:
3201 case PCI_DEVICE_ID_DELL_PERC5:
3202 default:
3203 instance->instancet = &megasas_instance_template_xscale;
3204 break;
3205 }
Sumant Patro1341c932006-01-25 12:02:40 -08003206
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003207 /*
3208 * We expect the FW state to be READY
3209 */
Sumant Patro1341c932006-01-25 12:02:40 -08003210 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003211 goto fail_ready_state;
3212
3213 /*
3214 * Get various operational parameters from status register
3215 */
Sumant Patro1341c932006-01-25 12:02:40 -08003216 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003217 /*
3218 * Reduce the max supported cmds by 1. This is to ensure that the
3219 * reply_q_sz (1 more than the max cmd that driver may send)
3220 * does not exceed max cmds that the FW can support
3221 */
3222 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08003223 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3224 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003225 /*
3226 * Create a pool of commands
3227 */
3228 if (megasas_alloc_cmds(instance))
3229 goto fail_alloc_cmds;
3230
3231 /*
3232 * Allocate memory for reply queue. Length of reply queue should
3233 * be _one_ more than the maximum commands handled by the firmware.
3234 *
3235 * Note: When FW completes commands, it places corresponding contex
3236 * values in this circular reply queue. This circular queue is a fairly
3237 * typical producer-consumer queue. FW is the producer (of completed
3238 * commands) and the driver is the consumer.
3239 */
3240 context_sz = sizeof(u32);
3241 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3242
3243 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3244 reply_q_sz,
3245 &instance->reply_queue_h);
3246
3247 if (!instance->reply_queue) {
3248 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3249 goto fail_reply_queue;
3250 }
3251
bo yang31ea7082007-11-07 12:09:50 -05003252 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003253 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003254
bo yang39a98552010-09-22 22:36:29 -04003255 instance->fw_support_ieee = 0;
3256 instance->fw_support_ieee =
3257 (instance->instancet->read_fw_status_reg(reg_set) &
3258 0x04000000);
3259
3260 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3261 instance->fw_support_ieee);
3262
3263 if (instance->fw_support_ieee)
3264 instance->flag_ieee = 1;
3265
3266 /** for passthrough
3267 * the following function will get the PD LIST.
3268 */
3269
Yang, Bo81e403c2009-10-06 14:27:54 -06003270 memset(instance->pd_list, 0 ,
3271 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3272 megasas_get_pd_list(instance);
3273
Yang, Bobdc6fb82009-12-06 08:30:19 -07003274 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3275 megasas_get_ld_list(instance);
3276
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003277 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3278
3279 /*
3280 * Compute the max allowed sectors per IO: The controller info has two
3281 * limits on max sectors. Driver should use the minimum of these two.
3282 *
3283 * 1 << stripe_sz_ops.min = max sectors per strip
3284 *
3285 * Note that older firmwares ( < FW ver 30) didn't report information
3286 * to calculate max_sectors_1. So the number ended up as zero always.
3287 */
bo yang14faea92007-11-09 04:14:00 -05003288 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003289 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3290
3291 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3292 ctrl_info->max_strips_per_io;
3293 max_sectors_2 = ctrl_info->max_request_size;
3294
bo yang14faea92007-11-09 04:14:00 -05003295 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bo yang39a98552010-09-22 22:36:29 -04003296 instance->disableOnlineCtrlReset =
3297 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bo yang14faea92007-11-09 04:14:00 -05003298 }
3299
3300 instance->max_sectors_per_req = instance->max_num_sge *
3301 PAGE_SIZE / 512;
3302 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3303 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003304
3305 kfree(ctrl_info);
3306
Sumant Patro5d018ad2006-10-03 13:13:18 -07003307 /*
3308 * Setup tasklet for cmd completion
3309 */
3310
bo yangad84db22007-11-09 04:40:16 -05003311 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3312 (unsigned long)instance);
3313
3314 /* Initialize the cmd completion timer */
3315 if (poll_mode_io)
3316 megasas_start_timer(instance, &instance->io_completion_timer,
3317 megasas_io_completion_timer,
3318 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003319 return 0;
3320
3321 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003322
3323 pci_free_consistent(instance->pdev, reply_q_sz,
3324 instance->reply_queue, instance->reply_queue_h);
3325 fail_reply_queue:
3326 megasas_free_cmds(instance);
3327
3328 fail_alloc_cmds:
3329 fail_ready_state:
3330 iounmap(instance->reg_set);
3331
3332 fail_ioremap:
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003333 pci_release_selected_regions(instance->pdev,
3334 pci_select_bars(instance->pdev, IORESOURCE_MEM));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003335
3336 return -EINVAL;
3337}
3338
3339/**
3340 * megasas_release_mfi - Reverses the FW initialization
3341 * @intance: Adapter soft state
3342 */
3343static void megasas_release_mfi(struct megasas_instance *instance)
3344{
3345 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
3346
3347 pci_free_consistent(instance->pdev, reply_q_sz,
3348 instance->reply_queue, instance->reply_queue_h);
3349
3350 megasas_free_cmds(instance);
3351
3352 iounmap(instance->reg_set);
3353
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003354 pci_release_selected_regions(instance->pdev,
3355 pci_select_bars(instance->pdev, IORESOURCE_MEM));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003356}
3357
3358/**
3359 * megasas_get_seq_num - Gets latest event sequence numbers
3360 * @instance: Adapter soft state
3361 * @eli: FW event log sequence numbers information
3362 *
3363 * FW maintains a log of all events in a non-volatile area. Upper layers would
3364 * usually find out the latest sequence number of the events, the seq number at
3365 * the boot etc. They would "read" all the events below the latest seq number
3366 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3367 * number), they would subsribe to AEN (asynchronous event notification) and
3368 * wait for the events to happen.
3369 */
3370static int
3371megasas_get_seq_num(struct megasas_instance *instance,
3372 struct megasas_evt_log_info *eli)
3373{
3374 struct megasas_cmd *cmd;
3375 struct megasas_dcmd_frame *dcmd;
3376 struct megasas_evt_log_info *el_info;
3377 dma_addr_t el_info_h = 0;
3378
3379 cmd = megasas_get_cmd(instance);
3380
3381 if (!cmd) {
3382 return -ENOMEM;
3383 }
3384
3385 dcmd = &cmd->frame->dcmd;
3386 el_info = pci_alloc_consistent(instance->pdev,
3387 sizeof(struct megasas_evt_log_info),
3388 &el_info_h);
3389
3390 if (!el_info) {
3391 megasas_return_cmd(instance, cmd);
3392 return -ENOMEM;
3393 }
3394
3395 memset(el_info, 0, sizeof(*el_info));
3396 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3397
3398 dcmd->cmd = MFI_CMD_DCMD;
3399 dcmd->cmd_status = 0x0;
3400 dcmd->sge_count = 1;
3401 dcmd->flags = MFI_FRAME_DIR_READ;
3402 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003403 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003404 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3405 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3406 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3407 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3408
3409 megasas_issue_blocked_cmd(instance, cmd);
3410
3411 /*
3412 * Copy the data back into callers buffer
3413 */
3414 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3415
3416 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3417 el_info, el_info_h);
3418
3419 megasas_return_cmd(instance, cmd);
3420
3421 return 0;
3422}
3423
3424/**
3425 * megasas_register_aen - Registers for asynchronous event notification
3426 * @instance: Adapter soft state
3427 * @seq_num: The starting sequence number
3428 * @class_locale: Class of the event
3429 *
3430 * This function subscribes for AEN for events beyond the @seq_num. It requests
3431 * to be notified if and only if the event is of type @class_locale
3432 */
3433static int
3434megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3435 u32 class_locale_word)
3436{
3437 int ret_val;
3438 struct megasas_cmd *cmd;
3439 struct megasas_dcmd_frame *dcmd;
3440 union megasas_evt_class_locale curr_aen;
3441 union megasas_evt_class_locale prev_aen;
3442
3443 /*
3444 * If there an AEN pending already (aen_cmd), check if the
3445 * class_locale of that pending AEN is inclusive of the new
3446 * AEN request we currently have. If it is, then we don't have
3447 * to do anything. In other words, whichever events the current
3448 * AEN request is subscribing to, have already been subscribed
3449 * to.
3450 *
3451 * If the old_cmd is _not_ inclusive, then we have to abort
3452 * that command, form a class_locale that is superset of both
3453 * old and current and re-issue to the FW
3454 */
3455
3456 curr_aen.word = class_locale_word;
3457
3458 if (instance->aen_cmd) {
3459
3460 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3461
3462 /*
3463 * A class whose enum value is smaller is inclusive of all
3464 * higher values. If a PROGRESS (= -1) was previously
3465 * registered, then a new registration requests for higher
3466 * classes need not be sent to FW. They are automatically
3467 * included.
3468 *
3469 * Locale numbers don't have such hierarchy. They are bitmap
3470 * values
3471 */
3472 if ((prev_aen.members.class <= curr_aen.members.class) &&
3473 !((prev_aen.members.locale & curr_aen.members.locale) ^
3474 curr_aen.members.locale)) {
3475 /*
3476 * Previously issued event registration includes
3477 * current request. Nothing to do.
3478 */
3479 return 0;
3480 } else {
3481 curr_aen.members.locale |= prev_aen.members.locale;
3482
3483 if (prev_aen.members.class < curr_aen.members.class)
3484 curr_aen.members.class = prev_aen.members.class;
3485
3486 instance->aen_cmd->abort_aen = 1;
3487 ret_val = megasas_issue_blocked_abort_cmd(instance,
3488 instance->
3489 aen_cmd);
3490
3491 if (ret_val) {
3492 printk(KERN_DEBUG "megasas: Failed to abort "
3493 "previous AEN command\n");
3494 return ret_val;
3495 }
3496 }
3497 }
3498
3499 cmd = megasas_get_cmd(instance);
3500
3501 if (!cmd)
3502 return -ENOMEM;
3503
3504 dcmd = &cmd->frame->dcmd;
3505
3506 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3507
3508 /*
3509 * Prepare DCMD for aen registration
3510 */
3511 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3512
3513 dcmd->cmd = MFI_CMD_DCMD;
3514 dcmd->cmd_status = 0x0;
3515 dcmd->sge_count = 1;
3516 dcmd->flags = MFI_FRAME_DIR_READ;
3517 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003518 dcmd->pad_0 = 0;
bo yang39a98552010-09-22 22:36:29 -04003519 instance->last_seq_num = seq_num;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003520 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3521 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3522 dcmd->mbox.w[0] = seq_num;
3523 dcmd->mbox.w[1] = curr_aen.word;
3524 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3525 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3526
Yang, Bof4c9a132009-10-06 14:43:28 -06003527 if (instance->aen_cmd != NULL) {
3528 megasas_return_cmd(instance, cmd);
3529 return 0;
3530 }
3531
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003532 /*
3533 * Store reference to the cmd used to register for AEN. When an
3534 * application wants us to register for AEN, we have to abort this
3535 * cmd and re-register with a new EVENT LOCALE supplied by that app
3536 */
3537 instance->aen_cmd = cmd;
3538
3539 /*
3540 * Issue the aen registration frame
3541 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003542 instance->instancet->fire_cmd(instance,
3543 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003544
3545 return 0;
3546}
3547
3548/**
3549 * megasas_start_aen - Subscribes to AEN during driver load time
3550 * @instance: Adapter soft state
3551 */
3552static int megasas_start_aen(struct megasas_instance *instance)
3553{
3554 struct megasas_evt_log_info eli;
3555 union megasas_evt_class_locale class_locale;
3556
3557 /*
3558 * Get the latest sequence number from FW
3559 */
3560 memset(&eli, 0, sizeof(eli));
3561
3562 if (megasas_get_seq_num(instance, &eli))
3563 return -1;
3564
3565 /*
3566 * Register AEN with FW for latest sequence number plus 1
3567 */
3568 class_locale.members.reserved = 0;
3569 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3570 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3571
3572 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3573 class_locale.word);
3574}
3575
3576/**
3577 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3578 * @instance: Adapter soft state
3579 */
3580static int megasas_io_attach(struct megasas_instance *instance)
3581{
3582 struct Scsi_Host *host = instance->host;
3583
3584 /*
3585 * Export parameters required by SCSI mid-layer
3586 */
3587 host->irq = instance->pdev->irq;
3588 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003589 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3590 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3591 host->can_queue =
3592 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3593 } else
3594 host->can_queue =
3595 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003596 host->this_id = instance->init_id;
3597 host->sg_tablesize = instance->max_num_sge;
Yang, Bo1fd10682010-10-12 07:18:50 -06003598 /*
3599 * Check if the module parameter value for max_sectors can be used
3600 */
3601 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3602 instance->max_sectors_per_req = max_sectors;
3603 else {
3604 if (max_sectors) {
3605 if (((instance->pdev->device ==
3606 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3607 (instance->pdev->device ==
3608 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3609 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3610 instance->max_sectors_per_req = max_sectors;
3611 } else {
3612 printk(KERN_INFO "megasas: max_sectors should be > 0"
3613 "and <= %d (or < 1MB for GEN2 controller)\n",
3614 instance->max_sectors_per_req);
3615 }
3616 }
3617 }
3618
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003619 host->max_sectors = instance->max_sectors_per_req;
3620 host->cmd_per_lun = 128;
3621 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3622 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3623 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08003624 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003625
3626 /*
3627 * Notify the mid-layer about the new controller
3628 */
3629 if (scsi_add_host(host, &instance->pdev->dev)) {
3630 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3631 return -ENODEV;
3632 }
3633
3634 /*
3635 * Trigger SCSI to scan our drives
3636 */
3637 scsi_scan_host(host);
3638 return 0;
3639}
3640
bo yang31ea7082007-11-07 12:09:50 -05003641static int
3642megasas_set_dma_mask(struct pci_dev *pdev)
3643{
3644 /*
3645 * All our contollers are capable of performing 64-bit DMA
3646 */
3647 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07003648 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05003649
Yang Hongyang284901a2009-04-06 19:01:15 -07003650 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003651 goto fail_set_dma_mask;
3652 }
3653 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003654 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05003655 goto fail_set_dma_mask;
3656 }
3657 return 0;
3658
3659fail_set_dma_mask:
3660 return 1;
3661}
3662
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003663/**
3664 * megasas_probe_one - PCI hotplug entry point
3665 * @pdev: PCI device structure
3666 * @id: PCI ids of supported hotplugged adapter
3667 */
3668static int __devinit
3669megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3670{
3671 int rval;
3672 struct Scsi_Host *host;
3673 struct megasas_instance *instance;
3674
3675 /*
3676 * Announce PCI information
3677 */
3678 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3679 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3680 pdev->subsystem_device);
3681
3682 printk("bus %d:slot %d:func %d\n",
3683 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3684
3685 /*
3686 * PCI prepping: enable device set bus mastering and dma mask
3687 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09003688 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003689
3690 if (rval) {
3691 return rval;
3692 }
3693
3694 pci_set_master(pdev);
3695
bo yang31ea7082007-11-07 12:09:50 -05003696 if (megasas_set_dma_mask(pdev))
3697 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003698
3699 host = scsi_host_alloc(&megasas_template,
3700 sizeof(struct megasas_instance));
3701
3702 if (!host) {
3703 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3704 goto fail_alloc_instance;
3705 }
3706
3707 instance = (struct megasas_instance *)host->hostdata;
3708 memset(instance, 0, sizeof(*instance));
bo yang39a98552010-09-22 22:36:29 -04003709 atomic_set( &instance->fw_reset_no_pci_access, 0 );
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003710
3711 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
3712 &instance->producer_h);
3713 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
3714 &instance->consumer_h);
3715
3716 if (!instance->producer || !instance->consumer) {
3717 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3718 "producer, consumer\n");
3719 goto fail_alloc_dma_buf;
3720 }
3721
3722 *instance->producer = 0;
3723 *instance->consumer = 0;
Yang, Boc3518832009-10-06 14:18:02 -06003724 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06003725 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003726 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04003727 instance->issuepend_done = 1;
3728 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3729 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003730
3731 instance->evt_detail = pci_alloc_consistent(pdev,
3732 sizeof(struct
3733 megasas_evt_detail),
3734 &instance->evt_detail_h);
3735
3736 if (!instance->evt_detail) {
3737 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3738 "event detail structure\n");
3739 goto fail_alloc_dma_buf;
3740 }
3741
3742 /*
3743 * Initialize locks and queues
3744 */
3745 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04003746 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003747
Sumant Patroe4a082c2006-05-30 12:03:37 -07003748 atomic_set(&instance->fw_outstanding,0);
3749
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003750 init_waitqueue_head(&instance->int_cmd_wait_q);
3751 init_waitqueue_head(&instance->abort_cmd_wait_q);
3752
3753 spin_lock_init(&instance->cmd_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04003754 spin_lock_init(&instance->hba_lock);
bo yang7343eb62007-11-09 04:35:44 -05003755 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06003756 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003757
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003758 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003759
3760 /*
3761 * Initialize PCI related and misc parameters
3762 */
3763 instance->pdev = pdev;
3764 instance->host = host;
3765 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3766 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3767
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003768 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3769 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06003770 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003771 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3772 } else
3773 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3774
Sumant Patro658dced2006-10-03 13:09:14 -07003775 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003776 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06003777 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003778 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04003779 instance->disableOnlineCtrlReset = 1;
3780
3781 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07003782
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003783 /*
3784 * Initialize MFI Firmware
3785 */
3786 if (megasas_init_mfi(instance))
3787 goto fail_init_mfi;
3788
3789 /*
3790 * Register IRQ
3791 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07003792 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003793 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3794 goto fail_irq;
3795 }
3796
Sumant Patro1341c932006-01-25 12:02:40 -08003797 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003798
3799 /*
3800 * Store instance in PCI softstate
3801 */
3802 pci_set_drvdata(pdev, instance);
3803
3804 /*
3805 * Add this controller to megasas_mgmt_info structure so that it
3806 * can be exported to management applications
3807 */
3808 megasas_mgmt_info.count++;
3809 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3810 megasas_mgmt_info.max_index++;
3811
3812 /*
3813 * Initiate AEN (Asynchronous Event Notification)
3814 */
3815 if (megasas_start_aen(instance)) {
3816 printk(KERN_DEBUG "megasas: start aen failed\n");
3817 goto fail_start_aen;
3818 }
3819
3820 /*
3821 * Register with SCSI mid-layer
3822 */
3823 if (megasas_io_attach(instance))
3824 goto fail_io_attach;
3825
Yang, Bo0c79e682009-10-06 14:47:35 -06003826 instance->unload = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003827 return 0;
3828
3829 fail_start_aen:
3830 fail_io_attach:
3831 megasas_mgmt_info.count--;
3832 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3833 megasas_mgmt_info.max_index--;
3834
3835 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07003836 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003837 free_irq(instance->pdev->irq, instance);
3838
3839 megasas_release_mfi(instance);
3840
3841 fail_irq:
3842 fail_init_mfi:
3843 fail_alloc_dma_buf:
3844 if (instance->evt_detail)
3845 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3846 instance->evt_detail,
3847 instance->evt_detail_h);
3848
3849 if (instance->producer)
3850 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3851 instance->producer_h);
3852 if (instance->consumer)
3853 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3854 instance->consumer_h);
3855 scsi_host_put(host);
3856
3857 fail_alloc_instance:
3858 fail_set_dma_mask:
3859 pci_disable_device(pdev);
3860
3861 return -ENODEV;
3862}
3863
3864/**
3865 * megasas_flush_cache - Requests FW to flush all its caches
3866 * @instance: Adapter soft state
3867 */
3868static void megasas_flush_cache(struct megasas_instance *instance)
3869{
3870 struct megasas_cmd *cmd;
3871 struct megasas_dcmd_frame *dcmd;
3872
bo yang39a98552010-09-22 22:36:29 -04003873 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3874 return;
3875
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003876 cmd = megasas_get_cmd(instance);
3877
3878 if (!cmd)
3879 return;
3880
3881 dcmd = &cmd->frame->dcmd;
3882
3883 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3884
3885 dcmd->cmd = MFI_CMD_DCMD;
3886 dcmd->cmd_status = 0x0;
3887 dcmd->sge_count = 0;
3888 dcmd->flags = MFI_FRAME_DIR_NONE;
3889 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003890 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003891 dcmd->data_xfer_len = 0;
3892 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3893 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3894
3895 megasas_issue_blocked_cmd(instance, cmd);
3896
3897 megasas_return_cmd(instance, cmd);
3898
3899 return;
3900}
3901
3902/**
3903 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3904 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05003905 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003906 */
bo yang31ea7082007-11-07 12:09:50 -05003907static void megasas_shutdown_controller(struct megasas_instance *instance,
3908 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003909{
3910 struct megasas_cmd *cmd;
3911 struct megasas_dcmd_frame *dcmd;
3912
bo yang39a98552010-09-22 22:36:29 -04003913 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3914 return;
3915
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003916 cmd = megasas_get_cmd(instance);
3917
3918 if (!cmd)
3919 return;
3920
3921 if (instance->aen_cmd)
3922 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3923
3924 dcmd = &cmd->frame->dcmd;
3925
3926 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3927
3928 dcmd->cmd = MFI_CMD_DCMD;
3929 dcmd->cmd_status = 0x0;
3930 dcmd->sge_count = 0;
3931 dcmd->flags = MFI_FRAME_DIR_NONE;
3932 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07003933 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003934 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05003935 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003936
3937 megasas_issue_blocked_cmd(instance, cmd);
3938
3939 megasas_return_cmd(instance, cmd);
3940
3941 return;
3942}
3943
Jiri Slaby33139b22008-05-01 17:56:02 +02003944#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003945/**
bo yangad84db22007-11-09 04:40:16 -05003946 * megasas_suspend - driver suspend entry point
3947 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05003948 * @state: PCI power state to suspend routine
3949 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003950static int
bo yang31ea7082007-11-07 12:09:50 -05003951megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3952{
3953 struct Scsi_Host *host;
3954 struct megasas_instance *instance;
3955
3956 instance = pci_get_drvdata(pdev);
3957 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06003958 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05003959
bo yangad84db22007-11-09 04:40:16 -05003960 if (poll_mode_io)
3961 del_timer_sync(&instance->io_completion_timer);
3962
bo yang31ea7082007-11-07 12:09:50 -05003963 megasas_flush_cache(instance);
3964 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003965
3966 /* cancel the delayed work if this work still in queue */
3967 if (instance->ev != NULL) {
3968 struct megasas_aen_event *ev = instance->ev;
3969 cancel_delayed_work(
3970 (struct delayed_work *)&ev->hotplug_work);
3971 flush_scheduled_work();
3972 instance->ev = NULL;
3973 }
3974
bo yang31ea7082007-11-07 12:09:50 -05003975 tasklet_kill(&instance->isr_tasklet);
3976
3977 pci_set_drvdata(instance->pdev, instance);
3978 instance->instancet->disable_intr(instance->reg_set);
3979 free_irq(instance->pdev->irq, instance);
3980
3981 pci_save_state(pdev);
3982 pci_disable_device(pdev);
3983
3984 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3985
3986 return 0;
3987}
3988
3989/**
3990 * megasas_resume- driver resume entry point
3991 * @pdev: PCI device structure
3992 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003993static int
bo yang31ea7082007-11-07 12:09:50 -05003994megasas_resume(struct pci_dev *pdev)
3995{
3996 int rval;
3997 struct Scsi_Host *host;
3998 struct megasas_instance *instance;
3999
4000 instance = pci_get_drvdata(pdev);
4001 host = instance->host;
4002 pci_set_power_state(pdev, PCI_D0);
4003 pci_enable_wake(pdev, PCI_D0, 0);
4004 pci_restore_state(pdev);
4005
4006 /*
4007 * PCI prepping: enable device set bus mastering and dma mask
4008 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09004009 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05004010
4011 if (rval) {
4012 printk(KERN_ERR "megasas: Enable device failed\n");
4013 return rval;
4014 }
4015
4016 pci_set_master(pdev);
4017
4018 if (megasas_set_dma_mask(pdev))
4019 goto fail_set_dma_mask;
4020
4021 /*
4022 * Initialize MFI Firmware
4023 */
4024
4025 *instance->producer = 0;
4026 *instance->consumer = 0;
4027
4028 atomic_set(&instance->fw_outstanding, 0);
4029
4030 /*
4031 * We expect the FW state to be READY
4032 */
4033 if (megasas_transition_to_ready(instance))
4034 goto fail_ready_state;
4035
4036 if (megasas_issue_init_mfi(instance))
4037 goto fail_init_mfi;
4038
4039 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
4040 (unsigned long)instance);
4041
4042 /*
4043 * Register IRQ
4044 */
4045 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
4046 "megasas", instance)) {
4047 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4048 goto fail_irq;
4049 }
4050
4051 instance->instancet->enable_intr(instance->reg_set);
4052
4053 /*
4054 * Initiate AEN (Asynchronous Event Notification)
4055 */
4056 if (megasas_start_aen(instance))
4057 printk(KERN_ERR "megasas: Start AEN failed\n");
4058
bo yangad84db22007-11-09 04:40:16 -05004059 /* Initialize the cmd completion timer */
4060 if (poll_mode_io)
4061 megasas_start_timer(instance, &instance->io_completion_timer,
4062 megasas_io_completion_timer,
4063 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06004064 instance->unload = 0;
4065
bo yang31ea7082007-11-07 12:09:50 -05004066 return 0;
4067
4068fail_irq:
4069fail_init_mfi:
4070 if (instance->evt_detail)
4071 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4072 instance->evt_detail,
4073 instance->evt_detail_h);
4074
4075 if (instance->producer)
4076 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4077 instance->producer_h);
4078 if (instance->consumer)
4079 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4080 instance->consumer_h);
4081 scsi_host_put(host);
4082
4083fail_set_dma_mask:
4084fail_ready_state:
4085
4086 pci_disable_device(pdev);
4087
4088 return -ENODEV;
4089}
Jiri Slaby33139b22008-05-01 17:56:02 +02004090#else
4091#define megasas_suspend NULL
4092#define megasas_resume NULL
4093#endif
bo yang31ea7082007-11-07 12:09:50 -05004094
4095/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004096 * megasas_detach_one - PCI hot"un"plug entry point
4097 * @pdev: PCI device structure
4098 */
Jiri Slaby33139b22008-05-01 17:56:02 +02004099static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004100{
4101 int i;
4102 struct Scsi_Host *host;
4103 struct megasas_instance *instance;
4104
4105 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06004106 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004107 host = instance->host;
4108
bo yangad84db22007-11-09 04:40:16 -05004109 if (poll_mode_io)
4110 del_timer_sync(&instance->io_completion_timer);
4111
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004112 scsi_remove_host(instance->host);
4113 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05004114 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004115
4116 /* cancel the delayed work if this work still in queue*/
4117 if (instance->ev != NULL) {
4118 struct megasas_aen_event *ev = instance->ev;
4119 cancel_delayed_work(
4120 (struct delayed_work *)&ev->hotplug_work);
4121 flush_scheduled_work();
4122 instance->ev = NULL;
4123 }
4124
Sumant Patro5d018ad2006-10-03 13:13:18 -07004125 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004126
4127 /*
4128 * Take the instance off the instance array. Note that we will not
4129 * decrement the max_index. We let this array be sparse array
4130 */
4131 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4132 if (megasas_mgmt_info.instance[i] == instance) {
4133 megasas_mgmt_info.count--;
4134 megasas_mgmt_info.instance[i] = NULL;
4135
4136 break;
4137 }
4138 }
4139
4140 pci_set_drvdata(instance->pdev, NULL);
4141
Sumant Patrob274cab2006-10-03 12:52:12 -07004142 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004143
4144 free_irq(instance->pdev->irq, instance);
4145
4146 megasas_release_mfi(instance);
4147
4148 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4149 instance->evt_detail, instance->evt_detail_h);
4150
4151 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4152 instance->producer_h);
4153
4154 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4155 instance->consumer_h);
4156
4157 scsi_host_put(host);
4158
4159 pci_set_drvdata(pdev, NULL);
4160
4161 pci_disable_device(pdev);
4162
4163 return;
4164}
4165
4166/**
4167 * megasas_shutdown - Shutdown entry point
4168 * @device: Generic device structure
4169 */
4170static void megasas_shutdown(struct pci_dev *pdev)
4171{
4172 struct megasas_instance *instance = pci_get_drvdata(pdev);
Yang, Bo0c79e682009-10-06 14:47:35 -06004173 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004174 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07004175 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004176}
4177
4178/**
4179 * megasas_mgmt_open - char node "open" entry point
4180 */
4181static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4182{
4183 /*
4184 * Allow only those users with admin rights
4185 */
4186 if (!capable(CAP_SYS_ADMIN))
4187 return -EACCES;
4188
4189 return 0;
4190}
4191
4192/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004193 * megasas_mgmt_fasync - Async notifier registration from applications
4194 *
4195 * This function adds the calling process to a driver global queue. When an
4196 * event occurs, SIGIO will be sent to all processes in this queue.
4197 */
4198static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4199{
4200 int rc;
4201
Arjan van de Ven0b950672006-01-11 13:16:10 +01004202 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004203
4204 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4205
Arjan van de Ven0b950672006-01-11 13:16:10 +01004206 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004207
4208 if (rc >= 0) {
4209 /* For sanity check when we get ioctl */
4210 filep->private_data = filep;
4211 return 0;
4212 }
4213
4214 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4215
4216 return rc;
4217}
4218
4219/**
Yang, Boc3518832009-10-06 14:18:02 -06004220 * megasas_mgmt_poll - char node "poll" entry point
4221 * */
4222static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4223{
4224 unsigned int mask;
4225 unsigned long flags;
4226 poll_wait(file, &megasas_poll_wait, wait);
4227 spin_lock_irqsave(&poll_aen_lock, flags);
4228 if (megasas_poll_wait_aen)
4229 mask = (POLLIN | POLLRDNORM);
4230 else
4231 mask = 0;
4232 spin_unlock_irqrestore(&poll_aen_lock, flags);
4233 return mask;
4234}
4235
4236/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004237 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4238 * @instance: Adapter soft state
4239 * @argp: User's ioctl packet
4240 */
4241static int
4242megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4243 struct megasas_iocpacket __user * user_ioc,
4244 struct megasas_iocpacket *ioc)
4245{
4246 struct megasas_sge32 *kern_sge32;
4247 struct megasas_cmd *cmd;
4248 void *kbuff_arr[MAX_IOCTL_SGE];
4249 dma_addr_t buf_handle = 0;
4250 int error = 0, i;
4251 void *sense = NULL;
4252 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06004253 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004254
4255 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4256
4257 if (ioc->sge_count > MAX_IOCTL_SGE) {
4258 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4259 ioc->sge_count, MAX_IOCTL_SGE);
4260 return -EINVAL;
4261 }
4262
4263 cmd = megasas_get_cmd(instance);
4264 if (!cmd) {
4265 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4266 return -ENOMEM;
4267 }
4268
4269 /*
4270 * User's IOCTL packet has 2 frames (maximum). Copy those two
4271 * frames into our cmd's frames. cmd->frame's context will get
4272 * overwritten when we copy from user's frames. So set that value
4273 * alone separately
4274 */
4275 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4276 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06004277 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004278
4279 /*
4280 * The management interface between applications and the fw uses
4281 * MFI frames. E.g, RAID configuration changes, LD property changes
4282 * etc are accomplishes through different kinds of MFI frames. The
4283 * driver needs to care only about substituting user buffers with
4284 * kernel buffers in SGLs. The location of SGL is embedded in the
4285 * struct iocpacket itself.
4286 */
4287 kern_sge32 = (struct megasas_sge32 *)
4288 ((unsigned long)cmd->frame + ioc->sgl_off);
4289
4290 /*
4291 * For each user buffer, create a mirror buffer and copy in
4292 */
4293 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004294 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004295 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08004296 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004297 if (!kbuff_arr[i]) {
4298 printk(KERN_DEBUG "megasas: Failed to alloc "
4299 "kernel SGL buffer for IOCTL \n");
4300 error = -ENOMEM;
4301 goto out;
4302 }
4303
4304 /*
4305 * We don't change the dma_coherent_mask, so
4306 * pci_alloc_consistent only returns 32bit addresses
4307 */
4308 kern_sge32[i].phys_addr = (u32) buf_handle;
4309 kern_sge32[i].length = ioc->sgl[i].iov_len;
4310
4311 /*
4312 * We created a kernel buffer corresponding to the
4313 * user buffer. Now copy in from the user buffer
4314 */
4315 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4316 (u32) (ioc->sgl[i].iov_len))) {
4317 error = -EFAULT;
4318 goto out;
4319 }
4320 }
4321
4322 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004323 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4324 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004325 if (!sense) {
4326 error = -ENOMEM;
4327 goto out;
4328 }
4329
4330 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06004331 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004332 *sense_ptr = sense_handle;
4333 }
4334
4335 /*
4336 * Set the sync_cmd flag so that the ISR knows not to complete this
4337 * cmd to the SCSI mid-layer
4338 */
4339 cmd->sync_cmd = 1;
4340 megasas_issue_blocked_cmd(instance, cmd);
4341 cmd->sync_cmd = 0;
4342
4343 /*
4344 * copy out the kernel buffers to user buffers
4345 */
4346 for (i = 0; i < ioc->sge_count; i++) {
4347 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4348 ioc->sgl[i].iov_len)) {
4349 error = -EFAULT;
4350 goto out;
4351 }
4352 }
4353
4354 /*
4355 * copy out the sense
4356 */
4357 if (ioc->sense_len) {
4358 /*
bo yangb70a41e2008-03-18 03:13:06 -04004359 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004360 * sense buffer address
4361 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06004362 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4363 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004364
bo yangb70a41e2008-03-18 03:13:06 -04004365 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4366 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05004367 printk(KERN_ERR "megasas: Failed to copy out to user "
4368 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004369 error = -EFAULT;
4370 goto out;
4371 }
4372 }
4373
4374 /*
4375 * copy the status codes returned by the fw
4376 */
4377 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4378 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4379 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4380 error = -EFAULT;
4381 }
4382
4383 out:
4384 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004385 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004386 sense, sense_handle);
4387 }
4388
4389 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08004390 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004391 kern_sge32[i].length,
4392 kbuff_arr[i], kern_sge32[i].phys_addr);
4393 }
4394
4395 megasas_return_cmd(instance, cmd);
4396 return error;
4397}
4398
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004399static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4400{
4401 struct megasas_iocpacket __user *user_ioc =
4402 (struct megasas_iocpacket __user *)arg;
4403 struct megasas_iocpacket *ioc;
4404 struct megasas_instance *instance;
4405 int error;
bo yang39a98552010-09-22 22:36:29 -04004406 int i;
4407 unsigned long flags;
4408 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004409
4410 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4411 if (!ioc)
4412 return -ENOMEM;
4413
4414 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4415 error = -EFAULT;
4416 goto out_kfree_ioc;
4417 }
4418
4419 instance = megasas_lookup_instance(ioc->host_no);
4420 if (!instance) {
4421 error = -ENODEV;
4422 goto out_kfree_ioc;
4423 }
4424
bo yang39a98552010-09-22 22:36:29 -04004425 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4426 printk(KERN_ERR "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06004427 error = -ENODEV;
4428 goto out_kfree_ioc;
4429 }
4430
4431 if (instance->unload == 1) {
4432 error = -ENODEV;
4433 goto out_kfree_ioc;
4434 }
4435
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004436 /*
4437 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4438 */
4439 if (down_interruptible(&instance->ioctl_sem)) {
4440 error = -ERESTARTSYS;
4441 goto out_kfree_ioc;
4442 }
bo yang39a98552010-09-22 22:36:29 -04004443
4444 for (i = 0; i < wait_time; i++) {
4445
4446 spin_lock_irqsave(&instance->hba_lock, flags);
4447 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4448 spin_unlock_irqrestore(&instance->hba_lock, flags);
4449 break;
4450 }
4451 spin_unlock_irqrestore(&instance->hba_lock, flags);
4452
4453 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4454 printk(KERN_NOTICE "megasas: waiting"
4455 "for controller reset to finish\n");
4456 }
4457
4458 msleep(1000);
4459 }
4460
4461 spin_lock_irqsave(&instance->hba_lock, flags);
4462 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4463 spin_unlock_irqrestore(&instance->hba_lock, flags);
4464
4465 printk(KERN_ERR "megaraid_sas: timed out while"
4466 "waiting for HBA to recover\n");
4467 error = -ENODEV;
4468 goto out_kfree_ioc;
4469 }
4470 spin_unlock_irqrestore(&instance->hba_lock, flags);
4471
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004472 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4473 up(&instance->ioctl_sem);
4474
4475 out_kfree_ioc:
4476 kfree(ioc);
4477 return error;
4478}
4479
4480static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4481{
4482 struct megasas_instance *instance;
4483 struct megasas_aen aen;
4484 int error;
bo yang39a98552010-09-22 22:36:29 -04004485 int i;
4486 unsigned long flags;
4487 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004488
4489 if (file->private_data != file) {
4490 printk(KERN_DEBUG "megasas: fasync_helper was not "
4491 "called first\n");
4492 return -EINVAL;
4493 }
4494
4495 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4496 return -EFAULT;
4497
4498 instance = megasas_lookup_instance(aen.host_no);
4499
4500 if (!instance)
4501 return -ENODEV;
4502
bo yang39a98552010-09-22 22:36:29 -04004503 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4504 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06004505 }
4506
4507 if (instance->unload == 1) {
4508 return -ENODEV;
4509 }
4510
bo yang39a98552010-09-22 22:36:29 -04004511 for (i = 0; i < wait_time; i++) {
4512
4513 spin_lock_irqsave(&instance->hba_lock, flags);
4514 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4515 spin_unlock_irqrestore(&instance->hba_lock,
4516 flags);
4517 break;
4518 }
4519
4520 spin_unlock_irqrestore(&instance->hba_lock, flags);
4521
4522 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4523 printk(KERN_NOTICE "megasas: waiting for"
4524 "controller reset to finish\n");
4525 }
4526
4527 msleep(1000);
4528 }
4529
4530 spin_lock_irqsave(&instance->hba_lock, flags);
4531 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4532 spin_unlock_irqrestore(&instance->hba_lock, flags);
4533 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4534 "for HBA to recover.\n");
4535 return -ENODEV;
4536 }
4537 spin_unlock_irqrestore(&instance->hba_lock, flags);
4538
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004539 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004540 error = megasas_register_aen(instance, aen.seq_num,
4541 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02004542 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004543 return error;
4544}
4545
4546/**
4547 * megasas_mgmt_ioctl - char node ioctl entry point
4548 */
4549static long
4550megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4551{
4552 switch (cmd) {
4553 case MEGASAS_IOC_FIRMWARE:
4554 return megasas_mgmt_ioctl_fw(file, arg);
4555
4556 case MEGASAS_IOC_GET_AEN:
4557 return megasas_mgmt_ioctl_aen(file, arg);
4558 }
4559
4560 return -ENOTTY;
4561}
4562
4563#ifdef CONFIG_COMPAT
4564static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4565{
4566 struct compat_megasas_iocpacket __user *cioc =
4567 (struct compat_megasas_iocpacket __user *)arg;
4568 struct megasas_iocpacket __user *ioc =
4569 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4570 int i;
4571 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004572 compat_uptr_t ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004573
Jeff Garzik83aabc12006-10-04 06:34:03 -04004574 if (clear_user(ioc, sizeof(*ioc)))
4575 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004576
4577 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4578 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4579 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4580 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4581 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4582 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4583 return -EFAULT;
4584
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004585 /*
4586 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4587 * sense_len is not null, so prepare the 64bit value under
4588 * the same condition.
4589 */
4590 if (ioc->sense_len) {
4591 void __user **sense_ioc_ptr =
4592 (void __user **)(ioc->frame.raw + ioc->sense_off);
4593 compat_uptr_t *sense_cioc_ptr =
4594 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4595 if (get_user(ptr, sense_cioc_ptr) ||
4596 put_user(compat_ptr(ptr), sense_ioc_ptr))
4597 return -EFAULT;
4598 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004599
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01004600 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004601 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4602 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4603 copy_in_user(&ioc->sgl[i].iov_len,
4604 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4605 return -EFAULT;
4606 }
4607
4608 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4609
4610 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4611 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4612 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4613 return -EFAULT;
4614 }
4615 return error;
4616}
4617
4618static long
4619megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4620 unsigned long arg)
4621{
4622 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08004623 case MEGASAS_IOC_FIRMWARE32:
4624 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004625 case MEGASAS_IOC_GET_AEN:
4626 return megasas_mgmt_ioctl_aen(file, arg);
4627 }
4628
4629 return -ENOTTY;
4630}
4631#endif
4632
4633/*
4634 * File operations structure for management interface
4635 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08004636static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004637 .owner = THIS_MODULE,
4638 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004639 .fasync = megasas_mgmt_fasync,
4640 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06004641 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004642#ifdef CONFIG_COMPAT
4643 .compat_ioctl = megasas_mgmt_compat_ioctl,
4644#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02004645 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004646};
4647
4648/*
4649 * PCI hotplug support registration structure
4650 */
4651static struct pci_driver megasas_pci_driver = {
4652
4653 .name = "megaraid_sas",
4654 .id_table = megasas_pci_table,
4655 .probe = megasas_probe_one,
4656 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05004657 .suspend = megasas_suspend,
4658 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004659 .shutdown = megasas_shutdown,
4660};
4661
4662/*
4663 * Sysfs driver attributes
4664 */
4665static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
4666{
4667 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
4668 MEGASAS_VERSION);
4669}
4670
4671static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
4672
4673static ssize_t
4674megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
4675{
4676 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
4677 MEGASAS_RELDATE);
4678}
4679
4680static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
4681 NULL);
4682
Sumant Patro658dced2006-10-03 13:09:14 -07004683static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06004684megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
4685{
4686 return sprintf(buf, "%u\n", support_poll_for_event);
4687}
4688
4689static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
4690 megasas_sysfs_show_support_poll_for_event, NULL);
4691
Yang, Bo837f5fe2010-10-11 06:59:20 -06004692 static ssize_t
4693megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
4694{
4695 return sprintf(buf, "%u\n", support_device_change);
4696}
4697
4698static DRIVER_ATTR(support_device_change, S_IRUGO,
4699 megasas_sysfs_show_support_device_change, NULL);
4700
Yang, Bo72c4fd32009-10-06 14:20:59 -06004701static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07004702megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
4703{
bo yangad84db22007-11-09 04:40:16 -05004704 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07004705}
4706
4707static ssize_t
4708megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
4709{
4710 int retval = count;
4711 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
4712 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
4713 retval = -EINVAL;
4714 }
4715 return retval;
4716}
4717
Joe Malicki66dca9b2008-08-14 17:14:48 -04004718static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05004719 megasas_sysfs_set_dbg_lvl);
4720
4721static ssize_t
4722megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
4723{
4724 return sprintf(buf, "%u\n", poll_mode_io);
4725}
4726
4727static ssize_t
4728megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
4729 const char *buf, size_t count)
4730{
4731 int retval = count;
4732 int tmp = poll_mode_io;
4733 int i;
4734 struct megasas_instance *instance;
4735
4736 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
4737 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
4738 retval = -EINVAL;
4739 }
4740
4741 /*
4742 * Check if poll_mode_io is already set or is same as previous value
4743 */
4744 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
4745 goto out;
4746
4747 if (poll_mode_io) {
4748 /*
4749 * Start timers for all adapters
4750 */
4751 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4752 instance = megasas_mgmt_info.instance[i];
4753 if (instance) {
4754 megasas_start_timer(instance,
4755 &instance->io_completion_timer,
4756 megasas_io_completion_timer,
4757 MEGASAS_COMPLETION_TIMER_INTERVAL);
4758 }
4759 }
4760 } else {
4761 /*
4762 * Delete timers for all adapters
4763 */
4764 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4765 instance = megasas_mgmt_info.instance[i];
4766 if (instance)
4767 del_timer_sync(&instance->io_completion_timer);
4768 }
4769 }
4770
4771out:
4772 return retval;
4773}
4774
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004775static void
4776megasas_aen_polling(struct work_struct *work)
4777{
4778 struct megasas_aen_event *ev =
4779 container_of(work, struct megasas_aen_event, hotplug_work);
4780 struct megasas_instance *instance = ev->instance;
4781 union megasas_evt_class_locale class_locale;
4782 struct Scsi_Host *host;
4783 struct scsi_device *sdev1;
4784 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07004785 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004786 int i, j, doscan = 0;
4787 u32 seq_num;
4788 int error;
4789
4790 if (!instance) {
4791 printk(KERN_ERR "invalid instance!\n");
4792 kfree(ev);
4793 return;
4794 }
4795 instance->ev = NULL;
4796 host = instance->host;
4797 if (instance->evt_detail) {
4798
4799 switch (instance->evt_detail->code) {
4800 case MR_EVT_PD_INSERTED:
Yang, Boc9786842009-12-06 08:39:25 -07004801 if (megasas_get_pd_list(instance) == 0) {
4802 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4803 for (j = 0;
4804 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4805 j++) {
4806
4807 pd_index =
4808 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4809
4810 sdev1 =
4811 scsi_device_lookup(host, i, j, 0);
4812
4813 if (instance->pd_list[pd_index].driveState
4814 == MR_PD_STATE_SYSTEM) {
4815 if (!sdev1) {
4816 scsi_add_device(host, i, j, 0);
4817 }
4818
4819 if (sdev1)
4820 scsi_device_put(sdev1);
4821 }
4822 }
4823 }
4824 }
4825 doscan = 0;
4826 break;
4827
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004828 case MR_EVT_PD_REMOVED:
Yang, Boc9786842009-12-06 08:39:25 -07004829 if (megasas_get_pd_list(instance) == 0) {
4830 megasas_get_pd_list(instance);
4831 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4832 for (j = 0;
4833 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4834 j++) {
4835
4836 pd_index =
4837 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4838
4839 sdev1 =
4840 scsi_device_lookup(host, i, j, 0);
4841
4842 if (instance->pd_list[pd_index].driveState
4843 == MR_PD_STATE_SYSTEM) {
4844 if (sdev1) {
4845 scsi_device_put(sdev1);
4846 }
4847 } else {
4848 if (sdev1) {
4849 scsi_remove_device(sdev1);
4850 scsi_device_put(sdev1);
4851 }
4852 }
4853 }
4854 }
4855 }
4856 doscan = 0;
4857 break;
4858
4859 case MR_EVT_LD_OFFLINE:
4860 case MR_EVT_LD_DELETED:
4861 megasas_get_ld_list(instance);
4862 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4863 for (j = 0;
4864 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4865 j++) {
4866
4867 ld_index =
4868 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4869
4870 sdev1 = scsi_device_lookup(host,
4871 i + MEGASAS_MAX_LD_CHANNELS,
4872 j,
4873 0);
4874
4875 if (instance->ld_ids[ld_index] != 0xff) {
4876 if (sdev1) {
4877 scsi_device_put(sdev1);
4878 }
4879 } else {
4880 if (sdev1) {
4881 scsi_remove_device(sdev1);
4882 scsi_device_put(sdev1);
4883 }
4884 }
4885 }
4886 }
4887 doscan = 0;
4888 break;
4889 case MR_EVT_LD_CREATED:
4890 megasas_get_ld_list(instance);
4891 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4892 for (j = 0;
4893 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4894 j++) {
4895 ld_index =
4896 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4897
4898 sdev1 = scsi_device_lookup(host,
4899 i+MEGASAS_MAX_LD_CHANNELS,
4900 j, 0);
4901
4902 if (instance->ld_ids[ld_index] !=
4903 0xff) {
4904 if (!sdev1) {
4905 scsi_add_device(host,
4906 i + 2,
4907 j, 0);
4908 }
4909 }
4910 if (sdev1) {
4911 scsi_device_put(sdev1);
4912 }
4913 }
4914 }
4915 doscan = 0;
4916 break;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004917 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
Yang, Boc9786842009-12-06 08:39:25 -07004918 case MR_EVT_FOREIGN_CFG_IMPORTED:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004919 doscan = 1;
4920 break;
4921 default:
4922 doscan = 0;
4923 break;
4924 }
4925 } else {
4926 printk(KERN_ERR "invalid evt_detail!\n");
4927 kfree(ev);
4928 return;
4929 }
4930
4931 if (doscan) {
4932 printk(KERN_INFO "scanning ...\n");
4933 megasas_get_pd_list(instance);
4934 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4935 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
4936 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
4937 sdev1 = scsi_device_lookup(host, i, j, 0);
4938 if (instance->pd_list[pd_index].driveState ==
4939 MR_PD_STATE_SYSTEM) {
4940 if (!sdev1) {
4941 scsi_add_device(host, i, j, 0);
4942 }
4943 if (sdev1)
4944 scsi_device_put(sdev1);
4945 } else {
4946 if (sdev1) {
4947 scsi_remove_device(sdev1);
4948 scsi_device_put(sdev1);
4949 }
4950 }
4951 }
4952 }
Yang, Boc9786842009-12-06 08:39:25 -07004953
4954 megasas_get_ld_list(instance);
4955 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4956 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
4957 ld_index =
4958 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4959
4960 sdev1 = scsi_device_lookup(host,
4961 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
4962 if (instance->ld_ids[ld_index] != 0xff) {
4963 if (!sdev1) {
4964 scsi_add_device(host,
4965 i+2,
4966 j, 0);
4967 } else {
4968 scsi_device_put(sdev1);
4969 }
4970 } else {
4971 if (sdev1) {
4972 scsi_remove_device(sdev1);
4973 scsi_device_put(sdev1);
4974 }
4975 }
4976 }
4977 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004978 }
4979
4980 if ( instance->aen_cmd != NULL ) {
4981 kfree(ev);
4982 return ;
4983 }
4984
4985 seq_num = instance->evt_detail->seq_num + 1;
4986
4987 /* Register AEN with FW for latest sequence number plus 1 */
4988 class_locale.members.reserved = 0;
4989 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4990 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4991 mutex_lock(&instance->aen_mutex);
4992 error = megasas_register_aen(instance, seq_num,
4993 class_locale.word);
4994 mutex_unlock(&instance->aen_mutex);
4995
4996 if (error)
4997 printk(KERN_ERR "register aen failed error %x\n", error);
4998
4999 kfree(ev);
5000}
5001
5002
Bryn M. Reevesbb7d3f22009-11-12 18:31:54 +00005003static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
bo yangad84db22007-11-09 04:40:16 -05005004 megasas_sysfs_show_poll_mode_io,
5005 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07005006
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005007/**
5008 * megasas_init - Driver load entry point
5009 */
5010static int __init megasas_init(void)
5011{
5012 int rval;
5013
5014 /*
5015 * Announce driver version and other information
5016 */
5017 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5018 MEGASAS_EXT_VERSION);
5019
Yang, Bo72c4fd32009-10-06 14:20:59 -06005020 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06005021 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005022
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005023 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5024
5025 /*
5026 * Register character device node
5027 */
5028 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5029
5030 if (rval < 0) {
5031 printk(KERN_DEBUG "megasas: failed to open device node\n");
5032 return rval;
5033 }
5034
5035 megasas_mgmt_majorno = rval;
5036
5037 /*
5038 * Register ourselves as PCI hotplug module
5039 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00005040 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005041
5042 if (rval) {
5043 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04005044 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005045 }
5046
Jeff Garzik83aabc12006-10-04 06:34:03 -04005047 rval = driver_create_file(&megasas_pci_driver.driver,
5048 &driver_attr_version);
5049 if (rval)
5050 goto err_dcf_attr_ver;
5051 rval = driver_create_file(&megasas_pci_driver.driver,
5052 &driver_attr_release_date);
5053 if (rval)
5054 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06005055
5056 rval = driver_create_file(&megasas_pci_driver.driver,
5057 &driver_attr_support_poll_for_event);
5058 if (rval)
5059 goto err_dcf_support_poll_for_event;
5060
Jeff Garzik83aabc12006-10-04 06:34:03 -04005061 rval = driver_create_file(&megasas_pci_driver.driver,
5062 &driver_attr_dbg_lvl);
5063 if (rval)
5064 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05005065 rval = driver_create_file(&megasas_pci_driver.driver,
5066 &driver_attr_poll_mode_io);
5067 if (rval)
5068 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005069
Yang, Bo837f5fe2010-10-11 06:59:20 -06005070 rval = driver_create_file(&megasas_pci_driver.driver,
5071 &driver_attr_support_device_change);
5072 if (rval)
5073 goto err_dcf_support_device_change;
5074
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005075 return rval;
bo yangad84db22007-11-09 04:40:16 -05005076
Yang, Bo837f5fe2010-10-11 06:59:20 -06005077err_dcf_support_device_change:
5078 driver_remove_file(&megasas_pci_driver.driver,
5079 &driver_attr_poll_mode_io);
5080
bo yangad84db22007-11-09 04:40:16 -05005081err_dcf_poll_mode_io:
5082 driver_remove_file(&megasas_pci_driver.driver,
5083 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005084err_dcf_dbg_lvl:
5085 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06005086 &driver_attr_support_poll_for_event);
5087
5088err_dcf_support_poll_for_event:
5089 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005090 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06005091
Jeff Garzik83aabc12006-10-04 06:34:03 -04005092err_dcf_rel_date:
5093 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5094err_dcf_attr_ver:
5095 pci_unregister_driver(&megasas_pci_driver);
5096err_pcidrv:
5097 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5098 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005099}
5100
5101/**
5102 * megasas_exit - Driver unload entry point
5103 */
5104static void __exit megasas_exit(void)
5105{
Sumant Patro658dced2006-10-03 13:09:14 -07005106 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05005107 &driver_attr_poll_mode_io);
5108 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07005109 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04005110 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06005111 &driver_attr_support_poll_for_event);
5112 driver_remove_file(&megasas_pci_driver.driver,
5113 &driver_attr_support_device_change);
5114 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04005115 &driver_attr_release_date);
5116 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005117
5118 pci_unregister_driver(&megasas_pci_driver);
5119 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5120}
5121
5122module_init(megasas_init);
5123module_exit(megasas_exit);