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