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