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