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