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