megaraid_sas: endianness related bug fixes and code optimization
[deliverable/linux.git] / drivers / scsi / megaraid / megaraid_sas_base.c
CommitLineData
c4a3e0a5 1/*
3f1530c1 2 * Linux MegaRAID driver for SAS based RAID controllers
c4a3e0a5 3 *
e399065b
SS
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
c4a3e0a5 6 *
3f1530c1 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 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 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 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 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 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 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 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 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 130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
131 /* Fusion */
229fe47c 132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
133 /* Plasma */
36807e67 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 634 /*
635 * Check if it is our interrupt
636 */
a3fda7dd 637 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
ebf054b0 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 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 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 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 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 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 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;
170c2387 1010 cmd->cmd_status = ENODATA;
c4a3e0a5 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 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 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
aa00832b
SS
1574 if (instance->unload == 1) {
1575 scmd->result = DID_NO_CONNECT << 16;
1576 scmd->scsi_done(scmd);
1577 return 0;
1578 }
1579
39a98554 1580 if (instance->issuepend_done == 0)
af37acfb
SP
1581 return SCSI_MLQUEUE_HOST_BUSY;
1582
39a98554 1583 spin_lock_irqsave(&instance->hba_lock, flags);
b09e66da 1584
229fe47c 1585 /* Check for an mpio path and adjust behavior */
1586 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1587 if (megasas_check_mpio_paths(instance, scmd) ==
1588 (DID_RESET << 16)) {
1589 spin_unlock_irqrestore(&instance->hba_lock, flags);
1590 return SCSI_MLQUEUE_HOST_BUSY;
1591 } else {
1592 spin_unlock_irqrestore(&instance->hba_lock, flags);
1593 scmd->result = DID_NO_CONNECT << 16;
fb1a24ff 1594 scmd->scsi_done(scmd);
229fe47c 1595 return 0;
1596 }
1597 }
1598
b09e66da
SS
1599 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1600 spin_unlock_irqrestore(&instance->hba_lock, flags);
229fe47c 1601 scmd->result = DID_NO_CONNECT << 16;
fb1a24ff 1602 scmd->scsi_done(scmd);
b09e66da
SS
1603 return 0;
1604 }
1605
39a98554 1606 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1607 spin_unlock_irqrestore(&instance->hba_lock, flags);
1608 return SCSI_MLQUEUE_HOST_BUSY;
1609 }
1610
1611 spin_unlock_irqrestore(&instance->hba_lock, flags);
1612
c4a3e0a5
BS
1613 scmd->result = 0;
1614
cb59aa6a 1615 if (MEGASAS_IS_LOGICAL(scmd) &&
51087a86
SS
1616 (scmd->device->id >= instance->fw_supported_vd_count ||
1617 scmd->device->lun)) {
cb59aa6a
SP
1618 scmd->result = DID_BAD_TARGET << 16;
1619 goto out_done;
c4a3e0a5
BS
1620 }
1621
02b01e01
SP
1622 switch (scmd->cmnd[0]) {
1623 case SYNCHRONIZE_CACHE:
1624 /*
1625 * FW takes care of flush cache on its own
1626 * No need to send it down
1627 */
1628 scmd->result = DID_OK << 16;
1629 goto out_done;
1630 default:
1631 break;
1632 }
1633
cd50ba8e 1634 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1635 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
cb59aa6a 1636 return SCSI_MLQUEUE_HOST_BUSY;
cd50ba8e 1637 }
c4a3e0a5
BS
1638
1639 return 0;
cb59aa6a 1640
cb59aa6a 1641 out_done:
fb1a24ff 1642 scmd->scsi_done(scmd);
cb59aa6a 1643 return 0;
c4a3e0a5
BS
1644}
1645
044833b5
YB
1646static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1647{
1648 int i;
1649
1650 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1651
1652 if ((megasas_mgmt_info.instance[i]) &&
1653 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1654 return megasas_mgmt_info.instance[i];
1655 }
1656
1657 return NULL;
1658}
1659
147aab6a
CH
1660static int megasas_slave_configure(struct scsi_device *sdev)
1661{
e5b3a65f 1662 /*
044833b5
YB
1663 * The RAID firmware may require extended timeouts.
1664 */
1665 blk_queue_rq_timeout(sdev->request_queue,
1666 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
07e38d94 1667
044833b5
YB
1668 return 0;
1669}
1670
1671static int megasas_slave_alloc(struct scsi_device *sdev)
1672{
1673 u16 pd_index = 0;
1674 struct megasas_instance *instance ;
1675 instance = megasas_lookup_instance(sdev->host->host_no);
07e38d94 1676 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
044833b5
YB
1677 /*
1678 * Open the OS scan to the SYSTEM PD
1679 */
1680 pd_index =
1681 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1682 sdev->id;
07e38d94
SS
1683 if (instance->pd_list[pd_index].driveState ==
1684 MR_PD_STATE_SYSTEM) {
044833b5
YB
1685 return 0;
1686 }
1687 return -ENXIO;
1688 }
147aab6a
CH
1689 return 0;
1690}
1691
9c915a8c 1692void megaraid_sas_kill_hba(struct megasas_instance *instance)
39a98554 1693{
1694 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
9c915a8c 1695 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
36807e67 1696 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
229fe47c 1697 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
21d3c710
SS
1698 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1699 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
9c915a8c 1700 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
229fe47c 1701 /* Flush */
1702 readl(&instance->reg_set->doorbell);
1703 if (instance->mpio && instance->requestorId)
1704 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
39a98554 1705 } else {
9c915a8c 1706 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1707 }
1708}
1709
1710 /**
1711 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1712 * restored to max value
1713 * @instance: Adapter soft state
1714 *
1715 */
1716void
1717megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1718{
1719 unsigned long flags;
1720 if (instance->flag & MEGASAS_FW_BUSY
c5daa6a9 1721 && time_after(jiffies, instance->last_time + 5 * HZ)
1722 && atomic_read(&instance->fw_outstanding) <
1723 instance->throttlequeuedepth + 1) {
9c915a8c 1724
1725 spin_lock_irqsave(instance->host->host_lock, flags);
1726 instance->flag &= ~MEGASAS_FW_BUSY;
404a8a1a 1727 if (instance->is_imr) {
9c915a8c 1728 instance->host->can_queue =
1729 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1730 } else
1731 instance->host->can_queue =
1732 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1733
1734 spin_unlock_irqrestore(instance->host->host_lock, flags);
39a98554 1735 }
1736}
1737
7343eb65 1738/**
1739 * megasas_complete_cmd_dpc - Returns FW's controller structure
1740 * @instance_addr: Address of adapter soft state
1741 *
1742 * Tasklet to complete cmds
1743 */
1744static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1745{
1746 u32 producer;
1747 u32 consumer;
1748 u32 context;
1749 struct megasas_cmd *cmd;
1750 struct megasas_instance *instance =
1751 (struct megasas_instance *)instance_addr;
1752 unsigned long flags;
1753
1754 /* If we have already declared adapter dead, donot complete cmds */
39a98554 1755 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
7343eb65 1756 return;
1757
1758 spin_lock_irqsave(&instance->completion_lock, flags);
1759
94cd65dd
SS
1760 producer = le32_to_cpu(*instance->producer);
1761 consumer = le32_to_cpu(*instance->consumer);
7343eb65 1762
1763 while (consumer != producer) {
94cd65dd 1764 context = le32_to_cpu(instance->reply_queue[consumer]);
39a98554 1765 if (context >= instance->max_fw_cmds) {
1766 printk(KERN_ERR "Unexpected context value %x\n",
1767 context);
1768 BUG();
1769 }
7343eb65 1770
1771 cmd = instance->cmd_list[context];
1772
1773 megasas_complete_cmd(instance, cmd, DID_OK);
1774
1775 consumer++;
1776 if (consumer == (instance->max_fw_cmds + 1)) {
1777 consumer = 0;
1778 }
1779 }
1780
94cd65dd 1781 *instance->consumer = cpu_to_le32(producer);
7343eb65 1782
1783 spin_unlock_irqrestore(&instance->completion_lock, flags);
1784
1785 /*
1786 * Check if we can restore can_queue
1787 */
9c915a8c 1788 megasas_check_and_restore_queue_depth(instance);
7343eb65 1789}
1790
229fe47c 1791/**
1792 * megasas_start_timer - Initializes a timer object
1793 * @instance: Adapter soft state
1794 * @timer: timer object to be initialized
1795 * @fn: timer function
1796 * @interval: time interval between timer function call
1797 *
1798 */
1799void megasas_start_timer(struct megasas_instance *instance,
1800 struct timer_list *timer,
1801 void *fn, unsigned long interval)
1802{
1803 init_timer(timer);
1804 timer->expires = jiffies + interval;
1805 timer->data = (unsigned long)instance;
1806 timer->function = fn;
1807 add_timer(timer);
1808}
1809
707e09bd
YB
1810static void
1811megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1812
1813static void
1814process_fw_state_change_wq(struct work_struct *work);
1815
1816void megasas_do_ocr(struct megasas_instance *instance)
1817{
1818 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1819 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1820 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
94cd65dd 1821 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
707e09bd 1822 }
d46a3ad6 1823 instance->instancet->disable_intr(instance);
707e09bd
YB
1824 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1825 instance->issuepend_done = 0;
1826
1827 atomic_set(&instance->fw_outstanding, 0);
1828 megasas_internal_reset_defer_cmds(instance);
1829 process_fw_state_change_wq(&instance->work_init);
1830}
1831
4cbfea88
AR
1832static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1833 int initial)
229fe47c 1834{
1835 struct megasas_cmd *cmd;
1836 struct megasas_dcmd_frame *dcmd;
229fe47c 1837 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
229fe47c 1838 dma_addr_t new_affiliation_111_h;
1839 int ld, retval = 0;
1840 u8 thisVf;
1841
1842 cmd = megasas_get_cmd(instance);
1843
1844 if (!cmd) {
4cbfea88
AR
1845 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation_111:"
1846 "Failed to get cmd for scsi%d.\n",
229fe47c 1847 instance->host->host_no);
1848 return -ENOMEM;
1849 }
1850
1851 dcmd = &cmd->frame->dcmd;
1852
4cbfea88 1853 if (!instance->vf_affiliation_111) {
229fe47c 1854 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1855 "affiliation for scsi%d.\n", instance->host->host_no);
1856 megasas_return_cmd(instance, cmd);
1857 return -ENOMEM;
1858 }
1859
1860 if (initial)
229fe47c 1861 memset(instance->vf_affiliation_111, 0,
1862 sizeof(struct MR_LD_VF_AFFILIATION_111));
229fe47c 1863 else {
4cbfea88
AR
1864 new_affiliation_111 =
1865 pci_alloc_consistent(instance->pdev,
1866 sizeof(struct MR_LD_VF_AFFILIATION_111),
1867 &new_affiliation_111_h);
1868 if (!new_affiliation_111) {
229fe47c 1869 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
1870 "memory for new affiliation for scsi%d.\n",
4cbfea88 1871 instance->host->host_no);
229fe47c 1872 megasas_return_cmd(instance, cmd);
1873 return -ENOMEM;
1874 }
4cbfea88
AR
1875 memset(new_affiliation_111, 0,
1876 sizeof(struct MR_LD_VF_AFFILIATION_111));
229fe47c 1877 }
1878
1879 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1880
1881 dcmd->cmd = MFI_CMD_DCMD;
1882 dcmd->cmd_status = 0xFF;
1883 dcmd->sge_count = 1;
1884 dcmd->flags = MFI_FRAME_DIR_BOTH;
1885 dcmd->timeout = 0;
1886 dcmd->pad_0 = 0;
4cbfea88
AR
1887 dcmd->data_xfer_len = sizeof(struct MR_LD_VF_AFFILIATION_111);
1888 dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111;
229fe47c 1889
4cbfea88
AR
1890 if (initial)
1891 dcmd->sgl.sge32[0].phys_addr =
1892 instance->vf_affiliation_111_h;
229fe47c 1893 else
4cbfea88
AR
1894 dcmd->sgl.sge32[0].phys_addr = new_affiliation_111_h;
1895
1896 dcmd->sgl.sge32[0].length =
1897 sizeof(struct MR_LD_VF_AFFILIATION_111);
229fe47c 1898
1899 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
1900 "scsi%d\n", instance->host->host_no);
1901
1902 megasas_issue_blocked_cmd(instance, cmd, 0);
1903
1904 if (dcmd->cmd_status) {
1905 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
1906 " failed with status 0x%x for scsi%d.\n",
1907 dcmd->cmd_status, instance->host->host_no);
1908 retval = 1; /* Do a scan if we couldn't get affiliation */
1909 goto out;
1910 }
1911
1912 if (!initial) {
4cbfea88
AR
1913 thisVf = new_affiliation_111->thisVf;
1914 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
1915 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
1916 new_affiliation_111->map[ld].policy[thisVf]) {
1917 printk(KERN_WARNING "megasas: SR-IOV: "
1918 "Got new LD/VF affiliation "
229fe47c 1919 "for scsi%d.\n",
229fe47c 1920 instance->host->host_no);
4cbfea88
AR
1921 memcpy(instance->vf_affiliation_111,
1922 new_affiliation_111,
1923 sizeof(struct MR_LD_VF_AFFILIATION_111));
229fe47c 1924 retval = 1;
1925 goto out;
1926 }
4cbfea88
AR
1927 }
1928out:
1929 if (new_affiliation_111) {
1930 pci_free_consistent(instance->pdev,
1931 sizeof(struct MR_LD_VF_AFFILIATION_111),
1932 new_affiliation_111,
1933 new_affiliation_111_h);
1934 }
90dc9d98
SS
1935
1936 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
1937 megasas_return_mfi_mpt_pthr(instance, cmd,
1938 cmd->mpt_pthr_cmd_blocked);
1939 else
1940 megasas_return_cmd(instance, cmd);
4cbfea88
AR
1941
1942 return retval;
1943}
1944
1945static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
1946 int initial)
1947{
1948 struct megasas_cmd *cmd;
1949 struct megasas_dcmd_frame *dcmd;
1950 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
1951 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
1952 dma_addr_t new_affiliation_h;
1953 int i, j, retval = 0, found = 0, doscan = 0;
1954 u8 thisVf;
1955
1956 cmd = megasas_get_cmd(instance);
1957
1958 if (!cmd) {
1959 printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation12: "
1960 "Failed to get cmd for scsi%d.\n",
1961 instance->host->host_no);
1962 return -ENOMEM;
1963 }
1964
1965 dcmd = &cmd->frame->dcmd;
1966
1967 if (!instance->vf_affiliation) {
1968 printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1969 "affiliation for scsi%d.\n", instance->host->host_no);
1970 megasas_return_cmd(instance, cmd);
1971 return -ENOMEM;
1972 }
1973
1974 if (initial)
1975 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
1976 sizeof(struct MR_LD_VF_AFFILIATION));
1977 else {
1978 new_affiliation =
1979 pci_alloc_consistent(instance->pdev,
1980 (MAX_LOGICAL_DRIVES + 1) *
1981 sizeof(struct MR_LD_VF_AFFILIATION),
1982 &new_affiliation_h);
1983 if (!new_affiliation) {
1984 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
1985 "memory for new affiliation for scsi%d.\n",
1986 instance->host->host_no);
1987 megasas_return_cmd(instance, cmd);
1988 return -ENOMEM;
1989 }
1990 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
1991 sizeof(struct MR_LD_VF_AFFILIATION));
1992 }
1993
1994 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1995
1996 dcmd->cmd = MFI_CMD_DCMD;
1997 dcmd->cmd_status = 0xFF;
1998 dcmd->sge_count = 1;
1999 dcmd->flags = MFI_FRAME_DIR_BOTH;
2000 dcmd->timeout = 0;
2001 dcmd->pad_0 = 0;
2002 dcmd->data_xfer_len = (MAX_LOGICAL_DRIVES + 1) *
2003 sizeof(struct MR_LD_VF_AFFILIATION);
2004 dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS;
2005
2006 if (initial)
2007 dcmd->sgl.sge32[0].phys_addr = instance->vf_affiliation_h;
2008 else
2009 dcmd->sgl.sge32[0].phys_addr = new_affiliation_h;
2010
2011 dcmd->sgl.sge32[0].length = (MAX_LOGICAL_DRIVES + 1) *
2012 sizeof(struct MR_LD_VF_AFFILIATION);
2013
2014 printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
2015 "scsi%d\n", instance->host->host_no);
2016
2017 megasas_issue_blocked_cmd(instance, cmd, 0);
2018
2019 if (dcmd->cmd_status) {
2020 printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
2021 " failed with status 0x%x for scsi%d.\n",
2022 dcmd->cmd_status, instance->host->host_no);
2023 retval = 1; /* Do a scan if we couldn't get affiliation */
2024 goto out;
2025 }
2026
2027 if (!initial) {
2028 if (!new_affiliation->ldCount) {
2029 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2030 "affiliation for passive path for scsi%d.\n",
2031 instance->host->host_no);
2032 retval = 1;
2033 goto out;
2034 }
2035 newmap = new_affiliation->map;
2036 savedmap = instance->vf_affiliation->map;
2037 thisVf = new_affiliation->thisVf;
2038 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2039 found = 0;
2040 for (j = 0; j < instance->vf_affiliation->ldCount;
2041 j++) {
2042 if (newmap->ref.targetId ==
2043 savedmap->ref.targetId) {
2044 found = 1;
2045 if (newmap->policy[thisVf] !=
2046 savedmap->policy[thisVf]) {
2047 doscan = 1;
2048 goto out;
2049 }
229fe47c 2050 }
2051 savedmap = (struct MR_LD_VF_MAP *)
2052 ((unsigned char *)savedmap +
2053 savedmap->size);
4cbfea88
AR
2054 }
2055 if (!found && newmap->policy[thisVf] !=
2056 MR_LD_ACCESS_HIDDEN) {
2057 doscan = 1;
2058 goto out;
2059 }
2060 newmap = (struct MR_LD_VF_MAP *)
2061 ((unsigned char *)newmap + newmap->size);
2062 }
2063
2064 newmap = new_affiliation->map;
2065 savedmap = instance->vf_affiliation->map;
2066
2067 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2068 found = 0;
2069 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2070 if (savedmap->ref.targetId ==
2071 newmap->ref.targetId) {
2072 found = 1;
2073 if (savedmap->policy[thisVf] !=
2074 newmap->policy[thisVf]) {
2075 doscan = 1;
2076 goto out;
2077 }
2078 }
229fe47c 2079 newmap = (struct MR_LD_VF_MAP *)
2080 ((unsigned char *)newmap +
2081 newmap->size);
2082 }
4cbfea88
AR
2083 if (!found && savedmap->policy[thisVf] !=
2084 MR_LD_ACCESS_HIDDEN) {
2085 doscan = 1;
2086 goto out;
2087 }
2088 savedmap = (struct MR_LD_VF_MAP *)
2089 ((unsigned char *)savedmap +
2090 savedmap->size);
229fe47c 2091 }
2092 }
2093out:
4cbfea88
AR
2094 if (doscan) {
2095 printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2096 "affiliation for scsi%d.\n", instance->host->host_no);
2097 memcpy(instance->vf_affiliation, new_affiliation,
2098 new_affiliation->size);
2099 retval = 1;
229fe47c 2100 }
4cbfea88
AR
2101
2102 if (new_affiliation)
2103 pci_free_consistent(instance->pdev,
2104 (MAX_LOGICAL_DRIVES + 1) *
2105 sizeof(struct MR_LD_VF_AFFILIATION),
2106 new_affiliation, new_affiliation_h);
90dc9d98
SS
2107 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2108 megasas_return_mfi_mpt_pthr(instance, cmd,
2109 cmd->mpt_pthr_cmd_blocked);
2110 else
2111 megasas_return_cmd(instance, cmd);
229fe47c 2112
2113 return retval;
2114}
2115
4cbfea88
AR
2116/* This function will get the current SR-IOV LD/VF affiliation */
2117static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2118 int initial)
2119{
2120 int retval;
2121
2122 if (instance->PlasmaFW111)
2123 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2124 else
2125 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2126 return retval;
2127}
2128
229fe47c 2129/* This function will tell FW to start the SR-IOV heartbeat */
2130int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2131 int initial)
2132{
2133 struct megasas_cmd *cmd;
2134 struct megasas_dcmd_frame *dcmd;
2135 int retval = 0;
2136
2137 cmd = megasas_get_cmd(instance);
2138
2139 if (!cmd) {
2140 printk(KERN_DEBUG "megasas: megasas_sriov_start_heartbeat: "
2141 "Failed to get cmd for scsi%d.\n",
2142 instance->host->host_no);
2143 return -ENOMEM;
2144 }
2145
2146 dcmd = &cmd->frame->dcmd;
2147
2148 if (initial) {
2149 instance->hb_host_mem =
7c845eb5
JP
2150 pci_zalloc_consistent(instance->pdev,
2151 sizeof(struct MR_CTRL_HB_HOST_MEM),
2152 &instance->hb_host_mem_h);
229fe47c 2153 if (!instance->hb_host_mem) {
2154 printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate"
2155 " memory for heartbeat host memory for "
2156 "scsi%d.\n", instance->host->host_no);
2157 retval = -ENOMEM;
2158 goto out;
2159 }
229fe47c 2160 }
2161
2162 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2163
2164 dcmd->mbox.s[0] = sizeof(struct MR_CTRL_HB_HOST_MEM);
2165 dcmd->cmd = MFI_CMD_DCMD;
2166 dcmd->cmd_status = 0xFF;
2167 dcmd->sge_count = 1;
2168 dcmd->flags = MFI_FRAME_DIR_BOTH;
2169 dcmd->timeout = 0;
2170 dcmd->pad_0 = 0;
2171 dcmd->data_xfer_len = sizeof(struct MR_CTRL_HB_HOST_MEM);
2172 dcmd->opcode = MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC;
2173 dcmd->sgl.sge32[0].phys_addr = instance->hb_host_mem_h;
2174 dcmd->sgl.sge32[0].length = sizeof(struct MR_CTRL_HB_HOST_MEM);
2175
2176 printk(KERN_WARNING "megasas: SR-IOV: Starting heartbeat for scsi%d\n",
2177 instance->host->host_no);
2178
2179 if (!megasas_issue_polled(instance, cmd)) {
2180 retval = 0;
2181 } else {
2182 printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2183 "_MEM_ALLOC DCMD timed out for scsi%d\n",
2184 instance->host->host_no);
2185 retval = 1;
2186 goto out;
2187 }
2188
2189
2190 if (dcmd->cmd_status) {
2191 printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2192 "_MEM_ALLOC DCMD failed with status 0x%x for scsi%d\n",
2193 dcmd->cmd_status,
2194 instance->host->host_no);
2195 retval = 1;
2196 goto out;
2197 }
2198
2199out:
2200 megasas_return_cmd(instance, cmd);
2201
2202 return retval;
2203}
2204
2205/* Handler for SR-IOV heartbeat */
2206void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2207{
2208 struct megasas_instance *instance =
2209 (struct megasas_instance *)instance_addr;
2210
2211 if (instance->hb_host_mem->HB.fwCounter !=
2212 instance->hb_host_mem->HB.driverCounter) {
2213 instance->hb_host_mem->HB.driverCounter =
2214 instance->hb_host_mem->HB.fwCounter;
2215 mod_timer(&instance->sriov_heartbeat_timer,
2216 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2217 } else {
2218 printk(KERN_WARNING "megasas: SR-IOV: Heartbeat never "
2219 "completed for scsi%d\n", instance->host->host_no);
2220 schedule_work(&instance->work_init);
2221 }
2222}
2223
c4a3e0a5
BS
2224/**
2225 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2226 * @instance: Adapter soft state
2227 *
25985edc 2228 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
c4a3e0a5
BS
2229 * complete all its outstanding commands. Returns error if one or more IOs
2230 * are pending after this time period. It also marks the controller dead.
2231 */
2232static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2233{
2234 int i;
39a98554 2235 u32 reset_index;
c4a3e0a5 2236 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
39a98554 2237 u8 adprecovery;
2238 unsigned long flags;
2239 struct list_head clist_local;
2240 struct megasas_cmd *reset_cmd;
707e09bd
YB
2241 u32 fw_state;
2242 u8 kill_adapter_flag;
39a98554 2243
2244 spin_lock_irqsave(&instance->hba_lock, flags);
2245 adprecovery = instance->adprecovery;
2246 spin_unlock_irqrestore(&instance->hba_lock, flags);
2247
2248 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2249
2250 INIT_LIST_HEAD(&clist_local);
2251 spin_lock_irqsave(&instance->hba_lock, flags);
2252 list_splice_init(&instance->internal_reset_pending_q,
2253 &clist_local);
2254 spin_unlock_irqrestore(&instance->hba_lock, flags);
2255
2256 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
2257 for (i = 0; i < wait_time; i++) {
2258 msleep(1000);
2259 spin_lock_irqsave(&instance->hba_lock, flags);
2260 adprecovery = instance->adprecovery;
2261 spin_unlock_irqrestore(&instance->hba_lock, flags);
2262 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2263 break;
2264 }
2265
2266 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2267 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
2268 spin_lock_irqsave(&instance->hba_lock, flags);
2269 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2270 spin_unlock_irqrestore(&instance->hba_lock, flags);
2271 return FAILED;
2272 }
2273
2274 reset_index = 0;
2275 while (!list_empty(&clist_local)) {
2276 reset_cmd = list_entry((&clist_local)->next,
2277 struct megasas_cmd, list);
2278 list_del_init(&reset_cmd->list);
2279 if (reset_cmd->scmd) {
2280 reset_cmd->scmd->result = DID_RESET << 16;
5cd049a5 2281 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
39a98554 2282 reset_index, reset_cmd,
5cd049a5 2283 reset_cmd->scmd->cmnd[0]);
39a98554 2284
2285 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2286 megasas_return_cmd(instance, reset_cmd);
2287 } else if (reset_cmd->sync_cmd) {
2288 printk(KERN_NOTICE "megasas:%p synch cmds"
2289 "reset queue\n",
2290 reset_cmd);
2291
2292 reset_cmd->cmd_status = ENODATA;
2293 instance->instancet->fire_cmd(instance,
2294 reset_cmd->frame_phys_addr,
2295 0, instance->reg_set);
2296 } else {
2297 printk(KERN_NOTICE "megasas: %p unexpected"
2298 "cmds lst\n",
2299 reset_cmd);
2300 }
2301 reset_index++;
2302 }
2303
2304 return SUCCESS;
2305 }
c4a3e0a5 2306
c007b8b2 2307 for (i = 0; i < resetwaittime; i++) {
c4a3e0a5 2308
e4a082c7
SP
2309 int outstanding = atomic_read(&instance->fw_outstanding);
2310
2311 if (!outstanding)
c4a3e0a5
BS
2312 break;
2313
2314 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2315 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
e4a082c7 2316 "commands to complete\n",i,outstanding);
7343eb65 2317 /*
2318 * Call cmd completion routine. Cmd to be
2319 * be completed directly without depending on isr.
2320 */
2321 megasas_complete_cmd_dpc((unsigned long)instance);
c4a3e0a5
BS
2322 }
2323
2324 msleep(1000);
2325 }
2326
707e09bd
YB
2327 i = 0;
2328 kill_adapter_flag = 0;
2329 do {
2330 fw_state = instance->instancet->read_fw_status_reg(
2331 instance->reg_set) & MFI_STATE_MASK;
2332 if ((fw_state == MFI_STATE_FAULT) &&
2333 (instance->disableOnlineCtrlReset == 0)) {
2334 if (i == 3) {
2335 kill_adapter_flag = 2;
2336 break;
2337 }
2338 megasas_do_ocr(instance);
2339 kill_adapter_flag = 1;
2340
2341 /* wait for 1 secs to let FW finish the pending cmds */
2342 msleep(1000);
2343 }
2344 i++;
2345 } while (i <= 3);
2346
2347 if (atomic_read(&instance->fw_outstanding) &&
2348 !kill_adapter_flag) {
2349 if (instance->disableOnlineCtrlReset == 0) {
2350
2351 megasas_do_ocr(instance);
2352
2353 /* wait for 5 secs to let FW finish the pending cmds */
2354 for (i = 0; i < wait_time; i++) {
2355 int outstanding =
2356 atomic_read(&instance->fw_outstanding);
2357 if (!outstanding)
2358 return SUCCESS;
2359 msleep(1000);
2360 }
2361 }
2362 }
2363
2364 if (atomic_read(&instance->fw_outstanding) ||
2365 (kill_adapter_flag == 2)) {
39a98554 2366 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
e3bbff9f
SP
2367 /*
2368 * Send signal to FW to stop processing any pending cmds.
2369 * The controller will be taken offline by the OS now.
2370 */
0c79e681
YB
2371 if ((instance->pdev->device ==
2372 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2373 (instance->pdev->device ==
2374 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2375 writel(MFI_STOP_ADP,
9c915a8c 2376 &instance->reg_set->doorbell);
0c79e681
YB
2377 } else {
2378 writel(MFI_STOP_ADP,
e3bbff9f 2379 &instance->reg_set->inbound_doorbell);
0c79e681 2380 }
658dcedb 2381 megasas_dump_pending_frames(instance);
39a98554 2382 spin_lock_irqsave(&instance->hba_lock, flags);
2383 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2384 spin_unlock_irqrestore(&instance->hba_lock, flags);
c4a3e0a5
BS
2385 return FAILED;
2386 }
2387
39a98554 2388 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
2389
c4a3e0a5
BS
2390 return SUCCESS;
2391}
2392
2393/**
2394 * megasas_generic_reset - Generic reset routine
2395 * @scmd: Mid-layer SCSI command
2396 *
2397 * This routine implements a generic reset handler for device, bus and host
2398 * reset requests. Device, bus and host specific reset handlers can use this
2399 * function after they do their specific tasks.
2400 */
2401static int megasas_generic_reset(struct scsi_cmnd *scmd)
2402{
2403 int ret_val;
2404 struct megasas_instance *instance;
2405
2406 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2407
5cd049a5
CH
2408 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2409 scmd->cmnd[0], scmd->retries);
c4a3e0a5 2410
39a98554 2411 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
c4a3e0a5
BS
2412 printk(KERN_ERR "megasas: cannot recover from previous reset "
2413 "failures\n");
2414 return FAILED;
2415 }
2416
c4a3e0a5 2417 ret_val = megasas_wait_for_outstanding(instance);
c4a3e0a5
BS
2418 if (ret_val == SUCCESS)
2419 printk(KERN_NOTICE "megasas: reset successful \n");
2420 else
2421 printk(KERN_ERR "megasas: failed to do reset\n");
2422
c4a3e0a5
BS
2423 return ret_val;
2424}
2425
05e9ebbe
SP
2426/**
2427 * megasas_reset_timer - quiesce the adapter if required
2428 * @scmd: scsi cmnd
2429 *
2430 * Sets the FW busy flag and reduces the host->can_queue if the
2431 * cmd has not been completed within the timeout period.
2432 */
2433static enum
242f9dcb 2434blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
05e9ebbe 2435{
05e9ebbe
SP
2436 struct megasas_instance *instance;
2437 unsigned long flags;
2438
2439 if (time_after(jiffies, scmd->jiffies_at_alloc +
2440 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
242f9dcb 2441 return BLK_EH_NOT_HANDLED;
05e9ebbe
SP
2442 }
2443
f575c5d3 2444 instance = (struct megasas_instance *)scmd->device->host->hostdata;
05e9ebbe
SP
2445 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2446 /* FW is busy, throttle IO */
2447 spin_lock_irqsave(instance->host->host_lock, flags);
2448
c5daa6a9 2449 instance->host->can_queue = instance->throttlequeuedepth;
05e9ebbe
SP
2450 instance->last_time = jiffies;
2451 instance->flag |= MEGASAS_FW_BUSY;
2452
2453 spin_unlock_irqrestore(instance->host->host_lock, flags);
2454 }
242f9dcb 2455 return BLK_EH_RESET_TIMER;
05e9ebbe
SP
2456}
2457
c4a3e0a5
BS
2458/**
2459 * megasas_reset_device - Device reset handler entry point
2460 */
2461static int megasas_reset_device(struct scsi_cmnd *scmd)
2462{
2463 int ret;
2464
2465 /*
2466 * First wait for all commands to complete
2467 */
2468 ret = megasas_generic_reset(scmd);
2469
2470 return ret;
2471}
2472
2473/**
2474 * megasas_reset_bus_host - Bus & host reset handler entry point
2475 */
2476static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2477{
2478 int ret;
9c915a8c 2479 struct megasas_instance *instance;
2480 instance = (struct megasas_instance *)scmd->device->host->hostdata;
c4a3e0a5
BS
2481
2482 /*
80682fa9 2483 * First wait for all commands to complete
c4a3e0a5 2484 */
36807e67 2485 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
229fe47c 2486 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
21d3c710
SS
2487 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2488 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
229fe47c 2489 ret = megasas_reset_fusion(scmd->device->host, 1);
9c915a8c 2490 else
2491 ret = megasas_generic_reset(scmd);
c4a3e0a5
BS
2492
2493 return ret;
2494}
2495
cf62a0a5
SP
2496/**
2497 * megasas_bios_param - Returns disk geometry for a disk
2498 * @sdev: device handle
2499 * @bdev: block device
2500 * @capacity: drive capacity
2501 * @geom: geometry parameters
2502 */
2503static int
2504megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2505 sector_t capacity, int geom[])
2506{
2507 int heads;
2508 int sectors;
2509 sector_t cylinders;
2510 unsigned long tmp;
2511 /* Default heads (64) & sectors (32) */
2512 heads = 64;
2513 sectors = 32;
2514
2515 tmp = heads * sectors;
2516 cylinders = capacity;
2517
2518 sector_div(cylinders, tmp);
2519
2520 /*
2521 * Handle extended translation size for logical drives > 1Gb
2522 */
2523
2524 if (capacity >= 0x200000) {
2525 heads = 255;
2526 sectors = 63;
2527 tmp = heads*sectors;
2528 cylinders = capacity;
2529 sector_div(cylinders, tmp);
2530 }
2531
2532 geom[0] = heads;
2533 geom[1] = sectors;
2534 geom[2] = cylinders;
2535
2536 return 0;
2537}
2538
7e8a75f4
YB
2539static void megasas_aen_polling(struct work_struct *work);
2540
c4a3e0a5
BS
2541/**
2542 * megasas_service_aen - Processes an event notification
2543 * @instance: Adapter soft state
2544 * @cmd: AEN command completed by the ISR
2545 *
2546 * For AEN, driver sends a command down to FW that is held by the FW till an
2547 * event occurs. When an event of interest occurs, FW completes the command
2548 * that it was previously holding.
2549 *
2550 * This routines sends SIGIO signal to processes that have registered with the
2551 * driver for AEN.
2552 */
2553static void
2554megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2555{
c3518837 2556 unsigned long flags;
c4a3e0a5
BS
2557 /*
2558 * Don't signal app if it is just an aborted previously registered aen
2559 */
c3518837
YB
2560 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2561 spin_lock_irqsave(&poll_aen_lock, flags);
2562 megasas_poll_wait_aen = 1;
2563 spin_unlock_irqrestore(&poll_aen_lock, flags);
2564 wake_up(&megasas_poll_wait);
c4a3e0a5 2565 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
c3518837 2566 }
c4a3e0a5
BS
2567 else
2568 cmd->abort_aen = 0;
2569
2570 instance->aen_cmd = NULL;
90dc9d98
SS
2571
2572 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2573 megasas_return_mfi_mpt_pthr(instance, cmd,
2574 cmd->mpt_pthr_cmd_blocked);
2575 else
2576 megasas_return_cmd(instance, cmd);
7e8a75f4 2577
39a98554 2578 if ((instance->unload == 0) &&
2579 ((instance->issuepend_done == 1))) {
7e8a75f4
YB
2580 struct megasas_aen_event *ev;
2581 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2582 if (!ev) {
2583 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2584 } else {
2585 ev->instance = instance;
2586 instance->ev = ev;
c1d390d8
XF
2587 INIT_DELAYED_WORK(&ev->hotplug_work,
2588 megasas_aen_polling);
2589 schedule_delayed_work(&ev->hotplug_work, 0);
7e8a75f4
YB
2590 }
2591 }
c4a3e0a5
BS
2592}
2593
fc62b3fc
SS
2594static ssize_t
2595megasas_fw_crash_buffer_store(struct device *cdev,
2596 struct device_attribute *attr, const char *buf, size_t count)
2597{
2598 struct Scsi_Host *shost = class_to_shost(cdev);
2599 struct megasas_instance *instance =
2600 (struct megasas_instance *) shost->hostdata;
2601 int val = 0;
2602 unsigned long flags;
2603
2604 if (kstrtoint(buf, 0, &val) != 0)
2605 return -EINVAL;
2606
2607 spin_lock_irqsave(&instance->crashdump_lock, flags);
2608 instance->fw_crash_buffer_offset = val;
2609 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2610 return strlen(buf);
2611}
2612
2613static ssize_t
2614megasas_fw_crash_buffer_show(struct device *cdev,
2615 struct device_attribute *attr, char *buf)
2616{
2617 struct Scsi_Host *shost = class_to_shost(cdev);
2618 struct megasas_instance *instance =
2619 (struct megasas_instance *) shost->hostdata;
2620 u32 size;
2621 unsigned long buff_addr;
2622 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2623 unsigned long src_addr;
2624 unsigned long flags;
2625 u32 buff_offset;
2626
2627 spin_lock_irqsave(&instance->crashdump_lock, flags);
2628 buff_offset = instance->fw_crash_buffer_offset;
2629 if (!instance->crash_dump_buf &&
2630 !((instance->fw_crash_state == AVAILABLE) ||
2631 (instance->fw_crash_state == COPYING))) {
2632 dev_err(&instance->pdev->dev,
2633 "Firmware crash dump is not available\n");
2634 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2635 return -EINVAL;
2636 }
2637
2638 buff_addr = (unsigned long) buf;
2639
2640 if (buff_offset >
2641 (instance->fw_crash_buffer_size * dmachunk)) {
2642 dev_err(&instance->pdev->dev,
2643 "Firmware crash dump offset is out of range\n");
2644 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2645 return 0;
2646 }
2647
2648 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2649 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2650
2651 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2652 (buff_offset % dmachunk);
2653 memcpy(buf, (void *)src_addr, size);
2654 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2655
2656 return size;
2657}
2658
2659static ssize_t
2660megasas_fw_crash_buffer_size_show(struct device *cdev,
2661 struct device_attribute *attr, char *buf)
2662{
2663 struct Scsi_Host *shost = class_to_shost(cdev);
2664 struct megasas_instance *instance =
2665 (struct megasas_instance *) shost->hostdata;
2666
2667 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2668 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2669}
2670
2671static ssize_t
2672megasas_fw_crash_state_store(struct device *cdev,
2673 struct device_attribute *attr, const char *buf, size_t count)
2674{
2675 struct Scsi_Host *shost = class_to_shost(cdev);
2676 struct megasas_instance *instance =
2677 (struct megasas_instance *) shost->hostdata;
2678 int val = 0;
2679 unsigned long flags;
2680
2681 if (kstrtoint(buf, 0, &val) != 0)
2682 return -EINVAL;
2683
2684 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2685 dev_err(&instance->pdev->dev, "application updates invalid "
2686 "firmware crash state\n");
2687 return -EINVAL;
2688 }
2689
2690 instance->fw_crash_state = val;
2691
2692 if ((val == COPIED) || (val == COPY_ERROR)) {
2693 spin_lock_irqsave(&instance->crashdump_lock, flags);
2694 megasas_free_host_crash_buffer(instance);
2695 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2696 if (val == COPY_ERROR)
2697 dev_info(&instance->pdev->dev, "application failed to "
2698 "copy Firmware crash dump\n");
2699 else
2700 dev_info(&instance->pdev->dev, "Firmware crash dump "
2701 "copied successfully\n");
2702 }
2703 return strlen(buf);
2704}
2705
2706static ssize_t
2707megasas_fw_crash_state_show(struct device *cdev,
2708 struct device_attribute *attr, char *buf)
2709{
2710 struct Scsi_Host *shost = class_to_shost(cdev);
2711 struct megasas_instance *instance =
2712 (struct megasas_instance *) shost->hostdata;
2713 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2714}
2715
2716static ssize_t
2717megasas_page_size_show(struct device *cdev,
2718 struct device_attribute *attr, char *buf)
2719{
2720 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2721}
2722
2723static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2724 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2725static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2726 megasas_fw_crash_buffer_size_show, NULL);
2727static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2728 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2729static DEVICE_ATTR(page_size, S_IRUGO,
2730 megasas_page_size_show, NULL);
2731
2732struct device_attribute *megaraid_host_attrs[] = {
2733 &dev_attr_fw_crash_buffer_size,
2734 &dev_attr_fw_crash_buffer,
2735 &dev_attr_fw_crash_state,
2736 &dev_attr_page_size,
2737 NULL,
2738};
2739
c4a3e0a5
BS
2740/*
2741 * Scsi host template for megaraid_sas driver
2742 */
2743static struct scsi_host_template megasas_template = {
2744
2745 .module = THIS_MODULE,
f28cd7cf 2746 .name = "LSI SAS based MegaRAID driver",
c4a3e0a5 2747 .proc_name = "megaraid_sas",
147aab6a 2748 .slave_configure = megasas_slave_configure,
044833b5 2749 .slave_alloc = megasas_slave_alloc,
c4a3e0a5
BS
2750 .queuecommand = megasas_queue_command,
2751 .eh_device_reset_handler = megasas_reset_device,
2752 .eh_bus_reset_handler = megasas_reset_bus_host,
2753 .eh_host_reset_handler = megasas_reset_bus_host,
05e9ebbe 2754 .eh_timed_out = megasas_reset_timer,
fc62b3fc 2755 .shost_attrs = megaraid_host_attrs,
cf62a0a5 2756 .bios_param = megasas_bios_param,
c4a3e0a5 2757 .use_clustering = ENABLE_CLUSTERING,
db5ed4df 2758 .change_queue_depth = scsi_change_queue_depth,
54b2b50c 2759 .no_write_same = 1,
c4a3e0a5
BS
2760};
2761
2762/**
2763 * megasas_complete_int_cmd - Completes an internal command
2764 * @instance: Adapter soft state
2765 * @cmd: Command to be completed
2766 *
2767 * The megasas_issue_blocked_cmd() function waits for a command to complete
2768 * after it issues a command. This function wakes up that waiting routine by
2769 * calling wake_up() on the wait queue.
2770 */
2771static void
2772megasas_complete_int_cmd(struct megasas_instance *instance,
2773 struct megasas_cmd *cmd)
2774{
2775 cmd->cmd_status = cmd->frame->io.cmd_status;
2776
2777 if (cmd->cmd_status == ENODATA) {
2778 cmd->cmd_status = 0;
2779 }
2780 wake_up(&instance->int_cmd_wait_q);
2781}
2782
2783/**
2784 * megasas_complete_abort - Completes aborting a command
2785 * @instance: Adapter soft state
2786 * @cmd: Cmd that was issued to abort another cmd
2787 *
0d49016b 2788 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2789 * after it issues an abort on a previously issued command. This function
c4a3e0a5
BS
2790 * wakes up all functions waiting on the same wait queue.
2791 */
2792static void
2793megasas_complete_abort(struct megasas_instance *instance,
2794 struct megasas_cmd *cmd)
2795{
2796 if (cmd->sync_cmd) {
2797 cmd->sync_cmd = 0;
2798 cmd->cmd_status = 0;
2799 wake_up(&instance->abort_cmd_wait_q);
2800 }
2801
2802 return;
2803}
2804
c4a3e0a5
BS
2805/**
2806 * megasas_complete_cmd - Completes a command
2807 * @instance: Adapter soft state
2808 * @cmd: Command to be completed
0d49016b 2809 * @alt_status: If non-zero, use this value as status to
c4a3e0a5
BS
2810 * SCSI mid-layer instead of the value returned
2811 * by the FW. This should be used if caller wants
2812 * an alternate status (as in the case of aborted
2813 * commands)
2814 */
9c915a8c 2815void
c4a3e0a5
BS
2816megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2817 u8 alt_status)
2818{
2819 int exception = 0;
2820 struct megasas_header *hdr = &cmd->frame->hdr;
c3518837 2821 unsigned long flags;
9c915a8c 2822 struct fusion_context *fusion = instance->ctrl_context;
94cd65dd 2823 u32 opcode;
c4a3e0a5 2824
39a98554 2825 /* flag for the retry reset */
2826 cmd->retry_for_fw_reset = 0;
2827
05e9ebbe
SP
2828 if (cmd->scmd)
2829 cmd->scmd->SCp.ptr = NULL;
c4a3e0a5
BS
2830
2831 switch (hdr->cmd) {
e5f93a36 2832 case MFI_CMD_INVALID:
2833 /* Some older 1068 controller FW may keep a pended
2834 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2835 when booting the kdump kernel. Ignore this command to
2836 prevent a kernel panic on shutdown of the kdump kernel. */
2837 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2838 "completed.\n");
2839 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2840 "other than PERC5, please upgrade your firmware.\n");
2841 break;
c4a3e0a5
BS
2842 case MFI_CMD_PD_SCSI_IO:
2843 case MFI_CMD_LD_SCSI_IO:
2844
2845 /*
2846 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2847 * issued either through an IO path or an IOCTL path. If it
2848 * was via IOCTL, we will send it to internal completion.
2849 */
2850 if (cmd->sync_cmd) {
2851 cmd->sync_cmd = 0;
2852 megasas_complete_int_cmd(instance, cmd);
2853 break;
2854 }
2855
c4a3e0a5
BS
2856 case MFI_CMD_LD_READ:
2857 case MFI_CMD_LD_WRITE:
2858
2859 if (alt_status) {
2860 cmd->scmd->result = alt_status << 16;
2861 exception = 1;
2862 }
2863
2864 if (exception) {
2865
e4a082c7 2866 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 2867
155d98f0 2868 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
2869 cmd->scmd->scsi_done(cmd->scmd);
2870 megasas_return_cmd(instance, cmd);
2871
2872 break;
2873 }
2874
2875 switch (hdr->cmd_status) {
2876
2877 case MFI_STAT_OK:
2878 cmd->scmd->result = DID_OK << 16;
2879 break;
2880
2881 case MFI_STAT_SCSI_IO_FAILED:
2882 case MFI_STAT_LD_INIT_IN_PROGRESS:
2883 cmd->scmd->result =
2884 (DID_ERROR << 16) | hdr->scsi_status;
2885 break;
2886
2887 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2888
2889 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2890
2891 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2892 memset(cmd->scmd->sense_buffer, 0,
2893 SCSI_SENSE_BUFFERSIZE);
2894 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2895 hdr->sense_len);
2896
2897 cmd->scmd->result |= DRIVER_SENSE << 24;
2898 }
2899
2900 break;
2901
2902 case MFI_STAT_LD_OFFLINE:
2903 case MFI_STAT_DEVICE_NOT_FOUND:
2904 cmd->scmd->result = DID_BAD_TARGET << 16;
2905 break;
2906
2907 default:
2908 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2909 hdr->cmd_status);
2910 cmd->scmd->result = DID_ERROR << 16;
2911 break;
2912 }
2913
e4a082c7 2914 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 2915
155d98f0 2916 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
2917 cmd->scmd->scsi_done(cmd->scmd);
2918 megasas_return_cmd(instance, cmd);
2919
2920 break;
2921
2922 case MFI_CMD_SMP:
2923 case MFI_CMD_STP:
2924 case MFI_CMD_DCMD:
94cd65dd 2925 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
9c915a8c 2926 /* Check for LD map update */
94cd65dd
SS
2927 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2928 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
bc93d425 2929 fusion->fast_path_io = 0;
9c915a8c 2930 spin_lock_irqsave(instance->host->host_lock, flags);
2931 if (cmd->frame->hdr.cmd_status != 0) {
2932 if (cmd->frame->hdr.cmd_status !=
2933 MFI_STAT_NOT_FOUND)
2934 printk(KERN_WARNING "megasas: map sync"
2935 "failed, status = 0x%x.\n",
2936 cmd->frame->hdr.cmd_status);
2937 else {
90dc9d98
SS
2938 megasas_return_mfi_mpt_pthr(instance,
2939 cmd, cmd->mpt_pthr_cmd_blocked);
9c915a8c 2940 spin_unlock_irqrestore(
2941 instance->host->host_lock,
2942 flags);
2943 break;
2944 }
2945 } else
2946 instance->map_id++;
90dc9d98
SS
2947 megasas_return_mfi_mpt_pthr(instance, cmd,
2948 cmd->mpt_pthr_cmd_blocked);
bc93d425
SS
2949
2950 /*
2951 * Set fast path IO to ZERO.
2952 * Validate Map will set proper value.
2953 * Meanwhile all IOs will go as LD IO.
2954 */
2955 if (MR_ValidateMapInfo(instance))
9c915a8c 2956 fusion->fast_path_io = 1;
2957 else
2958 fusion->fast_path_io = 0;
2959 megasas_sync_map_info(instance);
2960 spin_unlock_irqrestore(instance->host->host_lock,
2961 flags);
2962 break;
2963 }
94cd65dd
SS
2964 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2965 opcode == MR_DCMD_CTRL_EVENT_GET) {
c3518837
YB
2966 spin_lock_irqsave(&poll_aen_lock, flags);
2967 megasas_poll_wait_aen = 0;
2968 spin_unlock_irqrestore(&poll_aen_lock, flags);
2969 }
c4a3e0a5
BS
2970
2971 /*
2972 * See if got an event notification
2973 */
94cd65dd 2974 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
c4a3e0a5
BS
2975 megasas_service_aen(instance, cmd);
2976 else
2977 megasas_complete_int_cmd(instance, cmd);
2978
2979 break;
2980
2981 case MFI_CMD_ABORT:
2982 /*
2983 * Cmd issued to abort another cmd returned
2984 */
2985 megasas_complete_abort(instance, cmd);
2986 break;
2987
2988 default:
2989 printk("megasas: Unknown command completed! [0x%X]\n",
2990 hdr->cmd);
2991 break;
2992 }
2993}
2994
39a98554 2995/**
2996 * megasas_issue_pending_cmds_again - issue all pending cmds
2997 * in FW again because of the fw reset
2998 * @instance: Adapter soft state
2999 */
3000static inline void
3001megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3002{
3003 struct megasas_cmd *cmd;
3004 struct list_head clist_local;
3005 union megasas_evt_class_locale class_locale;
3006 unsigned long flags;
3007 u32 seq_num;
3008
3009 INIT_LIST_HEAD(&clist_local);
3010 spin_lock_irqsave(&instance->hba_lock, flags);
3011 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3012 spin_unlock_irqrestore(&instance->hba_lock, flags);
3013
3014 while (!list_empty(&clist_local)) {
3015 cmd = list_entry((&clist_local)->next,
3016 struct megasas_cmd, list);
3017 list_del_init(&cmd->list);
3018
3019 if (cmd->sync_cmd || cmd->scmd) {
3020 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
3021 "detected to be pending while HBA reset.\n",
3022 cmd, cmd->scmd, cmd->sync_cmd);
3023
3024 cmd->retry_for_fw_reset++;
3025
3026 if (cmd->retry_for_fw_reset == 3) {
3027 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
3028 "was tried multiple times during reset."
3029 "Shutting down the HBA\n",
3030 cmd, cmd->scmd, cmd->sync_cmd);
3031 megaraid_sas_kill_hba(instance);
3032
3033 instance->adprecovery =
3034 MEGASAS_HW_CRITICAL_ERROR;
3035 return;
3036 }
3037 }
3038
3039 if (cmd->sync_cmd == 1) {
3040 if (cmd->scmd) {
3041 printk(KERN_NOTICE "megaraid_sas: unexpected"
3042 "cmd attached to internal command!\n");
3043 }
3044 printk(KERN_NOTICE "megasas: %p synchronous cmd"
3045 "on the internal reset queue,"
3046 "issue it again.\n", cmd);
3047 cmd->cmd_status = ENODATA;
3048 instance->instancet->fire_cmd(instance,
3049 cmd->frame_phys_addr ,
3050 0, instance->reg_set);
3051 } else if (cmd->scmd) {
5cd049a5 3052 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
39a98554 3053 "detected on the internal queue, issue again.\n",
5cd049a5 3054 cmd, cmd->scmd->cmnd[0]);
39a98554 3055
3056 atomic_inc(&instance->fw_outstanding);
3057 instance->instancet->fire_cmd(instance,
3058 cmd->frame_phys_addr,
3059 cmd->frame_count-1, instance->reg_set);
3060 } else {
3061 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
3062 "internal reset defer list while re-issue!!\n",
3063 cmd);
3064 }
3065 }
3066
3067 if (instance->aen_cmd) {
3068 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
3069 megasas_return_cmd(instance, instance->aen_cmd);
3070
3071 instance->aen_cmd = NULL;
3072 }
3073
3074 /*
3075 * Initiate AEN (Asynchronous Event Notification)
3076 */
3077 seq_num = instance->last_seq_num;
3078 class_locale.members.reserved = 0;
3079 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3080 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3081
3082 megasas_register_aen(instance, seq_num, class_locale.word);
3083}
3084
3085/**
3086 * Move the internal reset pending commands to a deferred queue.
3087 *
3088 * We move the commands pending at internal reset time to a
3089 * pending queue. This queue would be flushed after successful
3090 * completion of the internal reset sequence. if the internal reset
3091 * did not complete in time, the kernel reset handler would flush
3092 * these commands.
3093 **/
3094static void
3095megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3096{
3097 struct megasas_cmd *cmd;
3098 int i;
3099 u32 max_cmd = instance->max_fw_cmds;
3100 u32 defer_index;
3101 unsigned long flags;
3102
3103 defer_index = 0;
90dc9d98 3104 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
39a98554 3105 for (i = 0; i < max_cmd; i++) {
3106 cmd = instance->cmd_list[i];
3107 if (cmd->sync_cmd == 1 || cmd->scmd) {
3108 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
3109 "on the defer queue as internal\n",
3110 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3111
3112 if (!list_empty(&cmd->list)) {
3113 printk(KERN_NOTICE "megaraid_sas: ERROR while"
3114 " moving this cmd:%p, %d %p, it was"
3115 "discovered on some list?\n",
3116 cmd, cmd->sync_cmd, cmd->scmd);
3117
3118 list_del_init(&cmd->list);
3119 }
3120 defer_index++;
3121 list_add_tail(&cmd->list,
3122 &instance->internal_reset_pending_q);
3123 }
3124 }
90dc9d98 3125 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
39a98554 3126}
3127
3128
3129static void
3130process_fw_state_change_wq(struct work_struct *work)
3131{
3132 struct megasas_instance *instance =
3133 container_of(work, struct megasas_instance, work_init);
3134 u32 wait;
3135 unsigned long flags;
3136
3137 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3138 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
3139 instance->adprecovery);
3140 return ;
3141 }
3142
3143 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3144 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
3145 "state, restarting it...\n");
3146
d46a3ad6 3147 instance->instancet->disable_intr(instance);
39a98554 3148 atomic_set(&instance->fw_outstanding, 0);
3149
3150 atomic_set(&instance->fw_reset_no_pci_access, 1);
3151 instance->instancet->adp_reset(instance, instance->reg_set);
3152 atomic_set(&instance->fw_reset_no_pci_access, 0 );
3153
3154 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
3155 "initiating next stage...\n");
3156
3157 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
3158 "state 2 starting...\n");
3159
3160 /*waitting for about 20 second before start the second init*/
3161 for (wait = 0; wait < 30; wait++) {
3162 msleep(1000);
3163 }
3164
058a8fac 3165 if (megasas_transition_to_ready(instance, 1)) {
39a98554 3166 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
3167
3168 megaraid_sas_kill_hba(instance);
3169 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
3170 return ;
3171 }
3172
3173 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3174 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3175 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3176 ) {
3177 *instance->consumer = *instance->producer;
3178 } else {
3179 *instance->consumer = 0;
3180 *instance->producer = 0;
3181 }
3182
3183 megasas_issue_init_mfi(instance);
3184
3185 spin_lock_irqsave(&instance->hba_lock, flags);
3186 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3187 spin_unlock_irqrestore(&instance->hba_lock, flags);
d46a3ad6 3188 instance->instancet->enable_intr(instance);
39a98554 3189
3190 megasas_issue_pending_cmds_again(instance);
3191 instance->issuepend_done = 1;
3192 }
3193 return ;
3194}
3195
c4a3e0a5
BS
3196/**
3197 * megasas_deplete_reply_queue - Processes all completed commands
3198 * @instance: Adapter soft state
3199 * @alt_status: Alternate status to be returned to
3200 * SCSI mid-layer instead of the status
3201 * returned by the FW
39a98554 3202 * Note: this must be called with hba lock held
c4a3e0a5 3203 */
858119e1 3204static int
39a98554 3205megasas_deplete_reply_queue(struct megasas_instance *instance,
3206 u8 alt_status)
c4a3e0a5 3207{
39a98554 3208 u32 mfiStatus;
3209 u32 fw_state;
3210
3211 if ((mfiStatus = instance->instancet->check_reset(instance,
3212 instance->reg_set)) == 1) {
3213 return IRQ_HANDLED;
3214 }
3215
3216 if ((mfiStatus = instance->instancet->clear_intr(
3217 instance->reg_set)
3218 ) == 0) {
e1419191 3219 /* Hardware may not set outbound_intr_status in MSI-X mode */
c8e858fe 3220 if (!instance->msix_vectors)
e1419191 3221 return IRQ_NONE;
39a98554 3222 }
3223
3224 instance->mfiStatus = mfiStatus;
3225
3226 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3227 fw_state = instance->instancet->read_fw_status_reg(
3228 instance->reg_set) & MFI_STATE_MASK;
3229
3230 if (fw_state != MFI_STATE_FAULT) {
3231 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
3232 fw_state);
3233 }
3234
3235 if ((fw_state == MFI_STATE_FAULT) &&
3236 (instance->disableOnlineCtrlReset == 0)) {
3237 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
3238
3239 if ((instance->pdev->device ==
3240 PCI_DEVICE_ID_LSI_SAS1064R) ||
3241 (instance->pdev->device ==
3242 PCI_DEVICE_ID_DELL_PERC5) ||
3243 (instance->pdev->device ==
3244 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3245
3246 *instance->consumer =
94cd65dd 3247 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
39a98554 3248 }
3249
3250
d46a3ad6 3251 instance->instancet->disable_intr(instance);
39a98554 3252 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3253 instance->issuepend_done = 0;
3254
3255 atomic_set(&instance->fw_outstanding, 0);
3256 megasas_internal_reset_defer_cmds(instance);
3257
3258 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
3259 fw_state, instance->adprecovery);
3260
3261 schedule_work(&instance->work_init);
3262 return IRQ_HANDLED;
3263
3264 } else {
3265 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
3266 fw_state, instance->disableOnlineCtrlReset);
3267 }
3268 }
c4a3e0a5 3269
5d018ad0 3270 tasklet_schedule(&instance->isr_tasklet);
c4a3e0a5
BS
3271 return IRQ_HANDLED;
3272}
c4a3e0a5
BS
3273/**
3274 * megasas_isr - isr entry point
3275 */
7d12e780 3276static irqreturn_t megasas_isr(int irq, void *devp)
c4a3e0a5 3277{
c8e858fe 3278 struct megasas_irq_context *irq_context = devp;
3279 struct megasas_instance *instance = irq_context->instance;
39a98554 3280 unsigned long flags;
3281 irqreturn_t rc;
3282
c8e858fe 3283 if (atomic_read(&instance->fw_reset_no_pci_access))
39a98554 3284 return IRQ_HANDLED;
3285
39a98554 3286 spin_lock_irqsave(&instance->hba_lock, flags);
3287 rc = megasas_deplete_reply_queue(instance, DID_OK);
3288 spin_unlock_irqrestore(&instance->hba_lock, flags);
3289
3290 return rc;
c4a3e0a5
BS
3291}
3292
3293/**
3294 * megasas_transition_to_ready - Move the FW to READY state
1341c939 3295 * @instance: Adapter soft state
c4a3e0a5
BS
3296 *
3297 * During the initialization, FW passes can potentially be in any one of
3298 * several possible states. If the FW in operational, waiting-for-handshake
3299 * states, driver must take steps to bring it to ready state. Otherwise, it
3300 * has to wait for the ready state.
3301 */
9c915a8c 3302int
058a8fac 3303megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
c4a3e0a5
BS
3304{
3305 int i;
3306 u8 max_wait;
3307 u32 fw_state;
3308 u32 cur_state;
7218df69 3309 u32 abs_state, curr_abs_state;
c4a3e0a5 3310
bc6ac5e8
TH
3311 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3312 fw_state = abs_state & MFI_STATE_MASK;
c4a3e0a5 3313
e3bbff9f 3314 if (fw_state != MFI_STATE_READY)
0d49016b 3315 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
3316 " state\n");
e3bbff9f 3317
c4a3e0a5
BS
3318 while (fw_state != MFI_STATE_READY) {
3319
c4a3e0a5
BS
3320 switch (fw_state) {
3321
3322 case MFI_STATE_FAULT:
c4a3e0a5 3323 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
058a8fac 3324 if (ocr) {
3325 max_wait = MEGASAS_RESET_WAIT_TIME;
3326 cur_state = MFI_STATE_FAULT;
3327 break;
3328 } else
3329 return -ENODEV;
c4a3e0a5
BS
3330
3331 case MFI_STATE_WAIT_HANDSHAKE:
3332 /*
3333 * Set the CLR bit in inbound doorbell
3334 */
0c79e681 3335 if ((instance->pdev->device ==
87911122
YB
3336 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3337 (instance->pdev->device ==
9c915a8c 3338 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3339 (instance->pdev->device ==
21d3c710 3340 PCI_DEVICE_ID_LSI_FUSION) ||
36807e67 3341 (instance->pdev->device ==
229fe47c 3342 PCI_DEVICE_ID_LSI_PLASMA) ||
3343 (instance->pdev->device ==
21d3c710
SS
3344 PCI_DEVICE_ID_LSI_INVADER) ||
3345 (instance->pdev->device ==
3346 PCI_DEVICE_ID_LSI_FURY)) {
87911122
YB
3347 writel(
3348 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
9c915a8c 3349 &instance->reg_set->doorbell);
87911122
YB
3350 } else {
3351 writel(
3352 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3353 &instance->reg_set->inbound_doorbell);
3354 }
c4a3e0a5 3355
7218df69 3356 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3357 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3358 break;
3359
e3bbff9f 3360 case MFI_STATE_BOOT_MESSAGE_PENDING:
87911122 3361 if ((instance->pdev->device ==
9c915a8c 3362 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3363 (instance->pdev->device ==
3364 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3365 (instance->pdev->device ==
36807e67 3366 PCI_DEVICE_ID_LSI_FUSION) ||
229fe47c 3367 (instance->pdev->device ==
3368 PCI_DEVICE_ID_LSI_PLASMA) ||
36807e67 3369 (instance->pdev->device ==
21d3c710
SS
3370 PCI_DEVICE_ID_LSI_INVADER) ||
3371 (instance->pdev->device ==
3372 PCI_DEVICE_ID_LSI_FURY)) {
87911122 3373 writel(MFI_INIT_HOTPLUG,
9c915a8c 3374 &instance->reg_set->doorbell);
87911122
YB
3375 } else
3376 writel(MFI_INIT_HOTPLUG,
3377 &instance->reg_set->inbound_doorbell);
e3bbff9f 3378
7218df69 3379 max_wait = MEGASAS_RESET_WAIT_TIME;
e3bbff9f
SP
3380 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3381 break;
3382
c4a3e0a5
BS
3383 case MFI_STATE_OPERATIONAL:
3384 /*
e3bbff9f 3385 * Bring it to READY state; assuming max wait 10 secs
c4a3e0a5 3386 */
d46a3ad6 3387 instance->instancet->disable_intr(instance);
87911122
YB
3388 if ((instance->pdev->device ==
3389 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3390 (instance->pdev->device ==
9c915a8c 3391 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3392 (instance->pdev->device
36807e67 3393 == PCI_DEVICE_ID_LSI_FUSION) ||
229fe47c 3394 (instance->pdev->device
3395 == PCI_DEVICE_ID_LSI_PLASMA) ||
36807e67 3396 (instance->pdev->device
21d3c710
SS
3397 == PCI_DEVICE_ID_LSI_INVADER) ||
3398 (instance->pdev->device
3399 == PCI_DEVICE_ID_LSI_FURY)) {
87911122 3400 writel(MFI_RESET_FLAGS,
9c915a8c 3401 &instance->reg_set->doorbell);
36807e67 3402 if ((instance->pdev->device ==
21d3c710
SS
3403 PCI_DEVICE_ID_LSI_FUSION) ||
3404 (instance->pdev->device ==
229fe47c 3405 PCI_DEVICE_ID_LSI_PLASMA) ||
3406 (instance->pdev->device ==
21d3c710
SS
3407 PCI_DEVICE_ID_LSI_INVADER) ||
3408 (instance->pdev->device ==
3409 PCI_DEVICE_ID_LSI_FURY)) {
9c915a8c 3410 for (i = 0; i < (10 * 1000); i += 20) {
3411 if (readl(
3412 &instance->
3413 reg_set->
3414 doorbell) & 1)
3415 msleep(20);
3416 else
3417 break;
3418 }
3419 }
87911122
YB
3420 } else
3421 writel(MFI_RESET_FLAGS,
3422 &instance->reg_set->inbound_doorbell);
c4a3e0a5 3423
7218df69 3424 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3425 cur_state = MFI_STATE_OPERATIONAL;
3426 break;
3427
3428 case MFI_STATE_UNDEFINED:
3429 /*
3430 * This state should not last for more than 2 seconds
3431 */
7218df69 3432 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3433 cur_state = MFI_STATE_UNDEFINED;
3434 break;
3435
3436 case MFI_STATE_BB_INIT:
7218df69 3437 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3438 cur_state = MFI_STATE_BB_INIT;
3439 break;
3440
3441 case MFI_STATE_FW_INIT:
7218df69 3442 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3443 cur_state = MFI_STATE_FW_INIT;
3444 break;
3445
3446 case MFI_STATE_FW_INIT_2:
7218df69 3447 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3448 cur_state = MFI_STATE_FW_INIT_2;
3449 break;
3450
3451 case MFI_STATE_DEVICE_SCAN:
7218df69 3452 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3453 cur_state = MFI_STATE_DEVICE_SCAN;
3454 break;
3455
3456 case MFI_STATE_FLUSH_CACHE:
7218df69 3457 max_wait = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
3458 cur_state = MFI_STATE_FLUSH_CACHE;
3459 break;
3460
3461 default:
3462 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
3463 fw_state);
3464 return -ENODEV;
3465 }
3466
3467 /*
3468 * The cur_state should not last for more than max_wait secs
3469 */
3470 for (i = 0; i < (max_wait * 1000); i++) {
bc6ac5e8
TH
3471 curr_abs_state = instance->instancet->
3472 read_fw_status_reg(instance->reg_set);
c4a3e0a5 3473
7218df69 3474 if (abs_state == curr_abs_state) {
c4a3e0a5
BS
3475 msleep(1);
3476 } else
3477 break;
3478 }
3479
3480 /*
3481 * Return error if fw_state hasn't changed after max_wait
3482 */
7218df69 3483 if (curr_abs_state == abs_state) {
c4a3e0a5
BS
3484 printk(KERN_DEBUG "FW state [%d] hasn't changed "
3485 "in %d secs\n", fw_state, max_wait);
3486 return -ENODEV;
3487 }
bc6ac5e8
TH
3488
3489 abs_state = curr_abs_state;
3490 fw_state = curr_abs_state & MFI_STATE_MASK;
39a98554 3491 }
0d49016b 3492 printk(KERN_INFO "megasas: FW now in Ready state\n");
c4a3e0a5
BS
3493
3494 return 0;
3495}
3496
3497/**
3498 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3499 * @instance: Adapter soft state
3500 */
3501static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3502{
3503 int i;
9c915a8c 3504 u32 max_cmd = instance->max_mfi_cmds;
c4a3e0a5
BS
3505 struct megasas_cmd *cmd;
3506
3507 if (!instance->frame_dma_pool)
3508 return;
3509
3510 /*
3511 * Return all frames to pool
3512 */
3513 for (i = 0; i < max_cmd; i++) {
3514
3515 cmd = instance->cmd_list[i];
3516
3517 if (cmd->frame)
3518 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3519 cmd->frame_phys_addr);
3520
3521 if (cmd->sense)
e3bbff9f 3522 pci_pool_free(instance->sense_dma_pool, cmd->sense,
c4a3e0a5
BS
3523 cmd->sense_phys_addr);
3524 }
3525
3526 /*
3527 * Now destroy the pool itself
3528 */
3529 pci_pool_destroy(instance->frame_dma_pool);
3530 pci_pool_destroy(instance->sense_dma_pool);
3531
3532 instance->frame_dma_pool = NULL;
3533 instance->sense_dma_pool = NULL;
3534}
3535
3536/**
3537 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3538 * @instance: Adapter soft state
3539 *
3540 * Each command packet has an embedded DMA memory buffer that is used for
3541 * filling MFI frame and the SG list that immediately follows the frame. This
3542 * function creates those DMA memory buffers for each command packet by using
3543 * PCI pool facility.
3544 */
3545static int megasas_create_frame_pool(struct megasas_instance *instance)
3546{
3547 int i;
3548 u32 max_cmd;
3549 u32 sge_sz;
c4a3e0a5
BS
3550 u32 total_sz;
3551 u32 frame_count;
3552 struct megasas_cmd *cmd;
3553
9c915a8c 3554 max_cmd = instance->max_mfi_cmds;
c4a3e0a5
BS
3555
3556 /*
3557 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3558 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3559 */
3560 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3561 sizeof(struct megasas_sge32);
3562
f4c9a131
YB
3563 if (instance->flag_ieee) {
3564 sge_sz = sizeof(struct megasas_sge_skinny);
3565 }
3566
c4a3e0a5 3567 /*
200aed58
SS
3568 * For MFI controllers.
3569 * max_num_sge = 60
3570 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3571 * Total 960 byte (15 MFI frame of 64 byte)
3572 *
3573 * Fusion adapter require only 3 extra frame.
3574 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3575 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3576 * Total 192 byte (3 MFI frame of 64 byte)
c4a3e0a5 3577 */
200aed58 3578 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
c4a3e0a5
BS
3579 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3580 /*
3581 * Use DMA pool facility provided by PCI layer
3582 */
3583 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
200aed58 3584 instance->pdev, total_sz, 256, 0);
c4a3e0a5
BS
3585
3586 if (!instance->frame_dma_pool) {
3587 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
3588 return -ENOMEM;
3589 }
3590
3591 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3592 instance->pdev, 128, 4, 0);
3593
3594 if (!instance->sense_dma_pool) {
3595 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
3596
3597 pci_pool_destroy(instance->frame_dma_pool);
3598 instance->frame_dma_pool = NULL;
3599
3600 return -ENOMEM;
3601 }
3602
3603 /*
3604 * Allocate and attach a frame to each of the commands in cmd_list.
3605 * By making cmd->index as the context instead of the &cmd, we can
3606 * always use 32bit context regardless of the architecture
3607 */
3608 for (i = 0; i < max_cmd; i++) {
3609
3610 cmd = instance->cmd_list[i];
3611
3612 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3613 GFP_KERNEL, &cmd->frame_phys_addr);
3614
3615 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3616 GFP_KERNEL, &cmd->sense_phys_addr);
3617
3618 /*
3619 * megasas_teardown_frame_pool() takes care of freeing
3620 * whatever has been allocated
3621 */
3622 if (!cmd->frame || !cmd->sense) {
3623 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
3624 megasas_teardown_frame_pool(instance);
3625 return -ENOMEM;
3626 }
3627
707e09bd 3628 memset(cmd->frame, 0, total_sz);
94cd65dd 3629 cmd->frame->io.context = cpu_to_le32(cmd->index);
7e8a75f4 3630 cmd->frame->io.pad_0 = 0;
e5f93a36 3631 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
229fe47c 3632 (instance->pdev->device != PCI_DEVICE_ID_LSI_PLASMA) &&
e5f93a36 3633 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
21d3c710 3634 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
e5f93a36 3635 (reset_devices))
3636 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
c4a3e0a5
BS
3637 }
3638
3639 return 0;
3640}
3641
3642/**
3643 * megasas_free_cmds - Free all the cmds in the free cmd pool
3644 * @instance: Adapter soft state
3645 */
9c915a8c 3646void megasas_free_cmds(struct megasas_instance *instance)
c4a3e0a5
BS
3647{
3648 int i;
3649 /* First free the MFI frame pool */
3650 megasas_teardown_frame_pool(instance);
3651
3652 /* Free all the commands in the cmd_list */
9c915a8c 3653 for (i = 0; i < instance->max_mfi_cmds; i++)
3654
c4a3e0a5
BS
3655 kfree(instance->cmd_list[i]);
3656
3657 /* Free the cmd_list buffer itself */
3658 kfree(instance->cmd_list);
3659 instance->cmd_list = NULL;
3660
3661 INIT_LIST_HEAD(&instance->cmd_pool);
3662}
3663
3664/**
3665 * megasas_alloc_cmds - Allocates the command packets
3666 * @instance: Adapter soft state
3667 *
3668 * Each command that is issued to the FW, whether IO commands from the OS or
3669 * internal commands like IOCTLs, are wrapped in local data structure called
3670 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3671 * the FW.
3672 *
3673 * Each frame has a 32-bit field called context (tag). This context is used
3674 * to get back the megasas_cmd from the frame when a frame gets completed in
3675 * the ISR. Typically the address of the megasas_cmd itself would be used as
3676 * the context. But we wanted to keep the differences between 32 and 64 bit
3677 * systems to the mininum. We always use 32 bit integers for the context. In
3678 * this driver, the 32 bit values are the indices into an array cmd_list.
3679 * This array is used only to look up the megasas_cmd given the context. The
3680 * free commands themselves are maintained in a linked list called cmd_pool.
3681 */
9c915a8c 3682int megasas_alloc_cmds(struct megasas_instance *instance)
c4a3e0a5
BS
3683{
3684 int i;
3685 int j;
3686 u32 max_cmd;
3687 struct megasas_cmd *cmd;
90dc9d98 3688 struct fusion_context *fusion;
c4a3e0a5 3689
90dc9d98 3690 fusion = instance->ctrl_context;
9c915a8c 3691 max_cmd = instance->max_mfi_cmds;
c4a3e0a5
BS
3692
3693 /*
3694 * instance->cmd_list is an array of struct megasas_cmd pointers.
3695 * Allocate the dynamic array first and then allocate individual
3696 * commands.
3697 */
dd00cc48 3698 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
c4a3e0a5
BS
3699
3700 if (!instance->cmd_list) {
3701 printk(KERN_DEBUG "megasas: out of memory\n");
3702 return -ENOMEM;
3703 }
3704
9c915a8c 3705 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
c4a3e0a5
BS
3706
3707 for (i = 0; i < max_cmd; i++) {
3708 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3709 GFP_KERNEL);
3710
3711 if (!instance->cmd_list[i]) {
3712
3713 for (j = 0; j < i; j++)
3714 kfree(instance->cmd_list[j]);
3715
3716 kfree(instance->cmd_list);
3717 instance->cmd_list = NULL;
3718
3719 return -ENOMEM;
3720 }
3721 }
3722
c4a3e0a5
BS
3723 for (i = 0; i < max_cmd; i++) {
3724 cmd = instance->cmd_list[i];
3725 memset(cmd, 0, sizeof(struct megasas_cmd));
3726 cmd->index = i;
90dc9d98 3727 atomic_set(&cmd->mfi_mpt_pthr, MFI_LIST_ADDED);
39a98554 3728 cmd->scmd = NULL;
c4a3e0a5
BS
3729 cmd->instance = instance;
3730
3731 list_add_tail(&cmd->list, &instance->cmd_pool);
3732 }
3733
3734 /*
3735 * Create a frame pool and assign one frame to each cmd
3736 */
3737 if (megasas_create_frame_pool(instance)) {
3738 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3739 megasas_free_cmds(instance);
3740 }
3741
3742 return 0;
3743}
3744
81e403ce
YB
3745/*
3746 * megasas_get_pd_list_info - Returns FW's pd_list structure
3747 * @instance: Adapter soft state
3748 * @pd_list: pd_list structure
3749 *
3750 * Issues an internal command (DCMD) to get the FW's controller PD
3751 * list structure. This information is mainly used to find out SYSTEM
3752 * supported by the FW.
3753 */
3754static int
3755megasas_get_pd_list(struct megasas_instance *instance)
3756{
3757 int ret = 0, pd_index = 0;
3758 struct megasas_cmd *cmd;
3759 struct megasas_dcmd_frame *dcmd;
3760 struct MR_PD_LIST *ci;
3761 struct MR_PD_ADDRESS *pd_addr;
3762 dma_addr_t ci_h = 0;
3763
3764 cmd = megasas_get_cmd(instance);
3765
3766 if (!cmd) {
3767 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3768 return -ENOMEM;
3769 }
3770
3771 dcmd = &cmd->frame->dcmd;
3772
3773 ci = pci_alloc_consistent(instance->pdev,
3774 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3775
3776 if (!ci) {
3777 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3778 megasas_return_cmd(instance, cmd);
3779 return -ENOMEM;
3780 }
3781
3782 memset(ci, 0, sizeof(*ci));
3783 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3784
3785 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3786 dcmd->mbox.b[1] = 0;
3787 dcmd->cmd = MFI_CMD_DCMD;
3788 dcmd->cmd_status = 0xFF;
3789 dcmd->sge_count = 1;
94cd65dd 3790 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
81e403ce 3791 dcmd->timeout = 0;
780a3762 3792 dcmd->pad_0 = 0;
94cd65dd
SS
3793 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3794 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3795 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3796 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
81e403ce 3797
90dc9d98
SS
3798 if (instance->ctrl_context && !instance->mask_interrupts)
3799 ret = megasas_issue_blocked_cmd(instance, cmd,
3800 MEGASAS_BLOCKED_CMD_TIMEOUT);
3801 else
3802 ret = megasas_issue_polled(instance, cmd);
81e403ce
YB
3803
3804 /*
3805 * the following function will get the instance PD LIST.
3806 */
3807
3808 pd_addr = ci->addr;
3809
3810 if ( ret == 0 &&
94cd65dd 3811 (le32_to_cpu(ci->count) <
81e403ce
YB
3812 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3813
999ece0a 3814 memset(instance->local_pd_list, 0,
81e403ce
YB
3815 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3816
94cd65dd 3817 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
81e403ce 3818
999ece0a 3819 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
94cd65dd 3820 le16_to_cpu(pd_addr->deviceId);
999ece0a 3821 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
81e403ce 3822 pd_addr->scsiDevType;
999ece0a 3823 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
81e403ce
YB
3824 MR_PD_STATE_SYSTEM;
3825 pd_addr++;
3826 }
999ece0a
SS
3827 memcpy(instance->pd_list, instance->local_pd_list,
3828 sizeof(instance->pd_list));
81e403ce
YB
3829 }
3830
3831 pci_free_consistent(instance->pdev,
3832 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3833 ci, ci_h);
90dc9d98
SS
3834
3835 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3836 megasas_return_mfi_mpt_pthr(instance, cmd,
3837 cmd->mpt_pthr_cmd_blocked);
3838 else
3839 megasas_return_cmd(instance, cmd);
81e403ce
YB
3840
3841 return ret;
3842}
3843
bdc6fb8d
YB
3844/*
3845 * megasas_get_ld_list_info - Returns FW's ld_list structure
3846 * @instance: Adapter soft state
3847 * @ld_list: ld_list structure
3848 *
3849 * Issues an internal command (DCMD) to get the FW's controller PD
3850 * list structure. This information is mainly used to find out SYSTEM
3851 * supported by the FW.
3852 */
3853static int
3854megasas_get_ld_list(struct megasas_instance *instance)
3855{
3856 int ret = 0, ld_index = 0, ids = 0;
3857 struct megasas_cmd *cmd;
3858 struct megasas_dcmd_frame *dcmd;
3859 struct MR_LD_LIST *ci;
3860 dma_addr_t ci_h = 0;
94cd65dd 3861 u32 ld_count;
bdc6fb8d
YB
3862
3863 cmd = megasas_get_cmd(instance);
3864
3865 if (!cmd) {
3866 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3867 return -ENOMEM;
3868 }
3869
3870 dcmd = &cmd->frame->dcmd;
3871
3872 ci = pci_alloc_consistent(instance->pdev,
3873 sizeof(struct MR_LD_LIST),
3874 &ci_h);
3875
3876 if (!ci) {
3877 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3878 megasas_return_cmd(instance, cmd);
3879 return -ENOMEM;
3880 }
3881
3882 memset(ci, 0, sizeof(*ci));
3883 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3884
51087a86
SS
3885 if (instance->supportmax256vd)
3886 dcmd->mbox.b[0] = 1;
bdc6fb8d
YB
3887 dcmd->cmd = MFI_CMD_DCMD;
3888 dcmd->cmd_status = 0xFF;
3889 dcmd->sge_count = 1;
94cd65dd 3890 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
bdc6fb8d 3891 dcmd->timeout = 0;
94cd65dd
SS
3892 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3893 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3894 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3895 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
bdc6fb8d
YB
3896 dcmd->pad_0 = 0;
3897
90dc9d98
SS
3898 if (instance->ctrl_context && !instance->mask_interrupts)
3899 ret = megasas_issue_blocked_cmd(instance, cmd,
3900 MEGASAS_BLOCKED_CMD_TIMEOUT);
3901 else
3902 ret = megasas_issue_polled(instance, cmd);
3903
bdc6fb8d 3904
94cd65dd
SS
3905 ld_count = le32_to_cpu(ci->ldCount);
3906
bdc6fb8d
YB
3907 /* the following function will get the instance PD LIST */
3908
51087a86
SS
3909 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3910 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
bdc6fb8d 3911
94cd65dd 3912 for (ld_index = 0; ld_index < ld_count; ld_index++) {
bdc6fb8d
YB
3913 if (ci->ldList[ld_index].state != 0) {
3914 ids = ci->ldList[ld_index].ref.targetId;
3915 instance->ld_ids[ids] =
3916 ci->ldList[ld_index].ref.targetId;
3917 }
3918 }
3919 }
3920
3921 pci_free_consistent(instance->pdev,
3922 sizeof(struct MR_LD_LIST),
3923 ci,
3924 ci_h);
3925
90dc9d98
SS
3926 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3927 megasas_return_mfi_mpt_pthr(instance, cmd,
3928 cmd->mpt_pthr_cmd_blocked);
3929 else
3930 megasas_return_cmd(instance, cmd);
bdc6fb8d
YB
3931 return ret;
3932}
3933
21c9e160 3934/**
3935 * megasas_ld_list_query - Returns FW's ld_list structure
3936 * @instance: Adapter soft state
3937 * @ld_list: ld_list structure
3938 *
3939 * Issues an internal command (DCMD) to get the FW's controller PD
3940 * list structure. This information is mainly used to find out SYSTEM
3941 * supported by the FW.
3942 */
3943static int
3944megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3945{
3946 int ret = 0, ld_index = 0, ids = 0;
3947 struct megasas_cmd *cmd;
3948 struct megasas_dcmd_frame *dcmd;
3949 struct MR_LD_TARGETID_LIST *ci;
3950 dma_addr_t ci_h = 0;
94cd65dd 3951 u32 tgtid_count;
21c9e160 3952
3953 cmd = megasas_get_cmd(instance);
3954
3955 if (!cmd) {
3956 printk(KERN_WARNING
3957 "megasas:(megasas_ld_list_query): Failed to get cmd\n");
3958 return -ENOMEM;
3959 }
3960
3961 dcmd = &cmd->frame->dcmd;
3962
3963 ci = pci_alloc_consistent(instance->pdev,
3964 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
3965
3966 if (!ci) {
3967 printk(KERN_WARNING
3968 "megasas: Failed to alloc mem for ld_list_query\n");
3969 megasas_return_cmd(instance, cmd);
3970 return -ENOMEM;
3971 }
3972
3973 memset(ci, 0, sizeof(*ci));
3974 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3975
3976 dcmd->mbox.b[0] = query_type;
51087a86
SS
3977 if (instance->supportmax256vd)
3978 dcmd->mbox.b[2] = 1;
21c9e160 3979
3980 dcmd->cmd = MFI_CMD_DCMD;
3981 dcmd->cmd_status = 0xFF;
3982 dcmd->sge_count = 1;
94cd65dd 3983 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
21c9e160 3984 dcmd->timeout = 0;
94cd65dd
SS
3985 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3986 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
3987 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3988 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
21c9e160 3989 dcmd->pad_0 = 0;
3990
90dc9d98
SS
3991 if (instance->ctrl_context && !instance->mask_interrupts)
3992 ret = megasas_issue_blocked_cmd(instance, cmd,
3993 MEGASAS_BLOCKED_CMD_TIMEOUT);
3994 else
3995 ret = megasas_issue_polled(instance, cmd);
21c9e160 3996
94cd65dd
SS
3997 tgtid_count = le32_to_cpu(ci->count);
3998
51087a86 3999 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
21c9e160 4000 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
94cd65dd 4001 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
21c9e160 4002 ids = ci->targetId[ld_index];
4003 instance->ld_ids[ids] = ci->targetId[ld_index];
4004 }
4005
4006 }
4007
4008 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4009 ci, ci_h);
4010
90dc9d98
SS
4011 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4012 megasas_return_mfi_mpt_pthr(instance, cmd,
4013 cmd->mpt_pthr_cmd_blocked);
4014 else
4015 megasas_return_cmd(instance, cmd);
21c9e160 4016
4017 return ret;
4018}
4019
d009b576
SS
4020/*
4021 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4022 * instance : Controller's instance
4023*/
4024static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4025{
4026 struct fusion_context *fusion;
4027 u32 old_map_sz;
4028 u32 new_map_sz;
4029
4030 fusion = instance->ctrl_context;
4031 /* For MFI based controllers return dummy success */
4032 if (!fusion)
4033 return;
4034
4035 instance->supportmax256vd =
4036 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4037 /* Below is additional check to address future FW enhancement */
4038 if (instance->ctrl_info->max_lds > 64)
4039 instance->supportmax256vd = 1;
4040
4041 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4042 * MEGASAS_MAX_DEV_PER_CHANNEL;
4043 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4044 * MEGASAS_MAX_DEV_PER_CHANNEL;
4045 if (instance->supportmax256vd) {
4046 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4047 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4048 } else {
4049 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4050 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4051 }
4052 dev_info(&instance->pdev->dev, "Firmware supports %d VD %d PD\n",
4053 instance->fw_supported_vd_count,
4054 instance->fw_supported_pd_count);
4055 dev_info(&instance->pdev->dev, "Driver supports %d VD %d PD\n",
4056 instance->drv_supported_vd_count,
4057 instance->drv_supported_pd_count);
4058
4059 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4060 (sizeof(struct MR_LD_SPAN_MAP) *
4061 (instance->fw_supported_vd_count - 1));
4062 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4063 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4064 (sizeof(struct MR_LD_SPAN_MAP) *
4065 (instance->drv_supported_vd_count - 1));
4066
4067 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4068
4069
4070 if (instance->supportmax256vd)
4071 fusion->current_map_sz = new_map_sz;
4072 else
4073 fusion->current_map_sz = old_map_sz;
4074
4075}
4076
c4a3e0a5
BS
4077/**
4078 * megasas_get_controller_info - Returns FW's controller structure
4079 * @instance: Adapter soft state
c4a3e0a5
BS
4080 *
4081 * Issues an internal command (DCMD) to get the FW's controller structure.
4082 * This information is mainly used to find out the maximum IO transfer per
4083 * command supported by the FW.
4084 */
51087a86 4085int
d009b576 4086megasas_get_ctrl_info(struct megasas_instance *instance)
c4a3e0a5
BS
4087{
4088 int ret = 0;
4089 struct megasas_cmd *cmd;
4090 struct megasas_dcmd_frame *dcmd;
4091 struct megasas_ctrl_info *ci;
d009b576 4092 struct megasas_ctrl_info *ctrl_info;
c4a3e0a5
BS
4093 dma_addr_t ci_h = 0;
4094
d009b576
SS
4095 ctrl_info = instance->ctrl_info;
4096
c4a3e0a5
BS
4097 cmd = megasas_get_cmd(instance);
4098
4099 if (!cmd) {
4100 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
4101 return -ENOMEM;
4102 }
4103
4104 dcmd = &cmd->frame->dcmd;
4105
4106 ci = pci_alloc_consistent(instance->pdev,
4107 sizeof(struct megasas_ctrl_info), &ci_h);
4108
4109 if (!ci) {
4110 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
4111 megasas_return_cmd(instance, cmd);
4112 return -ENOMEM;
4113 }
4114
4115 memset(ci, 0, sizeof(*ci));
4116 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4117
4118 dcmd->cmd = MFI_CMD_DCMD;
4119 dcmd->cmd_status = 0xFF;
4120 dcmd->sge_count = 1;
94cd65dd 4121 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
c4a3e0a5 4122 dcmd->timeout = 0;
780a3762 4123 dcmd->pad_0 = 0;
94cd65dd
SS
4124 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4125 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4126 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4127 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
51087a86 4128 dcmd->mbox.b[0] = 1;
c4a3e0a5 4129
90dc9d98
SS
4130 if (instance->ctrl_context && !instance->mask_interrupts)
4131 ret = megasas_issue_blocked_cmd(instance, cmd,
4132 MEGASAS_BLOCKED_CMD_TIMEOUT);
4133 else
4134 ret = megasas_issue_polled(instance, cmd);
4135
d009b576 4136 if (!ret) {
c4a3e0a5 4137 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
d009b576
SS
4138 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4139 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4140 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4141 megasas_update_ext_vd_details(instance);
4142 }
c4a3e0a5
BS
4143
4144 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4145 ci, ci_h);
4146
90dc9d98
SS
4147 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4148 megasas_return_mfi_mpt_pthr(instance, cmd,
4149 cmd->mpt_pthr_cmd_blocked);
4150 else
4151 megasas_return_cmd(instance, cmd);
c4a3e0a5
BS
4152 return ret;
4153}
4154
fc62b3fc
SS
4155/*
4156 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4157 * to firmware
4158 *
4159 * @instance: Adapter soft state
4160 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4161 MR_CRASH_BUF_TURN_OFF = 0
4162 MR_CRASH_BUF_TURN_ON = 1
4163 * @return 0 on success non-zero on failure.
4164 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4165 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4166 * that driver supports crash dump feature. This DCMD will be sent only if
4167 * crash dump feature is supported by the FW.
4168 *
4169 */
4170int megasas_set_crash_dump_params(struct megasas_instance *instance,
4171 u8 crash_buf_state)
4172{
4173 int ret = 0;
4174 struct megasas_cmd *cmd;
4175 struct megasas_dcmd_frame *dcmd;
4176
4177 cmd = megasas_get_cmd(instance);
4178
4179 if (!cmd) {
4180 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4181 return -ENOMEM;
4182 }
4183
4184
4185 dcmd = &cmd->frame->dcmd;
4186
4187 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4188 dcmd->mbox.b[0] = crash_buf_state;
4189 dcmd->cmd = MFI_CMD_DCMD;
4190 dcmd->cmd_status = 0xFF;
4191 dcmd->sge_count = 1;
4192 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4193 dcmd->timeout = 0;
4194 dcmd->pad_0 = 0;
4195 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4196 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4197 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4198 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4199
90dc9d98
SS
4200 if (instance->ctrl_context && !instance->mask_interrupts)
4201 ret = megasas_issue_blocked_cmd(instance, cmd,
4202 MEGASAS_BLOCKED_CMD_TIMEOUT);
fc62b3fc 4203 else
90dc9d98
SS
4204 ret = megasas_issue_polled(instance, cmd);
4205
4206 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4207 megasas_return_mfi_mpt_pthr(instance, cmd,
4208 cmd->mpt_pthr_cmd_blocked);
4209 else
4210 megasas_return_cmd(instance, cmd);
fc62b3fc
SS
4211 return ret;
4212}
4213
31ea7088 4214/**
4215 * megasas_issue_init_mfi - Initializes the FW
4216 * @instance: Adapter soft state
4217 *
4218 * Issues the INIT MFI cmd
4219 */
4220static int
4221megasas_issue_init_mfi(struct megasas_instance *instance)
4222{
4223 u32 context;
4224
4225 struct megasas_cmd *cmd;
4226
4227 struct megasas_init_frame *init_frame;
4228 struct megasas_init_queue_info *initq_info;
4229 dma_addr_t init_frame_h;
4230 dma_addr_t initq_info_h;
4231
4232 /*
4233 * Prepare a init frame. Note the init frame points to queue info
4234 * structure. Each frame has SGL allocated after first 64 bytes. For
4235 * this frame - since we don't need any SGL - we use SGL's space as
4236 * queue info structure
4237 *
4238 * We will not get a NULL command below. We just created the pool.
4239 */
4240 cmd = megasas_get_cmd(instance);
4241
4242 init_frame = (struct megasas_init_frame *)cmd->frame;
4243 initq_info = (struct megasas_init_queue_info *)
4244 ((unsigned long)init_frame + 64);
4245
4246 init_frame_h = cmd->frame_phys_addr;
4247 initq_info_h = init_frame_h + 64;
4248
4249 context = init_frame->context;
4250 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4251 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4252 init_frame->context = context;
4253
94cd65dd
SS
4254 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4255 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
31ea7088 4256
94cd65dd
SS
4257 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4258 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
31ea7088 4259
4260 init_frame->cmd = MFI_CMD_INIT;
4261 init_frame->cmd_status = 0xFF;
94cd65dd
SS
4262 init_frame->queue_info_new_phys_addr_lo =
4263 cpu_to_le32(lower_32_bits(initq_info_h));
4264 init_frame->queue_info_new_phys_addr_hi =
4265 cpu_to_le32(upper_32_bits(initq_info_h));
31ea7088 4266
94cd65dd 4267 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
31ea7088 4268
4269 /*
4270 * disable the intr before firing the init frame to FW
4271 */
d46a3ad6 4272 instance->instancet->disable_intr(instance);
31ea7088 4273
4274 /*
4275 * Issue the init frame in polled mode
4276 */
4277
4278 if (megasas_issue_polled(instance, cmd)) {
4279 printk(KERN_ERR "megasas: Failed to init firmware\n");
4280 megasas_return_cmd(instance, cmd);
4281 goto fail_fw_init;
4282 }
4283
4284 megasas_return_cmd(instance, cmd);
4285
4286 return 0;
4287
4288fail_fw_init:
4289 return -EINVAL;
4290}
4291
cd50ba8e 4292static u32
4293megasas_init_adapter_mfi(struct megasas_instance *instance)
c4a3e0a5 4294{
cd50ba8e 4295 struct megasas_register_set __iomem *reg_set;
c4a3e0a5
BS
4296 u32 context_sz;
4297 u32 reply_q_sz;
c4a3e0a5
BS
4298
4299 reg_set = instance->reg_set;
4300
c4a3e0a5
BS
4301 /*
4302 * Get various operational parameters from status register
4303 */
1341c939 4304 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
e3bbff9f
SP
4305 /*
4306 * Reduce the max supported cmds by 1. This is to ensure that the
4307 * reply_q_sz (1 more than the max cmd that driver may send)
4308 * does not exceed max cmds that the FW can support
4309 */
4310 instance->max_fw_cmds = instance->max_fw_cmds-1;
9c915a8c 4311 instance->max_mfi_cmds = instance->max_fw_cmds;
0d49016b 4312 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1341c939 4313 0x10;
c4a3e0a5
BS
4314 /*
4315 * Create a pool of commands
4316 */
4317 if (megasas_alloc_cmds(instance))
4318 goto fail_alloc_cmds;
4319
4320 /*
4321 * Allocate memory for reply queue. Length of reply queue should
4322 * be _one_ more than the maximum commands handled by the firmware.
4323 *
4324 * Note: When FW completes commands, it places corresponding contex
4325 * values in this circular reply queue. This circular queue is a fairly
4326 * typical producer-consumer queue. FW is the producer (of completed
4327 * commands) and the driver is the consumer.
4328 */
4329 context_sz = sizeof(u32);
4330 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4331
4332 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4333 reply_q_sz,
4334 &instance->reply_queue_h);
4335
4336 if (!instance->reply_queue) {
4337 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
4338 goto fail_reply_queue;
4339 }
4340
31ea7088 4341 if (megasas_issue_init_mfi(instance))
c4a3e0a5 4342 goto fail_fw_init;
c4a3e0a5 4343
d009b576 4344 if (megasas_get_ctrl_info(instance)) {
51087a86
SS
4345 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4346 "Fail from %s %d\n", instance->unique_id,
4347 __func__, __LINE__);
4348 goto fail_fw_init;
4349 }
4350
39a98554 4351 instance->fw_support_ieee = 0;
4352 instance->fw_support_ieee =
4353 (instance->instancet->read_fw_status_reg(reg_set) &
4354 0x04000000);
4355
4356 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
4357 instance->fw_support_ieee);
4358
4359 if (instance->fw_support_ieee)
4360 instance->flag_ieee = 1;
4361
cd50ba8e 4362 return 0;
4363
4364fail_fw_init:
4365
4366 pci_free_consistent(instance->pdev, reply_q_sz,
4367 instance->reply_queue, instance->reply_queue_h);
4368fail_reply_queue:
4369 megasas_free_cmds(instance);
4370
4371fail_alloc_cmds:
cd50ba8e 4372 return 1;
4373}
4374
4375/**
4376 * megasas_init_fw - Initializes the FW
4377 * @instance: Adapter soft state
4378 *
4379 * This is the main function for initializing firmware
4380 */
4381
4382static int megasas_init_fw(struct megasas_instance *instance)
4383{
4384 u32 max_sectors_1;
4385 u32 max_sectors_2;
d46a3ad6 4386 u32 tmp_sectors, msix_enable, scratch_pad_2;
11f8a7b3 4387 resource_size_t base_addr;
cd50ba8e 4388 struct megasas_register_set __iomem *reg_set;
51087a86 4389 struct megasas_ctrl_info *ctrl_info = NULL;
cd50ba8e 4390 unsigned long bar_list;
d46a3ad6 4391 int i, loop, fw_msix_count = 0;
229fe47c 4392 struct IOV_111 *iovPtr;
cd50ba8e 4393
4394 /* Find first memory bar */
4395 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4396 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
cd50ba8e 4397 if (pci_request_selected_regions(instance->pdev, instance->bar,
4398 "megasas: LSI")) {
4399 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
4400 return -EBUSY;
4401 }
4402
11f8a7b3
BC
4403 base_addr = pci_resource_start(instance->pdev, instance->bar);
4404 instance->reg_set = ioremap_nocache(base_addr, 8192);
cd50ba8e 4405
4406 if (!instance->reg_set) {
4407 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
4408 goto fail_ioremap;
4409 }
4410
4411 reg_set = instance->reg_set;
4412
4413 switch (instance->pdev->device) {
9c915a8c 4414 case PCI_DEVICE_ID_LSI_FUSION:
229fe47c 4415 case PCI_DEVICE_ID_LSI_PLASMA:
36807e67 4416 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 4417 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 4418 instance->instancet = &megasas_instance_template_fusion;
4419 break;
cd50ba8e 4420 case PCI_DEVICE_ID_LSI_SAS1078R:
4421 case PCI_DEVICE_ID_LSI_SAS1078DE:
4422 instance->instancet = &megasas_instance_template_ppc;
4423 break;
4424 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4425 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4426 instance->instancet = &megasas_instance_template_gen2;
4427 break;
4428 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4429 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4430 instance->instancet = &megasas_instance_template_skinny;
4431 break;
4432 case PCI_DEVICE_ID_LSI_SAS1064R:
4433 case PCI_DEVICE_ID_DELL_PERC5:
4434 default:
4435 instance->instancet = &megasas_instance_template_xscale;
4436 break;
4437 }
4438
6431f5d7
SS
4439 if (megasas_transition_to_ready(instance, 0)) {
4440 atomic_set(&instance->fw_reset_no_pci_access, 1);
4441 instance->instancet->adp_reset
4442 (instance, instance->reg_set);
4443 atomic_set(&instance->fw_reset_no_pci_access, 0);
4444 dev_info(&instance->pdev->dev,
4445 "megasas: FW restarted successfully from %s!\n",
4446 __func__);
4447
4448 /*waitting for about 30 second before retry*/
4449 ssleep(30);
4450
4451 if (megasas_transition_to_ready(instance, 0))
4452 goto fail_ready_state;
4453 }
cd50ba8e 4454
d46a3ad6
SS
4455 /*
4456 * MSI-X host index 0 is common for all adapter.
4457 * It is used for all MPT based Adapters.
4458 */
4459 instance->reply_post_host_index_addr[0] =
4460 (u32 *)((u8 *)instance->reg_set +
4461 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4462
3f1abce4 4463 /* Check if MSI-X is supported while in ready state */
4464 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4465 0x4000000) >> 0x1a;
c8e858fe 4466 if (msix_enable && !msix_disable) {
d46a3ad6
SS
4467 scratch_pad_2 = readl
4468 (&instance->reg_set->outbound_scratch_pad_2);
c8e858fe 4469 /* Check max MSI-X vectors */
229fe47c 4470 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4471 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA)) {
d46a3ad6
SS
4472 instance->msix_vectors = (scratch_pad_2
4473 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4474 fw_msix_count = instance->msix_vectors;
079eaddf 4475 if (msix_vectors)
4476 instance->msix_vectors =
4477 min(msix_vectors,
4478 instance->msix_vectors);
d46a3ad6
SS
4479 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
4480 || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
4481 /* Invader/Fury supports more than 8 MSI-X */
4482 instance->msix_vectors = ((scratch_pad_2
4483 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4484 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4485 fw_msix_count = instance->msix_vectors;
4486 /* Save 1-15 reply post index address to local memory
4487 * Index 0 is already saved from reg offset
4488 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4489 */
4490 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4491 instance->reply_post_host_index_addr[loop] =
4492 (u32 *)((u8 *)instance->reg_set +
4493 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4494 + (loop * 0x10));
4495 }
4496 if (msix_vectors)
4497 instance->msix_vectors = min(msix_vectors,
4498 instance->msix_vectors);
c8e858fe 4499 } else
4500 instance->msix_vectors = 1;
4501 /* Don't bother allocating more MSI-X vectors than cpus */
4502 instance->msix_vectors = min(instance->msix_vectors,
4503 (unsigned int)num_online_cpus());
4504 for (i = 0; i < instance->msix_vectors; i++)
4505 instance->msixentry[i].entry = i;
8ae80ed1
AG
4506 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4507 1, instance->msix_vectors);
c12de882 4508 if (i > 0)
8ae80ed1
AG
4509 instance->msix_vectors = i;
4510 else
c8e858fe 4511 instance->msix_vectors = 0;
d46a3ad6
SS
4512
4513 dev_info(&instance->pdev->dev, "[scsi%d]: FW supports"
4514 "<%d> MSIX vector,Online CPUs: <%d>,"
4515 "Current MSIX <%d>\n", instance->host->host_no,
4516 fw_msix_count, (unsigned int)num_online_cpus(),
4517 instance->msix_vectors);
c8e858fe 4518 }
3f1abce4 4519
51087a86
SS
4520 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4521 GFP_KERNEL);
4522 if (instance->ctrl_info == NULL)
4523 goto fail_init_adapter;
4524
4525 /*
4526 * Below are default value for legacy Firmware.
4527 * non-fusion based controllers
4528 */
4529 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4530 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
cd50ba8e 4531 /* Get operational params, sge flags, send init cmd to controller */
4532 if (instance->instancet->init_adapter(instance))
eb1b1237 4533 goto fail_init_adapter;
cd50ba8e 4534
4535 printk(KERN_ERR "megasas: INIT adapter done\n");
4536
39a98554 4537 /** for passthrough
4538 * the following function will get the PD LIST.
4539 */
4540
81e403ce
YB
4541 memset(instance->pd_list, 0 ,
4542 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
58968fc8
HR
4543 if (megasas_get_pd_list(instance) < 0) {
4544 printk(KERN_ERR "megasas: failed to get PD list\n");
4545 goto fail_init_adapter;
4546 }
81e403ce 4547
bdc6fb8d 4548 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
21c9e160 4549 if (megasas_ld_list_query(instance,
4550 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4551 megasas_get_ld_list(instance);
bdc6fb8d 4552
c4a3e0a5
BS
4553 /*
4554 * Compute the max allowed sectors per IO: The controller info has two
4555 * limits on max sectors. Driver should use the minimum of these two.
4556 *
4557 * 1 << stripe_sz_ops.min = max sectors per strip
4558 *
4559 * Note that older firmwares ( < FW ver 30) didn't report information
4560 * to calculate max_sectors_1. So the number ended up as zero always.
4561 */
14faea9f 4562 tmp_sectors = 0;
51087a86 4563 ctrl_info = instance->ctrl_info;
c4a3e0a5 4564
51087a86
SS
4565 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4566 le16_to_cpu(ctrl_info->max_strips_per_io);
4567 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
404a8a1a 4568
51087a86 4569 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
bc93d425 4570
51087a86
SS
4571 /*Check whether controller is iMR or MR */
4572 if (ctrl_info->memory_size) {
4573 instance->is_imr = 0;
4574 dev_info(&instance->pdev->dev, "Controller type: MR,"
4575 "Memory size is: %dMB\n",
4576 le16_to_cpu(ctrl_info->memory_size));
4577 } else {
4578 instance->is_imr = 1;
4579 dev_info(&instance->pdev->dev,
4580 "Controller type: iMR\n");
4581 }
51087a86
SS
4582 instance->disableOnlineCtrlReset =
4583 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
51087a86
SS
4584 instance->mpio = ctrl_info->adapterOperations2.mpio;
4585 instance->UnevenSpanSupport =
4586 ctrl_info->adapterOperations2.supportUnevenSpans;
4587 if (instance->UnevenSpanSupport) {
4588 struct fusion_context *fusion = instance->ctrl_context;
4589
4590 dev_info(&instance->pdev->dev, "FW supports: "
4591 "UnevenSpanSupport=%x\n", instance->UnevenSpanSupport);
4592 if (MR_ValidateMapInfo(instance))
4593 fusion->fast_path_io = 1;
4594 else
4595 fusion->fast_path_io = 0;
fc62b3fc 4596
51087a86
SS
4597 }
4598 if (ctrl_info->host_interface.SRIOV) {
4599 if (!ctrl_info->adapterOperations2.activePassive)
4600 instance->PlasmaFW111 = 1;
fc62b3fc 4601
51087a86
SS
4602 if (!instance->PlasmaFW111)
4603 instance->requestorId =
4604 ctrl_info->iov.requestorId;
4605 else {
4606 iovPtr = (struct IOV_111 *)((unsigned char *)ctrl_info + IOV_111_OFFSET);
4607 instance->requestorId = iovPtr->requestorId;
fc62b3fc 4608 }
51087a86
SS
4609 dev_warn(&instance->pdev->dev, "I am VF "
4610 "requestorId %d\n", instance->requestorId);
4611 }
4612
51087a86
SS
4613 instance->crash_dump_fw_support =
4614 ctrl_info->adapterOperations3.supportCrashDump;
4615 instance->crash_dump_drv_support =
4616 (instance->crash_dump_fw_support &&
4617 instance->crash_dump_buf);
4618 if (instance->crash_dump_drv_support) {
4619 dev_info(&instance->pdev->dev, "Firmware Crash dump "
4620 "feature is supported\n");
4621 megasas_set_crash_dump_params(instance,
4622 MR_CRASH_BUF_TURN_OFF);
4623
4624 } else {
4625 if (instance->crash_dump_buf)
4626 pci_free_consistent(instance->pdev,
4627 CRASH_DMA_BUF_SIZE,
4628 instance->crash_dump_buf,
4629 instance->crash_dump_h);
4630 instance->crash_dump_buf = NULL;
14faea9f 4631 }
14faea9f 4632 instance->max_sectors_per_req = instance->max_num_sge *
4633 PAGE_SIZE / 512;
4634 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4635 instance->max_sectors_per_req = tmp_sectors;
c4a3e0a5 4636
c5daa6a9 4637 /* Check for valid throttlequeuedepth module parameter */
404a8a1a 4638 if (instance->is_imr) {
c5daa6a9 4639 if (throttlequeuedepth > (instance->max_fw_cmds -
4640 MEGASAS_SKINNY_INT_CMDS))
4641 instance->throttlequeuedepth =
4642 MEGASAS_THROTTLE_QUEUE_DEPTH;
4643 else
4644 instance->throttlequeuedepth = throttlequeuedepth;
4645 } else {
4646 if (throttlequeuedepth > (instance->max_fw_cmds -
4647 MEGASAS_INT_CMDS))
4648 instance->throttlequeuedepth =
4649 MEGASAS_THROTTLE_QUEUE_DEPTH;
4650 else
4651 instance->throttlequeuedepth = throttlequeuedepth;
4652 }
4653
5d018ad0
SP
4654 /*
4655 * Setup tasklet for cmd completion
4656 */
4657
f86c5424 4658 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
ad84db2e 4659 (unsigned long)instance);
4660
229fe47c 4661 /* Launch SR-IOV heartbeat timer */
4662 if (instance->requestorId) {
4663 if (!megasas_sriov_start_heartbeat(instance, 1))
4664 megasas_start_timer(instance,
4665 &instance->sriov_heartbeat_timer,
4666 megasas_sriov_heartbeat_handler,
4667 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4668 else
4669 instance->skip_heartbeat_timer_del = 1;
4670 }
4671
c4a3e0a5
BS
4672 return 0;
4673
eb1b1237 4674fail_init_adapter:
cd50ba8e 4675fail_ready_state:
51087a86
SS
4676 kfree(instance->ctrl_info);
4677 instance->ctrl_info = NULL;
c4a3e0a5
BS
4678 iounmap(instance->reg_set);
4679
4680 fail_ioremap:
b6d5d880 4681 pci_release_selected_regions(instance->pdev, instance->bar);
c4a3e0a5
BS
4682
4683 return -EINVAL;
4684}
4685
4686/**
4687 * megasas_release_mfi - Reverses the FW initialization
4688 * @intance: Adapter soft state
4689 */
4690static void megasas_release_mfi(struct megasas_instance *instance)
4691{
9c915a8c 4692 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
c4a3e0a5 4693
9c915a8c 4694 if (instance->reply_queue)
4695 pci_free_consistent(instance->pdev, reply_q_sz,
c4a3e0a5
BS
4696 instance->reply_queue, instance->reply_queue_h);
4697
4698 megasas_free_cmds(instance);
4699
4700 iounmap(instance->reg_set);
4701
b6d5d880 4702 pci_release_selected_regions(instance->pdev, instance->bar);
c4a3e0a5
BS
4703}
4704
4705/**
4706 * megasas_get_seq_num - Gets latest event sequence numbers
4707 * @instance: Adapter soft state
4708 * @eli: FW event log sequence numbers information
4709 *
4710 * FW maintains a log of all events in a non-volatile area. Upper layers would
4711 * usually find out the latest sequence number of the events, the seq number at
4712 * the boot etc. They would "read" all the events below the latest seq number
4713 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4714 * number), they would subsribe to AEN (asynchronous event notification) and
4715 * wait for the events to happen.
4716 */
4717static int
4718megasas_get_seq_num(struct megasas_instance *instance,
4719 struct megasas_evt_log_info *eli)
4720{
4721 struct megasas_cmd *cmd;
4722 struct megasas_dcmd_frame *dcmd;
4723 struct megasas_evt_log_info *el_info;
4724 dma_addr_t el_info_h = 0;
4725
4726 cmd = megasas_get_cmd(instance);
4727
4728 if (!cmd) {
4729 return -ENOMEM;
4730 }
4731
4732 dcmd = &cmd->frame->dcmd;
4733 el_info = pci_alloc_consistent(instance->pdev,
4734 sizeof(struct megasas_evt_log_info),
4735 &el_info_h);
4736
4737 if (!el_info) {
4738 megasas_return_cmd(instance, cmd);
4739 return -ENOMEM;
4740 }
4741
4742 memset(el_info, 0, sizeof(*el_info));
4743 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4744
4745 dcmd->cmd = MFI_CMD_DCMD;
4746 dcmd->cmd_status = 0x0;
4747 dcmd->sge_count = 1;
94cd65dd 4748 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
c4a3e0a5 4749 dcmd->timeout = 0;
780a3762 4750 dcmd->pad_0 = 0;
94cd65dd
SS
4751 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4752 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4753 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4754 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
c4a3e0a5 4755
cfbe7554
SS
4756 if (megasas_issue_blocked_cmd(instance, cmd, 30))
4757 dev_err(&instance->pdev->dev, "Command timedout"
4758 "from %s\n", __func__);
4759 else {
4760 /*
4761 * Copy the data back into callers buffer
4762 */
4763 eli->newest_seq_num = le32_to_cpu(el_info->newest_seq_num);
4764 eli->oldest_seq_num = le32_to_cpu(el_info->oldest_seq_num);
4765 eli->clear_seq_num = le32_to_cpu(el_info->clear_seq_num);
4766 eli->shutdown_seq_num = le32_to_cpu(el_info->shutdown_seq_num);
4767 eli->boot_seq_num = le32_to_cpu(el_info->boot_seq_num);
4768 }
c4a3e0a5
BS
4769
4770 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
4771 el_info, el_info_h);
4772
90dc9d98
SS
4773 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4774 megasas_return_mfi_mpt_pthr(instance, cmd,
4775 cmd->mpt_pthr_cmd_blocked);
4776 else
4777 megasas_return_cmd(instance, cmd);
c4a3e0a5
BS
4778
4779 return 0;
4780}
4781
4782/**
4783 * megasas_register_aen - Registers for asynchronous event notification
4784 * @instance: Adapter soft state
4785 * @seq_num: The starting sequence number
4786 * @class_locale: Class of the event
4787 *
4788 * This function subscribes for AEN for events beyond the @seq_num. It requests
4789 * to be notified if and only if the event is of type @class_locale
4790 */
4791static int
4792megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
4793 u32 class_locale_word)
4794{
4795 int ret_val;
4796 struct megasas_cmd *cmd;
4797 struct megasas_dcmd_frame *dcmd;
4798 union megasas_evt_class_locale curr_aen;
4799 union megasas_evt_class_locale prev_aen;
4800
4801 /*
4802 * If there an AEN pending already (aen_cmd), check if the
4803 * class_locale of that pending AEN is inclusive of the new
4804 * AEN request we currently have. If it is, then we don't have
4805 * to do anything. In other words, whichever events the current
4806 * AEN request is subscribing to, have already been subscribed
4807 * to.
4808 *
4809 * If the old_cmd is _not_ inclusive, then we have to abort
4810 * that command, form a class_locale that is superset of both
4811 * old and current and re-issue to the FW
4812 */
4813
4814 curr_aen.word = class_locale_word;
4815
4816 if (instance->aen_cmd) {
4817
4818 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
94cd65dd 4819 prev_aen.members.locale = le16_to_cpu(prev_aen.members.locale);
c4a3e0a5
BS
4820
4821 /*
4822 * A class whose enum value is smaller is inclusive of all
4823 * higher values. If a PROGRESS (= -1) was previously
4824 * registered, then a new registration requests for higher
4825 * classes need not be sent to FW. They are automatically
4826 * included.
4827 *
4828 * Locale numbers don't have such hierarchy. They are bitmap
4829 * values
4830 */
4831 if ((prev_aen.members.class <= curr_aen.members.class) &&
3993a862 4832 !((prev_aen.members.locale & curr_aen.members.locale) ^
c4a3e0a5
BS
4833 curr_aen.members.locale)) {
4834 /*
4835 * Previously issued event registration includes
4836 * current request. Nothing to do.
4837 */
4838 return 0;
4839 } else {
3993a862 4840 curr_aen.members.locale |= prev_aen.members.locale;
c4a3e0a5
BS
4841
4842 if (prev_aen.members.class < curr_aen.members.class)
4843 curr_aen.members.class = prev_aen.members.class;
4844
4845 instance->aen_cmd->abort_aen = 1;
4846 ret_val = megasas_issue_blocked_abort_cmd(instance,
4847 instance->
cfbe7554 4848 aen_cmd, 30);
c4a3e0a5
BS
4849
4850 if (ret_val) {
4851 printk(KERN_DEBUG "megasas: Failed to abort "
4852 "previous AEN command\n");
4853 return ret_val;
4854 }
4855 }
4856 }
4857
4858 cmd = megasas_get_cmd(instance);
4859
4860 if (!cmd)
4861 return -ENOMEM;
4862
4863 dcmd = &cmd->frame->dcmd;
4864
4865 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4866
4867 /*
4868 * Prepare DCMD for aen registration
4869 */
4870 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4871
4872 dcmd->cmd = MFI_CMD_DCMD;
4873 dcmd->cmd_status = 0x0;
4874 dcmd->sge_count = 1;
94cd65dd 4875 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
c4a3e0a5 4876 dcmd->timeout = 0;
780a3762 4877 dcmd->pad_0 = 0;
94cd65dd
SS
4878 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4879 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4880 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
39a98554 4881 instance->last_seq_num = seq_num;
94cd65dd
SS
4882 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4883 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4884 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
c4a3e0a5 4885
f4c9a131
YB
4886 if (instance->aen_cmd != NULL) {
4887 megasas_return_cmd(instance, cmd);
4888 return 0;
4889 }
4890
c4a3e0a5
BS
4891 /*
4892 * Store reference to the cmd used to register for AEN. When an
4893 * application wants us to register for AEN, we have to abort this
4894 * cmd and re-register with a new EVENT LOCALE supplied by that app
4895 */
4896 instance->aen_cmd = cmd;
4897
4898 /*
4899 * Issue the aen registration frame
4900 */
9c915a8c 4901 instance->instancet->issue_dcmd(instance, cmd);
c4a3e0a5
BS
4902
4903 return 0;
4904}
4905
4906/**
4907 * megasas_start_aen - Subscribes to AEN during driver load time
4908 * @instance: Adapter soft state
4909 */
4910static int megasas_start_aen(struct megasas_instance *instance)
4911{
4912 struct megasas_evt_log_info eli;
4913 union megasas_evt_class_locale class_locale;
4914
4915 /*
4916 * Get the latest sequence number from FW
4917 */
4918 memset(&eli, 0, sizeof(eli));
4919
4920 if (megasas_get_seq_num(instance, &eli))
4921 return -1;
4922
4923 /*
4924 * Register AEN with FW for latest sequence number plus 1
4925 */
4926 class_locale.members.reserved = 0;
4927 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4928 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4929
94cd65dd 4930 return megasas_register_aen(instance,
3993a862 4931 eli.newest_seq_num + 1,
94cd65dd 4932 class_locale.word);
c4a3e0a5
BS
4933}
4934
4935/**
4936 * megasas_io_attach - Attaches this driver to SCSI mid-layer
4937 * @instance: Adapter soft state
4938 */
4939static int megasas_io_attach(struct megasas_instance *instance)
4940{
4941 struct Scsi_Host *host = instance->host;
4942
4943 /*
4944 * Export parameters required by SCSI mid-layer
4945 */
4946 host->irq = instance->pdev->irq;
4947 host->unique_id = instance->unique_id;
404a8a1a 4948 if (instance->is_imr) {
7bebf5c7
YB
4949 host->can_queue =
4950 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
4951 } else
4952 host->can_queue =
4953 instance->max_fw_cmds - MEGASAS_INT_CMDS;
c4a3e0a5
BS
4954 host->this_id = instance->init_id;
4955 host->sg_tablesize = instance->max_num_sge;
42a8d2b3 4956
4957 if (instance->fw_support_ieee)
4958 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
4959
1fd10685
YB
4960 /*
4961 * Check if the module parameter value for max_sectors can be used
4962 */
4963 if (max_sectors && max_sectors < instance->max_sectors_per_req)
4964 instance->max_sectors_per_req = max_sectors;
4965 else {
4966 if (max_sectors) {
4967 if (((instance->pdev->device ==
4968 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
4969 (instance->pdev->device ==
4970 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
4971 (max_sectors <= MEGASAS_MAX_SECTORS)) {
4972 instance->max_sectors_per_req = max_sectors;
4973 } else {
4974 printk(KERN_INFO "megasas: max_sectors should be > 0"
4975 "and <= %d (or < 1MB for GEN2 controller)\n",
4976 instance->max_sectors_per_req);
4977 }
4978 }
4979 }
4980
c4a3e0a5 4981 host->max_sectors = instance->max_sectors_per_req;
9c915a8c 4982 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
c4a3e0a5
BS
4983 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
4984 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
4985 host->max_lun = MEGASAS_MAX_LUN;
122da302 4986 host->max_cmd_len = 16;
c4a3e0a5 4987
9c915a8c 4988 /* Fusion only supports host reset */
36807e67 4989 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
229fe47c 4990 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
21d3c710
SS
4991 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4992 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
9c915a8c 4993 host->hostt->eh_device_reset_handler = NULL;
4994 host->hostt->eh_bus_reset_handler = NULL;
4995 }
4996
c4a3e0a5
BS
4997 /*
4998 * Notify the mid-layer about the new controller
4999 */
5000 if (scsi_add_host(host, &instance->pdev->dev)) {
5001 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
5002 return -ENODEV;
5003 }
5004
c4a3e0a5
BS
5005 return 0;
5006}
5007
31ea7088 5008static int
5009megasas_set_dma_mask(struct pci_dev *pdev)
5010{
5011 /*
5012 * All our contollers are capable of performing 64-bit DMA
5013 */
5014 if (IS_DMA64) {
6a35528a 5015 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
31ea7088 5016
284901a9 5017 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 5018 goto fail_set_dma_mask;
5019 }
5020 } else {
284901a9 5021 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 5022 goto fail_set_dma_mask;
5023 }
46de63e2
SS
5024 /*
5025 * Ensure that all data structures are allocated in 32-bit
5026 * memory.
5027 */
5028 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5029 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5030 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5031 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5032 dev_info(&pdev->dev, "set 32bit DMA mask"
5033 "and 32 bit consistent mask\n");
5034 else
5035 goto fail_set_dma_mask;
5036 }
94cd65dd 5037
31ea7088 5038 return 0;
5039
5040fail_set_dma_mask:
5041 return 1;
5042}
5043
c4a3e0a5
BS
5044/**
5045 * megasas_probe_one - PCI hotplug entry point
5046 * @pdev: PCI device structure
0d49016b 5047 * @id: PCI ids of supported hotplugged adapter
c4a3e0a5 5048 */
6f039790
GKH
5049static int megasas_probe_one(struct pci_dev *pdev,
5050 const struct pci_device_id *id)
c4a3e0a5 5051{
8058a169 5052 int rval, pos, i, j, cpu;
c4a3e0a5
BS
5053 struct Scsi_Host *host;
5054 struct megasas_instance *instance;
66192dfe 5055 u16 control = 0;
51087a86 5056 struct fusion_context *fusion = NULL;
66192dfe 5057
5058 /* Reset MSI-X in the kdump kernel */
5059 if (reset_devices) {
5060 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5061 if (pos) {
99369065 5062 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
66192dfe 5063 &control);
5064 if (control & PCI_MSIX_FLAGS_ENABLE) {
5065 dev_info(&pdev->dev, "resetting MSI-X\n");
5066 pci_write_config_word(pdev,
99369065 5067 pos + PCI_MSIX_FLAGS,
66192dfe 5068 control &
5069 ~PCI_MSIX_FLAGS_ENABLE);
5070 }
5071 }
5072 }
c4a3e0a5
BS
5073
5074 /*
5075 * Announce PCI information
5076 */
5077 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
5078 pdev->vendor, pdev->device, pdev->subsystem_vendor,
5079 pdev->subsystem_device);
5080
5081 printk("bus %d:slot %d:func %d\n",
5082 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
5083
5084 /*
5085 * PCI prepping: enable device set bus mastering and dma mask
5086 */
aeab3fd7 5087 rval = pci_enable_device_mem(pdev);
c4a3e0a5
BS
5088
5089 if (rval) {
5090 return rval;
5091 }
5092
5093 pci_set_master(pdev);
5094
31ea7088 5095 if (megasas_set_dma_mask(pdev))
5096 goto fail_set_dma_mask;
c4a3e0a5
BS
5097
5098 host = scsi_host_alloc(&megasas_template,
5099 sizeof(struct megasas_instance));
5100
5101 if (!host) {
5102 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
5103 goto fail_alloc_instance;
5104 }
5105
5106 instance = (struct megasas_instance *)host->hostdata;
5107 memset(instance, 0, sizeof(*instance));
39a98554 5108 atomic_set( &instance->fw_reset_no_pci_access, 0 );
9c915a8c 5109 instance->pdev = pdev;
c4a3e0a5 5110
9c915a8c 5111 switch (instance->pdev->device) {
5112 case PCI_DEVICE_ID_LSI_FUSION:
229fe47c 5113 case PCI_DEVICE_ID_LSI_PLASMA:
36807e67 5114 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 5115 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 5116 {
51087a86
SS
5117 instance->ctrl_context_pages =
5118 get_order(sizeof(struct fusion_context));
5119 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5120 instance->ctrl_context_pages);
9c915a8c 5121 if (!instance->ctrl_context) {
5122 printk(KERN_DEBUG "megasas: Failed to allocate "
5123 "memory for Fusion context info\n");
5124 goto fail_alloc_dma_buf;
5125 }
5126 fusion = instance->ctrl_context;
d009b576
SS
5127 memset(fusion, 0,
5128 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
9c915a8c 5129 INIT_LIST_HEAD(&fusion->cmd_pool);
90dc9d98 5130 spin_lock_init(&fusion->mpt_pool_lock);
51087a86
SS
5131 memset(fusion->load_balance_info, 0,
5132 sizeof(struct LD_LOAD_BALANCE_INFO) * MAX_LOGICAL_DRIVES_EXT);
9c915a8c 5133 }
5134 break;
5135 default: /* For all other supported controllers */
5136
5137 instance->producer =
5138 pci_alloc_consistent(pdev, sizeof(u32),
5139 &instance->producer_h);
5140 instance->consumer =
5141 pci_alloc_consistent(pdev, sizeof(u32),
5142 &instance->consumer_h);
5143
5144 if (!instance->producer || !instance->consumer) {
5145 printk(KERN_DEBUG "megasas: Failed to allocate"
5146 "memory for producer, consumer\n");
5147 goto fail_alloc_dma_buf;
5148 }
c4a3e0a5 5149
9c915a8c 5150 *instance->producer = 0;
5151 *instance->consumer = 0;
5152 break;
c4a3e0a5
BS
5153 }
5154
fc62b3fc
SS
5155 /* Crash dump feature related initialisation*/
5156 instance->drv_buf_index = 0;
5157 instance->drv_buf_alloc = 0;
5158 instance->crash_dump_fw_support = 0;
5159 instance->crash_dump_app_support = 0;
5160 instance->fw_crash_state = UNAVAILABLE;
5161 spin_lock_init(&instance->crashdump_lock);
5162 instance->crash_dump_buf = NULL;
5163
5164 if (!reset_devices)
5165 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5166 CRASH_DMA_BUF_SIZE,
5167 &instance->crash_dump_h);
5168 if (!instance->crash_dump_buf)
5169 dev_err(&instance->pdev->dev, "Can't allocate Firmware "
5170 "crash dump DMA buffer\n");
5171
c3518837 5172 megasas_poll_wait_aen = 0;
f4c9a131 5173 instance->flag_ieee = 0;
7e8a75f4 5174 instance->ev = NULL;
39a98554 5175 instance->issuepend_done = 1;
5176 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
404a8a1a 5177 instance->is_imr = 0;
c4a3e0a5
BS
5178
5179 instance->evt_detail = pci_alloc_consistent(pdev,
5180 sizeof(struct
5181 megasas_evt_detail),
5182 &instance->evt_detail_h);
5183
5184 if (!instance->evt_detail) {
5185 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
5186 "event detail structure\n");
5187 goto fail_alloc_dma_buf;
5188 }
5189
5190 /*
5191 * Initialize locks and queues
5192 */
5193 INIT_LIST_HEAD(&instance->cmd_pool);
39a98554 5194 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
c4a3e0a5 5195
e4a082c7
SP
5196 atomic_set(&instance->fw_outstanding,0);
5197
c4a3e0a5
BS
5198 init_waitqueue_head(&instance->int_cmd_wait_q);
5199 init_waitqueue_head(&instance->abort_cmd_wait_q);
5200
90dc9d98 5201 spin_lock_init(&instance->mfi_pool_lock);
39a98554 5202 spin_lock_init(&instance->hba_lock);
7343eb65 5203 spin_lock_init(&instance->completion_lock);
c4a3e0a5 5204
e5a69e27 5205 mutex_init(&instance->aen_mutex);
9c915a8c 5206 mutex_init(&instance->reset_mutex);
c4a3e0a5
BS
5207
5208 /*
5209 * Initialize PCI related and misc parameters
5210 */
c4a3e0a5
BS
5211 instance->host = host;
5212 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5213 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
51087a86 5214 instance->ctrl_info = NULL;
c4a3e0a5 5215
7bebf5c7
YB
5216 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5217 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
f4c9a131 5218 instance->flag_ieee = 1;
7bebf5c7
YB
5219 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5220 } else
90dc9d98 5221 sema_init(&instance->ioctl_sem, (MEGASAS_INT_CMDS - 5));
7bebf5c7 5222
658dcedb 5223 megasas_dbg_lvl = 0;
05e9ebbe 5224 instance->flag = 0;
0c79e681 5225 instance->unload = 1;
05e9ebbe 5226 instance->last_time = 0;
39a98554 5227 instance->disableOnlineCtrlReset = 1;
bc93d425 5228 instance->UnevenSpanSupport = 0;
39a98554 5229
36807e67 5230 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
229fe47c 5231 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
21d3c710 5232 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
fc62b3fc 5233 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
9c915a8c 5234 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
fc62b3fc
SS
5235 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5236 } else
9c915a8c 5237 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
658dcedb 5238
0a77066a 5239 /*
5240 * Initialize MFI Firmware
5241 */
5242 if (megasas_init_fw(instance))
5243 goto fail_init_mfi;
5244
229fe47c 5245 if (instance->requestorId) {
5246 if (instance->PlasmaFW111) {
5247 instance->vf_affiliation_111 =
5248 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5249 &instance->vf_affiliation_111_h);
5250 if (!instance->vf_affiliation_111)
5251 printk(KERN_WARNING "megasas: Can't allocate "
5252 "memory for VF affiliation buffer\n");
5253 } else {
5254 instance->vf_affiliation =
5255 pci_alloc_consistent(pdev,
5256 (MAX_LOGICAL_DRIVES + 1) *
5257 sizeof(struct MR_LD_VF_AFFILIATION),
5258 &instance->vf_affiliation_h);
5259 if (!instance->vf_affiliation)
5260 printk(KERN_WARNING "megasas: Can't allocate "
5261 "memory for VF affiliation buffer\n");
5262 }
5263 }
5264
d46a3ad6 5265retry_irq_register:
c4a3e0a5
BS
5266 /*
5267 * Register IRQ
5268 */
c8e858fe 5269 if (instance->msix_vectors) {
8058a169
SS
5270 cpu = cpumask_first(cpu_online_mask);
5271 for (i = 0; i < instance->msix_vectors; i++) {
c8e858fe 5272 instance->irq_context[i].instance = instance;
5273 instance->irq_context[i].MSIxIndex = i;
5274 if (request_irq(instance->msixentry[i].vector,
5275 instance->instancet->service_isr, 0,
5276 "megasas",
5277 &instance->irq_context[i])) {
5278 printk(KERN_DEBUG "megasas: Failed to "
5279 "register IRQ for vector %d.\n", i);
8058a169 5280 for (j = 0; j < i; j++) {
ac95136a
SS
5281 if (smp_affinity_enable)
5282 irq_set_affinity_hint(
5283 instance->msixentry[j].vector, NULL);
c8e858fe 5284 free_irq(
5285 instance->msixentry[j].vector,
5286 &instance->irq_context[j]);
8058a169 5287 }
d46a3ad6
SS
5288 /* Retry irq register for IO_APIC */
5289 instance->msix_vectors = 0;
5290 goto retry_irq_register;
c8e858fe 5291 }
ac95136a
SS
5292 if (smp_affinity_enable) {
5293 if (irq_set_affinity_hint(instance->msixentry[i].vector,
5294 get_cpu_mask(cpu)))
5295 dev_err(&instance->pdev->dev,
5296 "Error setting affinity hint "
5297 "for cpu %d\n", cpu);
5298 cpu = cpumask_next(cpu, cpu_online_mask);
5299 }
c8e858fe 5300 }
5301 } else {
5302 instance->irq_context[0].instance = instance;
5303 instance->irq_context[0].MSIxIndex = 0;
5304 if (request_irq(pdev->irq, instance->instancet->service_isr,
5305 IRQF_SHARED, "megasas",
5306 &instance->irq_context[0])) {
5307 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5308 goto fail_irq;
5309 }
c4a3e0a5
BS
5310 }
5311
d46a3ad6 5312 instance->instancet->enable_intr(instance);
c4a3e0a5
BS
5313
5314 /*
5315 * Store instance in PCI softstate
5316 */
5317 pci_set_drvdata(pdev, instance);
5318
5319 /*
5320 * Add this controller to megasas_mgmt_info structure so that it
5321 * can be exported to management applications
5322 */
5323 megasas_mgmt_info.count++;
5324 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5325 megasas_mgmt_info.max_index++;
5326
541f90b7 5327 /*
5328 * Register with SCSI mid-layer
5329 */
5330 if (megasas_io_attach(instance))
5331 goto fail_io_attach;
5332
5333 instance->unload = 0;
aa00832b
SS
5334 /*
5335 * Trigger SCSI to scan our drives
5336 */
5337 scsi_scan_host(host);
541f90b7 5338
c4a3e0a5
BS
5339 /*
5340 * Initiate AEN (Asynchronous Event Notification)
5341 */
5342 if (megasas_start_aen(instance)) {
5343 printk(KERN_DEBUG "megasas: start aen failed\n");
5344 goto fail_start_aen;
5345 }
5346
9ea81f81
AR
5347 /* Get current SR-IOV LD/VF affiliation */
5348 if (instance->requestorId)
5349 megasas_get_ld_vf_affiliation(instance, 1);
5350
c4a3e0a5
BS
5351 return 0;
5352
5353 fail_start_aen:
5354 fail_io_attach:
5355 megasas_mgmt_info.count--;
5356 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5357 megasas_mgmt_info.max_index--;
5358
d46a3ad6 5359 instance->instancet->disable_intr(instance);
c8e858fe 5360 if (instance->msix_vectors)
8058a169 5361 for (i = 0; i < instance->msix_vectors; i++) {
ac95136a
SS
5362 if (smp_affinity_enable)
5363 irq_set_affinity_hint(
5364 instance->msixentry[i].vector, NULL);
c8e858fe 5365 free_irq(instance->msixentry[i].vector,
5366 &instance->irq_context[i]);
8058a169 5367 }
c8e858fe 5368 else
5369 free_irq(instance->pdev->irq, &instance->irq_context[0]);
eb1b1237 5370fail_irq:
36807e67 5371 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
229fe47c 5372 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
21d3c710
SS
5373 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5374 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
eb1b1237 5375 megasas_release_fusion(instance);
5376 else
5377 megasas_release_mfi(instance);
c4a3e0a5 5378 fail_init_mfi:
c8e858fe 5379 if (instance->msix_vectors)
0a77066a 5380 pci_disable_msix(instance->pdev);
c4a3e0a5
BS
5381 fail_alloc_dma_buf:
5382 if (instance->evt_detail)
5383 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5384 instance->evt_detail,
5385 instance->evt_detail_h);
5386
eb1b1237 5387 if (instance->producer)
c4a3e0a5
BS
5388 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5389 instance->producer_h);
5390 if (instance->consumer)
5391 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5392 instance->consumer_h);
5393 scsi_host_put(host);
5394
5395 fail_alloc_instance:
5396 fail_set_dma_mask:
5397 pci_disable_device(pdev);
5398
5399 return -ENODEV;
5400}
5401
5402/**
5403 * megasas_flush_cache - Requests FW to flush all its caches
5404 * @instance: Adapter soft state
5405 */
5406static void megasas_flush_cache(struct megasas_instance *instance)
5407{
5408 struct megasas_cmd *cmd;
5409 struct megasas_dcmd_frame *dcmd;
5410
39a98554 5411 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5412 return;
5413
c4a3e0a5
BS
5414 cmd = megasas_get_cmd(instance);
5415
5416 if (!cmd)
5417 return;
5418
5419 dcmd = &cmd->frame->dcmd;
5420
5421 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5422
5423 dcmd->cmd = MFI_CMD_DCMD;
5424 dcmd->cmd_status = 0x0;
5425 dcmd->sge_count = 0;
94cd65dd 5426 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
c4a3e0a5 5427 dcmd->timeout = 0;
780a3762 5428 dcmd->pad_0 = 0;
c4a3e0a5 5429 dcmd->data_xfer_len = 0;
94cd65dd 5430 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
c4a3e0a5
BS
5431 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5432
cfbe7554
SS
5433 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5434 dev_err(&instance->pdev->dev, "Command timedout"
5435 " from %s\n", __func__);
c4a3e0a5 5436
90dc9d98
SS
5437 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5438 megasas_return_mfi_mpt_pthr(instance, cmd,
5439 cmd->mpt_pthr_cmd_blocked);
5440 else
5441 megasas_return_cmd(instance, cmd);
c4a3e0a5
BS
5442
5443 return;
5444}
5445
5446/**
5447 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5448 * @instance: Adapter soft state
31ea7088 5449 * @opcode: Shutdown/Hibernate
c4a3e0a5 5450 */
31ea7088 5451static void megasas_shutdown_controller(struct megasas_instance *instance,
5452 u32 opcode)
c4a3e0a5
BS
5453{
5454 struct megasas_cmd *cmd;
5455 struct megasas_dcmd_frame *dcmd;
5456
39a98554 5457 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5458 return;
5459
c4a3e0a5
BS
5460 cmd = megasas_get_cmd(instance);
5461
5462 if (!cmd)
5463 return;
5464
5465 if (instance->aen_cmd)
cfbe7554
SS
5466 megasas_issue_blocked_abort_cmd(instance,
5467 instance->aen_cmd, 30);
9c915a8c 5468 if (instance->map_update_cmd)
5469 megasas_issue_blocked_abort_cmd(instance,
cfbe7554 5470 instance->map_update_cmd, 30);
c4a3e0a5
BS
5471 dcmd = &cmd->frame->dcmd;
5472
5473 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5474
5475 dcmd->cmd = MFI_CMD_DCMD;
5476 dcmd->cmd_status = 0x0;
5477 dcmd->sge_count = 0;
94cd65dd 5478 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
c4a3e0a5 5479 dcmd->timeout = 0;
780a3762 5480 dcmd->pad_0 = 0;
c4a3e0a5 5481 dcmd->data_xfer_len = 0;
94cd65dd 5482 dcmd->opcode = cpu_to_le32(opcode);
c4a3e0a5 5483
cfbe7554
SS
5484 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5485 dev_err(&instance->pdev->dev, "Command timedout"
5486 "from %s\n", __func__);
c4a3e0a5 5487
90dc9d98
SS
5488 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5489 megasas_return_mfi_mpt_pthr(instance, cmd,
5490 cmd->mpt_pthr_cmd_blocked);
5491 else
5492 megasas_return_cmd(instance, cmd);
c4a3e0a5
BS
5493
5494 return;
5495}
5496
33139b21 5497#ifdef CONFIG_PM
31ea7088 5498/**
ad84db2e 5499 * megasas_suspend - driver suspend entry point
5500 * @pdev: PCI device structure
31ea7088 5501 * @state: PCI power state to suspend routine
5502 */
33139b21 5503static int
31ea7088 5504megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5505{
5506 struct Scsi_Host *host;
5507 struct megasas_instance *instance;
c8e858fe 5508 int i;
31ea7088 5509
5510 instance = pci_get_drvdata(pdev);
5511 host = instance->host;
0c79e681 5512 instance->unload = 1;
31ea7088 5513
229fe47c 5514 /* Shutdown SR-IOV heartbeat timer */
5515 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5516 del_timer_sync(&instance->sriov_heartbeat_timer);
5517
31ea7088 5518 megasas_flush_cache(instance);
5519 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7e8a75f4
YB
5520
5521 /* cancel the delayed work if this work still in queue */
5522 if (instance->ev != NULL) {
5523 struct megasas_aen_event *ev = instance->ev;
c1d390d8 5524 cancel_delayed_work_sync(&ev->hotplug_work);
7e8a75f4
YB
5525 instance->ev = NULL;
5526 }
5527
31ea7088 5528 tasklet_kill(&instance->isr_tasklet);
5529
5530 pci_set_drvdata(instance->pdev, instance);
d46a3ad6 5531 instance->instancet->disable_intr(instance);
c8e858fe 5532
5533 if (instance->msix_vectors)
8058a169 5534 for (i = 0; i < instance->msix_vectors; i++) {
ac95136a
SS
5535 if (smp_affinity_enable)
5536 irq_set_affinity_hint(
5537 instance->msixentry[i].vector, NULL);
c8e858fe 5538 free_irq(instance->msixentry[i].vector,
5539 &instance->irq_context[i]);
8058a169 5540 }
c8e858fe 5541 else
5542 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5543 if (instance->msix_vectors)
80d9da98 5544 pci_disable_msix(instance->pdev);
31ea7088 5545
5546 pci_save_state(pdev);
5547 pci_disable_device(pdev);
5548
5549 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5550
5551 return 0;
5552}
5553
5554/**
5555 * megasas_resume- driver resume entry point
5556 * @pdev: PCI device structure
5557 */
33139b21 5558static int
31ea7088 5559megasas_resume(struct pci_dev *pdev)
5560{
8058a169 5561 int rval, i, j, cpu;
31ea7088 5562 struct Scsi_Host *host;
5563 struct megasas_instance *instance;
5564
5565 instance = pci_get_drvdata(pdev);
5566 host = instance->host;
5567 pci_set_power_state(pdev, PCI_D0);
5568 pci_enable_wake(pdev, PCI_D0, 0);
5569 pci_restore_state(pdev);
5570
5571 /*
5572 * PCI prepping: enable device set bus mastering and dma mask
5573 */
aeab3fd7 5574 rval = pci_enable_device_mem(pdev);
31ea7088 5575
5576 if (rval) {
5577 printk(KERN_ERR "megasas: Enable device failed\n");
5578 return rval;
5579 }
5580
5581 pci_set_master(pdev);
5582
5583 if (megasas_set_dma_mask(pdev))
5584 goto fail_set_dma_mask;
5585
5586 /*
5587 * Initialize MFI Firmware
5588 */
5589
31ea7088 5590 atomic_set(&instance->fw_outstanding, 0);
5591
5592 /*
5593 * We expect the FW state to be READY
5594 */
058a8fac 5595 if (megasas_transition_to_ready(instance, 0))
31ea7088 5596 goto fail_ready_state;
5597
3f1abce4 5598 /* Now re-enable MSI-X */
dd088128 5599 if (instance->msix_vectors &&
8ae80ed1
AG
5600 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5601 instance->msix_vectors))
dd088128 5602 goto fail_reenable_msix;
3f1abce4 5603
9c915a8c 5604 switch (instance->pdev->device) {
5605 case PCI_DEVICE_ID_LSI_FUSION:
229fe47c 5606 case PCI_DEVICE_ID_LSI_PLASMA:
36807e67 5607 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 5608 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 5609 {
5610 megasas_reset_reply_desc(instance);
5611 if (megasas_ioc_init_fusion(instance)) {
5612 megasas_free_cmds(instance);
5613 megasas_free_cmds_fusion(instance);
5614 goto fail_init_mfi;
5615 }
5616 if (!megasas_get_map_info(instance))
5617 megasas_sync_map_info(instance);
5618 }
5619 break;
5620 default:
5621 *instance->producer = 0;
5622 *instance->consumer = 0;
5623 if (megasas_issue_init_mfi(instance))
5624 goto fail_init_mfi;
5625 break;
5626 }
31ea7088 5627
9c915a8c 5628 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5629 (unsigned long)instance);
31ea7088 5630
5631 /*
5632 * Register IRQ
5633 */
c8e858fe 5634 if (instance->msix_vectors) {
8058a169 5635 cpu = cpumask_first(cpu_online_mask);
c8e858fe 5636 for (i = 0 ; i < instance->msix_vectors; i++) {
5637 instance->irq_context[i].instance = instance;
5638 instance->irq_context[i].MSIxIndex = i;
5639 if (request_irq(instance->msixentry[i].vector,
5640 instance->instancet->service_isr, 0,
5641 "megasas",
5642 &instance->irq_context[i])) {
5643 printk(KERN_DEBUG "megasas: Failed to "
5644 "register IRQ for vector %d.\n", i);
8058a169 5645 for (j = 0; j < i; j++) {
ac95136a
SS
5646 if (smp_affinity_enable)
5647 irq_set_affinity_hint(
5648 instance->msixentry[j].vector, NULL);
c8e858fe 5649 free_irq(
5650 instance->msixentry[j].vector,
5651 &instance->irq_context[j]);
8058a169 5652 }
c8e858fe 5653 goto fail_irq;
5654 }
8058a169 5655
ac95136a
SS
5656 if (smp_affinity_enable) {
5657 if (irq_set_affinity_hint(instance->msixentry[i].vector,
5658 get_cpu_mask(cpu)))
5659 dev_err(&instance->pdev->dev, "Error "
5660 "setting affinity hint for cpu "
5661 "%d\n", cpu);
5662 cpu = cpumask_next(cpu, cpu_online_mask);
5663 }
c8e858fe 5664 }
5665 } else {
5666 instance->irq_context[0].instance = instance;
5667 instance->irq_context[0].MSIxIndex = 0;
5668 if (request_irq(pdev->irq, instance->instancet->service_isr,
5669 IRQF_SHARED, "megasas",
5670 &instance->irq_context[0])) {
5671 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5672 goto fail_irq;
5673 }
31ea7088 5674 }
5675
229fe47c 5676 /* Re-launch SR-IOV heartbeat timer */
5677 if (instance->requestorId) {
5678 if (!megasas_sriov_start_heartbeat(instance, 0))
5679 megasas_start_timer(instance,
5680 &instance->sriov_heartbeat_timer,
5681 megasas_sriov_heartbeat_handler,
5682 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5683 else
5684 instance->skip_heartbeat_timer_del = 1;
5685 }
5686
d46a3ad6 5687 instance->instancet->enable_intr(instance);
0c79e681
YB
5688 instance->unload = 0;
5689
541f90b7 5690 /*
5691 * Initiate AEN (Asynchronous Event Notification)
5692 */
5693 if (megasas_start_aen(instance))
5694 printk(KERN_ERR "megasas: Start AEN failed\n");
5695
31ea7088 5696 return 0;
5697
5698fail_irq:
5699fail_init_mfi:
5700 if (instance->evt_detail)
5701 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5702 instance->evt_detail,
5703 instance->evt_detail_h);
5704
5705 if (instance->producer)
5706 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5707 instance->producer_h);
5708 if (instance->consumer)
5709 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5710 instance->consumer_h);
5711 scsi_host_put(host);
5712
5713fail_set_dma_mask:
5714fail_ready_state:
dd088128 5715fail_reenable_msix:
31ea7088 5716
5717 pci_disable_device(pdev);
5718
5719 return -ENODEV;
5720}
33139b21
JS
5721#else
5722#define megasas_suspend NULL
5723#define megasas_resume NULL
5724#endif
31ea7088 5725
c4a3e0a5
BS
5726/**
5727 * megasas_detach_one - PCI hot"un"plug entry point
5728 * @pdev: PCI device structure
5729 */
6f039790 5730static void megasas_detach_one(struct pci_dev *pdev)
c4a3e0a5
BS
5731{
5732 int i;
5733 struct Scsi_Host *host;
5734 struct megasas_instance *instance;
9c915a8c 5735 struct fusion_context *fusion;
c4a3e0a5
BS
5736
5737 instance = pci_get_drvdata(pdev);
c3518837 5738 instance->unload = 1;
c4a3e0a5 5739 host = instance->host;
9c915a8c 5740 fusion = instance->ctrl_context;
c4a3e0a5 5741
229fe47c 5742 /* Shutdown SR-IOV heartbeat timer */
5743 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5744 del_timer_sync(&instance->sriov_heartbeat_timer);
5745
fc62b3fc
SS
5746 if (instance->fw_crash_state != UNAVAILABLE)
5747 megasas_free_host_crash_buffer(instance);
c4a3e0a5
BS
5748 scsi_remove_host(instance->host);
5749 megasas_flush_cache(instance);
31ea7088 5750 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7e8a75f4
YB
5751
5752 /* cancel the delayed work if this work still in queue*/
5753 if (instance->ev != NULL) {
5754 struct megasas_aen_event *ev = instance->ev;
c1d390d8 5755 cancel_delayed_work_sync(&ev->hotplug_work);
7e8a75f4
YB
5756 instance->ev = NULL;
5757 }
5758
cfbe7554
SS
5759 /* cancel all wait events */
5760 wake_up_all(&instance->int_cmd_wait_q);
5761
5d018ad0 5762 tasklet_kill(&instance->isr_tasklet);
c4a3e0a5
BS
5763
5764 /*
5765 * Take the instance off the instance array. Note that we will not
5766 * decrement the max_index. We let this array be sparse array
5767 */
5768 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5769 if (megasas_mgmt_info.instance[i] == instance) {
5770 megasas_mgmt_info.count--;
5771 megasas_mgmt_info.instance[i] = NULL;
5772
5773 break;
5774 }
5775 }
5776
d46a3ad6 5777 instance->instancet->disable_intr(instance);
c4a3e0a5 5778
c8e858fe 5779 if (instance->msix_vectors)
8058a169 5780 for (i = 0; i < instance->msix_vectors; i++) {
ac95136a
SS
5781 if (smp_affinity_enable)
5782 irq_set_affinity_hint(
5783 instance->msixentry[i].vector, NULL);
c8e858fe 5784 free_irq(instance->msixentry[i].vector,
5785 &instance->irq_context[i]);
8058a169 5786 }
c8e858fe 5787 else
5788 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5789 if (instance->msix_vectors)
80d9da98 5790 pci_disable_msix(instance->pdev);
c4a3e0a5 5791
9c915a8c 5792 switch (instance->pdev->device) {
5793 case PCI_DEVICE_ID_LSI_FUSION:
229fe47c 5794 case PCI_DEVICE_ID_LSI_PLASMA:
36807e67 5795 case PCI_DEVICE_ID_LSI_INVADER:
21d3c710 5796 case PCI_DEVICE_ID_LSI_FURY:
9c915a8c 5797 megasas_release_fusion(instance);
51087a86 5798 for (i = 0; i < 2 ; i++) {
9c915a8c 5799 if (fusion->ld_map[i])
5800 dma_free_coherent(&instance->pdev->dev,
51087a86 5801 fusion->max_map_sz,
9c915a8c 5802 fusion->ld_map[i],
51087a86
SS
5803 fusion->ld_map_phys[i]);
5804 if (fusion->ld_drv_map[i])
5805 free_pages((ulong)fusion->ld_drv_map[i],
5806 fusion->drv_map_pages);
5807 }
5808 free_pages((ulong)instance->ctrl_context,
5809 instance->ctrl_context_pages);
9c915a8c 5810 break;
5811 default:
5812 megasas_release_mfi(instance);
9c915a8c 5813 pci_free_consistent(pdev, sizeof(u32),
5814 instance->producer,
5815 instance->producer_h);
5816 pci_free_consistent(pdev, sizeof(u32),
5817 instance->consumer,
5818 instance->consumer_h);
5819 break;
5820 }
c4a3e0a5 5821
51087a86
SS
5822 kfree(instance->ctrl_info);
5823
105900d5
SS
5824 if (instance->evt_detail)
5825 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5826 instance->evt_detail, instance->evt_detail_h);
229fe47c 5827
5828 if (instance->vf_affiliation)
5829 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5830 sizeof(struct MR_LD_VF_AFFILIATION),
5831 instance->vf_affiliation,
5832 instance->vf_affiliation_h);
5833
5834 if (instance->vf_affiliation_111)
5835 pci_free_consistent(pdev,
5836 sizeof(struct MR_LD_VF_AFFILIATION_111),
5837 instance->vf_affiliation_111,
5838 instance->vf_affiliation_111_h);
5839
5840 if (instance->hb_host_mem)
5841 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5842 instance->hb_host_mem,
5843 instance->hb_host_mem_h);
5844
fc62b3fc
SS
5845 if (instance->crash_dump_buf)
5846 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5847 instance->crash_dump_buf, instance->crash_dump_h);
5848
c4a3e0a5
BS
5849 scsi_host_put(host);
5850
c4a3e0a5
BS
5851 pci_disable_device(pdev);
5852
5853 return;
5854}
5855
5856/**
5857 * megasas_shutdown - Shutdown entry point
5858 * @device: Generic device structure
5859 */
5860static void megasas_shutdown(struct pci_dev *pdev)
5861{
c8e858fe 5862 int i;
c4a3e0a5 5863 struct megasas_instance *instance = pci_get_drvdata(pdev);
c8e858fe 5864
0c79e681 5865 instance->unload = 1;
c4a3e0a5 5866 megasas_flush_cache(instance);
530e6fc1 5867 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
d46a3ad6 5868 instance->instancet->disable_intr(instance);
c8e858fe 5869 if (instance->msix_vectors)
8058a169 5870 for (i = 0; i < instance->msix_vectors; i++) {
ac95136a
SS
5871 if (smp_affinity_enable)
5872 irq_set_affinity_hint(
5873 instance->msixentry[i].vector, NULL);
c8e858fe 5874 free_irq(instance->msixentry[i].vector,
5875 &instance->irq_context[i]);
8058a169 5876 }
c8e858fe 5877 else
5878 free_irq(instance->pdev->irq, &instance->irq_context[0]);
5879 if (instance->msix_vectors)
46fd256e 5880 pci_disable_msix(instance->pdev);
c4a3e0a5
BS
5881}
5882
5883/**
5884 * megasas_mgmt_open - char node "open" entry point
5885 */
5886static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5887{
5888 /*
5889 * Allow only those users with admin rights
5890 */
5891 if (!capable(CAP_SYS_ADMIN))
5892 return -EACCES;
5893
5894 return 0;
5895}
5896
c4a3e0a5
BS
5897/**
5898 * megasas_mgmt_fasync - Async notifier registration from applications
5899 *
5900 * This function adds the calling process to a driver global queue. When an
5901 * event occurs, SIGIO will be sent to all processes in this queue.
5902 */
5903static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5904{
5905 int rc;
5906
0b950672 5907 mutex_lock(&megasas_async_queue_mutex);
c4a3e0a5
BS
5908
5909 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
5910
0b950672 5911 mutex_unlock(&megasas_async_queue_mutex);
c4a3e0a5
BS
5912
5913 if (rc >= 0) {
5914 /* For sanity check when we get ioctl */
5915 filep->private_data = filep;
5916 return 0;
5917 }
5918
5919 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
5920
5921 return rc;
5922}
5923
c3518837
YB
5924/**
5925 * megasas_mgmt_poll - char node "poll" entry point
5926 * */
5927static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
5928{
5929 unsigned int mask;
5930 unsigned long flags;
5931 poll_wait(file, &megasas_poll_wait, wait);
5932 spin_lock_irqsave(&poll_aen_lock, flags);
5933 if (megasas_poll_wait_aen)
5934 mask = (POLLIN | POLLRDNORM);
51087a86 5935
c3518837
YB
5936 else
5937 mask = 0;
51087a86 5938 megasas_poll_wait_aen = 0;
c3518837
YB
5939 spin_unlock_irqrestore(&poll_aen_lock, flags);
5940 return mask;
5941}
5942
fc62b3fc
SS
5943/*
5944 * megasas_set_crash_dump_params_ioctl:
5945 * Send CRASH_DUMP_MODE DCMD to all controllers
5946 * @cmd: MFI command frame
5947 */
5948
5949static int megasas_set_crash_dump_params_ioctl(
5950 struct megasas_cmd *cmd)
5951{
5952 struct megasas_instance *local_instance;
5953 int i, error = 0;
5954 int crash_support;
5955
5956 crash_support = cmd->frame->dcmd.mbox.w[0];
5957
5958 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5959 local_instance = megasas_mgmt_info.instance[i];
5960 if (local_instance && local_instance->crash_dump_drv_support) {
5961 if ((local_instance->adprecovery ==
5962 MEGASAS_HBA_OPERATIONAL) &&
5963 !megasas_set_crash_dump_params(local_instance,
5964 crash_support)) {
5965 local_instance->crash_dump_app_support =
5966 crash_support;
5967 dev_info(&local_instance->pdev->dev,
5968 "Application firmware crash "
5969 "dump mode set success\n");
5970 error = 0;
5971 } else {
5972 dev_info(&local_instance->pdev->dev,
5973 "Application firmware crash "
5974 "dump mode set failed\n");
5975 error = -1;
5976 }
5977 }
5978 }
5979 return error;
5980}
5981
c4a3e0a5
BS
5982/**
5983 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
5984 * @instance: Adapter soft state
5985 * @argp: User's ioctl packet
5986 */
5987static int
5988megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
5989 struct megasas_iocpacket __user * user_ioc,
5990 struct megasas_iocpacket *ioc)
5991{
5992 struct megasas_sge32 *kern_sge32;
5993 struct megasas_cmd *cmd;
5994 void *kbuff_arr[MAX_IOCTL_SGE];
5995 dma_addr_t buf_handle = 0;
5996 int error = 0, i;
5997 void *sense = NULL;
5998 dma_addr_t sense_handle;
7b2519af 5999 unsigned long *sense_ptr;
c4a3e0a5
BS
6000
6001 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6002
6003 if (ioc->sge_count > MAX_IOCTL_SGE) {
6004 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
6005 ioc->sge_count, MAX_IOCTL_SGE);
6006 return -EINVAL;
6007 }
6008
6009 cmd = megasas_get_cmd(instance);
6010 if (!cmd) {
6011 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
6012 return -ENOMEM;
6013 }
6014
6015 /*
6016 * User's IOCTL packet has 2 frames (maximum). Copy those two
6017 * frames into our cmd's frames. cmd->frame's context will get
6018 * overwritten when we copy from user's frames. So set that value
6019 * alone separately
6020 */
6021 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
94cd65dd 6022 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
c3518837 6023 cmd->frame->hdr.pad_0 = 0;
94cd65dd
SS
6024 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6025 MFI_FRAME_SGL64 |
6026 MFI_FRAME_SENSE64));
c4a3e0a5 6027
fc62b3fc
SS
6028 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6029 error = megasas_set_crash_dump_params_ioctl(cmd);
6030 megasas_return_cmd(instance, cmd);
6031 return error;
6032 }
6033
c4a3e0a5
BS
6034 /*
6035 * The management interface between applications and the fw uses
6036 * MFI frames. E.g, RAID configuration changes, LD property changes
6037 * etc are accomplishes through different kinds of MFI frames. The
6038 * driver needs to care only about substituting user buffers with
6039 * kernel buffers in SGLs. The location of SGL is embedded in the
6040 * struct iocpacket itself.
6041 */
6042 kern_sge32 = (struct megasas_sge32 *)
6043 ((unsigned long)cmd->frame + ioc->sgl_off);
6044
6045 /*
6046 * For each user buffer, create a mirror buffer and copy in
6047 */
6048 for (i = 0; i < ioc->sge_count; i++) {
98cb7e44
BM
6049 if (!ioc->sgl[i].iov_len)
6050 continue;
6051
9f35fa8a 6052 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
c4a3e0a5 6053 ioc->sgl[i].iov_len,
9f35fa8a 6054 &buf_handle, GFP_KERNEL);
c4a3e0a5
BS
6055 if (!kbuff_arr[i]) {
6056 printk(KERN_DEBUG "megasas: Failed to alloc "
6057 "kernel SGL buffer for IOCTL \n");
6058 error = -ENOMEM;
6059 goto out;
6060 }
6061
6062 /*
6063 * We don't change the dma_coherent_mask, so
6064 * pci_alloc_consistent only returns 32bit addresses
6065 */
94cd65dd
SS
6066 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6067 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
c4a3e0a5
BS
6068
6069 /*
6070 * We created a kernel buffer corresponding to the
6071 * user buffer. Now copy in from the user buffer
6072 */
6073 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6074 (u32) (ioc->sgl[i].iov_len))) {
6075 error = -EFAULT;
6076 goto out;
6077 }
6078 }
6079
6080 if (ioc->sense_len) {
9f35fa8a
SP
6081 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6082 &sense_handle, GFP_KERNEL);
c4a3e0a5
BS
6083 if (!sense) {
6084 error = -ENOMEM;
6085 goto out;
6086 }
6087
6088 sense_ptr =
7b2519af 6089 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
94cd65dd 6090 *sense_ptr = cpu_to_le32(sense_handle);
c4a3e0a5
BS
6091 }
6092
6093 /*
6094 * Set the sync_cmd flag so that the ISR knows not to complete this
6095 * cmd to the SCSI mid-layer
6096 */
6097 cmd->sync_cmd = 1;
cfbe7554 6098 megasas_issue_blocked_cmd(instance, cmd, 0);
c4a3e0a5
BS
6099 cmd->sync_cmd = 0;
6100
aa00832b
SS
6101 if (instance->unload == 1) {
6102 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6103 "don't submit data to application\n");
6104 goto out;
6105 }
c4a3e0a5
BS
6106 /*
6107 * copy out the kernel buffers to user buffers
6108 */
6109 for (i = 0; i < ioc->sge_count; i++) {
6110 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6111 ioc->sgl[i].iov_len)) {
6112 error = -EFAULT;
6113 goto out;
6114 }
6115 }
6116
6117 /*
6118 * copy out the sense
6119 */
6120 if (ioc->sense_len) {
6121 /*
b70a41e0 6122 * sense_ptr points to the location that has the user
c4a3e0a5
BS
6123 * sense buffer address
6124 */
7b2519af
YB
6125 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6126 ioc->sense_off);
c4a3e0a5 6127
b70a41e0 6128 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6129 sense, ioc->sense_len)) {
b10c36a5 6130 printk(KERN_ERR "megasas: Failed to copy out to user "
6131 "sense data\n");
c4a3e0a5
BS
6132 error = -EFAULT;
6133 goto out;
6134 }
6135 }
6136
6137 /*
6138 * copy the status codes returned by the fw
6139 */
6140 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6141 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6142 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
6143 error = -EFAULT;
6144 }
6145
6146 out:
6147 if (sense) {
9f35fa8a 6148 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
c4a3e0a5
BS
6149 sense, sense_handle);
6150 }
6151
7a6a731b
BM
6152 for (i = 0; i < ioc->sge_count; i++) {
6153 if (kbuff_arr[i])
6154 dma_free_coherent(&instance->pdev->dev,
94cd65dd 6155 le32_to_cpu(kern_sge32[i].length),
7a6a731b 6156 kbuff_arr[i],
94cd65dd 6157 le32_to_cpu(kern_sge32[i].phys_addr));
90dc9d98 6158 kbuff_arr[i] = NULL;
c4a3e0a5
BS
6159 }
6160
90dc9d98
SS
6161 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
6162 megasas_return_mfi_mpt_pthr(instance, cmd,
6163 cmd->mpt_pthr_cmd_blocked);
6164 else
6165 megasas_return_cmd(instance, cmd);
c4a3e0a5
BS
6166 return error;
6167}
6168
c4a3e0a5
BS
6169static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6170{
6171 struct megasas_iocpacket __user *user_ioc =
6172 (struct megasas_iocpacket __user *)arg;
6173 struct megasas_iocpacket *ioc;
6174 struct megasas_instance *instance;
6175 int error;
39a98554 6176 int i;
6177 unsigned long flags;
6178 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
6179
6180 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6181 if (!ioc)
6182 return -ENOMEM;
6183
6184 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6185 error = -EFAULT;
6186 goto out_kfree_ioc;
6187 }
6188
6189 instance = megasas_lookup_instance(ioc->host_no);
6190 if (!instance) {
6191 error = -ENODEV;
6192 goto out_kfree_ioc;
6193 }
6194
229fe47c 6195 /* Adjust ioctl wait time for VF mode */
6196 if (instance->requestorId)
6197 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6198
6199 /* Block ioctls in VF mode */
6200 if (instance->requestorId && !allow_vf_ioctls) {
6201 error = -ENODEV;
6202 goto out_kfree_ioc;
6203 }
6204
39a98554 6205 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6206 printk(KERN_ERR "Controller in crit error\n");
0c79e681
YB
6207 error = -ENODEV;
6208 goto out_kfree_ioc;
6209 }
6210
6211 if (instance->unload == 1) {
6212 error = -ENODEV;
6213 goto out_kfree_ioc;
6214 }
6215
c4a3e0a5
BS
6216 /*
6217 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
6218 */
6219 if (down_interruptible(&instance->ioctl_sem)) {
6220 error = -ERESTARTSYS;
6221 goto out_kfree_ioc;
6222 }
39a98554 6223
6224 for (i = 0; i < wait_time; i++) {
6225
6226 spin_lock_irqsave(&instance->hba_lock, flags);
6227 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6228 spin_unlock_irqrestore(&instance->hba_lock, flags);
6229 break;
6230 }
6231 spin_unlock_irqrestore(&instance->hba_lock, flags);
6232
6233 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6234 printk(KERN_NOTICE "megasas: waiting"
6235 "for controller reset to finish\n");
6236 }
6237
6238 msleep(1000);
6239 }
6240
6241 spin_lock_irqsave(&instance->hba_lock, flags);
6242 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6243 spin_unlock_irqrestore(&instance->hba_lock, flags);
6244
6245 printk(KERN_ERR "megaraid_sas: timed out while"
6246 "waiting for HBA to recover\n");
6247 error = -ENODEV;
c64e483e 6248 goto out_up;
39a98554 6249 }
6250 spin_unlock_irqrestore(&instance->hba_lock, flags);
6251
c4a3e0a5 6252 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
c64e483e 6253 out_up:
c4a3e0a5
BS
6254 up(&instance->ioctl_sem);
6255
6256 out_kfree_ioc:
6257 kfree(ioc);
6258 return error;
6259}
6260
6261static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6262{
6263 struct megasas_instance *instance;
6264 struct megasas_aen aen;
6265 int error;
39a98554 6266 int i;
6267 unsigned long flags;
6268 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
6269
6270 if (file->private_data != file) {
6271 printk(KERN_DEBUG "megasas: fasync_helper was not "
6272 "called first\n");
6273 return -EINVAL;
6274 }
6275
6276 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6277 return -EFAULT;
6278
6279 instance = megasas_lookup_instance(aen.host_no);
6280
6281 if (!instance)
6282 return -ENODEV;
6283
39a98554 6284 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6285 return -ENODEV;
0c79e681
YB
6286 }
6287
6288 if (instance->unload == 1) {
6289 return -ENODEV;
6290 }
6291
39a98554 6292 for (i = 0; i < wait_time; i++) {
6293
6294 spin_lock_irqsave(&instance->hba_lock, flags);
6295 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6296 spin_unlock_irqrestore(&instance->hba_lock,
6297 flags);
6298 break;
6299 }
6300
6301 spin_unlock_irqrestore(&instance->hba_lock, flags);
6302
6303 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6304 printk(KERN_NOTICE "megasas: waiting for"
6305 "controller reset to finish\n");
6306 }
6307
6308 msleep(1000);
6309 }
6310
6311 spin_lock_irqsave(&instance->hba_lock, flags);
6312 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6313 spin_unlock_irqrestore(&instance->hba_lock, flags);
6314 printk(KERN_ERR "megaraid_sas: timed out while waiting"
6315 "for HBA to recover.\n");
6316 return -ENODEV;
6317 }
6318 spin_unlock_irqrestore(&instance->hba_lock, flags);
6319
e5a69e27 6320 mutex_lock(&instance->aen_mutex);
c4a3e0a5
BS
6321 error = megasas_register_aen(instance, aen.seq_num,
6322 aen.class_locale_word);
e5a69e27 6323 mutex_unlock(&instance->aen_mutex);
c4a3e0a5
BS
6324 return error;
6325}
6326
6327/**
6328 * megasas_mgmt_ioctl - char node ioctl entry point
6329 */
6330static long
6331megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6332{
6333 switch (cmd) {
6334 case MEGASAS_IOC_FIRMWARE:
6335 return megasas_mgmt_ioctl_fw(file, arg);
6336
6337 case MEGASAS_IOC_GET_AEN:
6338 return megasas_mgmt_ioctl_aen(file, arg);
6339 }
6340
6341 return -ENOTTY;
6342}
6343
6344#ifdef CONFIG_COMPAT
6345static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6346{
6347 struct compat_megasas_iocpacket __user *cioc =
6348 (struct compat_megasas_iocpacket __user *)arg;
6349 struct megasas_iocpacket __user *ioc =
6350 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6351 int i;
6352 int error = 0;
b3dc1a21 6353 compat_uptr_t ptr;
c4a3e0a5 6354
83aabc1b
JG
6355 if (clear_user(ioc, sizeof(*ioc)))
6356 return -EFAULT;
c4a3e0a5
BS
6357
6358 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6359 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6360 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6361 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6362 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6363 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6364 return -EFAULT;
6365
b3dc1a21
TH
6366 /*
6367 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6368 * sense_len is not null, so prepare the 64bit value under
6369 * the same condition.
6370 */
6371 if (ioc->sense_len) {
6372 void __user **sense_ioc_ptr =
6373 (void __user **)(ioc->frame.raw + ioc->sense_off);
6374 compat_uptr_t *sense_cioc_ptr =
6375 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6376 if (get_user(ptr, sense_cioc_ptr) ||
6377 put_user(compat_ptr(ptr), sense_ioc_ptr))
6378 return -EFAULT;
6379 }
c4a3e0a5 6380
b3dc1a21 6381 for (i = 0; i < MAX_IOCTL_SGE; i++) {
c4a3e0a5
BS
6382 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6383 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6384 copy_in_user(&ioc->sgl[i].iov_len,
6385 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6386 return -EFAULT;
6387 }
6388
6389 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6390
6391 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6392 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6393 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6394 return -EFAULT;
6395 }
6396 return error;
6397}
6398
6399static long
6400megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6401 unsigned long arg)
6402{
6403 switch (cmd) {
cb59aa6a
SP
6404 case MEGASAS_IOC_FIRMWARE32:
6405 return megasas_mgmt_compat_ioctl_fw(file, arg);
c4a3e0a5
BS
6406 case MEGASAS_IOC_GET_AEN:
6407 return megasas_mgmt_ioctl_aen(file, arg);
6408 }
6409
6410 return -ENOTTY;
6411}
6412#endif
6413
6414/*
6415 * File operations structure for management interface
6416 */
00977a59 6417static const struct file_operations megasas_mgmt_fops = {
c4a3e0a5
BS
6418 .owner = THIS_MODULE,
6419 .open = megasas_mgmt_open,
c4a3e0a5
BS
6420 .fasync = megasas_mgmt_fasync,
6421 .unlocked_ioctl = megasas_mgmt_ioctl,
c3518837 6422 .poll = megasas_mgmt_poll,
c4a3e0a5
BS
6423#ifdef CONFIG_COMPAT
6424 .compat_ioctl = megasas_mgmt_compat_ioctl,
6425#endif
6038f373 6426 .llseek = noop_llseek,
c4a3e0a5
BS
6427};
6428
6429/*
6430 * PCI hotplug support registration structure
6431 */
6432static struct pci_driver megasas_pci_driver = {
6433
6434 .name = "megaraid_sas",
6435 .id_table = megasas_pci_table,
6436 .probe = megasas_probe_one,
6f039790 6437 .remove = megasas_detach_one,
31ea7088 6438 .suspend = megasas_suspend,
6439 .resume = megasas_resume,
c4a3e0a5
BS
6440 .shutdown = megasas_shutdown,
6441};
6442
6443/*
6444 * Sysfs driver attributes
6445 */
6446static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6447{
6448 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6449 MEGASAS_VERSION);
6450}
6451
6452static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6453
72c4fd36
YB
6454static ssize_t
6455megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6456{
6457 return sprintf(buf, "%u\n", support_poll_for_event);
6458}
6459
6460static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6461 megasas_sysfs_show_support_poll_for_event, NULL);
6462
837f5fe8
YB
6463 static ssize_t
6464megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6465{
6466 return sprintf(buf, "%u\n", support_device_change);
6467}
6468
6469static DRIVER_ATTR(support_device_change, S_IRUGO,
6470 megasas_sysfs_show_support_device_change, NULL);
6471
658dcedb
SP
6472static ssize_t
6473megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6474{
ad84db2e 6475 return sprintf(buf, "%u\n", megasas_dbg_lvl);
658dcedb
SP
6476}
6477
6478static ssize_t
6479megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6480{
6481 int retval = count;
6482 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
6483 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6484 retval = -EINVAL;
6485 }
6486 return retval;
6487}
6488
66dca9b8 6489static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
ad84db2e 6490 megasas_sysfs_set_dbg_lvl);
6491
7e8a75f4
YB
6492static void
6493megasas_aen_polling(struct work_struct *work)
6494{
6495 struct megasas_aen_event *ev =
c1d390d8 6496 container_of(work, struct megasas_aen_event, hotplug_work.work);
7e8a75f4
YB
6497 struct megasas_instance *instance = ev->instance;
6498 union megasas_evt_class_locale class_locale;
6499 struct Scsi_Host *host;
6500 struct scsi_device *sdev1;
6501 u16 pd_index = 0;
c9786842 6502 u16 ld_index = 0;
7e8a75f4 6503 int i, j, doscan = 0;
229fe47c 6504 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
7e8a75f4
YB
6505 int error;
6506
6507 if (!instance) {
6508 printk(KERN_ERR "invalid instance!\n");
6509 kfree(ev);
6510 return;
6511 }
229fe47c 6512
6513 /* Adjust event workqueue thread wait time for VF mode */
6514 if (instance->requestorId)
6515 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6516
6517 /* Don't run the event workqueue thread if OCR is running */
6518 for (i = 0; i < wait_time; i++) {
6519 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6520 break;
6521 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6522 printk(KERN_NOTICE "megasas: %s waiting for "
6523 "controller reset to finish for scsi%d\n",
6524 __func__, instance->host->host_no);
6525 }
6526 msleep(1000);
6527 }
6528
7e8a75f4
YB
6529 instance->ev = NULL;
6530 host = instance->host;
6531 if (instance->evt_detail) {
6532
94cd65dd 6533 switch (le32_to_cpu(instance->evt_detail->code)) {
7e8a75f4 6534 case MR_EVT_PD_INSERTED:
c9786842
YB
6535 if (megasas_get_pd_list(instance) == 0) {
6536 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6537 for (j = 0;
6538 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6539 j++) {
6540
6541 pd_index =
6542 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6543
6544 sdev1 =
6545 scsi_device_lookup(host, i, j, 0);
6546
6547 if (instance->pd_list[pd_index].driveState
6548 == MR_PD_STATE_SYSTEM) {
6549 if (!sdev1) {
6550 scsi_add_device(host, i, j, 0);
6551 }
6552
6553 if (sdev1)
6554 scsi_device_put(sdev1);
6555 }
6556 }
6557 }
6558 }
6559 doscan = 0;
6560 break;
6561
7e8a75f4 6562 case MR_EVT_PD_REMOVED:
c9786842 6563 if (megasas_get_pd_list(instance) == 0) {
c9786842
YB
6564 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6565 for (j = 0;
6566 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6567 j++) {
6568
6569 pd_index =
6570 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6571
6572 sdev1 =
6573 scsi_device_lookup(host, i, j, 0);
6574
6575 if (instance->pd_list[pd_index].driveState
6576 == MR_PD_STATE_SYSTEM) {
6577 if (sdev1) {
6578 scsi_device_put(sdev1);
6579 }
6580 } else {
6581 if (sdev1) {
6582 scsi_remove_device(sdev1);
6583 scsi_device_put(sdev1);
6584 }
6585 }
6586 }
6587 }
6588 }
6589 doscan = 0;
6590 break;
6591
6592 case MR_EVT_LD_OFFLINE:
4c598b23 6593 case MR_EVT_CFG_CLEARED:
c9786842 6594 case MR_EVT_LD_DELETED:
229fe47c 6595 if (!instance->requestorId ||
6596 (instance->requestorId &&
6597 megasas_get_ld_vf_affiliation(instance, 0))) {
6598 if (megasas_ld_list_query(instance,
6599 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6600 megasas_get_ld_list(instance);
6601 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6602 for (j = 0;
6603 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6604 j++) {
6605
6606 ld_index =
6607 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6608
6609 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6610
6611 if (instance->ld_ids[ld_index]
6612 != 0xff) {
6613 if (sdev1)
6614 scsi_device_put(sdev1);
6615 } else {
6616 if (sdev1) {
6617 scsi_remove_device(sdev1);
6618 scsi_device_put(sdev1);
6619 }
6620 }
c9786842
YB
6621 }
6622 }
229fe47c 6623 doscan = 0;
c9786842 6624 }
c9786842
YB
6625 break;
6626 case MR_EVT_LD_CREATED:
229fe47c 6627 if (!instance->requestorId ||
6628 (instance->requestorId &&
6629 megasas_get_ld_vf_affiliation(instance, 0))) {
6630 if (megasas_ld_list_query(instance,
6631 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6632 megasas_get_ld_list(instance);
6633 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6634 for (j = 0;
6635 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6636 j++) {
6637 ld_index =
6638 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6639
6640 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6641
6642 if (instance->ld_ids[ld_index]
6643 != 0xff) {
6644 if (!sdev1)
6645 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
c9786842 6646 }
229fe47c 6647 if (sdev1)
6648 scsi_device_put(sdev1);
c9786842
YB
6649 }
6650 }
229fe47c 6651 doscan = 0;
c9786842 6652 }
c9786842 6653 break;
7e8a75f4 6654 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
c9786842 6655 case MR_EVT_FOREIGN_CFG_IMPORTED:
9c915a8c 6656 case MR_EVT_LD_STATE_CHANGE:
7e8a75f4
YB
6657 doscan = 1;
6658 break;
6659 default:
6660 doscan = 0;
6661 break;
6662 }
6663 } else {
6664 printk(KERN_ERR "invalid evt_detail!\n");
6665 kfree(ev);
6666 return;
6667 }
6668
6669 if (doscan) {
229fe47c 6670 printk(KERN_INFO "megaraid_sas: scanning for scsi%d...\n",
6671 instance->host->host_no);
58968fc8
HR
6672 if (megasas_get_pd_list(instance) == 0) {
6673 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6674 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6675 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6676 sdev1 = scsi_device_lookup(host, i, j, 0);
6677 if (instance->pd_list[pd_index].driveState ==
6678 MR_PD_STATE_SYSTEM) {
6679 if (!sdev1) {
6680 scsi_add_device(host, i, j, 0);
6681 }
6682 if (sdev1)
6683 scsi_device_put(sdev1);
6684 } else {
6685 if (sdev1) {
6686 scsi_remove_device(sdev1);
6687 scsi_device_put(sdev1);
6688 }
7e8a75f4
YB
6689 }
6690 }
6691 }
6692 }
c9786842 6693
229fe47c 6694 if (!instance->requestorId ||
6695 (instance->requestorId &&
6696 megasas_get_ld_vf_affiliation(instance, 0))) {
6697 if (megasas_ld_list_query(instance,
6698 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6699 megasas_get_ld_list(instance);
6700 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6701 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6702 j++) {
6703 ld_index =
6704 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
c9786842 6705
229fe47c 6706 sdev1 = scsi_device_lookup(host,
6707 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6708 if (instance->ld_ids[ld_index]
6709 != 0xff) {
6710 if (!sdev1)
6711 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6712 else
6713 scsi_device_put(sdev1);
c9786842 6714 } else {
229fe47c 6715 if (sdev1) {
6716 scsi_remove_device(sdev1);
6717 scsi_device_put(sdev1);
6718 }
c9786842
YB
6719 }
6720 }
6721 }
6722 }
7e8a75f4
YB
6723 }
6724
6725 if ( instance->aen_cmd != NULL ) {
6726 kfree(ev);
6727 return ;
6728 }
6729
94cd65dd 6730 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7e8a75f4
YB
6731
6732 /* Register AEN with FW for latest sequence number plus 1 */
6733 class_locale.members.reserved = 0;
6734 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6735 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6736 mutex_lock(&instance->aen_mutex);
6737 error = megasas_register_aen(instance, seq_num,
6738 class_locale.word);
6739 mutex_unlock(&instance->aen_mutex);
6740
6741 if (error)
6742 printk(KERN_ERR "register aen failed error %x\n", error);
6743
6744 kfree(ev);
6745}
6746
c4a3e0a5
BS
6747/**
6748 * megasas_init - Driver load entry point
6749 */
6750static int __init megasas_init(void)
6751{
6752 int rval;
6753
6754 /*
6755 * Announce driver version and other information
6756 */
d98a6deb 6757 pr_info("megasas: %s\n", MEGASAS_VERSION);
c4a3e0a5 6758
bd8d6dd4
KD
6759 spin_lock_init(&poll_aen_lock);
6760
72c4fd36 6761 support_poll_for_event = 2;
837f5fe8 6762 support_device_change = 1;
72c4fd36 6763
c4a3e0a5
BS
6764 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6765
6766 /*
6767 * Register character device node
6768 */
6769 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6770
6771 if (rval < 0) {
6772 printk(KERN_DEBUG "megasas: failed to open device node\n");
6773 return rval;
6774 }
6775
6776 megasas_mgmt_majorno = rval;
6777
6778 /*
6779 * Register ourselves as PCI hotplug module
6780 */
4041b9cd 6781 rval = pci_register_driver(&megasas_pci_driver);
c4a3e0a5
BS
6782
6783 if (rval) {
6774def6 6784 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
83aabc1b
JG
6785 goto err_pcidrv;
6786 }
6787
6788 rval = driver_create_file(&megasas_pci_driver.driver,
6789 &driver_attr_version);
6790 if (rval)
6791 goto err_dcf_attr_ver;
72c4fd36
YB
6792
6793 rval = driver_create_file(&megasas_pci_driver.driver,
6794 &driver_attr_support_poll_for_event);
6795 if (rval)
6796 goto err_dcf_support_poll_for_event;
6797
83aabc1b
JG
6798 rval = driver_create_file(&megasas_pci_driver.driver,
6799 &driver_attr_dbg_lvl);
6800 if (rval)
6801 goto err_dcf_dbg_lvl;
837f5fe8
YB
6802 rval = driver_create_file(&megasas_pci_driver.driver,
6803 &driver_attr_support_device_change);
6804 if (rval)
6805 goto err_dcf_support_device_change;
6806
c4a3e0a5 6807 return rval;
ad84db2e 6808
837f5fe8 6809err_dcf_support_device_change:
ad84db2e 6810 driver_remove_file(&megasas_pci_driver.driver,
6811 &driver_attr_dbg_lvl);
83aabc1b 6812err_dcf_dbg_lvl:
72c4fd36
YB
6813 driver_remove_file(&megasas_pci_driver.driver,
6814 &driver_attr_support_poll_for_event);
72c4fd36 6815err_dcf_support_poll_for_event:
83aabc1b
JG
6816 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6817err_dcf_attr_ver:
6818 pci_unregister_driver(&megasas_pci_driver);
6819err_pcidrv:
6820 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
0d49016b 6821 return rval;
c4a3e0a5
BS
6822}
6823
6824/**
6825 * megasas_exit - Driver unload entry point
6826 */
6827static void __exit megasas_exit(void)
6828{
658dcedb
SP
6829 driver_remove_file(&megasas_pci_driver.driver,
6830 &driver_attr_dbg_lvl);
837f5fe8
YB
6831 driver_remove_file(&megasas_pci_driver.driver,
6832 &driver_attr_support_poll_for_event);
6833 driver_remove_file(&megasas_pci_driver.driver,
6834 &driver_attr_support_device_change);
83aabc1b 6835 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
c4a3e0a5
BS
6836
6837 pci_unregister_driver(&megasas_pci_driver);
6838 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6839}
6840
6841module_init(megasas_init);
6842module_exit(megasas_exit);
This page took 1.102631 seconds and 5 git commands to generate.