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