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