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