[SCSI] megaraid_sas: Add support for Uneven Span PRL11
[deliverable/linux.git] / drivers / scsi / megaraid / megaraid_sas_base.c
CommitLineData
c4a3e0a5 1/*
3f1530c1 2 * Linux MegaRAID driver for SAS based RAID controllers
c4a3e0a5 3 *
ae59057b 4 * Copyright (c) 2003-2012 LSI Corporation.
c4a3e0a5 5 *
3f1530c1 6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
c4a3e0a5 10 *
3f1530c1 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
c4a3e0a5 15 *
3f1530c1 16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c4a3e0a5 19 *
3f1530c1 20 * FILE: megaraid_sas_base.c
5eca4a67 21 * Version : v06.506.00.00-rc1
c4a3e0a5 22 *
3f1530c1 23 * Authors: LSI Corporation
24 * Sreenivas Bagalkote
25 * Sumant Patro
26 * Bo Yang
00fa2b19 27 * Adam Radford <linuxraid@lsi.com>
c4a3e0a5 28 *
3f1530c1 29 * Send feedback to: <megaraidlinux@lsi.com>
30 *
31 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
32 * ATTN: Linuxraid
c4a3e0a5
BS
33 */
34
35#include <linux/kernel.h>
36#include <linux/types.h>
37#include <linux/pci.h>
38#include <linux/list.h>
c4a3e0a5
BS
39#include <linux/moduleparam.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/interrupt.h>
43#include <linux/delay.h>
44#include <linux/uio.h>
5a0e3ad6 45#include <linux/slab.h>
c4a3e0a5 46#include <asm/uaccess.h>
43399236 47#include <linux/fs.h>
c4a3e0a5 48#include <linux/compat.h>
cf62a0a5 49#include <linux/blkdev.h>
0b950672 50#include <linux/mutex.h>
c3518837 51#include <linux/poll.h>
c4a3e0a5
BS
52
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
4bcde509 57#include <scsi/scsi_tcq.h>
9c915a8c 58#include "megaraid_sas_fusion.h"
c4a3e0a5
BS
59#include "megaraid_sas.h"
60
1fd10685
YB
61/*
62 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
64 */
65static unsigned int max_sectors;
66module_param_named(max_sectors, max_sectors, int, 0);
67MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
69
80d9da98 70static int msix_disable;
71module_param(msix_disable, int, S_IRUGO);
72MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73
079eaddf 74static unsigned int msix_vectors;
75module_param(msix_vectors, int, S_IRUGO);
76MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77
c5daa6a9 78static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
79module_param(throttlequeuedepth, int, S_IRUGO);
80MODULE_PARM_DESC(throttlequeuedepth,
81 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
82
c007b8b2 83int resetwaittime = MEGASAS_RESET_WAIT_TIME;
84module_param(resetwaittime, int, S_IRUGO);
85MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
86 "before resetting adapter. Default: 180");
87
c4a3e0a5
BS
88MODULE_LICENSE("GPL");
89MODULE_VERSION(MEGASAS_VERSION);
3d6d174a 90MODULE_AUTHOR("megaraidlinux@lsi.com");
f28cd7cf 91MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
c4a3e0a5 92
058a8fac 93int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
39a98554 94static int megasas_get_pd_list(struct megasas_instance *instance);
95static int megasas_issue_init_mfi(struct megasas_instance *instance);
96static int megasas_register_aen(struct megasas_instance *instance,
97 u32 seq_num, u32 class_locale_word);
c4a3e0a5
BS
98/*
99 * PCI ID table for all supported controllers
100 */
101static struct pci_device_id megasas_pci_table[] = {
102
f3d7271c
HK
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
104 /* xscale IOP */
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
106 /* ppc IOP */
af7a5647 107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
108 /* ppc IOP */
6610a6b3
YB
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
110 /* gen2*/
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
112 /* gen2*/
87911122
YB
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
114 /* skinny*/
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
116 /* skinny*/
f3d7271c
HK
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
118 /* xscale IOP, vega */
119 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
120 /* xscale IOP */
9c915a8c 121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
122 /* Fusion */
36807e67 123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
124 /* Invader */
21d3c710
SS
125 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
126 /* Fury */
f3d7271c 127 {}
c4a3e0a5
BS
128};
129
130MODULE_DEVICE_TABLE(pci, megasas_pci_table);
131
132static int megasas_mgmt_majorno;
133static struct megasas_mgmt_info megasas_mgmt_info;
134static struct fasync_struct *megasas_async_queue;
0b950672 135static DEFINE_MUTEX(megasas_async_queue_mutex);
c4a3e0a5 136
c3518837
YB
137static int megasas_poll_wait_aen;
138static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
72c4fd36 139static u32 support_poll_for_event;
9c915a8c 140u32 megasas_dbg_lvl;
837f5fe8 141static u32 support_device_change;
658dcedb 142
c3518837
YB
143/* define lock for aen poll */
144spinlock_t poll_aen_lock;
145
9c915a8c 146void
7343eb65 147megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
148 u8 alt_status);
ebf054b0 149static u32
150megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
151static int
152megasas_adp_reset_gen2(struct megasas_instance *instance,
153 struct megasas_register_set __iomem *reg_set);
cd50ba8e 154static irqreturn_t megasas_isr(int irq, void *devp);
155static u32
156megasas_init_adapter_mfi(struct megasas_instance *instance);
157u32
158megasas_build_and_issue_cmd(struct megasas_instance *instance,
159 struct scsi_cmnd *scmd);
160static void megasas_complete_cmd_dpc(unsigned long instance_addr);
9c915a8c 161void
162megasas_release_fusion(struct megasas_instance *instance);
163int
164megasas_ioc_init_fusion(struct megasas_instance *instance);
165void
166megasas_free_cmds_fusion(struct megasas_instance *instance);
167u8
168megasas_get_map_info(struct megasas_instance *instance);
169int
170megasas_sync_map_info(struct megasas_instance *instance);
171int
172wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
173void megasas_reset_reply_desc(struct megasas_instance *instance);
9c915a8c 174int megasas_reset_fusion(struct Scsi_Host *shost);
175void megasas_fusion_ocr_wq(struct work_struct *work);
cd50ba8e 176
177void
178megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
179{
180 instance->instancet->fire_cmd(instance,
181 cmd->frame_phys_addr, 0, instance->reg_set);
182}
7343eb65 183
c4a3e0a5
BS
184/**
185 * megasas_get_cmd - Get a command from the free pool
186 * @instance: Adapter soft state
187 *
188 * Returns a free command from the pool
189 */
9c915a8c 190struct megasas_cmd *megasas_get_cmd(struct megasas_instance
c4a3e0a5
BS
191 *instance)
192{
193 unsigned long flags;
194 struct megasas_cmd *cmd = NULL;
195
196 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
197
198 if (!list_empty(&instance->cmd_pool)) {
199 cmd = list_entry((&instance->cmd_pool)->next,
200 struct megasas_cmd, list);
201 list_del_init(&cmd->list);
202 } else {
203 printk(KERN_ERR "megasas: Command pool empty!\n");
204 }
205
206 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
207 return cmd;
208}
209
210/**
211 * megasas_return_cmd - Return a cmd to free command pool
212 * @instance: Adapter soft state
213 * @cmd: Command packet to be returned to free command pool
214 */
9c915a8c 215inline void
c4a3e0a5
BS
216megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
221
222 cmd->scmd = NULL;
9c915a8c 223 cmd->frame_count = 0;
e5f93a36 224 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
225 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
21d3c710 226 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
e5f93a36 227 (reset_devices))
228 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
c4a3e0a5
BS
229 list_add_tail(&cmd->list, &instance->cmd_pool);
230
231 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
232}
233
1341c939
SP
234
235/**
0d49016b 236* The following functions are defined for xscale
1341c939
SP
237* (deviceid : 1064R, PERC5) controllers
238*/
239
c4a3e0a5 240/**
1341c939 241 * megasas_enable_intr_xscale - Enables interrupts
c4a3e0a5
BS
242 * @regs: MFI register set
243 */
244static inline void
d46a3ad6 245megasas_enable_intr_xscale(struct megasas_instance *instance)
c4a3e0a5 246{
d46a3ad6
SS
247 struct megasas_register_set __iomem *regs;
248 regs = instance->reg_set;
39a98554 249 writel(0, &(regs)->outbound_intr_mask);
c4a3e0a5
BS
250
251 /* Dummy readl to force pci flush */
252 readl(&regs->outbound_intr_mask);
253}
254
b274cab7
SP
255/**
256 * megasas_disable_intr_xscale -Disables interrupt
257 * @regs: MFI register set
258 */
259static inline void
d46a3ad6 260megasas_disable_intr_xscale(struct megasas_instance *instance)
b274cab7 261{
d46a3ad6 262 struct megasas_register_set __iomem *regs;
b274cab7 263 u32 mask = 0x1f;
d46a3ad6 264 regs = instance->reg_set;
b274cab7
SP
265 writel(mask, &regs->outbound_intr_mask);
266 /* Dummy readl to force pci flush */
267 readl(&regs->outbound_intr_mask);
268}
269
1341c939
SP
270/**
271 * megasas_read_fw_status_reg_xscale - returns the current FW status value
272 * @regs: MFI register set
273 */
274static u32
275megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
276{
277 return readl(&(regs)->outbound_msg_0);
278}
279/**
280 * megasas_clear_interrupt_xscale - Check & clear interrupt
281 * @regs: MFI register set
282 */
0d49016b 283static int
1341c939
SP
284megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
285{
286 u32 status;
39a98554 287 u32 mfiStatus = 0;
1341c939
SP
288 /*
289 * Check if it is our interrupt
290 */
291 status = readl(&regs->outbound_intr_status);
292
39a98554 293 if (status & MFI_OB_INTR_STATUS_MASK)
294 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
295 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
296 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
1341c939
SP
297
298 /*
299 * Clear the interrupt by writing back the same value
300 */
39a98554 301 if (mfiStatus)
302 writel(status, &regs->outbound_intr_status);
1341c939 303
06f579de
YB
304 /* Dummy readl to force pci flush */
305 readl(&regs->outbound_intr_status);
306
39a98554 307 return mfiStatus;
1341c939
SP
308}
309
310/**
311 * megasas_fire_cmd_xscale - Sends command to the FW
312 * @frame_phys_addr : Physical address of cmd
313 * @frame_count : Number of frames for the command
314 * @regs : MFI register set
315 */
0d49016b 316static inline void
0c79e681
YB
317megasas_fire_cmd_xscale(struct megasas_instance *instance,
318 dma_addr_t frame_phys_addr,
319 u32 frame_count,
320 struct megasas_register_set __iomem *regs)
1341c939 321{
39a98554 322 unsigned long flags;
323 spin_lock_irqsave(&instance->hba_lock, flags);
1341c939
SP
324 writel((frame_phys_addr >> 3)|(frame_count),
325 &(regs)->inbound_queue_port);
39a98554 326 spin_unlock_irqrestore(&instance->hba_lock, flags);
327}
328
329/**
330 * megasas_adp_reset_xscale - For controller reset
331 * @regs: MFI register set
332 */
333static int
334megasas_adp_reset_xscale(struct megasas_instance *instance,
335 struct megasas_register_set __iomem *regs)
336{
337 u32 i;
338 u32 pcidata;
339 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
340
341 for (i = 0; i < 3; i++)
342 msleep(1000); /* sleep for 3 secs */
343 pcidata = 0;
344 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
345 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
346 if (pcidata & 0x2) {
347 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
348 pcidata &= ~0x2;
349 pci_write_config_dword(instance->pdev,
350 MFI_1068_PCSR_OFFSET, pcidata);
351
352 for (i = 0; i < 2; i++)
353 msleep(1000); /* need to wait 2 secs again */
354
355 pcidata = 0;
356 pci_read_config_dword(instance->pdev,
357 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
358 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
359 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
360 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
361 pcidata = 0;
362 pci_write_config_dword(instance->pdev,
363 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
364 }
365 }
366 return 0;
367}
368
369/**
370 * megasas_check_reset_xscale - For controller reset check
371 * @regs: MFI register set
372 */
373static int
374megasas_check_reset_xscale(struct megasas_instance *instance,
375 struct megasas_register_set __iomem *regs)
376{
377 u32 consumer;
378 consumer = *instance->consumer;
379
380 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
381 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
382 return 1;
383 }
384 return 0;
1341c939
SP
385}
386
387static struct megasas_instance_template megasas_instance_template_xscale = {
388
389 .fire_cmd = megasas_fire_cmd_xscale,
390 .enable_intr = megasas_enable_intr_xscale,
b274cab7 391 .disable_intr = megasas_disable_intr_xscale,
1341c939
SP
392 .clear_intr = megasas_clear_intr_xscale,
393 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
39a98554 394 .adp_reset = megasas_adp_reset_xscale,
395 .check_reset = megasas_check_reset_xscale,
cd50ba8e 396 .service_isr = megasas_isr,
397 .tasklet = megasas_complete_cmd_dpc,
398 .init_adapter = megasas_init_adapter_mfi,
399 .build_and_issue_cmd = megasas_build_and_issue_cmd,
400 .issue_dcmd = megasas_issue_dcmd,
1341c939
SP
401};
402
403/**
0d49016b 404* This is the end of set of functions & definitions specific
1341c939
SP
405* to xscale (deviceid : 1064R, PERC5) controllers
406*/
407
f9876f0b 408/**
0d49016b 409* The following functions are defined for ppc (deviceid : 0x60)
f9876f0b
SP
410* controllers
411*/
412
413/**
414 * megasas_enable_intr_ppc - Enables interrupts
415 * @regs: MFI register set
416 */
417static inline void
d46a3ad6 418megasas_enable_intr_ppc(struct megasas_instance *instance)
f9876f0b 419{
d46a3ad6
SS
420 struct megasas_register_set __iomem *regs;
421 regs = instance->reg_set;
f9876f0b 422 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
0d49016b 423
39a98554 424 writel(~0x80000000, &(regs)->outbound_intr_mask);
f9876f0b
SP
425
426 /* Dummy readl to force pci flush */
427 readl(&regs->outbound_intr_mask);
428}
429
b274cab7
SP
430/**
431 * megasas_disable_intr_ppc - Disable interrupt
432 * @regs: MFI register set
433 */
434static inline void
d46a3ad6 435megasas_disable_intr_ppc(struct megasas_instance *instance)
b274cab7 436{
d46a3ad6 437 struct megasas_register_set __iomem *regs;
b274cab7 438 u32 mask = 0xFFFFFFFF;
d46a3ad6 439 regs = instance->reg_set;
b274cab7
SP
440 writel(mask, &regs->outbound_intr_mask);
441 /* Dummy readl to force pci flush */
442 readl(&regs->outbound_intr_mask);
443}
444
f9876f0b
SP
445/**
446 * megasas_read_fw_status_reg_ppc - returns the current FW status value
447 * @regs: MFI register set
448 */
449static u32
450megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
451{
452 return readl(&(regs)->outbound_scratch_pad);
453}
454
455/**
456 * megasas_clear_interrupt_ppc - Check & clear interrupt
457 * @regs: MFI register set
458 */
0d49016b 459static int
f9876f0b
SP
460megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
461{
3cc6851f 462 u32 status, mfiStatus = 0;
463
f9876f0b
SP
464 /*
465 * Check if it is our interrupt
466 */
467 status = readl(&regs->outbound_intr_status);
468
3cc6851f 469 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
470 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
471
472 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
473 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
f9876f0b
SP
474
475 /*
476 * Clear the interrupt by writing back the same value
477 */
478 writel(status, &regs->outbound_doorbell_clear);
479
06f579de
YB
480 /* Dummy readl to force pci flush */
481 readl(&regs->outbound_doorbell_clear);
482
3cc6851f 483 return mfiStatus;
f9876f0b 484}
3cc6851f 485
f9876f0b
SP
486/**
487 * megasas_fire_cmd_ppc - Sends command to the FW
488 * @frame_phys_addr : Physical address of cmd
489 * @frame_count : Number of frames for the command
490 * @regs : MFI register set
491 */
0d49016b 492static inline void
0c79e681
YB
493megasas_fire_cmd_ppc(struct megasas_instance *instance,
494 dma_addr_t frame_phys_addr,
495 u32 frame_count,
496 struct megasas_register_set __iomem *regs)
f9876f0b 497{
39a98554 498 unsigned long flags;
499 spin_lock_irqsave(&instance->hba_lock, flags);
0d49016b 500 writel((frame_phys_addr | (frame_count<<1))|1,
f9876f0b 501 &(regs)->inbound_queue_port);
39a98554 502 spin_unlock_irqrestore(&instance->hba_lock, flags);
f9876f0b
SP
503}
504
39a98554 505/**
506 * megasas_check_reset_ppc - For controller reset check
507 * @regs: MFI register set
508 */
509static int
510megasas_check_reset_ppc(struct megasas_instance *instance,
511 struct megasas_register_set __iomem *regs)
512{
3cc6851f 513 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
514 return 1;
515
39a98554 516 return 0;
517}
3cc6851f 518
f9876f0b 519static struct megasas_instance_template megasas_instance_template_ppc = {
0d49016b 520
f9876f0b
SP
521 .fire_cmd = megasas_fire_cmd_ppc,
522 .enable_intr = megasas_enable_intr_ppc,
b274cab7 523 .disable_intr = megasas_disable_intr_ppc,
f9876f0b
SP
524 .clear_intr = megasas_clear_intr_ppc,
525 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
3cc6851f 526 .adp_reset = megasas_adp_reset_xscale,
39a98554 527 .check_reset = megasas_check_reset_ppc,
cd50ba8e 528 .service_isr = megasas_isr,
529 .tasklet = megasas_complete_cmd_dpc,
530 .init_adapter = megasas_init_adapter_mfi,
531 .build_and_issue_cmd = megasas_build_and_issue_cmd,
532 .issue_dcmd = megasas_issue_dcmd,
f9876f0b
SP
533};
534
87911122
YB
535/**
536 * megasas_enable_intr_skinny - Enables interrupts
537 * @regs: MFI register set
538 */
539static inline void
d46a3ad6 540megasas_enable_intr_skinny(struct megasas_instance *instance)
87911122 541{
d46a3ad6
SS
542 struct megasas_register_set __iomem *regs;
543 regs = instance->reg_set;
87911122
YB
544 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
545
546 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
547
548 /* Dummy readl to force pci flush */
549 readl(&regs->outbound_intr_mask);
550}
551
552/**
553 * megasas_disable_intr_skinny - Disables interrupt
554 * @regs: MFI register set
555 */
556static inline void
d46a3ad6 557megasas_disable_intr_skinny(struct megasas_instance *instance)
87911122 558{
d46a3ad6 559 struct megasas_register_set __iomem *regs;
87911122 560 u32 mask = 0xFFFFFFFF;
d46a3ad6 561 regs = instance->reg_set;
87911122
YB
562 writel(mask, &regs->outbound_intr_mask);
563 /* Dummy readl to force pci flush */
564 readl(&regs->outbound_intr_mask);
565}
566
567/**
568 * megasas_read_fw_status_reg_skinny - returns the current FW status value
569 * @regs: MFI register set
570 */
571static u32
572megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
573{
574 return readl(&(regs)->outbound_scratch_pad);
575}
576
577/**
578 * megasas_clear_interrupt_skinny - Check & clear interrupt
579 * @regs: MFI register set
580 */
581static int
582megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
583{
584 u32 status;
ebf054b0 585 u32 mfiStatus = 0;
586
87911122
YB
587 /*
588 * Check if it is our interrupt
589 */
590 status = readl(&regs->outbound_intr_status);
591
592 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
39a98554 593 return 0;
87911122
YB
594 }
595
ebf054b0 596 /*
597 * Check if it is our interrupt
598 */
599 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
600 MFI_STATE_FAULT) {
601 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
602 } else
603 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
604
87911122
YB
605 /*
606 * Clear the interrupt by writing back the same value
607 */
608 writel(status, &regs->outbound_intr_status);
609
610 /*
611 * dummy read to flush PCI
612 */
613 readl(&regs->outbound_intr_status);
614
ebf054b0 615 return mfiStatus;
87911122
YB
616}
617
618/**
619 * megasas_fire_cmd_skinny - Sends command to the FW
620 * @frame_phys_addr : Physical address of cmd
621 * @frame_count : Number of frames for the command
622 * @regs : MFI register set
623 */
624static inline void
0c79e681
YB
625megasas_fire_cmd_skinny(struct megasas_instance *instance,
626 dma_addr_t frame_phys_addr,
627 u32 frame_count,
87911122
YB
628 struct megasas_register_set __iomem *regs)
629{
0c79e681 630 unsigned long flags;
39a98554 631 spin_lock_irqsave(&instance->hba_lock, flags);
87911122
YB
632 writel(0, &(regs)->inbound_high_queue_port);
633 writel((frame_phys_addr | (frame_count<<1))|1,
634 &(regs)->inbound_low_queue_port);
39a98554 635 spin_unlock_irqrestore(&instance->hba_lock, flags);
636}
637
39a98554 638/**
639 * megasas_check_reset_skinny - For controller reset check
640 * @regs: MFI register set
641 */
642static int
643megasas_check_reset_skinny(struct megasas_instance *instance,
644 struct megasas_register_set __iomem *regs)
645{
3cc6851f 646 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
647 return 1;
648
39a98554 649 return 0;
87911122
YB
650}
651
652static struct megasas_instance_template megasas_instance_template_skinny = {
653
654 .fire_cmd = megasas_fire_cmd_skinny,
655 .enable_intr = megasas_enable_intr_skinny,
656 .disable_intr = megasas_disable_intr_skinny,
657 .clear_intr = megasas_clear_intr_skinny,
658 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
ebf054b0 659 .adp_reset = megasas_adp_reset_gen2,
39a98554 660 .check_reset = megasas_check_reset_skinny,
cd50ba8e 661 .service_isr = megasas_isr,
662 .tasklet = megasas_complete_cmd_dpc,
663 .init_adapter = megasas_init_adapter_mfi,
664 .build_and_issue_cmd = megasas_build_and_issue_cmd,
665 .issue_dcmd = megasas_issue_dcmd,
87911122
YB
666};
667
668
6610a6b3
YB
669/**
670* The following functions are defined for gen2 (deviceid : 0x78 0x79)
671* controllers
672*/
673
674/**
675 * megasas_enable_intr_gen2 - Enables interrupts
676 * @regs: MFI register set
677 */
678static inline void
d46a3ad6 679megasas_enable_intr_gen2(struct megasas_instance *instance)
6610a6b3 680{
d46a3ad6
SS
681 struct megasas_register_set __iomem *regs;
682 regs = instance->reg_set;
6610a6b3
YB
683 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
684
685 /* write ~0x00000005 (4 & 1) to the intr mask*/
686 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
687
688 /* Dummy readl to force pci flush */
689 readl(&regs->outbound_intr_mask);
690}
691
692/**
693 * megasas_disable_intr_gen2 - Disables interrupt
694 * @regs: MFI register set
695 */
696static inline void
d46a3ad6 697megasas_disable_intr_gen2(struct megasas_instance *instance)
6610a6b3 698{
d46a3ad6 699 struct megasas_register_set __iomem *regs;
6610a6b3 700 u32 mask = 0xFFFFFFFF;
d46a3ad6 701 regs = instance->reg_set;
6610a6b3
YB
702 writel(mask, &regs->outbound_intr_mask);
703 /* Dummy readl to force pci flush */
704 readl(&regs->outbound_intr_mask);
705}
706
707/**
708 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
709 * @regs: MFI register set
710 */
711static u32
712megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
713{
714 return readl(&(regs)->outbound_scratch_pad);
715}
716
717/**
718 * megasas_clear_interrupt_gen2 - Check & clear interrupt
719 * @regs: MFI register set
720 */
721static int
722megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
723{
724 u32 status;
39a98554 725 u32 mfiStatus = 0;
6610a6b3
YB
726 /*
727 * Check if it is our interrupt
728 */
729 status = readl(&regs->outbound_intr_status);
730
b5bccadd 731 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
39a98554 732 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
733 }
734 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
735 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
736 }
6610a6b3
YB
737
738 /*
739 * Clear the interrupt by writing back the same value
740 */
39a98554 741 if (mfiStatus)
742 writel(status, &regs->outbound_doorbell_clear);
6610a6b3
YB
743
744 /* Dummy readl to force pci flush */
745 readl(&regs->outbound_intr_status);
746
39a98554 747 return mfiStatus;
6610a6b3
YB
748}
749/**
750 * megasas_fire_cmd_gen2 - Sends command to the FW
751 * @frame_phys_addr : Physical address of cmd
752 * @frame_count : Number of frames for the command
753 * @regs : MFI register set
754 */
755static inline void
0c79e681
YB
756megasas_fire_cmd_gen2(struct megasas_instance *instance,
757 dma_addr_t frame_phys_addr,
758 u32 frame_count,
6610a6b3
YB
759 struct megasas_register_set __iomem *regs)
760{
39a98554 761 unsigned long flags;
762 spin_lock_irqsave(&instance->hba_lock, flags);
6610a6b3
YB
763 writel((frame_phys_addr | (frame_count<<1))|1,
764 &(regs)->inbound_queue_port);
39a98554 765 spin_unlock_irqrestore(&instance->hba_lock, flags);
766}
767
768/**
769 * megasas_adp_reset_gen2 - For controller reset
770 * @regs: MFI register set
771 */
772static int
773megasas_adp_reset_gen2(struct megasas_instance *instance,
774 struct megasas_register_set __iomem *reg_set)
775{
776 u32 retry = 0 ;
777 u32 HostDiag;
ebf054b0 778 u32 *seq_offset = &reg_set->seq_offset;
779 u32 *hostdiag_offset = &reg_set->host_diag;
780
781 if (instance->instancet == &megasas_instance_template_skinny) {
782 seq_offset = &reg_set->fusion_seq_offset;
783 hostdiag_offset = &reg_set->fusion_host_diag;
784 }
785
786 writel(0, seq_offset);
787 writel(4, seq_offset);
788 writel(0xb, seq_offset);
789 writel(2, seq_offset);
790 writel(7, seq_offset);
791 writel(0xd, seq_offset);
39a98554 792
39a98554 793 msleep(1000);
794
ebf054b0 795 HostDiag = (u32)readl(hostdiag_offset);
39a98554 796
797 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
798 msleep(100);
ebf054b0 799 HostDiag = (u32)readl(hostdiag_offset);
39a98554 800 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
801 retry, HostDiag);
802
803 if (retry++ >= 100)
804 return 1;
805
806 }
807
808 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
809
ebf054b0 810 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
39a98554 811
812 ssleep(10);
813
ebf054b0 814 HostDiag = (u32)readl(hostdiag_offset);
39a98554 815 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
816 msleep(100);
ebf054b0 817 HostDiag = (u32)readl(hostdiag_offset);
39a98554 818 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
819 retry, HostDiag);
820
821 if (retry++ >= 1000)
822 return 1;
823
824 }
825 return 0;
826}
827
828/**
829 * megasas_check_reset_gen2 - For controller reset check
830 * @regs: MFI register set
831 */
832static int
833megasas_check_reset_gen2(struct megasas_instance *instance,
834 struct megasas_register_set __iomem *regs)
835{
707e09bd
YB
836 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
837 return 1;
838 }
839
39a98554 840 return 0;
6610a6b3
YB
841}
842
843static struct megasas_instance_template megasas_instance_template_gen2 = {
844
845 .fire_cmd = megasas_fire_cmd_gen2,
846 .enable_intr = megasas_enable_intr_gen2,
847 .disable_intr = megasas_disable_intr_gen2,
848 .clear_intr = megasas_clear_intr_gen2,
849 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
39a98554 850 .adp_reset = megasas_adp_reset_gen2,
851 .check_reset = megasas_check_reset_gen2,
cd50ba8e 852 .service_isr = megasas_isr,
853 .tasklet = megasas_complete_cmd_dpc,
854 .init_adapter = megasas_init_adapter_mfi,
855 .build_and_issue_cmd = megasas_build_and_issue_cmd,
856 .issue_dcmd = megasas_issue_dcmd,
6610a6b3
YB
857};
858
f9876f0b
SP
859/**
860* This is the end of set of functions & definitions
39a98554 861* specific to gen2 (deviceid : 0x78, 0x79) controllers
f9876f0b
SP
862*/
863
9c915a8c 864/*
865 * Template added for TB (Fusion)
866 */
867extern struct megasas_instance_template megasas_instance_template_fusion;
868
c4a3e0a5
BS
869/**
870 * megasas_issue_polled - Issues a polling command
871 * @instance: Adapter soft state
0d49016b 872 * @cmd: Command packet to be issued
c4a3e0a5
BS
873 *
874 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
875 */
9c915a8c 876int
c4a3e0a5
BS
877megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
878{
c4a3e0a5
BS
879
880 struct megasas_header *frame_hdr = &cmd->frame->hdr;
881
882 frame_hdr->cmd_status = 0xFF;
883 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
884
885 /*
886 * Issue the frame using inbound queue port
887 */
9c915a8c 888 instance->instancet->issue_dcmd(instance, cmd);
c4a3e0a5
BS
889
890 /*
891 * Wait for cmd_status to change
892 */
9c915a8c 893 return wait_and_poll(instance, cmd);
c4a3e0a5
BS
894}
895
896/**
897 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
898 * @instance: Adapter soft state
899 * @cmd: Command to be issued
900 *
901 * This function waits on an event for the command to be returned from ISR.
2a3681e5 902 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
c4a3e0a5
BS
903 * Used to issue ioctl commands.
904 */
905static int
906megasas_issue_blocked_cmd(struct megasas_instance *instance,
907 struct megasas_cmd *cmd)
908{
909 cmd->cmd_status = ENODATA;
910
9c915a8c 911 instance->instancet->issue_dcmd(instance, cmd);
c4a3e0a5 912
39a98554 913 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
c4a3e0a5
BS
914
915 return 0;
916}
917
918/**
919 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
920 * @instance: Adapter soft state
921 * @cmd_to_abort: Previously issued cmd to be aborted
922 *
8e572bab 923 * MFI firmware can abort previously issued AEN command (automatic event
c4a3e0a5 924 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
2a3681e5
SP
925 * cmd and waits for return status.
926 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
c4a3e0a5
BS
927 */
928static int
929megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
930 struct megasas_cmd *cmd_to_abort)
931{
932 struct megasas_cmd *cmd;
933 struct megasas_abort_frame *abort_fr;
934
935 cmd = megasas_get_cmd(instance);
936
937 if (!cmd)
938 return -1;
939
940 abort_fr = &cmd->frame->abort;
941
942 /*
943 * Prepare and issue the abort frame
944 */
945 abort_fr->cmd = MFI_CMD_ABORT;
946 abort_fr->cmd_status = 0xFF;
947 abort_fr->flags = 0;
948 abort_fr->abort_context = cmd_to_abort->index;
949 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
950 abort_fr->abort_mfi_phys_addr_hi = 0;
951
952 cmd->sync_cmd = 1;
953 cmd->cmd_status = 0xFF;
954
9c915a8c 955 instance->instancet->issue_dcmd(instance, cmd);
c4a3e0a5
BS
956
957 /*
958 * Wait for this cmd to complete
959 */
39a98554 960 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
961 cmd->sync_cmd = 0;
c4a3e0a5
BS
962
963 megasas_return_cmd(instance, cmd);
964 return 0;
965}
966
967/**
968 * megasas_make_sgl32 - Prepares 32-bit SGL
969 * @instance: Adapter soft state
970 * @scp: SCSI command from the mid-layer
971 * @mfi_sgl: SGL to be filled in
972 *
973 * If successful, this function returns the number of SG elements. Otherwise,
974 * it returnes -1.
975 */
858119e1 976static int
c4a3e0a5
BS
977megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
978 union megasas_sgl *mfi_sgl)
979{
980 int i;
981 int sge_count;
982 struct scatterlist *os_sgl;
983
155d98f0
FT
984 sge_count = scsi_dma_map(scp);
985 BUG_ON(sge_count < 0);
c4a3e0a5 986
155d98f0
FT
987 if (sge_count) {
988 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
989 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
990 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
991 }
c4a3e0a5 992 }
c4a3e0a5
BS
993 return sge_count;
994}
995
996/**
997 * megasas_make_sgl64 - Prepares 64-bit SGL
998 * @instance: Adapter soft state
999 * @scp: SCSI command from the mid-layer
1000 * @mfi_sgl: SGL to be filled in
1001 *
1002 * If successful, this function returns the number of SG elements. Otherwise,
1003 * it returnes -1.
1004 */
858119e1 1005static int
c4a3e0a5
BS
1006megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1007 union megasas_sgl *mfi_sgl)
1008{
1009 int i;
1010 int sge_count;
1011 struct scatterlist *os_sgl;
1012
155d98f0
FT
1013 sge_count = scsi_dma_map(scp);
1014 BUG_ON(sge_count < 0);
c4a3e0a5 1015
155d98f0
FT
1016 if (sge_count) {
1017 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1018 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
1019 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1020 }
c4a3e0a5 1021 }
c4a3e0a5
BS
1022 return sge_count;
1023}
1024
f4c9a131
YB
1025/**
1026 * megasas_make_sgl_skinny - Prepares IEEE SGL
1027 * @instance: Adapter soft state
1028 * @scp: SCSI command from the mid-layer
1029 * @mfi_sgl: SGL to be filled in
1030 *
1031 * If successful, this function returns the number of SG elements. Otherwise,
1032 * it returnes -1.
1033 */
1034static int
1035megasas_make_sgl_skinny(struct megasas_instance *instance,
1036 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1037{
1038 int i;
1039 int sge_count;
1040 struct scatterlist *os_sgl;
1041
1042 sge_count = scsi_dma_map(scp);
1043
1044 if (sge_count) {
1045 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1046 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1047 mfi_sgl->sge_skinny[i].phys_addr =
1048 sg_dma_address(os_sgl);
707e09bd 1049 mfi_sgl->sge_skinny[i].flag = 0;
f4c9a131
YB
1050 }
1051 }
1052 return sge_count;
1053}
1054
b1df99d9
SP
1055 /**
1056 * megasas_get_frame_count - Computes the number of frames
d532dbe2 1057 * @frame_type : type of frame- io or pthru frame
b1df99d9
SP
1058 * @sge_count : number of sg elements
1059 *
1060 * Returns the number of frames required for numnber of sge's (sge_count)
1061 */
1062
f4c9a131
YB
1063static u32 megasas_get_frame_count(struct megasas_instance *instance,
1064 u8 sge_count, u8 frame_type)
b1df99d9
SP
1065{
1066 int num_cnt;
1067 int sge_bytes;
1068 u32 sge_sz;
1069 u32 frame_count=0;
1070
1071 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1072 sizeof(struct megasas_sge32);
1073
f4c9a131
YB
1074 if (instance->flag_ieee) {
1075 sge_sz = sizeof(struct megasas_sge_skinny);
1076 }
1077
b1df99d9 1078 /*
d532dbe2 1079 * Main frame can contain 2 SGEs for 64-bit SGLs and
1080 * 3 SGEs for 32-bit SGLs for ldio &
1081 * 1 SGEs for 64-bit SGLs and
1082 * 2 SGEs for 32-bit SGLs for pthru frame
1083 */
1084 if (unlikely(frame_type == PTHRU_FRAME)) {
f4c9a131
YB
1085 if (instance->flag_ieee == 1) {
1086 num_cnt = sge_count - 1;
1087 } else if (IS_DMA64)
d532dbe2 1088 num_cnt = sge_count - 1;
1089 else
1090 num_cnt = sge_count - 2;
1091 } else {
f4c9a131
YB
1092 if (instance->flag_ieee == 1) {
1093 num_cnt = sge_count - 1;
1094 } else if (IS_DMA64)
d532dbe2 1095 num_cnt = sge_count - 2;
1096 else
1097 num_cnt = sge_count - 3;
1098 }
b1df99d9
SP
1099
1100 if(num_cnt>0){
1101 sge_bytes = sge_sz * num_cnt;
1102
1103 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1104 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1105 }
1106 /* Main frame */
1107 frame_count +=1;
1108
1109 if (frame_count > 7)
1110 frame_count = 8;
1111 return frame_count;
1112}
1113
c4a3e0a5
BS
1114/**
1115 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1116 * @instance: Adapter soft state
1117 * @scp: SCSI command
1118 * @cmd: Command to be prepared in
1119 *
1120 * This function prepares CDB commands. These are typcially pass-through
1121 * commands to the devices.
1122 */
858119e1 1123static int
c4a3e0a5
BS
1124megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1125 struct megasas_cmd *cmd)
1126{
c4a3e0a5
BS
1127 u32 is_logical;
1128 u32 device_id;
1129 u16 flags = 0;
1130 struct megasas_pthru_frame *pthru;
1131
1132 is_logical = MEGASAS_IS_LOGICAL(scp);
1133 device_id = MEGASAS_DEV_INDEX(instance, scp);
1134 pthru = (struct megasas_pthru_frame *)cmd->frame;
1135
1136 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1137 flags = MFI_FRAME_DIR_WRITE;
1138 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1139 flags = MFI_FRAME_DIR_READ;
1140 else if (scp->sc_data_direction == PCI_DMA_NONE)
1141 flags = MFI_FRAME_DIR_NONE;
1142
f4c9a131
YB
1143 if (instance->flag_ieee == 1) {
1144 flags |= MFI_FRAME_IEEE;
1145 }
1146
c4a3e0a5
BS
1147 /*
1148 * Prepare the DCDB frame
1149 */
1150 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1151 pthru->cmd_status = 0x0;
1152 pthru->scsi_status = 0x0;
1153 pthru->target_id = device_id;
1154 pthru->lun = scp->device->lun;
1155 pthru->cdb_len = scp->cmd_len;
1156 pthru->timeout = 0;
780a3762 1157 pthru->pad_0 = 0;
c4a3e0a5 1158 pthru->flags = flags;
155d98f0 1159 pthru->data_xfer_len = scsi_bufflen(scp);
c4a3e0a5
BS
1160
1161 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1162
8d568253
YB
1163 /*
1164 * If the command is for the tape device, set the
1165 * pthru timeout to the os layer timeout value.
1166 */
1167 if (scp->device->type == TYPE_TAPE) {
1168 if ((scp->request->timeout / HZ) > 0xFFFF)
1169 pthru->timeout = 0xFFFF;
1170 else
1171 pthru->timeout = scp->request->timeout / HZ;
1172 }
1173
c4a3e0a5
BS
1174 /*
1175 * Construct SGL
1176 */
f4c9a131
YB
1177 if (instance->flag_ieee == 1) {
1178 pthru->flags |= MFI_FRAME_SGL64;
1179 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1180 &pthru->sgl);
1181 } else if (IS_DMA64) {
c4a3e0a5
BS
1182 pthru->flags |= MFI_FRAME_SGL64;
1183 pthru->sge_count = megasas_make_sgl64(instance, scp,
1184 &pthru->sgl);
1185 } else
1186 pthru->sge_count = megasas_make_sgl32(instance, scp,
1187 &pthru->sgl);
1188
bdc6fb8d
YB
1189 if (pthru->sge_count > instance->max_num_sge) {
1190 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1191 pthru->sge_count);
1192 return 0;
1193 }
1194
c4a3e0a5
BS
1195 /*
1196 * Sense info specific
1197 */
1198 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1199 pthru->sense_buf_phys_addr_hi = 0;
1200 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1201
c4a3e0a5
BS
1202 /*
1203 * Compute the total number of frames this command consumes. FW uses
1204 * this number to pull sufficient number of frames from host memory.
1205 */
f4c9a131 1206 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
d532dbe2 1207 PTHRU_FRAME);
c4a3e0a5
BS
1208
1209 return cmd->frame_count;
1210}
1211
1212/**
1213 * megasas_build_ldio - Prepares IOs to logical devices
1214 * @instance: Adapter soft state
1215 * @scp: SCSI command
fd589a8f 1216 * @cmd: Command to be prepared
c4a3e0a5
BS
1217 *
1218 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1219 */
858119e1 1220static int
c4a3e0a5
BS
1221megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1222 struct megasas_cmd *cmd)
1223{
c4a3e0a5
BS
1224 u32 device_id;
1225 u8 sc = scp->cmnd[0];
1226 u16 flags = 0;
1227 struct megasas_io_frame *ldio;
1228
1229 device_id = MEGASAS_DEV_INDEX(instance, scp);
1230 ldio = (struct megasas_io_frame *)cmd->frame;
1231
1232 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1233 flags = MFI_FRAME_DIR_WRITE;
1234 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1235 flags = MFI_FRAME_DIR_READ;
1236
f4c9a131
YB
1237 if (instance->flag_ieee == 1) {
1238 flags |= MFI_FRAME_IEEE;
1239 }
1240
c4a3e0a5 1241 /*
b1df99d9 1242 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
c4a3e0a5
BS
1243 */
1244 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1245 ldio->cmd_status = 0x0;
1246 ldio->scsi_status = 0x0;
1247 ldio->target_id = device_id;
1248 ldio->timeout = 0;
1249 ldio->reserved_0 = 0;
1250 ldio->pad_0 = 0;
1251 ldio->flags = flags;
1252 ldio->start_lba_hi = 0;
1253 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1254
1255 /*
1256 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1257 */
1258 if (scp->cmd_len == 6) {
1259 ldio->lba_count = (u32) scp->cmnd[4];
1260 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1261 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1262
1263 ldio->start_lba_lo &= 0x1FFFFF;
1264 }
1265
1266 /*
1267 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1268 */
1269 else if (scp->cmd_len == 10) {
1270 ldio->lba_count = (u32) scp->cmnd[8] |
1271 ((u32) scp->cmnd[7] << 8);
1272 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1273 ((u32) scp->cmnd[3] << 16) |
1274 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1275 }
1276
1277 /*
1278 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1279 */
1280 else if (scp->cmd_len == 12) {
1281 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1282 ((u32) scp->cmnd[7] << 16) |
1283 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1284
1285 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1286 ((u32) scp->cmnd[3] << 16) |
1287 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1288 }
1289
1290 /*
1291 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1292 */
1293 else if (scp->cmd_len == 16) {
1294 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1295 ((u32) scp->cmnd[11] << 16) |
1296 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1297
1298 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1299 ((u32) scp->cmnd[7] << 16) |
1300 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1301
1302 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1303 ((u32) scp->cmnd[3] << 16) |
1304 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1305
1306 }
1307
1308 /*
1309 * Construct SGL
1310 */
f4c9a131
YB
1311 if (instance->flag_ieee) {
1312 ldio->flags |= MFI_FRAME_SGL64;
1313 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1314 &ldio->sgl);
1315 } else if (IS_DMA64) {
c4a3e0a5
BS
1316 ldio->flags |= MFI_FRAME_SGL64;
1317 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1318 } else
1319 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1320
bdc6fb8d
YB
1321 if (ldio->sge_count > instance->max_num_sge) {
1322 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1323 ldio->sge_count);
1324 return 0;
1325 }
1326
c4a3e0a5
BS
1327 /*
1328 * Sense info specific
1329 */
1330 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1331 ldio->sense_buf_phys_addr_hi = 0;
1332 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1333
b1df99d9
SP
1334 /*
1335 * Compute the total number of frames this command consumes. FW uses
1336 * this number to pull sufficient number of frames from host memory.
1337 */
f4c9a131
YB
1338 cmd->frame_count = megasas_get_frame_count(instance,
1339 ldio->sge_count, IO_FRAME);
c4a3e0a5
BS
1340
1341 return cmd->frame_count;
1342}
1343
1344/**
cb59aa6a
SP
1345 * megasas_is_ldio - Checks if the cmd is for logical drive
1346 * @scmd: SCSI command
0d49016b 1347 *
cb59aa6a 1348 * Called by megasas_queue_command to find out if the command to be queued
0d49016b 1349 * is a logical drive command
c4a3e0a5 1350 */
9c915a8c 1351inline int megasas_is_ldio(struct scsi_cmnd *cmd)
c4a3e0a5 1352{
cb59aa6a
SP
1353 if (!MEGASAS_IS_LOGICAL(cmd))
1354 return 0;
1355 switch (cmd->cmnd[0]) {
1356 case READ_10:
1357 case WRITE_10:
1358 case READ_12:
1359 case WRITE_12:
1360 case READ_6:
1361 case WRITE_6:
1362 case READ_16:
1363 case WRITE_16:
1364 return 1;
1365 default:
1366 return 0;
c4a3e0a5 1367 }
c4a3e0a5
BS
1368}
1369
658dcedb
SP
1370 /**
1371 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1372 * in FW
1373 * @instance: Adapter soft state
1374 */
1375static inline void
1376megasas_dump_pending_frames(struct megasas_instance *instance)
1377{
1378 struct megasas_cmd *cmd;
1379 int i,n;
1380 union megasas_sgl *mfi_sgl;
1381 struct megasas_io_frame *ldio;
1382 struct megasas_pthru_frame *pthru;
1383 u32 sgcount;
1384 u32 max_cmd = instance->max_fw_cmds;
1385
1386 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1387 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1388 if (IS_DMA64)
1389 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1390 else
1391 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1392
1393 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1394 for (i = 0; i < max_cmd; i++) {
1395 cmd = instance->cmd_list[i];
1396 if(!cmd->scmd)
1397 continue;
1398 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1399 if (megasas_is_ldio(cmd->scmd)){
1400 ldio = (struct megasas_io_frame *)cmd->frame;
1401 mfi_sgl = &ldio->sgl;
1402 sgcount = ldio->sge_count;
1403 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);
1404 }
1405 else {
1406 pthru = (struct megasas_pthru_frame *) cmd->frame;
1407 mfi_sgl = &pthru->sgl;
1408 sgcount = pthru->sge_count;
1409 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);
1410 }
1411 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1412 for (n = 0; n < sgcount; n++){
1413 if (IS_DMA64)
1414 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) ;
1415 else
1416 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1417 }
1418 }
1419 printk(KERN_ERR "\n");
1420 } /*for max_cmd*/
1421 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1422 for (i = 0; i < max_cmd; i++) {
1423
1424 cmd = instance->cmd_list[i];
1425
1426 if(cmd->sync_cmd == 1){
1427 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1428 }
1429 }
1430 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1431}
1432
cd50ba8e 1433u32
1434megasas_build_and_issue_cmd(struct megasas_instance *instance,
1435 struct scsi_cmnd *scmd)
1436{
1437 struct megasas_cmd *cmd;
1438 u32 frame_count;
1439
1440 cmd = megasas_get_cmd(instance);
1441 if (!cmd)
1442 return SCSI_MLQUEUE_HOST_BUSY;
1443
1444 /*
1445 * Logical drive command
1446 */
1447 if (megasas_is_ldio(scmd))
1448 frame_count = megasas_build_ldio(instance, scmd, cmd);
1449 else
1450 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1451
1452 if (!frame_count)
1453 goto out_return_cmd;
1454
1455 cmd->scmd = scmd;
1456 scmd->SCp.ptr = (char *)cmd;
1457
1458 /*
1459 * Issue the command to the FW
1460 */
1461 atomic_inc(&instance->fw_outstanding);
1462
1463 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1464 cmd->frame_count-1, instance->reg_set);
cd50ba8e 1465
1466 return 0;
1467out_return_cmd:
1468 megasas_return_cmd(instance, cmd);
1469 return 1;
1470}
1471
1472
c4a3e0a5
BS
1473/**
1474 * megasas_queue_command - Queue entry point
1475 * @scmd: SCSI command to be queued
1476 * @done: Callback entry point
1477 */
1478static int
f281233d 1479megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
c4a3e0a5 1480{
c4a3e0a5 1481 struct megasas_instance *instance;
39a98554 1482 unsigned long flags;
c4a3e0a5
BS
1483
1484 instance = (struct megasas_instance *)
1485 scmd->device->host->hostdata;
af37acfb 1486
39a98554 1487 if (instance->issuepend_done == 0)
af37acfb
SP
1488 return SCSI_MLQUEUE_HOST_BUSY;
1489
39a98554 1490 spin_lock_irqsave(&instance->hba_lock, flags);
b09e66da
SS
1491
1492 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1493 spin_unlock_irqrestore(&instance->hba_lock, flags);
1494 scmd->result = DID_ERROR << 16;
1495 done(scmd);
1496 return 0;
1497 }
1498
39a98554 1499 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1500 spin_unlock_irqrestore(&instance->hba_lock, flags);
1501 return SCSI_MLQUEUE_HOST_BUSY;
1502 }
1503
1504 spin_unlock_irqrestore(&instance->hba_lock, flags);
1505
c4a3e0a5
BS
1506 scmd->scsi_done = done;
1507 scmd->result = 0;
1508
cb59aa6a
SP
1509 if (MEGASAS_IS_LOGICAL(scmd) &&
1510 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1511 scmd->result = DID_BAD_TARGET << 16;
1512 goto out_done;
c4a3e0a5
BS
1513 }
1514
02b01e01
SP
1515 switch (scmd->cmnd[0]) {
1516 case SYNCHRONIZE_CACHE:
1517 /*
1518 * FW takes care of flush cache on its own
1519 * No need to send it down
1520 */
1521 scmd->result = DID_OK << 16;
1522 goto out_done;
1523 default:
1524 break;
1525 }
1526
cd50ba8e 1527 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1528 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
cb59aa6a 1529 return SCSI_MLQUEUE_HOST_BUSY;
cd50ba8e 1530 }
c4a3e0a5
BS
1531
1532 return 0;
cb59aa6a 1533
cb59aa6a
SP
1534 out_done:
1535 done(scmd);
1536 return 0;
c4a3e0a5
BS
1537}
1538
f281233d
JG
1539static DEF_SCSI_QCMD(megasas_queue_command)
1540
044833b5
YB
1541static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1542{
1543 int i;
1544
1545 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1546
1547 if ((megasas_mgmt_info.instance[i]) &&
1548 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1549 return megasas_mgmt_info.instance[i];
1550 }
1551
1552 return NULL;
1553}
1554
147aab6a
CH
1555static int megasas_slave_configure(struct scsi_device *sdev)
1556{
044833b5
YB
1557 u16 pd_index = 0;
1558 struct megasas_instance *instance ;
1559
1560 instance = megasas_lookup_instance(sdev->host->host_no);
1561
147aab6a 1562 /*
044833b5
YB
1563 * Don't export physical disk devices to the disk driver.
1564 *
1565 * FIXME: Currently we don't export them to the midlayer at all.
1566 * That will be fixed once LSI engineers have audited the
1567 * firmware for possible issues.
1568 */
1569 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1570 sdev->type == TYPE_DISK) {
1571 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1572 sdev->id;
1573 if (instance->pd_list[pd_index].driveState ==
1574 MR_PD_STATE_SYSTEM) {
1575 blk_queue_rq_timeout(sdev->request_queue,
1576 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1577 return 0;
1578 }
147aab6a 1579 return -ENXIO;
044833b5 1580 }
e5b3a65f
CH
1581
1582 /*
044833b5
YB
1583 * The RAID firmware may require extended timeouts.
1584 */
1585 blk_queue_rq_timeout(sdev->request_queue,
1586 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1587 return 0;
1588}
1589
1590static int megasas_slave_alloc(struct scsi_device *sdev)
1591{
1592 u16 pd_index = 0;
1593 struct megasas_instance *instance ;
1594 instance = megasas_lookup_instance(sdev->host->host_no);
1595 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1596 (sdev->type == TYPE_DISK)) {
1597 /*
1598 * Open the OS scan to the SYSTEM PD
1599 */
1600 pd_index =
1601 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1602 sdev->id;
1603 if ((instance->pd_list[pd_index].driveState ==
1604 MR_PD_STATE_SYSTEM) &&
1605 (instance->pd_list[pd_index].driveType ==
1606 TYPE_DISK)) {
1607 return 0;
1608 }
1609 return -ENXIO;
1610 }
147aab6a
CH
1611 return 0;
1612}
1613
9c915a8c 1614void megaraid_sas_kill_hba(struct megasas_instance *instance)
39a98554 1615{
1616 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
9c915a8c 1617 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
36807e67 1618 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
21d3c710
SS
1619 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1620 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
9c915a8c 1621 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
39a98554 1622 } else {
9c915a8c 1623 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1624 }
1625}
1626
1627 /**
1628 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1629 * restored to max value
1630 * @instance: Adapter soft state
1631 *
1632 */
1633void
1634megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1635{
1636 unsigned long flags;
1637 if (instance->flag & MEGASAS_FW_BUSY
c5daa6a9 1638 && time_after(jiffies, instance->last_time + 5 * HZ)
1639 && atomic_read(&instance->fw_outstanding) <
1640 instance->throttlequeuedepth + 1) {
9c915a8c 1641
1642 spin_lock_irqsave(instance->host->host_lock, flags);
1643 instance->flag &= ~MEGASAS_FW_BUSY;
1644 if ((instance->pdev->device ==
1645 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1646 (instance->pdev->device ==
1647 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1648 instance->host->can_queue =
1649 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1650 } else
1651 instance->host->can_queue =
1652 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1653
1654 spin_unlock_irqrestore(instance->host->host_lock, flags);
39a98554 1655 }
1656}
1657
7343eb65 1658/**
1659 * megasas_complete_cmd_dpc - Returns FW's controller structure
1660 * @instance_addr: Address of adapter soft state
1661 *
1662 * Tasklet to complete cmds
1663 */
1664static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1665{
1666 u32 producer;
1667 u32 consumer;
1668 u32 context;
1669 struct megasas_cmd *cmd;
1670 struct megasas_instance *instance =
1671 (struct megasas_instance *)instance_addr;
1672 unsigned long flags;
1673
1674 /* If we have already declared adapter dead, donot complete cmds */
39a98554 1675 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
7343eb65 1676 return;
1677
1678 spin_lock_irqsave(&instance->completion_lock, flags);
1679
1680 producer = *instance->producer;
1681 consumer = *instance->consumer;
1682
1683 while (consumer != producer) {
1684 context = instance->reply_queue[consumer];
39a98554 1685 if (context >= instance->max_fw_cmds) {
1686 printk(KERN_ERR "Unexpected context value %x\n",
1687 context);
1688 BUG();
1689 }
7343eb65 1690
1691 cmd = instance->cmd_list[context];
1692
1693 megasas_complete_cmd(instance, cmd, DID_OK);
1694
1695 consumer++;
1696 if (consumer == (instance->max_fw_cmds + 1)) {
1697 consumer = 0;
1698 }
1699 }
1700
1701 *instance->consumer = producer;
1702
1703 spin_unlock_irqrestore(&instance->completion_lock, flags);
1704
1705 /*
1706 * Check if we can restore can_queue
1707 */
9c915a8c 1708 megasas_check_and_restore_queue_depth(instance);
7343eb65 1709}
1710
707e09bd
YB
1711static void
1712megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1713
1714static void
1715process_fw_state_change_wq(struct work_struct *work);
1716
1717void megasas_do_ocr(struct megasas_instance *instance)
1718{
1719 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1720 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1721 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1722 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1723 }
d46a3ad6 1724 instance->instancet->disable_intr(instance);
707e09bd
YB
1725 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1726 instance->issuepend_done = 0;
1727
1728 atomic_set(&instance->fw_outstanding, 0);
1729 megasas_internal_reset_defer_cmds(instance);
1730 process_fw_state_change_wq(&instance->work_init);
1731}
1732
c4a3e0a5
BS
1733/**
1734 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1735 * @instance: Adapter soft state
1736 *
25985edc 1737 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
c4a3e0a5
BS
1738 * complete all its outstanding commands. Returns error if one or more IOs
1739 * are pending after this time period. It also marks the controller dead.
1740 */
1741static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1742{
1743 int i;
39a98554 1744 u32 reset_index;
c4a3e0a5 1745 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
39a98554 1746 u8 adprecovery;
1747 unsigned long flags;
1748 struct list_head clist_local;
1749 struct megasas_cmd *reset_cmd;
707e09bd
YB
1750 u32 fw_state;
1751 u8 kill_adapter_flag;
39a98554 1752
1753 spin_lock_irqsave(&instance->hba_lock, flags);
1754 adprecovery = instance->adprecovery;
1755 spin_unlock_irqrestore(&instance->hba_lock, flags);
1756
1757 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1758
1759 INIT_LIST_HEAD(&clist_local);
1760 spin_lock_irqsave(&instance->hba_lock, flags);
1761 list_splice_init(&instance->internal_reset_pending_q,
1762 &clist_local);
1763 spin_unlock_irqrestore(&instance->hba_lock, flags);
1764
1765 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1766 for (i = 0; i < wait_time; i++) {
1767 msleep(1000);
1768 spin_lock_irqsave(&instance->hba_lock, flags);
1769 adprecovery = instance->adprecovery;
1770 spin_unlock_irqrestore(&instance->hba_lock, flags);
1771 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1772 break;
1773 }
1774
1775 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1776 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1777 spin_lock_irqsave(&instance->hba_lock, flags);
1778 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1779 spin_unlock_irqrestore(&instance->hba_lock, flags);
1780 return FAILED;
1781 }
1782
1783 reset_index = 0;
1784 while (!list_empty(&clist_local)) {
1785 reset_cmd = list_entry((&clist_local)->next,
1786 struct megasas_cmd, list);
1787 list_del_init(&reset_cmd->list);
1788 if (reset_cmd->scmd) {
1789 reset_cmd->scmd->result = DID_RESET << 16;
5cd049a5 1790 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
39a98554 1791 reset_index, reset_cmd,
5cd049a5 1792 reset_cmd->scmd->cmnd[0]);
39a98554 1793
1794 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1795 megasas_return_cmd(instance, reset_cmd);
1796 } else if (reset_cmd->sync_cmd) {
1797 printk(KERN_NOTICE "megasas:%p synch cmds"
1798 "reset queue\n",
1799 reset_cmd);
1800
1801 reset_cmd->cmd_status = ENODATA;
1802 instance->instancet->fire_cmd(instance,
1803 reset_cmd->frame_phys_addr,
1804 0, instance->reg_set);
1805 } else {
1806 printk(KERN_NOTICE "megasas: %p unexpected"
1807 "cmds lst\n",
1808 reset_cmd);
1809 }
1810 reset_index++;
1811 }
1812
1813 return SUCCESS;
1814 }
c4a3e0a5 1815
c007b8b2 1816 for (i = 0; i < resetwaittime; i++) {
c4a3e0a5 1817
e4a082c7
SP
1818 int outstanding = atomic_read(&instance->fw_outstanding);
1819
1820 if (!outstanding)
c4a3e0a5
BS
1821 break;
1822
1823 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1824 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
e4a082c7 1825 "commands to complete\n",i,outstanding);
7343eb65 1826 /*
1827 * Call cmd completion routine. Cmd to be
1828 * be completed directly without depending on isr.
1829 */
1830 megasas_complete_cmd_dpc((unsigned long)instance);
c4a3e0a5
BS
1831 }
1832
1833 msleep(1000);
1834 }
1835
707e09bd
YB
1836 i = 0;
1837 kill_adapter_flag = 0;
1838 do {
1839 fw_state = instance->instancet->read_fw_status_reg(
1840 instance->reg_set) & MFI_STATE_MASK;
1841 if ((fw_state == MFI_STATE_FAULT) &&
1842 (instance->disableOnlineCtrlReset == 0)) {
1843 if (i == 3) {
1844 kill_adapter_flag = 2;
1845 break;
1846 }
1847 megasas_do_ocr(instance);
1848 kill_adapter_flag = 1;
1849
1850 /* wait for 1 secs to let FW finish the pending cmds */
1851 msleep(1000);
1852 }
1853 i++;
1854 } while (i <= 3);
1855
1856 if (atomic_read(&instance->fw_outstanding) &&
1857 !kill_adapter_flag) {
1858 if (instance->disableOnlineCtrlReset == 0) {
1859
1860 megasas_do_ocr(instance);
1861
1862 /* wait for 5 secs to let FW finish the pending cmds */
1863 for (i = 0; i < wait_time; i++) {
1864 int outstanding =
1865 atomic_read(&instance->fw_outstanding);
1866 if (!outstanding)
1867 return SUCCESS;
1868 msleep(1000);
1869 }
1870 }
1871 }
1872
1873 if (atomic_read(&instance->fw_outstanding) ||
1874 (kill_adapter_flag == 2)) {
39a98554 1875 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
e3bbff9f
SP
1876 /*
1877 * Send signal to FW to stop processing any pending cmds.
1878 * The controller will be taken offline by the OS now.
1879 */
0c79e681
YB
1880 if ((instance->pdev->device ==
1881 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1882 (instance->pdev->device ==
1883 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1884 writel(MFI_STOP_ADP,
9c915a8c 1885 &instance->reg_set->doorbell);
0c79e681
YB
1886 } else {
1887 writel(MFI_STOP_ADP,
e3bbff9f 1888 &instance->reg_set->inbound_doorbell);
0c79e681 1889 }
658dcedb 1890 megasas_dump_pending_frames(instance);
39a98554 1891 spin_lock_irqsave(&instance->hba_lock, flags);
1892 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1893 spin_unlock_irqrestore(&instance->hba_lock, flags);
c4a3e0a5
BS
1894 return FAILED;
1895 }
1896
39a98554 1897 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1898
c4a3e0a5
BS
1899 return SUCCESS;
1900}
1901
1902/**
1903 * megasas_generic_reset - Generic reset routine
1904 * @scmd: Mid-layer SCSI command
1905 *
1906 * This routine implements a generic reset handler for device, bus and host
1907 * reset requests. Device, bus and host specific reset handlers can use this
1908 * function after they do their specific tasks.
1909 */
1910static int megasas_generic_reset(struct scsi_cmnd *scmd)
1911{
1912 int ret_val;
1913 struct megasas_instance *instance;
1914
1915 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1916
5cd049a5
CH
1917 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1918 scmd->cmnd[0], scmd->retries);
c4a3e0a5 1919
39a98554 1920 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
c4a3e0a5
BS
1921 printk(KERN_ERR "megasas: cannot recover from previous reset "
1922 "failures\n");
1923 return FAILED;
1924 }
1925
c4a3e0a5 1926 ret_val = megasas_wait_for_outstanding(instance);
c4a3e0a5
BS
1927 if (ret_val == SUCCESS)
1928 printk(KERN_NOTICE "megasas: reset successful \n");
1929 else
1930 printk(KERN_ERR "megasas: failed to do reset\n");
1931
c4a3e0a5
BS
1932 return ret_val;
1933}
1934
05e9ebbe
SP
1935/**
1936 * megasas_reset_timer - quiesce the adapter if required
1937 * @scmd: scsi cmnd
1938 *
1939 * Sets the FW busy flag and reduces the host->can_queue if the
1940 * cmd has not been completed within the timeout period.
1941 */
1942static enum
242f9dcb 1943blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
05e9ebbe 1944{
05e9ebbe
SP
1945 struct megasas_instance *instance;
1946 unsigned long flags;
1947
1948 if (time_after(jiffies, scmd->jiffies_at_alloc +
1949 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
242f9dcb 1950 return BLK_EH_NOT_HANDLED;
05e9ebbe
SP
1951 }
1952
f575c5d3 1953 instance = (struct megasas_instance *)scmd->device->host->hostdata;
05e9ebbe
SP
1954 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1955 /* FW is busy, throttle IO */
1956 spin_lock_irqsave(instance->host->host_lock, flags);
1957
c5daa6a9 1958 instance->host->can_queue = instance->throttlequeuedepth;
05e9ebbe
SP
1959 instance->last_time = jiffies;
1960 instance->flag |= MEGASAS_FW_BUSY;
1961
1962 spin_unlock_irqrestore(instance->host->host_lock, flags);
1963 }
242f9dcb 1964 return BLK_EH_RESET_TIMER;
05e9ebbe
SP
1965}
1966
c4a3e0a5
BS
1967/**
1968 * megasas_reset_device - Device reset handler entry point
1969 */
1970static int megasas_reset_device(struct scsi_cmnd *scmd)
1971{
1972 int ret;
1973
1974 /*
1975 * First wait for all commands to complete
1976 */
1977 ret = megasas_generic_reset(scmd);
1978
1979 return ret;
1980}
1981
1982/**
1983 * megasas_reset_bus_host - Bus & host reset handler entry point
1984 */
1985static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1986{
1987 int ret;
9c915a8c 1988 struct megasas_instance *instance;
1989 instance = (struct megasas_instance *)scmd->device->host->hostdata;
c4a3e0a5
BS
1990
1991 /*
80682fa9 1992 * First wait for all commands to complete
c4a3e0a5 1993 */
36807e67 1994 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
21d3c710
SS
1995 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1996 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
9c915a8c 1997 ret = megasas_reset_fusion(scmd->device->host);
1998 else
1999 ret = megasas_generic_reset(scmd);
c4a3e0a5
BS
2000
2001 return ret;
2002}
2003
cf62a0a5
SP
2004/**
2005 * megasas_bios_param - Returns disk geometry for a disk
2006 * @sdev: device handle
2007 * @bdev: block device
2008 * @capacity: drive capacity
2009 * @geom: geometry parameters
2010 */
2011static int
2012megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2013 sector_t capacity, int geom[])
2014{
2015 int heads;
2016 int sectors;
2017 sector_t cylinders;
2018 unsigned long tmp;
2019 /* Default heads (64) & sectors (32) */
2020 heads = 64;
2021 sectors = 32;
2022
2023 tmp = heads * sectors;
2024 cylinders = capacity;
2025
2026 sector_div(cylinders, tmp);
2027
2028 /*
2029 * Handle extended translation size for logical drives > 1Gb
2030 */
2031
2032 if (capacity >= 0x200000) {
2033 heads = 255;
2034 sectors = 63;
2035 tmp = heads*sectors;
2036 cylinders = capacity;
2037 sector_div(cylinders, tmp);
2038 }
2039
2040 geom[0] = heads;
2041 geom[1] = sectors;
2042 geom[2] = cylinders;
2043
2044 return 0;
2045}
2046
7e8a75f4
YB
2047static void megasas_aen_polling(struct work_struct *work);
2048
c4a3e0a5
BS
2049/**
2050 * megasas_service_aen - Processes an event notification
2051 * @instance: Adapter soft state
2052 * @cmd: AEN command completed by the ISR
2053 *
2054 * For AEN, driver sends a command down to FW that is held by the FW till an
2055 * event occurs. When an event of interest occurs, FW completes the command
2056 * that it was previously holding.
2057 *
2058 * This routines sends SIGIO signal to processes that have registered with the
2059 * driver for AEN.
2060 */
2061static void
2062megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2063{
c3518837 2064 unsigned long flags;
c4a3e0a5
BS
2065 /*
2066 * Don't signal app if it is just an aborted previously registered aen
2067 */
c3518837
YB
2068 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2069 spin_lock_irqsave(&poll_aen_lock, flags);
2070 megasas_poll_wait_aen = 1;
2071 spin_unlock_irqrestore(&poll_aen_lock, flags);
2072 wake_up(&megasas_poll_wait);
c4a3e0a5 2073 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
c3518837 2074 }
c4a3e0a5
BS
2075 else
2076 cmd->abort_aen = 0;
2077
2078 instance->aen_cmd = NULL;
2079 megasas_return_cmd(instance, cmd);
7e8a75f4 2080
39a98554 2081 if ((instance->unload == 0) &&
2082 ((instance->issuepend_done == 1))) {
7e8a75f4
YB
2083 struct megasas_aen_event *ev;
2084 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2085 if (!ev) {
2086 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2087 } else {
2088 ev->instance = instance;
2089 instance->ev = ev;
c1d390d8
XF
2090 INIT_DELAYED_WORK(&ev->hotplug_work,
2091 megasas_aen_polling);
2092 schedule_delayed_work(&ev->hotplug_work, 0);
7e8a75f4
YB
2093 }
2094 }
c4a3e0a5
BS
2095}
2096
4bcde509 2097static int megasas_change_queue_depth(struct scsi_device *sdev,
2098 int queue_depth, int reason)
2099{
2100 if (reason != SCSI_QDEPTH_DEFAULT)
2101 return -EOPNOTSUPP;
2102
2103 if (queue_depth > sdev->host->can_queue)
2104 queue_depth = sdev->host->can_queue;
2105 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2106 queue_depth);
2107
2108 return queue_depth;
2109}
2110
c4a3e0a5
BS
2111/*
2112 * Scsi host template for megaraid_sas driver
2113 */
2114static struct scsi_host_template megasas_template = {
2115
2116 .module = THIS_MODULE,
f28cd7cf 2117 .name = "LSI SAS based MegaRAID driver",
c4a3e0a5 2118 .proc_name = "megaraid_sas",
147aab6a 2119 .slave_configure = megasas_slave_configure,
044833b5 2120 .slave_alloc = megasas_slave_alloc,
c4a3e0a5
BS
2121 .queuecommand = megasas_queue_command,
2122 .eh_device_reset_handler = megasas_reset_device,
2123 .eh_bus_reset_handler = megasas_reset_bus_host,
2124 .eh_host_reset_handler = megasas_reset_bus_host,
05e9ebbe 2125 .eh_timed_out = megasas_reset_timer,
cf62a0a5 2126 .bios_param = megasas_bios_param,
c4a3e0a5 2127 .use_clustering = ENABLE_CLUSTERING,
4bcde509 2128 .change_queue_depth = megasas_change_queue_depth,
c4a3e0a5
BS
2129};
2130
2131/**
2132 * megasas_complete_int_cmd - Completes an internal command
2133 * @instance: Adapter soft state
2134 * @cmd: Command to be completed
2135 *
2136 * The megasas_issue_blocked_cmd() function waits for a command to complete
2137 * after it issues a command. This function wakes up that waiting routine by
2138 * calling wake_up() on the wait queue.
2139 */
2140static void
2141megasas_complete_int_cmd(struct megasas_instance *instance,
2142 struct megasas_cmd *cmd)
2143{
2144 cmd->cmd_status = cmd->frame->io.cmd_status;
2145
2146 if (cmd->cmd_status == ENODATA) {
2147 cmd->cmd_status = 0;
2148 }
2149 wake_up(&instance->int_cmd_wait_q);
2150}
2151
2152/**
2153 * megasas_complete_abort - Completes aborting a command
2154 * @instance: Adapter soft state
2155 * @cmd: Cmd that was issued to abort another cmd
2156 *
0d49016b 2157 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2158 * after it issues an abort on a previously issued command. This function
c4a3e0a5
BS
2159 * wakes up all functions waiting on the same wait queue.
2160 */
2161static void
2162megasas_complete_abort(struct megasas_instance *instance,
2163 struct megasas_cmd *cmd)
2164{
2165 if (cmd->sync_cmd) {
2166 cmd->sync_cmd = 0;
2167 cmd->cmd_status = 0;
2168 wake_up(&instance->abort_cmd_wait_q);
2169 }
2170
2171 return;
2172}
2173
c4a3e0a5
BS
2174/**
2175 * megasas_complete_cmd - Completes a command
2176 * @instance: Adapter soft state
2177 * @cmd: Command to be completed
0d49016b 2178 * @alt_status: If non-zero, use this value as status to
c4a3e0a5
BS
2179 * SCSI mid-layer instead of the value returned
2180 * by the FW. This should be used if caller wants
2181 * an alternate status (as in the case of aborted
2182 * commands)
2183 */
9c915a8c 2184void
c4a3e0a5
BS
2185megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2186 u8 alt_status)
2187{
2188 int exception = 0;
2189 struct megasas_header *hdr = &cmd->frame->hdr;
c3518837 2190 unsigned long flags;
9c915a8c 2191 struct fusion_context *fusion = instance->ctrl_context;
c4a3e0a5 2192
39a98554 2193 /* flag for the retry reset */
2194 cmd->retry_for_fw_reset = 0;
2195
05e9ebbe
SP
2196 if (cmd->scmd)
2197 cmd->scmd->SCp.ptr = NULL;
c4a3e0a5
BS
2198
2199 switch (hdr->cmd) {
e5f93a36 2200 case MFI_CMD_INVALID:
2201 /* Some older 1068 controller FW may keep a pended
2202 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2203 when booting the kdump kernel. Ignore this command to
2204 prevent a kernel panic on shutdown of the kdump kernel. */
2205 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2206 "completed.\n");
2207 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2208 "other than PERC5, please upgrade your firmware.\n");
2209 break;
c4a3e0a5
BS
2210 case MFI_CMD_PD_SCSI_IO:
2211 case MFI_CMD_LD_SCSI_IO:
2212
2213 /*
2214 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2215 * issued either through an IO path or an IOCTL path. If it
2216 * was via IOCTL, we will send it to internal completion.
2217 */
2218 if (cmd->sync_cmd) {
2219 cmd->sync_cmd = 0;
2220 megasas_complete_int_cmd(instance, cmd);
2221 break;
2222 }
2223
c4a3e0a5
BS
2224 case MFI_CMD_LD_READ:
2225 case MFI_CMD_LD_WRITE:
2226
2227 if (alt_status) {
2228 cmd->scmd->result = alt_status << 16;
2229 exception = 1;
2230 }
2231
2232 if (exception) {
2233
e4a082c7 2234 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 2235
155d98f0 2236 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
2237 cmd->scmd->scsi_done(cmd->scmd);
2238 megasas_return_cmd(instance, cmd);
2239
2240 break;
2241 }
2242
2243 switch (hdr->cmd_status) {
2244
2245 case MFI_STAT_OK:
2246 cmd->scmd->result = DID_OK << 16;
2247 break;
2248
2249 case MFI_STAT_SCSI_IO_FAILED:
2250 case MFI_STAT_LD_INIT_IN_PROGRESS:
2251 cmd->scmd->result =
2252 (DID_ERROR << 16) | hdr->scsi_status;
2253 break;
2254
2255 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2256
2257 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2258
2259 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2260 memset(cmd->scmd->sense_buffer, 0,
2261 SCSI_SENSE_BUFFERSIZE);
2262 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2263 hdr->sense_len);
2264
2265 cmd->scmd->result |= DRIVER_SENSE << 24;
2266 }
2267
2268 break;
2269
2270 case MFI_STAT_LD_OFFLINE:
2271 case MFI_STAT_DEVICE_NOT_FOUND:
2272 cmd->scmd->result = DID_BAD_TARGET << 16;
2273 break;
2274
2275 default:
2276 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2277 hdr->cmd_status);
2278 cmd->scmd->result = DID_ERROR << 16;
2279 break;
2280 }
2281
e4a082c7 2282 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 2283
155d98f0 2284 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
2285 cmd->scmd->scsi_done(cmd->scmd);
2286 megasas_return_cmd(instance, cmd);
2287
2288 break;
2289
2290 case MFI_CMD_SMP:
2291 case MFI_CMD_STP:
2292 case MFI_CMD_DCMD:
9c915a8c 2293 /* Check for LD map update */
2294 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2295 (cmd->frame->dcmd.mbox.b[1] == 1)) {
bc93d425 2296 fusion->fast_path_io = 0;
9c915a8c 2297 spin_lock_irqsave(instance->host->host_lock, flags);
2298 if (cmd->frame->hdr.cmd_status != 0) {
2299 if (cmd->frame->hdr.cmd_status !=
2300 MFI_STAT_NOT_FOUND)
2301 printk(KERN_WARNING "megasas: map sync"
2302 "failed, status = 0x%x.\n",
2303 cmd->frame->hdr.cmd_status);
2304 else {
2305 megasas_return_cmd(instance, cmd);
2306 spin_unlock_irqrestore(
2307 instance->host->host_lock,
2308 flags);
2309 break;
2310 }
2311 } else
2312 instance->map_id++;
2313 megasas_return_cmd(instance, cmd);
bc93d425
SS
2314
2315 /*
2316 * Set fast path IO to ZERO.
2317 * Validate Map will set proper value.
2318 * Meanwhile all IOs will go as LD IO.
2319 */
2320 if (MR_ValidateMapInfo(instance))
9c915a8c 2321 fusion->fast_path_io = 1;
2322 else
2323 fusion->fast_path_io = 0;
2324 megasas_sync_map_info(instance);
2325 spin_unlock_irqrestore(instance->host->host_lock,
2326 flags);
2327 break;
2328 }
c3518837
YB
2329 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2330 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2331 spin_lock_irqsave(&poll_aen_lock, flags);
2332 megasas_poll_wait_aen = 0;
2333 spin_unlock_irqrestore(&poll_aen_lock, flags);
2334 }
c4a3e0a5
BS
2335
2336 /*
2337 * See if got an event notification
2338 */
2339 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2340 megasas_service_aen(instance, cmd);
2341 else
2342 megasas_complete_int_cmd(instance, cmd);
2343
2344 break;
2345
2346 case MFI_CMD_ABORT:
2347 /*
2348 * Cmd issued to abort another cmd returned
2349 */
2350 megasas_complete_abort(instance, cmd);
2351 break;
2352
2353 default:
2354 printk("megasas: Unknown command completed! [0x%X]\n",
2355 hdr->cmd);
2356 break;
2357 }
2358}
2359
39a98554 2360/**
2361 * megasas_issue_pending_cmds_again - issue all pending cmds
2362 * in FW again because of the fw reset
2363 * @instance: Adapter soft state
2364 */
2365static inline void
2366megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2367{
2368 struct megasas_cmd *cmd;
2369 struct list_head clist_local;
2370 union megasas_evt_class_locale class_locale;
2371 unsigned long flags;
2372 u32 seq_num;
2373
2374 INIT_LIST_HEAD(&clist_local);
2375 spin_lock_irqsave(&instance->hba_lock, flags);
2376 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2377 spin_unlock_irqrestore(&instance->hba_lock, flags);
2378
2379 while (!list_empty(&clist_local)) {
2380 cmd = list_entry((&clist_local)->next,
2381 struct megasas_cmd, list);
2382 list_del_init(&cmd->list);
2383
2384 if (cmd->sync_cmd || cmd->scmd) {
2385 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2386 "detected to be pending while HBA reset.\n",
2387 cmd, cmd->scmd, cmd->sync_cmd);
2388
2389 cmd->retry_for_fw_reset++;
2390
2391 if (cmd->retry_for_fw_reset == 3) {
2392 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2393 "was tried multiple times during reset."
2394 "Shutting down the HBA\n",
2395 cmd, cmd->scmd, cmd->sync_cmd);
2396 megaraid_sas_kill_hba(instance);
2397
2398 instance->adprecovery =
2399 MEGASAS_HW_CRITICAL_ERROR;
2400 return;
2401 }
2402 }
2403
2404 if (cmd->sync_cmd == 1) {
2405 if (cmd->scmd) {
2406 printk(KERN_NOTICE "megaraid_sas: unexpected"
2407 "cmd attached to internal command!\n");
2408 }
2409 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2410 "on the internal reset queue,"
2411 "issue it again.\n", cmd);
2412 cmd->cmd_status = ENODATA;
2413 instance->instancet->fire_cmd(instance,
2414 cmd->frame_phys_addr ,
2415 0, instance->reg_set);
2416 } else if (cmd->scmd) {
5cd049a5 2417 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
39a98554 2418 "detected on the internal queue, issue again.\n",
5cd049a5 2419 cmd, cmd->scmd->cmnd[0]);
39a98554 2420
2421 atomic_inc(&instance->fw_outstanding);
2422 instance->instancet->fire_cmd(instance,
2423 cmd->frame_phys_addr,
2424 cmd->frame_count-1, instance->reg_set);
2425 } else {
2426 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2427 "internal reset defer list while re-issue!!\n",
2428 cmd);
2429 }
2430 }
2431
2432 if (instance->aen_cmd) {
2433 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2434 megasas_return_cmd(instance, instance->aen_cmd);
2435
2436 instance->aen_cmd = NULL;
2437 }
2438
2439 /*
2440 * Initiate AEN (Asynchronous Event Notification)
2441 */
2442 seq_num = instance->last_seq_num;
2443 class_locale.members.reserved = 0;
2444 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2445 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2446
2447 megasas_register_aen(instance, seq_num, class_locale.word);
2448}
2449
2450/**
2451 * Move the internal reset pending commands to a deferred queue.
2452 *
2453 * We move the commands pending at internal reset time to a
2454 * pending queue. This queue would be flushed after successful
2455 * completion of the internal reset sequence. if the internal reset
2456 * did not complete in time, the kernel reset handler would flush
2457 * these commands.
2458 **/
2459static void
2460megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2461{
2462 struct megasas_cmd *cmd;
2463 int i;
2464 u32 max_cmd = instance->max_fw_cmds;
2465 u32 defer_index;
2466 unsigned long flags;
2467
2468 defer_index = 0;
2469 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2470 for (i = 0; i < max_cmd; i++) {
2471 cmd = instance->cmd_list[i];
2472 if (cmd->sync_cmd == 1 || cmd->scmd) {
2473 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2474 "on the defer queue as internal\n",
2475 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2476
2477 if (!list_empty(&cmd->list)) {
2478 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2479 " moving this cmd:%p, %d %p, it was"
2480 "discovered on some list?\n",
2481 cmd, cmd->sync_cmd, cmd->scmd);
2482
2483 list_del_init(&cmd->list);
2484 }
2485 defer_index++;
2486 list_add_tail(&cmd->list,
2487 &instance->internal_reset_pending_q);
2488 }
2489 }
2490 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2491}
2492
2493
2494static void
2495process_fw_state_change_wq(struct work_struct *work)
2496{
2497 struct megasas_instance *instance =
2498 container_of(work, struct megasas_instance, work_init);
2499 u32 wait;
2500 unsigned long flags;
2501
2502 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2503 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2504 instance->adprecovery);
2505 return ;
2506 }
2507
2508 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2509 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2510 "state, restarting it...\n");
2511
d46a3ad6 2512 instance->instancet->disable_intr(instance);
39a98554 2513 atomic_set(&instance->fw_outstanding, 0);
2514
2515 atomic_set(&instance->fw_reset_no_pci_access, 1);
2516 instance->instancet->adp_reset(instance, instance->reg_set);
2517 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2518
2519 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2520 "initiating next stage...\n");
2521
2522 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2523 "state 2 starting...\n");
2524
2525 /*waitting for about 20 second before start the second init*/
2526 for (wait = 0; wait < 30; wait++) {
2527 msleep(1000);
2528 }
2529
058a8fac 2530 if (megasas_transition_to_ready(instance, 1)) {
39a98554 2531 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2532
2533 megaraid_sas_kill_hba(instance);
2534 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2535 return ;
2536 }
2537
2538 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2539 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2540 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2541 ) {
2542 *instance->consumer = *instance->producer;
2543 } else {
2544 *instance->consumer = 0;
2545 *instance->producer = 0;
2546 }
2547
2548 megasas_issue_init_mfi(instance);
2549
2550 spin_lock_irqsave(&instance->hba_lock, flags);
2551 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2552 spin_unlock_irqrestore(&instance->hba_lock, flags);
d46a3ad6 2553 instance->instancet->enable_intr(instance);
39a98554 2554
2555 megasas_issue_pending_cmds_again(instance);
2556 instance->issuepend_done = 1;
2557 }
2558 return ;
2559}
2560
c4a3e0a5
BS
2561/**
2562 * megasas_deplete_reply_queue - Processes all completed commands
2563 * @instance: Adapter soft state
2564 * @alt_status: Alternate status to be returned to
2565 * SCSI mid-layer instead of the status
2566 * returned by the FW
39a98554 2567 * Note: this must be called with hba lock held
c4a3e0a5 2568 */
858119e1 2569static int
39a98554 2570megasas_deplete_reply_queue(struct megasas_instance *instance,
2571 u8 alt_status)
c4a3e0a5 2572{
39a98554 2573 u32 mfiStatus;
2574 u32 fw_state;
2575
2576 if ((mfiStatus = instance->instancet->check_reset(instance,
2577 instance->reg_set)) == 1) {
2578 return IRQ_HANDLED;
2579 }
2580
2581 if ((mfiStatus = instance->instancet->clear_intr(
2582 instance->reg_set)
2583 ) == 0) {
e1419191 2584 /* Hardware may not set outbound_intr_status in MSI-X mode */
c8e858fe 2585 if (!instance->msix_vectors)
e1419191 2586 return IRQ_NONE;
39a98554 2587 }
2588
2589 instance->mfiStatus = mfiStatus;
2590
2591 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2592 fw_state = instance->instancet->read_fw_status_reg(
2593 instance->reg_set) & MFI_STATE_MASK;
2594
2595 if (fw_state != MFI_STATE_FAULT) {
2596 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2597 fw_state);
2598 }
2599
2600 if ((fw_state == MFI_STATE_FAULT) &&
2601 (instance->disableOnlineCtrlReset == 0)) {
2602 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2603
2604 if ((instance->pdev->device ==
2605 PCI_DEVICE_ID_LSI_SAS1064R) ||
2606 (instance->pdev->device ==
2607 PCI_DEVICE_ID_DELL_PERC5) ||
2608 (instance->pdev->device ==
2609 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2610
2611 *instance->consumer =
2612 MEGASAS_ADPRESET_INPROG_SIGN;
2613 }
2614
2615
d46a3ad6 2616 instance->instancet->disable_intr(instance);
39a98554 2617 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2618 instance->issuepend_done = 0;
2619
2620 atomic_set(&instance->fw_outstanding, 0);
2621 megasas_internal_reset_defer_cmds(instance);
2622
2623 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2624 fw_state, instance->adprecovery);
2625
2626 schedule_work(&instance->work_init);
2627 return IRQ_HANDLED;
2628
2629 } else {
2630 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2631 fw_state, instance->disableOnlineCtrlReset);
2632 }
2633 }
c4a3e0a5 2634
5d018ad0 2635 tasklet_schedule(&instance->isr_tasklet);
c4a3e0a5
BS
2636 return IRQ_HANDLED;
2637}
c4a3e0a5
BS
2638/**
2639 * megasas_isr - isr entry point
2640 */
7d12e780 2641static irqreturn_t megasas_isr(int irq, void *devp)
c4a3e0a5 2642{
c8e858fe 2643 struct megasas_irq_context *irq_context = devp;
2644 struct megasas_instance *instance = irq_context->instance;
39a98554 2645 unsigned long flags;
2646 irqreturn_t rc;
2647
c8e858fe 2648 if (atomic_read(&instance->fw_reset_no_pci_access))
39a98554 2649 return IRQ_HANDLED;
2650
39a98554 2651 spin_lock_irqsave(&instance->hba_lock, flags);
2652 rc = megasas_deplete_reply_queue(instance, DID_OK);
2653 spin_unlock_irqrestore(&instance->hba_lock, flags);
2654
2655 return rc;
c4a3e0a5
BS
2656}
2657
2658/**
2659 * megasas_transition_to_ready - Move the FW to READY state
1341c939 2660 * @instance: Adapter soft state
c4a3e0a5
BS
2661 *
2662 * During the initialization, FW passes can potentially be in any one of
2663 * several possible states. If the FW in operational, waiting-for-handshake
2664 * states, driver must take steps to bring it to ready state. Otherwise, it
2665 * has to wait for the ready state.
2666 */
9c915a8c 2667int
058a8fac 2668megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
c4a3e0a5
BS
2669{
2670 int i;
2671 u8 max_wait;
2672 u32 fw_state;
2673 u32 cur_state;
7218df69 2674 u32 abs_state, curr_abs_state;
c4a3e0a5 2675
1341c939 2676 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
c4a3e0a5 2677
e3bbff9f 2678 if (fw_state != MFI_STATE_READY)
0d49016b 2679 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2680 " state\n");
e3bbff9f 2681
c4a3e0a5
BS
2682 while (fw_state != MFI_STATE_READY) {
2683
7218df69
YB
2684 abs_state =
2685 instance->instancet->read_fw_status_reg(instance->reg_set);
2686
c4a3e0a5
BS
2687 switch (fw_state) {
2688
2689 case MFI_STATE_FAULT:
c4a3e0a5 2690 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
058a8fac 2691 if (ocr) {
2692 max_wait = MEGASAS_RESET_WAIT_TIME;
2693 cur_state = MFI_STATE_FAULT;
2694 break;
2695 } else
2696 return -ENODEV;
c4a3e0a5
BS
2697
2698 case MFI_STATE_WAIT_HANDSHAKE:
2699 /*
2700 * Set the CLR bit in inbound doorbell
2701 */
0c79e681 2702 if ((instance->pdev->device ==
87911122
YB
2703 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2704 (instance->pdev->device ==
9c915a8c 2705 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2706 (instance->pdev->device ==
21d3c710 2707 PCI_DEVICE_ID_LSI_FUSION) ||
36807e67 2708 (instance->pdev->device ==
21d3c710
SS
2709 PCI_DEVICE_ID_LSI_INVADER) ||
2710 (instance->pdev->device ==
2711 PCI_DEVICE_ID_LSI_FURY)) {
87911122
YB
2712 writel(
2713 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
9c915a8c 2714 &instance->reg_set->doorbell);
87911122
YB
2715 } else {
2716 writel(
2717 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2718 &instance->reg_set->inbound_doorbell);
2719 }
c4a3e0a5 2720
7218df69 2721 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2722 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2723 break;
2724
e3bbff9f 2725 case MFI_STATE_BOOT_MESSAGE_PENDING:
87911122 2726 if ((instance->pdev->device ==
9c915a8c 2727 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2728 (instance->pdev->device ==
2729 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2730 (instance->pdev->device ==
36807e67 2731 PCI_DEVICE_ID_LSI_FUSION) ||
2732 (instance->pdev->device ==
21d3c710
SS
2733 PCI_DEVICE_ID_LSI_INVADER) ||
2734 (instance->pdev->device ==
2735 PCI_DEVICE_ID_LSI_FURY)) {
87911122 2736 writel(MFI_INIT_HOTPLUG,
9c915a8c 2737 &instance->reg_set->doorbell);
87911122
YB
2738 } else
2739 writel(MFI_INIT_HOTPLUG,
2740 &instance->reg_set->inbound_doorbell);
e3bbff9f 2741
7218df69 2742 max_wait = MEGASAS_RESET_WAIT_TIME;
e3bbff9f
SP
2743 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2744 break;
2745
c4a3e0a5
BS
2746 case MFI_STATE_OPERATIONAL:
2747 /*
e3bbff9f 2748 * Bring it to READY state; assuming max wait 10 secs
c4a3e0a5 2749 */
d46a3ad6 2750 instance->instancet->disable_intr(instance);
87911122
YB
2751 if ((instance->pdev->device ==
2752 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2753 (instance->pdev->device ==
9c915a8c 2754 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2755 (instance->pdev->device
36807e67 2756 == PCI_DEVICE_ID_LSI_FUSION) ||
2757 (instance->pdev->device
21d3c710
SS
2758 == PCI_DEVICE_ID_LSI_INVADER) ||
2759 (instance->pdev->device
2760 == PCI_DEVICE_ID_LSI_FURY)) {
87911122 2761 writel(MFI_RESET_FLAGS,
9c915a8c 2762 &instance->reg_set->doorbell);
36807e67 2763 if ((instance->pdev->device ==
21d3c710
SS
2764 PCI_DEVICE_ID_LSI_FUSION) ||
2765 (instance->pdev->device ==
2766 PCI_DEVICE_ID_LSI_INVADER) ||
2767 (instance->pdev->device ==
2768 PCI_DEVICE_ID_LSI_FURY)) {
9c915a8c 2769 for (i = 0; i < (10 * 1000); i += 20) {
2770 if (readl(
2771 &instance->
2772 reg_set->
2773 doorbell) & 1)
2774 msleep(20);
2775 else
2776 break;
2777 }
2778 }
87911122
YB
2779 } else
2780 writel(MFI_RESET_FLAGS,
2781 &instance->reg_set->inbound_doorbell);
c4a3e0a5 2782
7218df69 2783 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2784 cur_state = MFI_STATE_OPERATIONAL;
2785 break;
2786
2787 case MFI_STATE_UNDEFINED:
2788 /*
2789 * This state should not last for more than 2 seconds
2790 */
7218df69 2791 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2792 cur_state = MFI_STATE_UNDEFINED;
2793 break;
2794
2795 case MFI_STATE_BB_INIT:
7218df69 2796 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2797 cur_state = MFI_STATE_BB_INIT;
2798 break;
2799
2800 case MFI_STATE_FW_INIT:
7218df69 2801 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2802 cur_state = MFI_STATE_FW_INIT;
2803 break;
2804
2805 case MFI_STATE_FW_INIT_2:
7218df69 2806 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2807 cur_state = MFI_STATE_FW_INIT_2;
2808 break;
2809
2810 case MFI_STATE_DEVICE_SCAN:
7218df69 2811 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2812 cur_state = MFI_STATE_DEVICE_SCAN;
2813 break;
2814
2815 case MFI_STATE_FLUSH_CACHE:
7218df69 2816 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
2817 cur_state = MFI_STATE_FLUSH_CACHE;
2818 break;
2819
2820 default:
2821 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2822 fw_state);
2823 return -ENODEV;
2824 }
2825
2826 /*
2827 * The cur_state should not last for more than max_wait secs
2828 */
2829 for (i = 0; i < (max_wait * 1000); i++) {
0d49016b 2830 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1341c939 2831 MFI_STATE_MASK ;
7218df69
YB
2832 curr_abs_state =
2833 instance->instancet->read_fw_status_reg(instance->reg_set);
c4a3e0a5 2834
7218df69 2835 if (abs_state == curr_abs_state) {
c4a3e0a5
BS
2836 msleep(1);
2837 } else
2838 break;
2839 }
2840
2841 /*
2842 * Return error if fw_state hasn't changed after max_wait
2843 */
7218df69 2844 if (curr_abs_state == abs_state) {
c4a3e0a5
BS
2845 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2846 "in %d secs\n", fw_state, max_wait);
2847 return -ENODEV;
2848 }
39a98554 2849 }
0d49016b 2850 printk(KERN_INFO "megasas: FW now in Ready state\n");
c4a3e0a5
BS
2851
2852 return 0;
2853}
2854
2855/**
2856 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2857 * @instance: Adapter soft state
2858 */
2859static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2860{
2861 int i;
9c915a8c 2862 u32 max_cmd = instance->max_mfi_cmds;
c4a3e0a5
BS
2863 struct megasas_cmd *cmd;
2864
2865 if (!instance->frame_dma_pool)
2866 return;
2867
2868 /*
2869 * Return all frames to pool
2870 */
2871 for (i = 0; i < max_cmd; i++) {
2872
2873 cmd = instance->cmd_list[i];
2874
2875 if (cmd->frame)
2876 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2877 cmd->frame_phys_addr);
2878
2879 if (cmd->sense)
e3bbff9f 2880 pci_pool_free(instance->sense_dma_pool, cmd->sense,
c4a3e0a5
BS
2881 cmd->sense_phys_addr);
2882 }
2883
2884 /*
2885 * Now destroy the pool itself
2886 */
2887 pci_pool_destroy(instance->frame_dma_pool);
2888 pci_pool_destroy(instance->sense_dma_pool);
2889
2890 instance->frame_dma_pool = NULL;
2891 instance->sense_dma_pool = NULL;
2892}
2893
2894/**
2895 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2896 * @instance: Adapter soft state
2897 *
2898 * Each command packet has an embedded DMA memory buffer that is used for
2899 * filling MFI frame and the SG list that immediately follows the frame. This
2900 * function creates those DMA memory buffers for each command packet by using
2901 * PCI pool facility.
2902 */
2903static int megasas_create_frame_pool(struct megasas_instance *instance)
2904{
2905 int i;
2906 u32 max_cmd;
2907 u32 sge_sz;
2908 u32 sgl_sz;
2909 u32 total_sz;
2910 u32 frame_count;
2911 struct megasas_cmd *cmd;
2912
9c915a8c 2913 max_cmd = instance->max_mfi_cmds;
c4a3e0a5
BS
2914
2915 /*
2916 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2917 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2918 */
2919 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2920 sizeof(struct megasas_sge32);
2921
f4c9a131
YB
2922 if (instance->flag_ieee) {
2923 sge_sz = sizeof(struct megasas_sge_skinny);
2924 }
2925
c4a3e0a5
BS
2926 /*
2927 * Calculated the number of 64byte frames required for SGL
2928 */
2929 sgl_sz = sge_sz * instance->max_num_sge;
2930 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
39a98554 2931 frame_count = 15;
c4a3e0a5
BS
2932
2933 /*
2934 * We need one extra frame for the MFI command
2935 */
2936 frame_count++;
2937
2938 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2939 /*
2940 * Use DMA pool facility provided by PCI layer
2941 */
2942 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2943 instance->pdev, total_sz, 64,
2944 0);
2945
2946 if (!instance->frame_dma_pool) {
2947 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2948 return -ENOMEM;
2949 }
2950
2951 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2952 instance->pdev, 128, 4, 0);
2953
2954 if (!instance->sense_dma_pool) {
2955 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2956
2957 pci_pool_destroy(instance->frame_dma_pool);
2958 instance->frame_dma_pool = NULL;
2959
2960 return -ENOMEM;
2961 }
2962
2963 /*
2964 * Allocate and attach a frame to each of the commands in cmd_list.
2965 * By making cmd->index as the context instead of the &cmd, we can
2966 * always use 32bit context regardless of the architecture
2967 */
2968 for (i = 0; i < max_cmd; i++) {
2969
2970 cmd = instance->cmd_list[i];
2971
2972 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2973 GFP_KERNEL, &cmd->frame_phys_addr);
2974
2975 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2976 GFP_KERNEL, &cmd->sense_phys_addr);
2977
2978 /*
2979 * megasas_teardown_frame_pool() takes care of freeing
2980 * whatever has been allocated
2981 */
2982 if (!cmd->frame || !cmd->sense) {
2983 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2984 megasas_teardown_frame_pool(instance);
2985 return -ENOMEM;
2986 }
2987
707e09bd 2988 memset(cmd->frame, 0, total_sz);
c4a3e0a5 2989 cmd->frame->io.context = cmd->index;
7e8a75f4 2990 cmd->frame->io.pad_0 = 0;
e5f93a36 2991 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2992 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
21d3c710 2993 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
e5f93a36 2994 (reset_devices))
2995 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
c4a3e0a5
BS
2996 }
2997
2998 return 0;
2999}
3000
3001/**
3002 * megasas_free_cmds - Free all the cmds in the free cmd pool
3003 * @instance: Adapter soft state
3004 */
9c915a8c 3005void megasas_free_cmds(struct megasas_instance *instance)
c4a3e0a5
BS
3006{
3007 int i;
3008 /* First free the MFI frame pool */
3009 megasas_teardown_frame_pool(instance);
3010
3011 /* Free all the commands in the cmd_list */
9c915a8c 3012 for (i = 0; i < instance->max_mfi_cmds; i++)
3013
c4a3e0a5
BS
3014 kfree(instance->cmd_list[i]);
3015
3016 /* Free the cmd_list buffer itself */
3017 kfree(instance->cmd_list);
3018 instance->cmd_list = NULL;
3019
3020 INIT_LIST_HEAD(&instance->cmd_pool);
3021}
3022
3023/**
3024 * megasas_alloc_cmds - Allocates the command packets
3025 * @instance: Adapter soft state
3026 *
3027 * Each command that is issued to the FW, whether IO commands from the OS or
3028 * internal commands like IOCTLs, are wrapped in local data structure called
3029 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3030 * the FW.
3031 *
3032 * Each frame has a 32-bit field called context (tag). This context is used
3033 * to get back the megasas_cmd from the frame when a frame gets completed in
3034 * the ISR. Typically the address of the megasas_cmd itself would be used as
3035 * the context. But we wanted to keep the differences between 32 and 64 bit
3036 * systems to the mininum. We always use 32 bit integers for the context. In
3037 * this driver, the 32 bit values are the indices into an array cmd_list.
3038 * This array is used only to look up the megasas_cmd given the context. The
3039 * free commands themselves are maintained in a linked list called cmd_pool.
3040 */
9c915a8c 3041int megasas_alloc_cmds(struct megasas_instance *instance)
c4a3e0a5
BS
3042{
3043 int i;
3044 int j;
3045 u32 max_cmd;
3046 struct megasas_cmd *cmd;
3047
9c915a8c 3048 max_cmd = instance->max_mfi_cmds;
c4a3e0a5
BS
3049
3050 /*
3051 * instance->cmd_list is an array of struct megasas_cmd pointers.
3052 * Allocate the dynamic array first and then allocate individual
3053 * commands.
3054 */
dd00cc48 3055 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
c4a3e0a5
BS
3056
3057 if (!instance->cmd_list) {
3058 printk(KERN_DEBUG "megasas: out of memory\n");
3059 return -ENOMEM;
3060 }
3061
9c915a8c 3062 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
c4a3e0a5
BS
3063
3064 for (i = 0; i < max_cmd; i++) {
3065 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3066 GFP_KERNEL);
3067
3068 if (!instance->cmd_list[i]) {
3069
3070 for (j = 0; j < i; j++)
3071 kfree(instance->cmd_list[j]);
3072
3073 kfree(instance->cmd_list);
3074 instance->cmd_list = NULL;
3075
3076 return -ENOMEM;
3077 }
3078 }
3079
3080 /*
3081 * Add all the commands to command pool (instance->cmd_pool)
3082 */
3083 for (i = 0; i < max_cmd; i++) {
3084 cmd = instance->cmd_list[i];
3085 memset(cmd, 0, sizeof(struct megasas_cmd));
3086 cmd->index = i;
39a98554 3087 cmd->scmd = NULL;
c4a3e0a5
BS
3088 cmd->instance = instance;
3089
3090 list_add_tail(&cmd->list, &instance->cmd_pool);
3091 }
3092
3093 /*
3094 * Create a frame pool and assign one frame to each cmd
3095 */
3096 if (megasas_create_frame_pool(instance)) {
3097 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3098 megasas_free_cmds(instance);
3099 }
3100
3101 return 0;
3102}
3103
81e403ce
YB
3104/*
3105 * megasas_get_pd_list_info - Returns FW's pd_list structure
3106 * @instance: Adapter soft state
3107 * @pd_list: pd_list structure
3108 *
3109 * Issues an internal command (DCMD) to get the FW's controller PD
3110 * list structure. This information is mainly used to find out SYSTEM
3111 * supported by the FW.
3112 */
3113static int
3114megasas_get_pd_list(struct megasas_instance *instance)
3115{
3116 int ret = 0, pd_index = 0;
3117 struct megasas_cmd *cmd;
3118 struct megasas_dcmd_frame *dcmd;
3119 struct MR_PD_LIST *ci;
3120 struct MR_PD_ADDRESS *pd_addr;
3121 dma_addr_t ci_h = 0;
3122
3123 cmd = megasas_get_cmd(instance);
3124
3125 if (!cmd) {
3126 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3127 return -ENOMEM;
3128 }
3129
3130 dcmd = &cmd->frame->dcmd;
3131
3132 ci = pci_alloc_consistent(instance->pdev,
3133 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3134
3135 if (!ci) {
3136 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3137 megasas_return_cmd(instance, cmd);
3138 return -ENOMEM;
3139 }
3140
3141 memset(ci, 0, sizeof(*ci));
3142 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3143
3144 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3145 dcmd->mbox.b[1] = 0;
3146 dcmd->cmd = MFI_CMD_DCMD;
3147 dcmd->cmd_status = 0xFF;
3148 dcmd->sge_count = 1;
3149 dcmd->flags = MFI_FRAME_DIR_READ;
3150 dcmd->timeout = 0;
780a3762 3151 dcmd->pad_0 = 0;
81e403ce
YB
3152 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3153 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3154 dcmd->sgl.sge32[0].phys_addr = ci_h;
3155 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3156
3157 if (!megasas_issue_polled(instance, cmd)) {
3158 ret = 0;
3159 } else {
3160 ret = -1;
3161 }
3162
3163 /*
3164 * the following function will get the instance PD LIST.
3165 */
3166
3167 pd_addr = ci->addr;
3168
3169 if ( ret == 0 &&
3170 (ci->count <
3171 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3172
3173 memset(instance->pd_list, 0,
3174 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3175
3176 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3177
3178 instance->pd_list[pd_addr->deviceId].tid =
3179 pd_addr->deviceId;
3180 instance->pd_list[pd_addr->deviceId].driveType =
3181 pd_addr->scsiDevType;
3182 instance->pd_list[pd_addr->deviceId].driveState =
3183 MR_PD_STATE_SYSTEM;
3184 pd_addr++;
3185 }
3186 }
3187
3188 pci_free_consistent(instance->pdev,
3189 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3190 ci, ci_h);
3191 megasas_return_cmd(instance, cmd);
3192
3193 return ret;
3194}
3195
bdc6fb8d
YB
3196/*
3197 * megasas_get_ld_list_info - Returns FW's ld_list structure
3198 * @instance: Adapter soft state
3199 * @ld_list: ld_list structure
3200 *
3201 * Issues an internal command (DCMD) to get the FW's controller PD
3202 * list structure. This information is mainly used to find out SYSTEM
3203 * supported by the FW.
3204 */
3205static int
3206megasas_get_ld_list(struct megasas_instance *instance)
3207{
3208 int ret = 0, ld_index = 0, ids = 0;
3209 struct megasas_cmd *cmd;
3210 struct megasas_dcmd_frame *dcmd;
3211 struct MR_LD_LIST *ci;
3212 dma_addr_t ci_h = 0;
3213
3214 cmd = megasas_get_cmd(instance);
3215
3216 if (!cmd) {
3217 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3218 return -ENOMEM;
3219 }
3220
3221 dcmd = &cmd->frame->dcmd;
3222
3223 ci = pci_alloc_consistent(instance->pdev,
3224 sizeof(struct MR_LD_LIST),
3225 &ci_h);
3226
3227 if (!ci) {
3228 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3229 megasas_return_cmd(instance, cmd);
3230 return -ENOMEM;
3231 }
3232
3233 memset(ci, 0, sizeof(*ci));
3234 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3235
3236 dcmd->cmd = MFI_CMD_DCMD;
3237 dcmd->cmd_status = 0xFF;
3238 dcmd->sge_count = 1;
3239 dcmd->flags = MFI_FRAME_DIR_READ;
3240 dcmd->timeout = 0;
3241 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3242 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3243 dcmd->sgl.sge32[0].phys_addr = ci_h;
3244 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3245 dcmd->pad_0 = 0;
3246
3247 if (!megasas_issue_polled(instance, cmd)) {
3248 ret = 0;
3249 } else {
3250 ret = -1;
3251 }
3252
3253 /* the following function will get the instance PD LIST */
3254
39a98554 3255 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
bdc6fb8d
YB
3256 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3257
3258 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3259 if (ci->ldList[ld_index].state != 0) {
3260 ids = ci->ldList[ld_index].ref.targetId;
3261 instance->ld_ids[ids] =
3262 ci->ldList[ld_index].ref.targetId;
3263 }
3264 }
3265 }
3266
3267 pci_free_consistent(instance->pdev,
3268 sizeof(struct MR_LD_LIST),
3269 ci,
3270 ci_h);
3271
3272 megasas_return_cmd(instance, cmd);
3273 return ret;
3274}
3275
c4a3e0a5
BS
3276/**
3277 * megasas_get_controller_info - Returns FW's controller structure
3278 * @instance: Adapter soft state
3279 * @ctrl_info: Controller information structure
3280 *
3281 * Issues an internal command (DCMD) to get the FW's controller structure.
3282 * This information is mainly used to find out the maximum IO transfer per
3283 * command supported by the FW.
3284 */
3285static int
3286megasas_get_ctrl_info(struct megasas_instance *instance,
3287 struct megasas_ctrl_info *ctrl_info)
3288{
3289 int ret = 0;
3290 struct megasas_cmd *cmd;
3291 struct megasas_dcmd_frame *dcmd;
3292 struct megasas_ctrl_info *ci;
3293 dma_addr_t ci_h = 0;
3294
3295 cmd = megasas_get_cmd(instance);
3296
3297 if (!cmd) {
3298 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3299 return -ENOMEM;
3300 }
3301
3302 dcmd = &cmd->frame->dcmd;
3303
3304 ci = pci_alloc_consistent(instance->pdev,
3305 sizeof(struct megasas_ctrl_info), &ci_h);
3306
3307 if (!ci) {
3308 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3309 megasas_return_cmd(instance, cmd);
3310 return -ENOMEM;
3311 }
3312
3313 memset(ci, 0, sizeof(*ci));
3314 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3315
3316 dcmd->cmd = MFI_CMD_DCMD;
3317 dcmd->cmd_status = 0xFF;
3318 dcmd->sge_count = 1;
3319 dcmd->flags = MFI_FRAME_DIR_READ;
3320 dcmd->timeout = 0;
780a3762 3321 dcmd->pad_0 = 0;
c4a3e0a5
BS
3322 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3323 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3324 dcmd->sgl.sge32[0].phys_addr = ci_h;
3325 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3326
3327 if (!megasas_issue_polled(instance, cmd)) {
3328 ret = 0;
3329 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3330 } else {
3331 ret = -1;
3332 }
3333
3334 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3335 ci, ci_h);
3336
3337 megasas_return_cmd(instance, cmd);
3338 return ret;
3339}
3340
31ea7088 3341/**
3342 * megasas_issue_init_mfi - Initializes the FW
3343 * @instance: Adapter soft state
3344 *
3345 * Issues the INIT MFI cmd
3346 */
3347static int
3348megasas_issue_init_mfi(struct megasas_instance *instance)
3349{
3350 u32 context;
3351
3352 struct megasas_cmd *cmd;
3353
3354 struct megasas_init_frame *init_frame;
3355 struct megasas_init_queue_info *initq_info;
3356 dma_addr_t init_frame_h;
3357 dma_addr_t initq_info_h;
3358
3359 /*
3360 * Prepare a init frame. Note the init frame points to queue info
3361 * structure. Each frame has SGL allocated after first 64 bytes. For
3362 * this frame - since we don't need any SGL - we use SGL's space as
3363 * queue info structure
3364 *
3365 * We will not get a NULL command below. We just created the pool.
3366 */
3367 cmd = megasas_get_cmd(instance);
3368
3369 init_frame = (struct megasas_init_frame *)cmd->frame;
3370 initq_info = (struct megasas_init_queue_info *)
3371 ((unsigned long)init_frame + 64);
3372
3373 init_frame_h = cmd->frame_phys_addr;
3374 initq_info_h = init_frame_h + 64;
3375
3376 context = init_frame->context;
3377 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3378 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3379 init_frame->context = context;
3380
3381 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3382 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3383
3384 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3385 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3386
3387 init_frame->cmd = MFI_CMD_INIT;
3388 init_frame->cmd_status = 0xFF;
3389 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3390
3391 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3392
3393 /*
3394 * disable the intr before firing the init frame to FW
3395 */
d46a3ad6 3396 instance->instancet->disable_intr(instance);
31ea7088 3397
3398 /*
3399 * Issue the init frame in polled mode
3400 */
3401
3402 if (megasas_issue_polled(instance, cmd)) {
3403 printk(KERN_ERR "megasas: Failed to init firmware\n");
3404 megasas_return_cmd(instance, cmd);
3405 goto fail_fw_init;
3406 }
3407
3408 megasas_return_cmd(instance, cmd);
3409
3410 return 0;
3411
3412fail_fw_init:
3413 return -EINVAL;
3414}
3415
cd50ba8e 3416static u32
3417megasas_init_adapter_mfi(struct megasas_instance *instance)
c4a3e0a5 3418{
cd50ba8e 3419 struct megasas_register_set __iomem *reg_set;
c4a3e0a5
BS
3420 u32 context_sz;
3421 u32 reply_q_sz;
c4a3e0a5
BS
3422
3423 reg_set = instance->reg_set;
3424
c4a3e0a5
BS
3425 /*
3426 * Get various operational parameters from status register
3427 */
1341c939 3428 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
e3bbff9f
SP
3429 /*
3430 * Reduce the max supported cmds by 1. This is to ensure that the
3431 * reply_q_sz (1 more than the max cmd that driver may send)
3432 * does not exceed max cmds that the FW can support
3433 */
3434 instance->max_fw_cmds = instance->max_fw_cmds-1;
9c915a8c 3435 instance->max_mfi_cmds = instance->max_fw_cmds;
0d49016b 3436 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1341c939 3437 0x10;
c4a3e0a5
BS
3438 /*
3439 * Create a pool of commands
3440 */
3441 if (megasas_alloc_cmds(instance))
3442 goto fail_alloc_cmds;
3443
3444 /*
3445 * Allocate memory for reply queue. Length of reply queue should
3446 * be _one_ more than the maximum commands handled by the firmware.
3447 *
3448 * Note: When FW completes commands, it places corresponding contex
3449 * values in this circular reply queue. This circular queue is a fairly
3450 * typical producer-consumer queue. FW is the producer (of completed
3451 * commands) and the driver is the consumer.
3452 */
3453 context_sz = sizeof(u32);
3454 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3455
3456 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3457 reply_q_sz,
3458 &instance->reply_queue_h);
3459
3460 if (!instance->reply_queue) {
3461 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3462 goto fail_reply_queue;
3463 }
3464
31ea7088 3465 if (megasas_issue_init_mfi(instance))
c4a3e0a5 3466 goto fail_fw_init;
c4a3e0a5 3467
39a98554 3468 instance->fw_support_ieee = 0;
3469 instance->fw_support_ieee =
3470 (instance->instancet->read_fw_status_reg(reg_set) &
3471 0x04000000);
3472
3473 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3474 instance->fw_support_ieee);
3475
3476 if (instance->fw_support_ieee)
3477 instance->flag_ieee = 1;
3478
cd50ba8e 3479 return 0;
3480
3481fail_fw_init:
3482
3483 pci_free_consistent(instance->pdev, reply_q_sz,
3484 instance->reply_queue, instance->reply_queue_h);
3485fail_reply_queue:
3486 megasas_free_cmds(instance);
3487
3488fail_alloc_cmds:
cd50ba8e 3489 return 1;
3490}
3491
3492/**
3493 * megasas_init_fw - Initializes the FW
3494 * @instance: Adapter soft state
3495 *
3496 * This is the main function for initializing firmware
3497 */
3498
3499static int megasas_init_fw(struct megasas_instance *instance)
3500{
3501 u32 max_sectors_1;
3502 u32 max_sectors_2;
d46a3ad6 3503 u32 tmp_sectors, msix_enable, scratch_pad_2;
cd50ba8e 3504 struct megasas_register_set __iomem *reg_set;
3505 struct megasas_ctrl_info *ctrl_info;
3506 unsigned long bar_list;
d46a3ad6 3507 int i, loop, fw_msix_count = 0;
cd50ba8e 3508
3509 /* Find first memory bar */
3510 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3511 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3512 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3513 if (pci_request_selected_regions(instance->pdev, instance->bar,
3514 "megasas: LSI")) {
3515 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3516 return -EBUSY;
3517 }
3518
3519 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3520
3521 if (!instance->reg_set) {
3522 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3523 goto fail_ioremap;
3524 }
3525
3526 reg_set = instance->reg_set;
3527
3528 switch (instance->pdev->device) {
9c915a8c 3529 case PCI_DEVICE_ID_LSI_FUSION:
36807e67 3530 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 3531 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 3532 instance->instancet = &megasas_instance_template_fusion;
3533 break;
cd50ba8e 3534 case PCI_DEVICE_ID_LSI_SAS1078R:
3535 case PCI_DEVICE_ID_LSI_SAS1078DE:
3536 instance->instancet = &megasas_instance_template_ppc;
3537 break;
3538 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3539 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3540 instance->instancet = &megasas_instance_template_gen2;
3541 break;
3542 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3543 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3544 instance->instancet = &megasas_instance_template_skinny;
3545 break;
3546 case PCI_DEVICE_ID_LSI_SAS1064R:
3547 case PCI_DEVICE_ID_DELL_PERC5:
3548 default:
3549 instance->instancet = &megasas_instance_template_xscale;
3550 break;
3551 }
3552
3553 /*
3554 * We expect the FW state to be READY
3555 */
058a8fac 3556 if (megasas_transition_to_ready(instance, 0))
cd50ba8e 3557 goto fail_ready_state;
3558
d46a3ad6
SS
3559 /*
3560 * MSI-X host index 0 is common for all adapter.
3561 * It is used for all MPT based Adapters.
3562 */
3563 instance->reply_post_host_index_addr[0] =
3564 (u32 *)((u8 *)instance->reg_set +
3565 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
3566
3f1abce4 3567 /* Check if MSI-X is supported while in ready state */
3568 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3569 0x4000000) >> 0x1a;
c8e858fe 3570 if (msix_enable && !msix_disable) {
d46a3ad6
SS
3571 scratch_pad_2 = readl
3572 (&instance->reg_set->outbound_scratch_pad_2);
c8e858fe 3573 /* Check max MSI-X vectors */
d46a3ad6
SS
3574 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
3575 instance->msix_vectors = (scratch_pad_2
3576 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
3577 fw_msix_count = instance->msix_vectors;
079eaddf 3578 if (msix_vectors)
3579 instance->msix_vectors =
3580 min(msix_vectors,
3581 instance->msix_vectors);
d46a3ad6
SS
3582 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
3583 || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
3584 /* Invader/Fury supports more than 8 MSI-X */
3585 instance->msix_vectors = ((scratch_pad_2
3586 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
3587 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
3588 fw_msix_count = instance->msix_vectors;
3589 /* Save 1-15 reply post index address to local memory
3590 * Index 0 is already saved from reg offset
3591 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
3592 */
3593 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
3594 instance->reply_post_host_index_addr[loop] =
3595 (u32 *)((u8 *)instance->reg_set +
3596 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
3597 + (loop * 0x10));
3598 }
3599 if (msix_vectors)
3600 instance->msix_vectors = min(msix_vectors,
3601 instance->msix_vectors);
c8e858fe 3602 } else
3603 instance->msix_vectors = 1;
3604 /* Don't bother allocating more MSI-X vectors than cpus */
3605 instance->msix_vectors = min(instance->msix_vectors,
3606 (unsigned int)num_online_cpus());
3607 for (i = 0; i < instance->msix_vectors; i++)
3608 instance->msixentry[i].entry = i;
3609 i = pci_enable_msix(instance->pdev, instance->msixentry,
3610 instance->msix_vectors);
3611 if (i >= 0) {
3612 if (i) {
3613 if (!pci_enable_msix(instance->pdev,
3614 instance->msixentry, i))
3615 instance->msix_vectors = i;
3616 else
3617 instance->msix_vectors = 0;
3618 }
3619 } else
3620 instance->msix_vectors = 0;
d46a3ad6
SS
3621
3622 dev_info(&instance->pdev->dev, "[scsi%d]: FW supports"
3623 "<%d> MSIX vector,Online CPUs: <%d>,"
3624 "Current MSIX <%d>\n", instance->host->host_no,
3625 fw_msix_count, (unsigned int)num_online_cpus(),
3626 instance->msix_vectors);
c8e858fe 3627 }
3f1abce4 3628
cd50ba8e 3629 /* Get operational params, sge flags, send init cmd to controller */
3630 if (instance->instancet->init_adapter(instance))
eb1b1237 3631 goto fail_init_adapter;
cd50ba8e 3632
3633 printk(KERN_ERR "megasas: INIT adapter done\n");
3634
39a98554 3635 /** for passthrough
3636 * the following function will get the PD LIST.
3637 */
3638
81e403ce
YB
3639 memset(instance->pd_list, 0 ,
3640 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3641 megasas_get_pd_list(instance);
3642
bdc6fb8d
YB
3643 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3644 megasas_get_ld_list(instance);
3645
c4a3e0a5
BS
3646 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3647
3648 /*
3649 * Compute the max allowed sectors per IO: The controller info has two
3650 * limits on max sectors. Driver should use the minimum of these two.
3651 *
3652 * 1 << stripe_sz_ops.min = max sectors per strip
3653 *
3654 * Note that older firmwares ( < FW ver 30) didn't report information
3655 * to calculate max_sectors_1. So the number ended up as zero always.
3656 */
14faea9f 3657 tmp_sectors = 0;
c4a3e0a5
BS
3658 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3659
3660 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3661 ctrl_info->max_strips_per_io;
3662 max_sectors_2 = ctrl_info->max_request_size;
3663
14faea9f 3664 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
39a98554 3665 instance->disableOnlineCtrlReset =
3666 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
bc93d425
SS
3667 instance->UnevenSpanSupport =
3668 ctrl_info->adapterOperations2.supportUnevenSpans;
3669 if (instance->UnevenSpanSupport) {
3670 struct fusion_context *fusion = instance->ctrl_context;
3671 dev_info(&instance->pdev->dev, "FW supports: "
3672 "UnevenSpanSupport=%x\n", instance->UnevenSpanSupport);
3673 if (MR_ValidateMapInfo(instance))
3674 fusion->fast_path_io = 1;
3675 else
3676 fusion->fast_path_io = 0;
3677
3678 }
14faea9f 3679 }
3680
3681 instance->max_sectors_per_req = instance->max_num_sge *
3682 PAGE_SIZE / 512;
3683 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3684 instance->max_sectors_per_req = tmp_sectors;
c4a3e0a5
BS
3685
3686 kfree(ctrl_info);
3687
c5daa6a9 3688 /* Check for valid throttlequeuedepth module parameter */
3689 if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3690 instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3691 if (throttlequeuedepth > (instance->max_fw_cmds -
3692 MEGASAS_SKINNY_INT_CMDS))
3693 instance->throttlequeuedepth =
3694 MEGASAS_THROTTLE_QUEUE_DEPTH;
3695 else
3696 instance->throttlequeuedepth = throttlequeuedepth;
3697 } else {
3698 if (throttlequeuedepth > (instance->max_fw_cmds -
3699 MEGASAS_INT_CMDS))
3700 instance->throttlequeuedepth =
3701 MEGASAS_THROTTLE_QUEUE_DEPTH;
3702 else
3703 instance->throttlequeuedepth = throttlequeuedepth;
3704 }
3705
5d018ad0
SP
3706 /*
3707 * Setup tasklet for cmd completion
3708 */
3709
f86c5424 3710 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
ad84db2e 3711 (unsigned long)instance);
3712
c4a3e0a5
BS
3713 return 0;
3714
eb1b1237 3715fail_init_adapter:
cd50ba8e 3716fail_ready_state:
c4a3e0a5
BS
3717 iounmap(instance->reg_set);
3718
3719 fail_ioremap:
b6d5d880 3720 pci_release_selected_regions(instance->pdev, instance->bar);
c4a3e0a5
BS
3721
3722 return -EINVAL;
3723}
3724
3725/**
3726 * megasas_release_mfi - Reverses the FW initialization
3727 * @intance: Adapter soft state
3728 */
3729static void megasas_release_mfi(struct megasas_instance *instance)
3730{
9c915a8c 3731 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
c4a3e0a5 3732
9c915a8c 3733 if (instance->reply_queue)
3734 pci_free_consistent(instance->pdev, reply_q_sz,
c4a3e0a5
BS
3735 instance->reply_queue, instance->reply_queue_h);
3736
3737 megasas_free_cmds(instance);
3738
3739 iounmap(instance->reg_set);
3740
b6d5d880 3741 pci_release_selected_regions(instance->pdev, instance->bar);
c4a3e0a5
BS
3742}
3743
3744/**
3745 * megasas_get_seq_num - Gets latest event sequence numbers
3746 * @instance: Adapter soft state
3747 * @eli: FW event log sequence numbers information
3748 *
3749 * FW maintains a log of all events in a non-volatile area. Upper layers would
3750 * usually find out the latest sequence number of the events, the seq number at
3751 * the boot etc. They would "read" all the events below the latest seq number
3752 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3753 * number), they would subsribe to AEN (asynchronous event notification) and
3754 * wait for the events to happen.
3755 */
3756static int
3757megasas_get_seq_num(struct megasas_instance *instance,
3758 struct megasas_evt_log_info *eli)
3759{
3760 struct megasas_cmd *cmd;
3761 struct megasas_dcmd_frame *dcmd;
3762 struct megasas_evt_log_info *el_info;
3763 dma_addr_t el_info_h = 0;
3764
3765 cmd = megasas_get_cmd(instance);
3766
3767 if (!cmd) {
3768 return -ENOMEM;
3769 }
3770
3771 dcmd = &cmd->frame->dcmd;
3772 el_info = pci_alloc_consistent(instance->pdev,
3773 sizeof(struct megasas_evt_log_info),
3774 &el_info_h);
3775
3776 if (!el_info) {
3777 megasas_return_cmd(instance, cmd);
3778 return -ENOMEM;
3779 }
3780
3781 memset(el_info, 0, sizeof(*el_info));
3782 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3783
3784 dcmd->cmd = MFI_CMD_DCMD;
3785 dcmd->cmd_status = 0x0;
3786 dcmd->sge_count = 1;
3787 dcmd->flags = MFI_FRAME_DIR_READ;
3788 dcmd->timeout = 0;
780a3762 3789 dcmd->pad_0 = 0;
c4a3e0a5
BS
3790 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3791 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3792 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3793 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3794
3795 megasas_issue_blocked_cmd(instance, cmd);
3796
3797 /*
3798 * Copy the data back into callers buffer
3799 */
3800 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3801
3802 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3803 el_info, el_info_h);
3804
3805 megasas_return_cmd(instance, cmd);
3806
3807 return 0;
3808}
3809
3810/**
3811 * megasas_register_aen - Registers for asynchronous event notification
3812 * @instance: Adapter soft state
3813 * @seq_num: The starting sequence number
3814 * @class_locale: Class of the event
3815 *
3816 * This function subscribes for AEN for events beyond the @seq_num. It requests
3817 * to be notified if and only if the event is of type @class_locale
3818 */
3819static int
3820megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3821 u32 class_locale_word)
3822{
3823 int ret_val;
3824 struct megasas_cmd *cmd;
3825 struct megasas_dcmd_frame *dcmd;
3826 union megasas_evt_class_locale curr_aen;
3827 union megasas_evt_class_locale prev_aen;
3828
3829 /*
3830 * If there an AEN pending already (aen_cmd), check if the
3831 * class_locale of that pending AEN is inclusive of the new
3832 * AEN request we currently have. If it is, then we don't have
3833 * to do anything. In other words, whichever events the current
3834 * AEN request is subscribing to, have already been subscribed
3835 * to.
3836 *
3837 * If the old_cmd is _not_ inclusive, then we have to abort
3838 * that command, form a class_locale that is superset of both
3839 * old and current and re-issue to the FW
3840 */
3841
3842 curr_aen.word = class_locale_word;
3843
3844 if (instance->aen_cmd) {
3845
3846 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3847
3848 /*
3849 * A class whose enum value is smaller is inclusive of all
3850 * higher values. If a PROGRESS (= -1) was previously
3851 * registered, then a new registration requests for higher
3852 * classes need not be sent to FW. They are automatically
3853 * included.
3854 *
3855 * Locale numbers don't have such hierarchy. They are bitmap
3856 * values
3857 */
3858 if ((prev_aen.members.class <= curr_aen.members.class) &&
3859 !((prev_aen.members.locale & curr_aen.members.locale) ^
3860 curr_aen.members.locale)) {
3861 /*
3862 * Previously issued event registration includes
3863 * current request. Nothing to do.
3864 */
3865 return 0;
3866 } else {
3867 curr_aen.members.locale |= prev_aen.members.locale;
3868
3869 if (prev_aen.members.class < curr_aen.members.class)
3870 curr_aen.members.class = prev_aen.members.class;
3871
3872 instance->aen_cmd->abort_aen = 1;
3873 ret_val = megasas_issue_blocked_abort_cmd(instance,
3874 instance->
3875 aen_cmd);
3876
3877 if (ret_val) {
3878 printk(KERN_DEBUG "megasas: Failed to abort "
3879 "previous AEN command\n");
3880 return ret_val;
3881 }
3882 }
3883 }
3884
3885 cmd = megasas_get_cmd(instance);
3886
3887 if (!cmd)
3888 return -ENOMEM;
3889
3890 dcmd = &cmd->frame->dcmd;
3891
3892 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3893
3894 /*
3895 * Prepare DCMD for aen registration
3896 */
3897 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3898
3899 dcmd->cmd = MFI_CMD_DCMD;
3900 dcmd->cmd_status = 0x0;
3901 dcmd->sge_count = 1;
3902 dcmd->flags = MFI_FRAME_DIR_READ;
3903 dcmd->timeout = 0;
780a3762 3904 dcmd->pad_0 = 0;
39a98554 3905 instance->last_seq_num = seq_num;
c4a3e0a5
BS
3906 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3907 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3908 dcmd->mbox.w[0] = seq_num;
3909 dcmd->mbox.w[1] = curr_aen.word;
3910 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3911 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3912
f4c9a131
YB
3913 if (instance->aen_cmd != NULL) {
3914 megasas_return_cmd(instance, cmd);
3915 return 0;
3916 }
3917
c4a3e0a5
BS
3918 /*
3919 * Store reference to the cmd used to register for AEN. When an
3920 * application wants us to register for AEN, we have to abort this
3921 * cmd and re-register with a new EVENT LOCALE supplied by that app
3922 */
3923 instance->aen_cmd = cmd;
3924
3925 /*
3926 * Issue the aen registration frame
3927 */
9c915a8c 3928 instance->instancet->issue_dcmd(instance, cmd);
c4a3e0a5
BS
3929
3930 return 0;
3931}
3932
3933/**
3934 * megasas_start_aen - Subscribes to AEN during driver load time
3935 * @instance: Adapter soft state
3936 */
3937static int megasas_start_aen(struct megasas_instance *instance)
3938{
3939 struct megasas_evt_log_info eli;
3940 union megasas_evt_class_locale class_locale;
3941
3942 /*
3943 * Get the latest sequence number from FW
3944 */
3945 memset(&eli, 0, sizeof(eli));
3946
3947 if (megasas_get_seq_num(instance, &eli))
3948 return -1;
3949
3950 /*
3951 * Register AEN with FW for latest sequence number plus 1
3952 */
3953 class_locale.members.reserved = 0;
3954 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3955 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3956
3957 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3958 class_locale.word);
3959}
3960
3961/**
3962 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3963 * @instance: Adapter soft state
3964 */
3965static int megasas_io_attach(struct megasas_instance *instance)
3966{
3967 struct Scsi_Host *host = instance->host;
3968
3969 /*
3970 * Export parameters required by SCSI mid-layer
3971 */
3972 host->irq = instance->pdev->irq;
3973 host->unique_id = instance->unique_id;
7bebf5c7
YB
3974 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3975 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3976 host->can_queue =
3977 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3978 } else
3979 host->can_queue =
3980 instance->max_fw_cmds - MEGASAS_INT_CMDS;
c4a3e0a5
BS
3981 host->this_id = instance->init_id;
3982 host->sg_tablesize = instance->max_num_sge;
42a8d2b3 3983
3984 if (instance->fw_support_ieee)
3985 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3986
1fd10685
YB
3987 /*
3988 * Check if the module parameter value for max_sectors can be used
3989 */
3990 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3991 instance->max_sectors_per_req = max_sectors;
3992 else {
3993 if (max_sectors) {
3994 if (((instance->pdev->device ==
3995 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3996 (instance->pdev->device ==
3997 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3998 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3999 instance->max_sectors_per_req = max_sectors;
4000 } else {
4001 printk(KERN_INFO "megasas: max_sectors should be > 0"
4002 "and <= %d (or < 1MB for GEN2 controller)\n",
4003 instance->max_sectors_per_req);
4004 }
4005 }
4006 }
4007
c4a3e0a5 4008 host->max_sectors = instance->max_sectors_per_req;
9c915a8c 4009 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
c4a3e0a5
BS
4010 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
4011 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
4012 host->max_lun = MEGASAS_MAX_LUN;
122da302 4013 host->max_cmd_len = 16;
c4a3e0a5 4014
9c915a8c 4015 /* Fusion only supports host reset */
36807e67 4016 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
21d3c710
SS
4017 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4018 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
9c915a8c 4019 host->hostt->eh_device_reset_handler = NULL;
4020 host->hostt->eh_bus_reset_handler = NULL;
4021 }
4022
c4a3e0a5
BS
4023 /*
4024 * Notify the mid-layer about the new controller
4025 */
4026 if (scsi_add_host(host, &instance->pdev->dev)) {
4027 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
4028 return -ENODEV;
4029 }
4030
4031 /*
4032 * Trigger SCSI to scan our drives
4033 */
4034 scsi_scan_host(host);
4035 return 0;
4036}
4037
31ea7088 4038static int
4039megasas_set_dma_mask(struct pci_dev *pdev)
4040{
4041 /*
4042 * All our contollers are capable of performing 64-bit DMA
4043 */
4044 if (IS_DMA64) {
6a35528a 4045 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
31ea7088 4046
284901a9 4047 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 4048 goto fail_set_dma_mask;
4049 }
4050 } else {
284901a9 4051 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 4052 goto fail_set_dma_mask;
4053 }
4054 return 0;
4055
4056fail_set_dma_mask:
4057 return 1;
4058}
4059
c4a3e0a5
BS
4060/**
4061 * megasas_probe_one - PCI hotplug entry point
4062 * @pdev: PCI device structure
0d49016b 4063 * @id: PCI ids of supported hotplugged adapter
c4a3e0a5 4064 */
6f039790
GKH
4065static int megasas_probe_one(struct pci_dev *pdev,
4066 const struct pci_device_id *id)
c4a3e0a5 4067{
c8e858fe 4068 int rval, pos, i, j;
c4a3e0a5
BS
4069 struct Scsi_Host *host;
4070 struct megasas_instance *instance;
66192dfe 4071 u16 control = 0;
4072
4073 /* Reset MSI-X in the kdump kernel */
4074 if (reset_devices) {
4075 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
4076 if (pos) {
99369065 4077 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
66192dfe 4078 &control);
4079 if (control & PCI_MSIX_FLAGS_ENABLE) {
4080 dev_info(&pdev->dev, "resetting MSI-X\n");
4081 pci_write_config_word(pdev,
99369065 4082 pos + PCI_MSIX_FLAGS,
66192dfe 4083 control &
4084 ~PCI_MSIX_FLAGS_ENABLE);
4085 }
4086 }
4087 }
c4a3e0a5
BS
4088
4089 /*
4090 * Announce PCI information
4091 */
4092 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4093 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4094 pdev->subsystem_device);
4095
4096 printk("bus %d:slot %d:func %d\n",
4097 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4098
4099 /*
4100 * PCI prepping: enable device set bus mastering and dma mask
4101 */
aeab3fd7 4102 rval = pci_enable_device_mem(pdev);
c4a3e0a5
BS
4103
4104 if (rval) {
4105 return rval;
4106 }
4107
4108 pci_set_master(pdev);
4109
31ea7088 4110 if (megasas_set_dma_mask(pdev))
4111 goto fail_set_dma_mask;
c4a3e0a5
BS
4112
4113 host = scsi_host_alloc(&megasas_template,
4114 sizeof(struct megasas_instance));
4115
4116 if (!host) {
4117 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4118 goto fail_alloc_instance;
4119 }
4120
4121 instance = (struct megasas_instance *)host->hostdata;
4122 memset(instance, 0, sizeof(*instance));
39a98554 4123 atomic_set( &instance->fw_reset_no_pci_access, 0 );
9c915a8c 4124 instance->pdev = pdev;
c4a3e0a5 4125
9c915a8c 4126 switch (instance->pdev->device) {
4127 case PCI_DEVICE_ID_LSI_FUSION:
36807e67 4128 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 4129 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 4130 {
4131 struct fusion_context *fusion;
4132
4133 instance->ctrl_context =
4134 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4135 if (!instance->ctrl_context) {
4136 printk(KERN_DEBUG "megasas: Failed to allocate "
4137 "memory for Fusion context info\n");
4138 goto fail_alloc_dma_buf;
4139 }
4140 fusion = instance->ctrl_context;
4141 INIT_LIST_HEAD(&fusion->cmd_pool);
4142 spin_lock_init(&fusion->cmd_pool_lock);
4143 }
4144 break;
4145 default: /* For all other supported controllers */
4146
4147 instance->producer =
4148 pci_alloc_consistent(pdev, sizeof(u32),
4149 &instance->producer_h);
4150 instance->consumer =
4151 pci_alloc_consistent(pdev, sizeof(u32),
4152 &instance->consumer_h);
4153
4154 if (!instance->producer || !instance->consumer) {
4155 printk(KERN_DEBUG "megasas: Failed to allocate"
4156 "memory for producer, consumer\n");
4157 goto fail_alloc_dma_buf;
4158 }
c4a3e0a5 4159
9c915a8c 4160 *instance->producer = 0;
4161 *instance->consumer = 0;
4162 break;
c4a3e0a5
BS
4163 }
4164
c3518837 4165 megasas_poll_wait_aen = 0;
f4c9a131 4166 instance->flag_ieee = 0;
7e8a75f4 4167 instance->ev = NULL;
39a98554 4168 instance->issuepend_done = 1;
4169 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4170 megasas_poll_wait_aen = 0;
c4a3e0a5
BS
4171
4172 instance->evt_detail = pci_alloc_consistent(pdev,
4173 sizeof(struct
4174 megasas_evt_detail),
4175 &instance->evt_detail_h);
4176
4177 if (!instance->evt_detail) {
4178 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4179 "event detail structure\n");
4180 goto fail_alloc_dma_buf;
4181 }
4182
4183 /*
4184 * Initialize locks and queues
4185 */
4186 INIT_LIST_HEAD(&instance->cmd_pool);
39a98554 4187 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
c4a3e0a5 4188
e4a082c7
SP
4189 atomic_set(&instance->fw_outstanding,0);
4190
c4a3e0a5
BS
4191 init_waitqueue_head(&instance->int_cmd_wait_q);
4192 init_waitqueue_head(&instance->abort_cmd_wait_q);
4193
4194 spin_lock_init(&instance->cmd_pool_lock);
39a98554 4195 spin_lock_init(&instance->hba_lock);
7343eb65 4196 spin_lock_init(&instance->completion_lock);
c4a3e0a5 4197
e5a69e27 4198 mutex_init(&instance->aen_mutex);
9c915a8c 4199 mutex_init(&instance->reset_mutex);
c4a3e0a5
BS
4200
4201 /*
4202 * Initialize PCI related and misc parameters
4203 */
c4a3e0a5
BS
4204 instance->host = host;
4205 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4206 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4207
7bebf5c7
YB
4208 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4209 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
f4c9a131 4210 instance->flag_ieee = 1;
7bebf5c7
YB
4211 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4212 } else
4213 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4214
658dcedb 4215 megasas_dbg_lvl = 0;
05e9ebbe 4216 instance->flag = 0;
0c79e681 4217 instance->unload = 1;
05e9ebbe 4218 instance->last_time = 0;
39a98554 4219 instance->disableOnlineCtrlReset = 1;
bc93d425 4220 instance->UnevenSpanSupport = 0;
39a98554 4221
36807e67 4222 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
21d3c710
SS
4223 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4224 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
9c915a8c 4225 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4226 else
4227 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
658dcedb 4228
0a77066a 4229 /*
4230 * Initialize MFI Firmware
4231 */
4232 if (megasas_init_fw(instance))
4233 goto fail_init_mfi;
4234
d46a3ad6 4235retry_irq_register:
c4a3e0a5
BS
4236 /*
4237 * Register IRQ
4238 */
c8e858fe 4239 if (instance->msix_vectors) {
4240 for (i = 0 ; i < instance->msix_vectors; i++) {
4241 instance->irq_context[i].instance = instance;
4242 instance->irq_context[i].MSIxIndex = i;
4243 if (request_irq(instance->msixentry[i].vector,
4244 instance->instancet->service_isr, 0,
4245 "megasas",
4246 &instance->irq_context[i])) {
4247 printk(KERN_DEBUG "megasas: Failed to "
4248 "register IRQ for vector %d.\n", i);
4249 for (j = 0 ; j < i ; j++)
4250 free_irq(
4251 instance->msixentry[j].vector,
4252 &instance->irq_context[j]);
d46a3ad6
SS
4253 /* Retry irq register for IO_APIC */
4254 instance->msix_vectors = 0;
4255 goto retry_irq_register;
c8e858fe 4256 }
4257 }
4258 } else {
4259 instance->irq_context[0].instance = instance;
4260 instance->irq_context[0].MSIxIndex = 0;
4261 if (request_irq(pdev->irq, instance->instancet->service_isr,
4262 IRQF_SHARED, "megasas",
4263 &instance->irq_context[0])) {
4264 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4265 goto fail_irq;
4266 }
c4a3e0a5
BS
4267 }
4268
d46a3ad6 4269 instance->instancet->enable_intr(instance);
c4a3e0a5
BS
4270
4271 /*
4272 * Store instance in PCI softstate
4273 */
4274 pci_set_drvdata(pdev, instance);
4275
4276 /*
4277 * Add this controller to megasas_mgmt_info structure so that it
4278 * can be exported to management applications
4279 */
4280 megasas_mgmt_info.count++;
4281 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4282 megasas_mgmt_info.max_index++;
4283
541f90b7 4284 /*
4285 * Register with SCSI mid-layer
4286 */
4287 if (megasas_io_attach(instance))
4288 goto fail_io_attach;
4289
4290 instance->unload = 0;
4291
c4a3e0a5
BS
4292 /*
4293 * Initiate AEN (Asynchronous Event Notification)
4294 */
4295 if (megasas_start_aen(instance)) {
4296 printk(KERN_DEBUG "megasas: start aen failed\n");
4297 goto fail_start_aen;
4298 }
4299
c4a3e0a5
BS
4300 return 0;
4301
4302 fail_start_aen:
4303 fail_io_attach:
4304 megasas_mgmt_info.count--;
4305 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4306 megasas_mgmt_info.max_index--;
4307
4308 pci_set_drvdata(pdev, NULL);
d46a3ad6 4309 instance->instancet->disable_intr(instance);
c8e858fe 4310 if (instance->msix_vectors)
4311 for (i = 0 ; i < instance->msix_vectors; i++)
4312 free_irq(instance->msixentry[i].vector,
4313 &instance->irq_context[i]);
4314 else
4315 free_irq(instance->pdev->irq, &instance->irq_context[0]);
eb1b1237 4316fail_irq:
36807e67 4317 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
21d3c710
SS
4318 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4319 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
eb1b1237 4320 megasas_release_fusion(instance);
4321 else
4322 megasas_release_mfi(instance);
c4a3e0a5 4323 fail_init_mfi:
c8e858fe 4324 if (instance->msix_vectors)
0a77066a 4325 pci_disable_msix(instance->pdev);
c4a3e0a5
BS
4326 fail_alloc_dma_buf:
4327 if (instance->evt_detail)
4328 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4329 instance->evt_detail,
4330 instance->evt_detail_h);
4331
eb1b1237 4332 if (instance->producer)
c4a3e0a5
BS
4333 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4334 instance->producer_h);
4335 if (instance->consumer)
4336 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4337 instance->consumer_h);
4338 scsi_host_put(host);
4339
4340 fail_alloc_instance:
4341 fail_set_dma_mask:
4342 pci_disable_device(pdev);
4343
4344 return -ENODEV;
4345}
4346
4347/**
4348 * megasas_flush_cache - Requests FW to flush all its caches
4349 * @instance: Adapter soft state
4350 */
4351static void megasas_flush_cache(struct megasas_instance *instance)
4352{
4353 struct megasas_cmd *cmd;
4354 struct megasas_dcmd_frame *dcmd;
4355
39a98554 4356 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4357 return;
4358
c4a3e0a5
BS
4359 cmd = megasas_get_cmd(instance);
4360
4361 if (!cmd)
4362 return;
4363
4364 dcmd = &cmd->frame->dcmd;
4365
4366 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4367
4368 dcmd->cmd = MFI_CMD_DCMD;
4369 dcmd->cmd_status = 0x0;
4370 dcmd->sge_count = 0;
4371 dcmd->flags = MFI_FRAME_DIR_NONE;
4372 dcmd->timeout = 0;
780a3762 4373 dcmd->pad_0 = 0;
c4a3e0a5
BS
4374 dcmd->data_xfer_len = 0;
4375 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4376 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4377
4378 megasas_issue_blocked_cmd(instance, cmd);
4379
4380 megasas_return_cmd(instance, cmd);
4381
4382 return;
4383}
4384
4385/**
4386 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4387 * @instance: Adapter soft state
31ea7088 4388 * @opcode: Shutdown/Hibernate
c4a3e0a5 4389 */
31ea7088 4390static void megasas_shutdown_controller(struct megasas_instance *instance,
4391 u32 opcode)
c4a3e0a5
BS
4392{
4393 struct megasas_cmd *cmd;
4394 struct megasas_dcmd_frame *dcmd;
4395
39a98554 4396 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4397 return;
4398
c4a3e0a5
BS
4399 cmd = megasas_get_cmd(instance);
4400
4401 if (!cmd)
4402 return;
4403
4404 if (instance->aen_cmd)
4405 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
9c915a8c 4406 if (instance->map_update_cmd)
4407 megasas_issue_blocked_abort_cmd(instance,
4408 instance->map_update_cmd);
c4a3e0a5
BS
4409 dcmd = &cmd->frame->dcmd;
4410
4411 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4412
4413 dcmd->cmd = MFI_CMD_DCMD;
4414 dcmd->cmd_status = 0x0;
4415 dcmd->sge_count = 0;
4416 dcmd->flags = MFI_FRAME_DIR_NONE;
4417 dcmd->timeout = 0;
780a3762 4418 dcmd->pad_0 = 0;
c4a3e0a5 4419 dcmd->data_xfer_len = 0;
31ea7088 4420 dcmd->opcode = opcode;
c4a3e0a5
BS
4421
4422 megasas_issue_blocked_cmd(instance, cmd);
4423
4424 megasas_return_cmd(instance, cmd);
4425
4426 return;
4427}
4428
33139b21 4429#ifdef CONFIG_PM
31ea7088 4430/**
ad84db2e 4431 * megasas_suspend - driver suspend entry point
4432 * @pdev: PCI device structure
31ea7088 4433 * @state: PCI power state to suspend routine
4434 */
33139b21 4435static int
31ea7088 4436megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4437{
4438 struct Scsi_Host *host;
4439 struct megasas_instance *instance;
c8e858fe 4440 int i;
31ea7088 4441
4442 instance = pci_get_drvdata(pdev);
4443 host = instance->host;
0c79e681 4444 instance->unload = 1;
31ea7088 4445
4446 megasas_flush_cache(instance);
4447 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7e8a75f4
YB
4448
4449 /* cancel the delayed work if this work still in queue */
4450 if (instance->ev != NULL) {
4451 struct megasas_aen_event *ev = instance->ev;
c1d390d8 4452 cancel_delayed_work_sync(&ev->hotplug_work);
7e8a75f4
YB
4453 instance->ev = NULL;
4454 }
4455
31ea7088 4456 tasklet_kill(&instance->isr_tasklet);
4457
4458 pci_set_drvdata(instance->pdev, instance);
d46a3ad6 4459 instance->instancet->disable_intr(instance);
c8e858fe 4460
4461 if (instance->msix_vectors)
4462 for (i = 0 ; i < instance->msix_vectors; i++)
4463 free_irq(instance->msixentry[i].vector,
4464 &instance->irq_context[i]);
4465 else
4466 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4467 if (instance->msix_vectors)
80d9da98 4468 pci_disable_msix(instance->pdev);
31ea7088 4469
4470 pci_save_state(pdev);
4471 pci_disable_device(pdev);
4472
4473 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4474
4475 return 0;
4476}
4477
4478/**
4479 * megasas_resume- driver resume entry point
4480 * @pdev: PCI device structure
4481 */
33139b21 4482static int
31ea7088 4483megasas_resume(struct pci_dev *pdev)
4484{
c8e858fe 4485 int rval, i, j;
31ea7088 4486 struct Scsi_Host *host;
4487 struct megasas_instance *instance;
4488
4489 instance = pci_get_drvdata(pdev);
4490 host = instance->host;
4491 pci_set_power_state(pdev, PCI_D0);
4492 pci_enable_wake(pdev, PCI_D0, 0);
4493 pci_restore_state(pdev);
4494
4495 /*
4496 * PCI prepping: enable device set bus mastering and dma mask
4497 */
aeab3fd7 4498 rval = pci_enable_device_mem(pdev);
31ea7088 4499
4500 if (rval) {
4501 printk(KERN_ERR "megasas: Enable device failed\n");
4502 return rval;
4503 }
4504
4505 pci_set_master(pdev);
4506
4507 if (megasas_set_dma_mask(pdev))
4508 goto fail_set_dma_mask;
4509
4510 /*
4511 * Initialize MFI Firmware
4512 */
4513
31ea7088 4514 atomic_set(&instance->fw_outstanding, 0);
4515
4516 /*
4517 * We expect the FW state to be READY
4518 */
058a8fac 4519 if (megasas_transition_to_ready(instance, 0))
31ea7088 4520 goto fail_ready_state;
4521
3f1abce4 4522 /* Now re-enable MSI-X */
c8e858fe 4523 if (instance->msix_vectors)
4524 pci_enable_msix(instance->pdev, instance->msixentry,
4525 instance->msix_vectors);
3f1abce4 4526
9c915a8c 4527 switch (instance->pdev->device) {
4528 case PCI_DEVICE_ID_LSI_FUSION:
36807e67 4529 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 4530 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 4531 {
4532 megasas_reset_reply_desc(instance);
4533 if (megasas_ioc_init_fusion(instance)) {
4534 megasas_free_cmds(instance);
4535 megasas_free_cmds_fusion(instance);
4536 goto fail_init_mfi;
4537 }
4538 if (!megasas_get_map_info(instance))
4539 megasas_sync_map_info(instance);
4540 }
4541 break;
4542 default:
4543 *instance->producer = 0;
4544 *instance->consumer = 0;
4545 if (megasas_issue_init_mfi(instance))
4546 goto fail_init_mfi;
4547 break;
4548 }
31ea7088 4549
9c915a8c 4550 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4551 (unsigned long)instance);
31ea7088 4552
4553 /*
4554 * Register IRQ
4555 */
c8e858fe 4556 if (instance->msix_vectors) {
4557 for (i = 0 ; i < instance->msix_vectors; i++) {
4558 instance->irq_context[i].instance = instance;
4559 instance->irq_context[i].MSIxIndex = i;
4560 if (request_irq(instance->msixentry[i].vector,
4561 instance->instancet->service_isr, 0,
4562 "megasas",
4563 &instance->irq_context[i])) {
4564 printk(KERN_DEBUG "megasas: Failed to "
4565 "register IRQ for vector %d.\n", i);
4566 for (j = 0 ; j < i ; j++)
4567 free_irq(
4568 instance->msixentry[j].vector,
4569 &instance->irq_context[j]);
4570 goto fail_irq;
4571 }
4572 }
4573 } else {
4574 instance->irq_context[0].instance = instance;
4575 instance->irq_context[0].MSIxIndex = 0;
4576 if (request_irq(pdev->irq, instance->instancet->service_isr,
4577 IRQF_SHARED, "megasas",
4578 &instance->irq_context[0])) {
4579 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4580 goto fail_irq;
4581 }
31ea7088 4582 }
4583
d46a3ad6 4584 instance->instancet->enable_intr(instance);
0c79e681
YB
4585 instance->unload = 0;
4586
541f90b7 4587 /*
4588 * Initiate AEN (Asynchronous Event Notification)
4589 */
4590 if (megasas_start_aen(instance))
4591 printk(KERN_ERR "megasas: Start AEN failed\n");
4592
31ea7088 4593 return 0;
4594
4595fail_irq:
4596fail_init_mfi:
4597 if (instance->evt_detail)
4598 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4599 instance->evt_detail,
4600 instance->evt_detail_h);
4601
4602 if (instance->producer)
4603 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4604 instance->producer_h);
4605 if (instance->consumer)
4606 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4607 instance->consumer_h);
4608 scsi_host_put(host);
4609
4610fail_set_dma_mask:
4611fail_ready_state:
4612
4613 pci_disable_device(pdev);
4614
4615 return -ENODEV;
4616}
33139b21
JS
4617#else
4618#define megasas_suspend NULL
4619#define megasas_resume NULL
4620#endif
31ea7088 4621
c4a3e0a5
BS
4622/**
4623 * megasas_detach_one - PCI hot"un"plug entry point
4624 * @pdev: PCI device structure
4625 */
6f039790 4626static void megasas_detach_one(struct pci_dev *pdev)
c4a3e0a5
BS
4627{
4628 int i;
4629 struct Scsi_Host *host;
4630 struct megasas_instance *instance;
9c915a8c 4631 struct fusion_context *fusion;
c4a3e0a5
BS
4632
4633 instance = pci_get_drvdata(pdev);
c3518837 4634 instance->unload = 1;
c4a3e0a5 4635 host = instance->host;
9c915a8c 4636 fusion = instance->ctrl_context;
c4a3e0a5
BS
4637
4638 scsi_remove_host(instance->host);
4639 megasas_flush_cache(instance);
31ea7088 4640 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7e8a75f4
YB
4641
4642 /* cancel the delayed work if this work still in queue*/
4643 if (instance->ev != NULL) {
4644 struct megasas_aen_event *ev = instance->ev;
c1d390d8 4645 cancel_delayed_work_sync(&ev->hotplug_work);
7e8a75f4
YB
4646 instance->ev = NULL;
4647 }
4648
5d018ad0 4649 tasklet_kill(&instance->isr_tasklet);
c4a3e0a5
BS
4650
4651 /*
4652 * Take the instance off the instance array. Note that we will not
4653 * decrement the max_index. We let this array be sparse array
4654 */
4655 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4656 if (megasas_mgmt_info.instance[i] == instance) {
4657 megasas_mgmt_info.count--;
4658 megasas_mgmt_info.instance[i] = NULL;
4659
4660 break;
4661 }
4662 }
4663
4664 pci_set_drvdata(instance->pdev, NULL);
4665
d46a3ad6 4666 instance->instancet->disable_intr(instance);
c4a3e0a5 4667
c8e858fe 4668 if (instance->msix_vectors)
4669 for (i = 0 ; i < instance->msix_vectors; i++)
4670 free_irq(instance->msixentry[i].vector,
4671 &instance->irq_context[i]);
4672 else
4673 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4674 if (instance->msix_vectors)
80d9da98 4675 pci_disable_msix(instance->pdev);
c4a3e0a5 4676
9c915a8c 4677 switch (instance->pdev->device) {
4678 case PCI_DEVICE_ID_LSI_FUSION:
36807e67 4679 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 4680 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 4681 megasas_release_fusion(instance);
4682 for (i = 0; i < 2 ; i++)
4683 if (fusion->ld_map[i])
4684 dma_free_coherent(&instance->pdev->dev,
4685 fusion->map_sz,
4686 fusion->ld_map[i],
4687 fusion->
4688 ld_map_phys[i]);
4689 kfree(instance->ctrl_context);
4690 break;
4691 default:
4692 megasas_release_mfi(instance);
9c915a8c 4693 pci_free_consistent(pdev, sizeof(u32),
4694 instance->producer,
4695 instance->producer_h);
4696 pci_free_consistent(pdev, sizeof(u32),
4697 instance->consumer,
4698 instance->consumer_h);
4699 break;
4700 }
c4a3e0a5 4701
105900d5
SS
4702 if (instance->evt_detail)
4703 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4704 instance->evt_detail, instance->evt_detail_h);
c4a3e0a5
BS
4705 scsi_host_put(host);
4706
4707 pci_set_drvdata(pdev, NULL);
4708
4709 pci_disable_device(pdev);
4710
4711 return;
4712}
4713
4714/**
4715 * megasas_shutdown - Shutdown entry point
4716 * @device: Generic device structure
4717 */
4718static void megasas_shutdown(struct pci_dev *pdev)
4719{
c8e858fe 4720 int i;
c4a3e0a5 4721 struct megasas_instance *instance = pci_get_drvdata(pdev);
c8e858fe 4722
0c79e681 4723 instance->unload = 1;
c4a3e0a5 4724 megasas_flush_cache(instance);
530e6fc1 4725 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
d46a3ad6 4726 instance->instancet->disable_intr(instance);
c8e858fe 4727 if (instance->msix_vectors)
4728 for (i = 0 ; i < instance->msix_vectors; i++)
4729 free_irq(instance->msixentry[i].vector,
4730 &instance->irq_context[i]);
4731 else
4732 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4733 if (instance->msix_vectors)
46fd256e 4734 pci_disable_msix(instance->pdev);
c4a3e0a5
BS
4735}
4736
4737/**
4738 * megasas_mgmt_open - char node "open" entry point
4739 */
4740static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4741{
4742 /*
4743 * Allow only those users with admin rights
4744 */
4745 if (!capable(CAP_SYS_ADMIN))
4746 return -EACCES;
4747
4748 return 0;
4749}
4750
c4a3e0a5
BS
4751/**
4752 * megasas_mgmt_fasync - Async notifier registration from applications
4753 *
4754 * This function adds the calling process to a driver global queue. When an
4755 * event occurs, SIGIO will be sent to all processes in this queue.
4756 */
4757static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4758{
4759 int rc;
4760
0b950672 4761 mutex_lock(&megasas_async_queue_mutex);
c4a3e0a5
BS
4762
4763 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4764
0b950672 4765 mutex_unlock(&megasas_async_queue_mutex);
c4a3e0a5
BS
4766
4767 if (rc >= 0) {
4768 /* For sanity check when we get ioctl */
4769 filep->private_data = filep;
4770 return 0;
4771 }
4772
4773 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4774
4775 return rc;
4776}
4777
c3518837
YB
4778/**
4779 * megasas_mgmt_poll - char node "poll" entry point
4780 * */
4781static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4782{
4783 unsigned int mask;
4784 unsigned long flags;
4785 poll_wait(file, &megasas_poll_wait, wait);
4786 spin_lock_irqsave(&poll_aen_lock, flags);
4787 if (megasas_poll_wait_aen)
4788 mask = (POLLIN | POLLRDNORM);
4789 else
4790 mask = 0;
4791 spin_unlock_irqrestore(&poll_aen_lock, flags);
4792 return mask;
4793}
4794
c4a3e0a5
BS
4795/**
4796 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4797 * @instance: Adapter soft state
4798 * @argp: User's ioctl packet
4799 */
4800static int
4801megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4802 struct megasas_iocpacket __user * user_ioc,
4803 struct megasas_iocpacket *ioc)
4804{
4805 struct megasas_sge32 *kern_sge32;
4806 struct megasas_cmd *cmd;
4807 void *kbuff_arr[MAX_IOCTL_SGE];
4808 dma_addr_t buf_handle = 0;
4809 int error = 0, i;
4810 void *sense = NULL;
4811 dma_addr_t sense_handle;
7b2519af 4812 unsigned long *sense_ptr;
c4a3e0a5
BS
4813
4814 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4815
4816 if (ioc->sge_count > MAX_IOCTL_SGE) {
4817 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4818 ioc->sge_count, MAX_IOCTL_SGE);
4819 return -EINVAL;
4820 }
4821
4822 cmd = megasas_get_cmd(instance);
4823 if (!cmd) {
4824 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4825 return -ENOMEM;
4826 }
4827
4828 /*
4829 * User's IOCTL packet has 2 frames (maximum). Copy those two
4830 * frames into our cmd's frames. cmd->frame's context will get
4831 * overwritten when we copy from user's frames. So set that value
4832 * alone separately
4833 */
4834 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4835 cmd->frame->hdr.context = cmd->index;
c3518837 4836 cmd->frame->hdr.pad_0 = 0;
882be7c3 4837 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4838 MFI_FRAME_SENSE64);
c4a3e0a5
BS
4839
4840 /*
4841 * The management interface between applications and the fw uses
4842 * MFI frames. E.g, RAID configuration changes, LD property changes
4843 * etc are accomplishes through different kinds of MFI frames. The
4844 * driver needs to care only about substituting user buffers with
4845 * kernel buffers in SGLs. The location of SGL is embedded in the
4846 * struct iocpacket itself.
4847 */
4848 kern_sge32 = (struct megasas_sge32 *)
4849 ((unsigned long)cmd->frame + ioc->sgl_off);
4850
4851 /*
4852 * For each user buffer, create a mirror buffer and copy in
4853 */
4854 for (i = 0; i < ioc->sge_count; i++) {
98cb7e44
BM
4855 if (!ioc->sgl[i].iov_len)
4856 continue;
4857
9f35fa8a 4858 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
c4a3e0a5 4859 ioc->sgl[i].iov_len,
9f35fa8a 4860 &buf_handle, GFP_KERNEL);
c4a3e0a5
BS
4861 if (!kbuff_arr[i]) {
4862 printk(KERN_DEBUG "megasas: Failed to alloc "
4863 "kernel SGL buffer for IOCTL \n");
4864 error = -ENOMEM;
4865 goto out;
4866 }
4867
4868 /*
4869 * We don't change the dma_coherent_mask, so
4870 * pci_alloc_consistent only returns 32bit addresses
4871 */
4872 kern_sge32[i].phys_addr = (u32) buf_handle;
4873 kern_sge32[i].length = ioc->sgl[i].iov_len;
4874
4875 /*
4876 * We created a kernel buffer corresponding to the
4877 * user buffer. Now copy in from the user buffer
4878 */
4879 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4880 (u32) (ioc->sgl[i].iov_len))) {
4881 error = -EFAULT;
4882 goto out;
4883 }
4884 }
4885
4886 if (ioc->sense_len) {
9f35fa8a
SP
4887 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4888 &sense_handle, GFP_KERNEL);
c4a3e0a5
BS
4889 if (!sense) {
4890 error = -ENOMEM;
4891 goto out;
4892 }
4893
4894 sense_ptr =
7b2519af 4895 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
c4a3e0a5
BS
4896 *sense_ptr = sense_handle;
4897 }
4898
4899 /*
4900 * Set the sync_cmd flag so that the ISR knows not to complete this
4901 * cmd to the SCSI mid-layer
4902 */
4903 cmd->sync_cmd = 1;
4904 megasas_issue_blocked_cmd(instance, cmd);
4905 cmd->sync_cmd = 0;
4906
4907 /*
4908 * copy out the kernel buffers to user buffers
4909 */
4910 for (i = 0; i < ioc->sge_count; i++) {
4911 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4912 ioc->sgl[i].iov_len)) {
4913 error = -EFAULT;
4914 goto out;
4915 }
4916 }
4917
4918 /*
4919 * copy out the sense
4920 */
4921 if (ioc->sense_len) {
4922 /*
b70a41e0 4923 * sense_ptr points to the location that has the user
c4a3e0a5
BS
4924 * sense buffer address
4925 */
7b2519af
YB
4926 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4927 ioc->sense_off);
c4a3e0a5 4928
b70a41e0 4929 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4930 sense, ioc->sense_len)) {
b10c36a5 4931 printk(KERN_ERR "megasas: Failed to copy out to user "
4932 "sense data\n");
c4a3e0a5
BS
4933 error = -EFAULT;
4934 goto out;
4935 }
4936 }
4937
4938 /*
4939 * copy the status codes returned by the fw
4940 */
4941 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4942 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4943 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4944 error = -EFAULT;
4945 }
4946
4947 out:
4948 if (sense) {
9f35fa8a 4949 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
c4a3e0a5
BS
4950 sense, sense_handle);
4951 }
4952
4953 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
9f35fa8a 4954 dma_free_coherent(&instance->pdev->dev,
c4a3e0a5
BS
4955 kern_sge32[i].length,
4956 kbuff_arr[i], kern_sge32[i].phys_addr);
4957 }
4958
4959 megasas_return_cmd(instance, cmd);
4960 return error;
4961}
4962
c4a3e0a5
BS
4963static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4964{
4965 struct megasas_iocpacket __user *user_ioc =
4966 (struct megasas_iocpacket __user *)arg;
4967 struct megasas_iocpacket *ioc;
4968 struct megasas_instance *instance;
4969 int error;
39a98554 4970 int i;
4971 unsigned long flags;
4972 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
4973
4974 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4975 if (!ioc)
4976 return -ENOMEM;
4977
4978 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4979 error = -EFAULT;
4980 goto out_kfree_ioc;
4981 }
4982
4983 instance = megasas_lookup_instance(ioc->host_no);
4984 if (!instance) {
4985 error = -ENODEV;
4986 goto out_kfree_ioc;
4987 }
4988
39a98554 4989 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4990 printk(KERN_ERR "Controller in crit error\n");
0c79e681
YB
4991 error = -ENODEV;
4992 goto out_kfree_ioc;
4993 }
4994
4995 if (instance->unload == 1) {
4996 error = -ENODEV;
4997 goto out_kfree_ioc;
4998 }
4999
c4a3e0a5
BS
5000 /*
5001 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
5002 */
5003 if (down_interruptible(&instance->ioctl_sem)) {
5004 error = -ERESTARTSYS;
5005 goto out_kfree_ioc;
5006 }
39a98554 5007
5008 for (i = 0; i < wait_time; i++) {
5009
5010 spin_lock_irqsave(&instance->hba_lock, flags);
5011 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
5012 spin_unlock_irqrestore(&instance->hba_lock, flags);
5013 break;
5014 }
5015 spin_unlock_irqrestore(&instance->hba_lock, flags);
5016
5017 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5018 printk(KERN_NOTICE "megasas: waiting"
5019 "for controller reset to finish\n");
5020 }
5021
5022 msleep(1000);
5023 }
5024
5025 spin_lock_irqsave(&instance->hba_lock, flags);
5026 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5027 spin_unlock_irqrestore(&instance->hba_lock, flags);
5028
5029 printk(KERN_ERR "megaraid_sas: timed out while"
5030 "waiting for HBA to recover\n");
5031 error = -ENODEV;
c64e483e 5032 goto out_up;
39a98554 5033 }
5034 spin_unlock_irqrestore(&instance->hba_lock, flags);
5035
c4a3e0a5 5036 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
c64e483e 5037 out_up:
c4a3e0a5
BS
5038 up(&instance->ioctl_sem);
5039
5040 out_kfree_ioc:
5041 kfree(ioc);
5042 return error;
5043}
5044
5045static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
5046{
5047 struct megasas_instance *instance;
5048 struct megasas_aen aen;
5049 int error;
39a98554 5050 int i;
5051 unsigned long flags;
5052 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
5053
5054 if (file->private_data != file) {
5055 printk(KERN_DEBUG "megasas: fasync_helper was not "
5056 "called first\n");
5057 return -EINVAL;
5058 }
5059
5060 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
5061 return -EFAULT;
5062
5063 instance = megasas_lookup_instance(aen.host_no);
5064
5065 if (!instance)
5066 return -ENODEV;
5067
39a98554 5068 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
5069 return -ENODEV;
0c79e681
YB
5070 }
5071
5072 if (instance->unload == 1) {
5073 return -ENODEV;
5074 }
5075
39a98554 5076 for (i = 0; i < wait_time; i++) {
5077
5078 spin_lock_irqsave(&instance->hba_lock, flags);
5079 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
5080 spin_unlock_irqrestore(&instance->hba_lock,
5081 flags);
5082 break;
5083 }
5084
5085 spin_unlock_irqrestore(&instance->hba_lock, flags);
5086
5087 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5088 printk(KERN_NOTICE "megasas: waiting for"
5089 "controller reset to finish\n");
5090 }
5091
5092 msleep(1000);
5093 }
5094
5095 spin_lock_irqsave(&instance->hba_lock, flags);
5096 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5097 spin_unlock_irqrestore(&instance->hba_lock, flags);
5098 printk(KERN_ERR "megaraid_sas: timed out while waiting"
5099 "for HBA to recover.\n");
5100 return -ENODEV;
5101 }
5102 spin_unlock_irqrestore(&instance->hba_lock, flags);
5103
e5a69e27 5104 mutex_lock(&instance->aen_mutex);
c4a3e0a5
BS
5105 error = megasas_register_aen(instance, aen.seq_num,
5106 aen.class_locale_word);
e5a69e27 5107 mutex_unlock(&instance->aen_mutex);
c4a3e0a5
BS
5108 return error;
5109}
5110
5111/**
5112 * megasas_mgmt_ioctl - char node ioctl entry point
5113 */
5114static long
5115megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5116{
5117 switch (cmd) {
5118 case MEGASAS_IOC_FIRMWARE:
5119 return megasas_mgmt_ioctl_fw(file, arg);
5120
5121 case MEGASAS_IOC_GET_AEN:
5122 return megasas_mgmt_ioctl_aen(file, arg);
5123 }
5124
5125 return -ENOTTY;
5126}
5127
5128#ifdef CONFIG_COMPAT
5129static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5130{
5131 struct compat_megasas_iocpacket __user *cioc =
5132 (struct compat_megasas_iocpacket __user *)arg;
5133 struct megasas_iocpacket __user *ioc =
5134 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5135 int i;
5136 int error = 0;
b3dc1a21 5137 compat_uptr_t ptr;
c4a3e0a5 5138
83aabc1b
JG
5139 if (clear_user(ioc, sizeof(*ioc)))
5140 return -EFAULT;
c4a3e0a5
BS
5141
5142 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5143 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5144 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5145 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5146 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5147 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5148 return -EFAULT;
5149
b3dc1a21
TH
5150 /*
5151 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5152 * sense_len is not null, so prepare the 64bit value under
5153 * the same condition.
5154 */
5155 if (ioc->sense_len) {
5156 void __user **sense_ioc_ptr =
5157 (void __user **)(ioc->frame.raw + ioc->sense_off);
5158 compat_uptr_t *sense_cioc_ptr =
5159 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5160 if (get_user(ptr, sense_cioc_ptr) ||
5161 put_user(compat_ptr(ptr), sense_ioc_ptr))
5162 return -EFAULT;
5163 }
c4a3e0a5 5164
b3dc1a21 5165 for (i = 0; i < MAX_IOCTL_SGE; i++) {
c4a3e0a5
BS
5166 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5167 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5168 copy_in_user(&ioc->sgl[i].iov_len,
5169 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5170 return -EFAULT;
5171 }
5172
5173 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5174
5175 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5176 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5177 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5178 return -EFAULT;
5179 }
5180 return error;
5181}
5182
5183static long
5184megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5185 unsigned long arg)
5186{
5187 switch (cmd) {
cb59aa6a
SP
5188 case MEGASAS_IOC_FIRMWARE32:
5189 return megasas_mgmt_compat_ioctl_fw(file, arg);
c4a3e0a5
BS
5190 case MEGASAS_IOC_GET_AEN:
5191 return megasas_mgmt_ioctl_aen(file, arg);
5192 }
5193
5194 return -ENOTTY;
5195}
5196#endif
5197
5198/*
5199 * File operations structure for management interface
5200 */
00977a59 5201static const struct file_operations megasas_mgmt_fops = {
c4a3e0a5
BS
5202 .owner = THIS_MODULE,
5203 .open = megasas_mgmt_open,
c4a3e0a5
BS
5204 .fasync = megasas_mgmt_fasync,
5205 .unlocked_ioctl = megasas_mgmt_ioctl,
c3518837 5206 .poll = megasas_mgmt_poll,
c4a3e0a5
BS
5207#ifdef CONFIG_COMPAT
5208 .compat_ioctl = megasas_mgmt_compat_ioctl,
5209#endif
6038f373 5210 .llseek = noop_llseek,
c4a3e0a5
BS
5211};
5212
5213/*
5214 * PCI hotplug support registration structure
5215 */
5216static struct pci_driver megasas_pci_driver = {
5217
5218 .name = "megaraid_sas",
5219 .id_table = megasas_pci_table,
5220 .probe = megasas_probe_one,
6f039790 5221 .remove = megasas_detach_one,
31ea7088 5222 .suspend = megasas_suspend,
5223 .resume = megasas_resume,
c4a3e0a5
BS
5224 .shutdown = megasas_shutdown,
5225};
5226
5227/*
5228 * Sysfs driver attributes
5229 */
5230static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5231{
5232 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5233 MEGASAS_VERSION);
5234}
5235
5236static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5237
5238static ssize_t
5239megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5240{
5241 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5242 MEGASAS_RELDATE);
5243}
5244
5245static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5246 NULL);
5247
72c4fd36
YB
5248static ssize_t
5249megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5250{
5251 return sprintf(buf, "%u\n", support_poll_for_event);
5252}
5253
5254static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5255 megasas_sysfs_show_support_poll_for_event, NULL);
5256
837f5fe8
YB
5257 static ssize_t
5258megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5259{
5260 return sprintf(buf, "%u\n", support_device_change);
5261}
5262
5263static DRIVER_ATTR(support_device_change, S_IRUGO,
5264 megasas_sysfs_show_support_device_change, NULL);
5265
658dcedb
SP
5266static ssize_t
5267megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5268{
ad84db2e 5269 return sprintf(buf, "%u\n", megasas_dbg_lvl);
658dcedb
SP
5270}
5271
5272static ssize_t
5273megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5274{
5275 int retval = count;
5276 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5277 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5278 retval = -EINVAL;
5279 }
5280 return retval;
5281}
5282
66dca9b8 5283static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
ad84db2e 5284 megasas_sysfs_set_dbg_lvl);
5285
7e8a75f4
YB
5286static void
5287megasas_aen_polling(struct work_struct *work)
5288{
5289 struct megasas_aen_event *ev =
c1d390d8 5290 container_of(work, struct megasas_aen_event, hotplug_work.work);
7e8a75f4
YB
5291 struct megasas_instance *instance = ev->instance;
5292 union megasas_evt_class_locale class_locale;
5293 struct Scsi_Host *host;
5294 struct scsi_device *sdev1;
5295 u16 pd_index = 0;
c9786842 5296 u16 ld_index = 0;
7e8a75f4
YB
5297 int i, j, doscan = 0;
5298 u32 seq_num;
5299 int error;
5300
5301 if (!instance) {
5302 printk(KERN_ERR "invalid instance!\n");
5303 kfree(ev);
5304 return;
5305 }
5306 instance->ev = NULL;
5307 host = instance->host;
5308 if (instance->evt_detail) {
5309
5310 switch (instance->evt_detail->code) {
5311 case MR_EVT_PD_INSERTED:
c9786842
YB
5312 if (megasas_get_pd_list(instance) == 0) {
5313 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5314 for (j = 0;
5315 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5316 j++) {
5317
5318 pd_index =
5319 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5320
5321 sdev1 =
5322 scsi_device_lookup(host, i, j, 0);
5323
5324 if (instance->pd_list[pd_index].driveState
5325 == MR_PD_STATE_SYSTEM) {
5326 if (!sdev1) {
5327 scsi_add_device(host, i, j, 0);
5328 }
5329
5330 if (sdev1)
5331 scsi_device_put(sdev1);
5332 }
5333 }
5334 }
5335 }
5336 doscan = 0;
5337 break;
5338
7e8a75f4 5339 case MR_EVT_PD_REMOVED:
c9786842 5340 if (megasas_get_pd_list(instance) == 0) {
c9786842
YB
5341 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5342 for (j = 0;
5343 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5344 j++) {
5345
5346 pd_index =
5347 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5348
5349 sdev1 =
5350 scsi_device_lookup(host, i, j, 0);
5351
5352 if (instance->pd_list[pd_index].driveState
5353 == MR_PD_STATE_SYSTEM) {
5354 if (sdev1) {
5355 scsi_device_put(sdev1);
5356 }
5357 } else {
5358 if (sdev1) {
5359 scsi_remove_device(sdev1);
5360 scsi_device_put(sdev1);
5361 }
5362 }
5363 }
5364 }
5365 }
5366 doscan = 0;
5367 break;
5368
5369 case MR_EVT_LD_OFFLINE:
4c598b23 5370 case MR_EVT_CFG_CLEARED:
c9786842
YB
5371 case MR_EVT_LD_DELETED:
5372 megasas_get_ld_list(instance);
5373 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5374 for (j = 0;
5375 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5376 j++) {
5377
5378 ld_index =
5379 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5380
5381 sdev1 = scsi_device_lookup(host,
5382 i + MEGASAS_MAX_LD_CHANNELS,
5383 j,
5384 0);
5385
5386 if (instance->ld_ids[ld_index] != 0xff) {
5387 if (sdev1) {
5388 scsi_device_put(sdev1);
5389 }
5390 } else {
5391 if (sdev1) {
5392 scsi_remove_device(sdev1);
5393 scsi_device_put(sdev1);
5394 }
5395 }
5396 }
5397 }
5398 doscan = 0;
5399 break;
5400 case MR_EVT_LD_CREATED:
5401 megasas_get_ld_list(instance);
5402 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5403 for (j = 0;
5404 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5405 j++) {
5406 ld_index =
5407 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5408
5409 sdev1 = scsi_device_lookup(host,
5410 i+MEGASAS_MAX_LD_CHANNELS,
5411 j, 0);
5412
5413 if (instance->ld_ids[ld_index] !=
5414 0xff) {
5415 if (!sdev1) {
5416 scsi_add_device(host,
5417 i + 2,
5418 j, 0);
5419 }
5420 }
5421 if (sdev1) {
5422 scsi_device_put(sdev1);
5423 }
5424 }
5425 }
5426 doscan = 0;
5427 break;
7e8a75f4 5428 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
c9786842 5429 case MR_EVT_FOREIGN_CFG_IMPORTED:
9c915a8c 5430 case MR_EVT_LD_STATE_CHANGE:
7e8a75f4
YB
5431 doscan = 1;
5432 break;
5433 default:
5434 doscan = 0;
5435 break;
5436 }
5437 } else {
5438 printk(KERN_ERR "invalid evt_detail!\n");
5439 kfree(ev);
5440 return;
5441 }
5442
5443 if (doscan) {
5444 printk(KERN_INFO "scanning ...\n");
5445 megasas_get_pd_list(instance);
5446 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5447 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5448 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5449 sdev1 = scsi_device_lookup(host, i, j, 0);
5450 if (instance->pd_list[pd_index].driveState ==
5451 MR_PD_STATE_SYSTEM) {
5452 if (!sdev1) {
5453 scsi_add_device(host, i, j, 0);
5454 }
5455 if (sdev1)
5456 scsi_device_put(sdev1);
5457 } else {
5458 if (sdev1) {
5459 scsi_remove_device(sdev1);
5460 scsi_device_put(sdev1);
5461 }
5462 }
5463 }
5464 }
c9786842
YB
5465
5466 megasas_get_ld_list(instance);
5467 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5468 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5469 ld_index =
5470 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5471
5472 sdev1 = scsi_device_lookup(host,
5473 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5474 if (instance->ld_ids[ld_index] != 0xff) {
5475 if (!sdev1) {
5476 scsi_add_device(host,
5477 i+2,
5478 j, 0);
5479 } else {
5480 scsi_device_put(sdev1);
5481 }
5482 } else {
5483 if (sdev1) {
5484 scsi_remove_device(sdev1);
5485 scsi_device_put(sdev1);
5486 }
5487 }
5488 }
5489 }
7e8a75f4
YB
5490 }
5491
5492 if ( instance->aen_cmd != NULL ) {
5493 kfree(ev);
5494 return ;
5495 }
5496
5497 seq_num = instance->evt_detail->seq_num + 1;
5498
5499 /* Register AEN with FW for latest sequence number plus 1 */
5500 class_locale.members.reserved = 0;
5501 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5502 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5503 mutex_lock(&instance->aen_mutex);
5504 error = megasas_register_aen(instance, seq_num,
5505 class_locale.word);
5506 mutex_unlock(&instance->aen_mutex);
5507
5508 if (error)
5509 printk(KERN_ERR "register aen failed error %x\n", error);
5510
5511 kfree(ev);
5512}
5513
c4a3e0a5
BS
5514/**
5515 * megasas_init - Driver load entry point
5516 */
5517static int __init megasas_init(void)
5518{
5519 int rval;
5520
5521 /*
5522 * Announce driver version and other information
5523 */
5524 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5525 MEGASAS_EXT_VERSION);
5526
bd8d6dd4
KD
5527 spin_lock_init(&poll_aen_lock);
5528
72c4fd36 5529 support_poll_for_event = 2;
837f5fe8 5530 support_device_change = 1;
72c4fd36 5531
c4a3e0a5
BS
5532 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5533
5534 /*
5535 * Register character device node
5536 */
5537 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5538
5539 if (rval < 0) {
5540 printk(KERN_DEBUG "megasas: failed to open device node\n");
5541 return rval;
5542 }
5543
5544 megasas_mgmt_majorno = rval;
5545
5546 /*
5547 * Register ourselves as PCI hotplug module
5548 */
4041b9cd 5549 rval = pci_register_driver(&megasas_pci_driver);
c4a3e0a5
BS
5550
5551 if (rval) {
5552 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
83aabc1b
JG
5553 goto err_pcidrv;
5554 }
5555
5556 rval = driver_create_file(&megasas_pci_driver.driver,
5557 &driver_attr_version);
5558 if (rval)
5559 goto err_dcf_attr_ver;
5560 rval = driver_create_file(&megasas_pci_driver.driver,
5561 &driver_attr_release_date);
5562 if (rval)
5563 goto err_dcf_rel_date;
72c4fd36
YB
5564
5565 rval = driver_create_file(&megasas_pci_driver.driver,
5566 &driver_attr_support_poll_for_event);
5567 if (rval)
5568 goto err_dcf_support_poll_for_event;
5569
83aabc1b
JG
5570 rval = driver_create_file(&megasas_pci_driver.driver,
5571 &driver_attr_dbg_lvl);
5572 if (rval)
5573 goto err_dcf_dbg_lvl;
837f5fe8
YB
5574 rval = driver_create_file(&megasas_pci_driver.driver,
5575 &driver_attr_support_device_change);
5576 if (rval)
5577 goto err_dcf_support_device_change;
5578
c4a3e0a5 5579 return rval;
ad84db2e 5580
837f5fe8 5581err_dcf_support_device_change:
ad84db2e 5582 driver_remove_file(&megasas_pci_driver.driver,
5583 &driver_attr_dbg_lvl);
83aabc1b 5584err_dcf_dbg_lvl:
72c4fd36
YB
5585 driver_remove_file(&megasas_pci_driver.driver,
5586 &driver_attr_support_poll_for_event);
5587
5588err_dcf_support_poll_for_event:
83aabc1b
JG
5589 driver_remove_file(&megasas_pci_driver.driver,
5590 &driver_attr_release_date);
72c4fd36 5591
83aabc1b
JG
5592err_dcf_rel_date:
5593 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5594err_dcf_attr_ver:
5595 pci_unregister_driver(&megasas_pci_driver);
5596err_pcidrv:
5597 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
0d49016b 5598 return rval;
c4a3e0a5
BS
5599}
5600
5601/**
5602 * megasas_exit - Driver unload entry point
5603 */
5604static void __exit megasas_exit(void)
5605{
658dcedb
SP
5606 driver_remove_file(&megasas_pci_driver.driver,
5607 &driver_attr_dbg_lvl);
837f5fe8
YB
5608 driver_remove_file(&megasas_pci_driver.driver,
5609 &driver_attr_support_poll_for_event);
5610 driver_remove_file(&megasas_pci_driver.driver,
5611 &driver_attr_support_device_change);
83aabc1b
JG
5612 driver_remove_file(&megasas_pci_driver.driver,
5613 &driver_attr_release_date);
5614 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
c4a3e0a5
BS
5615
5616 pci_unregister_driver(&megasas_pci_driver);
5617 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5618}
5619
5620module_init(megasas_init);
5621module_exit(megasas_exit);
This page took 1.569575 seconds and 5 git commands to generate.