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