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