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