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